Java class annotation as safe for link comparison

I have a class that is multi-ton, so I know that given a certain key, there will never be two instances of the same class that exist. This means that instead of:

if (someObject.equals(anotherObject)) 

... it's safe for me:

 if (someObject == anotherObject) 

The class is also final , so I know that anything related to polymorphism can also cause problems for comparison.

IDEA obediently tells me that it is risky to compare two instances directly and that I have to use .equals() , but I know that this is not the case. Is there some kind of annotation that I can apply to my class to instruct IDEA and possibly other editors and, more importantly, other users, that it is safe to compare links for equality in instances of my class?

I know that I can simply say IDEA to suppress the warning, but I would have to do this for every comparison between the two types or globally, neither of which is a good idea. In addition, it is more important that users of my class know that it is safe, faster, and even preferable (convince me otherwise) over .equals() .

+7
java equality equals intellij-idea multiton
source share
1 answer

IntelliJ inspection has the option โ€œIgnore" == between objects of type with only private constructors. "If I understand correctly, enabling this option will disable the selection in your case.

IntelliJ IDEA does not have the ability to ignore annotation-based comparison == .

+1
source share

All Articles