7

I have a class that's a multiton, so I know that given a particular 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 to do this:

if (someObject == anotherObject)

The class is also final, so I know that nothing related to polymorphism could cause problems for comparison either.

IDEA dutifully informs me that it's risky to compare two instances directly and that I should use .equals(), but I know it's not in this case. Is there some annotation I can apply to my class to instruct IDEA, and potentially other editors and more importantly other users, that a direct reference comparison for equality on instances of my class is safe?

I know I could just tell IDEA to suppress the warning, but I'd have to do it for every comparison between these two types or globally, neither of which is a good idea. Plus, it's more important that I let users of my class know it's safe, faster, and even preferred (convince me otherwise) over .equals().

yole
  • 92,896
  • 20
  • 260
  • 197
user3466413
  • 805
  • 1
  • 7
  • 16
  • Oh, and serialization or other shenanigans shouldn't apply here. The object is only ever constructed by me, not incidentally through other means like deserialization. This is a GWT application which also means anything related to multithreading doesn't apply either. – user3466413 Mar 03 '15 at 18:42
  • 6
    I'm going to try to argue for `.equals` here: If you just have `.equals` refer to reference equality, it should optimize to `==`, and users don't have to remember that some objects are okay with `==` and some aren't. They can just use `.equals` everywhere and it'll have just as good performance as `==`. – Louis Wasserman Mar 03 '15 at 18:44
  • @LouisWasserman But then you have to be careful that your references aren't null. Or at least that your left-hand one isn't. – RealSkeptic Mar 03 '15 at 18:45
  • 1
    Good! You should already be careful about those things. Working As Intended. A `NullPointerException` is the proper result of passing around null when it wasn't supposed to be there. – Louis Wasserman Mar 03 '15 at 18:46
  • @LouisWasserman Unless it's allowed for them to be null. `a == b` is much more readable than, say, `a != null && a.equals(b) || a == null && b == null`. – RealSkeptic Mar 03 '15 at 18:51
  • 2
    Sure. That's when you use `Objects.equals(a, b)`, where `Objects` is `java.util.Objects`. – Louis Wasserman Mar 03 '15 at 18:52
  • 2
    I +1 @LouisWasserman : And just so you are safe you can add your final implementation of equals that just calls super (or do the ref equality) and prevent overriding by eventual subclasses. – benzonico Mar 03 '15 at 19:31
  • Same here, +1 @LouisWasserman. I see no point in complicating things in such a way that you have to annotate your code in order to deal with your IDE so that you can live in peace. Plus, code is much less error-prone, and safe, as suggested by @benzonico (+1 too) if you override `equals()` calling `super.equals()` and making it `final`. – fps Mar 03 '15 at 19:37
  • If `.equals()` is implemented as just `==`, will the compiler actually optimize the bytecode (for GWT, the Javascript) so all calls to `.equals()` when the left- and right-hand-side of the operator are of my type become `==`? – user3466413 Mar 03 '15 at 22:36

1 Answers1

1

The IntelliJ inspection has an option "Ignore '==' between objects of a type with only private constructors". If I understand correctly, enabling this option will turn off the highlighting in your case.

There is currently no possibility in IntelliJ IDEA to ignore the == comparison based on an annotation.

yole
  • 92,896
  • 20
  • 260
  • 197
  • Thanks for the IDEA configuration suggesiton. Is there a way of also adding some metadata to the class such that people who use Eclipse or other editors will also know the comparison is safe? – user3466413 Mar 05 '15 at 16:54
  • For a generic text editor, there is no way to add any metadata to the class definition so that the users of the class would see it. As for Eclipse, I'm not sufficiently familiar with it to tell you anything specific. – yole Mar 05 '15 at 22:08