You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucene.apache.org by Fuxiang Chen <fc...@cse.ust.hk> on 2015/07/12 08:29:02 UTC

Suggestion for overriding equals() method

Dear Developers,

We have found some inconsistencies in the overriding of the equals() method
in some files with respect to the conforming to the contract structure
based on the Java Specification.

Affected files:
1) ConstValueSource.java
2) DoubleConstValueSource.java
3) FixedBitSet.java
4) GeohashFunction.java
5) LongBitSet.java
6) SpanNearQuery.java
7) StringDistanceFunction.java
8) ValueSourceRangeFilter.java
9) VectorDistanceFunction.java

The above files all uses instanceof in the overridden equals() method in
comparing two objects.

According to the Java Specification, the equals() method must be reflexive,
symmetric, transitive and consistent. In the case of symmetric, it is
stated that x.equals(y) should return true if and only if y.equals(x)
returns true. Using instanceof is asymmetric and is not a valid symmetric
contract.

A more preferred way will be to compare the classes instead. i.e. if
(this.getClass() != o.getClass()).

However, if compiling the source code using JDK 7 and above, and if
developers still prefer to use instanceof, you can make use of the static
methods of Objects such as Objects.equals(this.id, that.id). (Making use of
the static methods of Objects is currently absent in the methods.) It will
be easier to override the equals() method and will ensure that the
overridden equals() method will fulfill the contract rules.

Do kindly consider making the suggestion for the changes.

Reference:
[1] http://stackoverflow.com/questions/19642810 (based on the latest
highest answer)
[2] http://docs.oracle.com/javase/7/docs/api/java/util/Objects.html (based
on [1])
[3] http://stackoverflow.com/questions/7132649 (based on the highest answer)

Thanks!

-- 
Warmest Regards,
    Fuxiang

Re: Suggestion for overriding equals() method

Posted by Chris Hostetter <ho...@fucit.org>.
Fuxiang: thank you for your email about this.

I've filed this in our jira tracker -- if you have a patch you'd like to 
provide to help move this issue forward, please attach there...

https://issues.apache.org/jira/browse/LUCENE-6744
https://wiki.apache.org/lucene-java/HowToContribute



: Date: Sun, 12 Jul 2015 14:29:02 +0800
: From: Fuxiang Chen <fc...@cse.ust.hk>
: Reply-To: dev@lucene.apache.org
: To: dev@lucene.apache.org
: Subject: Suggestion for overriding equals() method
: 
: Dear Developers,
: 
: We have found some inconsistencies in the overriding of the equals() method
: in some files with respect to the conforming to the contract structure
: based on the Java Specification.
: 
: Affected files:
: 1) ConstValueSource.java
: 2) DoubleConstValueSource.java
: 3) FixedBitSet.java
: 4) GeohashFunction.java
: 5) LongBitSet.java
: 6) SpanNearQuery.java
: 7) StringDistanceFunction.java
: 8) ValueSourceRangeFilter.java
: 9) VectorDistanceFunction.java
: 
: The above files all uses instanceof in the overridden equals() method in
: comparing two objects.
: 
: According to the Java Specification, the equals() method must be reflexive,
: symmetric, transitive and consistent. In the case of symmetric, it is
: stated that x.equals(y) should return true if and only if y.equals(x)
: returns true. Using instanceof is asymmetric and is not a valid symmetric
: contract.
: 
: A more preferred way will be to compare the classes instead. i.e. if
: (this.getClass() != o.getClass()).
: 
: However, if compiling the source code using JDK 7 and above, and if
: developers still prefer to use instanceof, you can make use of the static
: methods of Objects such as Objects.equals(this.id, that.id). (Making use of
: the static methods of Objects is currently absent in the methods.) It will
: be easier to override the equals() method and will ensure that the
: overridden equals() method will fulfill the contract rules.
: 
: Do kindly consider making the suggestion for the changes.
: 
: Reference:
: [1] http://stackoverflow.com/questions/19642810 (based on the latest
: highest answer)
: [2] http://docs.oracle.com/javase/7/docs/api/java/util/Objects.html (based
: on [1])
: [3] http://stackoverflow.com/questions/7132649 (based on the highest answer)
: 
: Thanks!
: 
: -- 
: Warmest Regards,
:     Fuxiang
: 

-Hoss
http://www.lucidworks.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org