From 800fcb8927d112be2c6703e9321d3ecad41b04d1 Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Thu, 5 Mar 2015 16:01:58 +0000 Subject: [PATCH] LUCENE-6333: Clean up overridden .equals and .hashCode methods in Query subclasses. git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1664384 13f79535-47bb-0310-9956-ffa450edef68 --- lucene/CHANGES.txt | 3 +++ .../java/org/apache/lucene/search/BooleanQuery.java | 4 ++-- .../apache/lucene/search/DisjunctionMaxQuery.java | 2 +- .../apache/lucene/search/DocValuesRangeQuery.java | 2 +- .../apache/lucene/search/DocValuesTermsQuery.java | 4 ++-- .../org/apache/lucene/search/FieldValueQuery.java | 2 +- .../src/java/org/apache/lucene/search/Filter.java | 2 +- .../org/apache/lucene/search/MatchAllDocsQuery.java | 13 ------------- .../org/apache/lucene/search/MultiPhraseQuery.java | 7 +++---- .../org/apache/lucene/search/MultiTermQuery.java | 2 +- .../java/org/apache/lucene/search/PhraseQuery.java | 4 ++-- .../java/org/apache/lucene/search/TermQuery.java | 9 +++------ .../org/apache/lucene/search/TestQueryRescorer.java | 2 +- .../java/org/apache/lucene/queries/TermsQuery.java | 11 +---------- .../lucene/queries/function/FunctionQuery.java | 6 ++---- .../queryparser/surround/query/RewriteQuery.java | 9 +++++---- .../apache/lucene/search/TermAutomatonQuery.java | 4 ++-- 17 files changed, 31 insertions(+), 55 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 8305ca6acb3..10c35f4661d 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -218,6 +218,9 @@ Other * LUCENE-6292: Seed StringHelper better. (Robert Muir) +* LUCENE-6333: Refactored queries to delegate their equals and hashcode + impls to the super class. (Lee Hinman via Adrien Grand) + Changes in Runtime Behavior * LUCENE-6255: PhraseQuery now ignores leading holes and requires that diff --git a/lucene/core/src/java/org/apache/lucene/search/BooleanQuery.java b/lucene/core/src/java/org/apache/lucene/search/BooleanQuery.java index f9b1904c490..c3d2f566bca 100644 --- a/lucene/core/src/java/org/apache/lucene/search/BooleanQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/BooleanQuery.java @@ -304,7 +304,7 @@ public class BooleanQuery extends Query implements Iterable { return false; } BooleanQuery other = (BooleanQuery)o; - return this.getBoost() == other.getBoost() + return super.equals(o) && this.clauses.equals(other.clauses) && this.getMinimumNumberShouldMatch() == other.getMinimumNumberShouldMatch() && this.disableCoord == other.disableCoord; @@ -313,7 +313,7 @@ public class BooleanQuery extends Query implements Iterable { /** Returns a hash code value for this object.*/ @Override public int hashCode() { - return Float.floatToIntBits(getBoost()) ^ clauses.hashCode() + return super.hashCode() ^ clauses.hashCode() + getMinimumNumberShouldMatch() + (disableCoord ? 17:0); } diff --git a/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java b/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java index 9a8cec02e16..c931d92def9 100644 --- a/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java @@ -284,7 +284,7 @@ public class DisjunctionMaxQuery extends Query implements Iterable { public boolean equals(Object o) { if (! (o instanceof DisjunctionMaxQuery) ) return false; DisjunctionMaxQuery other = (DisjunctionMaxQuery)o; - return this.getBoost() == other.getBoost() + return super.equals(o) && this.tieBreakerMultiplier == other.tieBreakerMultiplier && this.disjuncts.equals(other.disjuncts); } diff --git a/lucene/core/src/java/org/apache/lucene/search/DocValuesRangeQuery.java b/lucene/core/src/java/org/apache/lucene/search/DocValuesRangeQuery.java index 45c1c3a302b..5d66d0b775c 100644 --- a/lucene/core/src/java/org/apache/lucene/search/DocValuesRangeQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/DocValuesRangeQuery.java @@ -85,7 +85,7 @@ public final class DocValuesRangeQuery extends Query { && Objects.equals(upperVal, that.upperVal) && includeLower == that.includeLower && includeUpper == that.includeUpper - && getBoost() == that.getBoost(); + && super.equals(obj); } @Override diff --git a/lucene/core/src/java/org/apache/lucene/search/DocValuesTermsQuery.java b/lucene/core/src/java/org/apache/lucene/search/DocValuesTermsQuery.java index 30a13b5fcb4..4875b16dce7 100644 --- a/lucene/core/src/java/org/apache/lucene/search/DocValuesTermsQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/DocValuesTermsQuery.java @@ -117,10 +117,10 @@ public class DocValuesTermsQuery extends Query { return false; } DocValuesTermsQuery that = (DocValuesTermsQuery) obj; - if (!field.equals(that.field)) { + if (!super.equals(obj)) { return false; } - if (getBoost() != that.getBoost()) { + if (!field.equals(that.field)) { return false; } return Arrays.equals(terms, that.terms); diff --git a/lucene/core/src/java/org/apache/lucene/search/FieldValueQuery.java b/lucene/core/src/java/org/apache/lucene/search/FieldValueQuery.java index 1dbe390f1b1..2195ed061cc 100644 --- a/lucene/core/src/java/org/apache/lucene/search/FieldValueQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/FieldValueQuery.java @@ -46,7 +46,7 @@ public final class FieldValueQuery extends Query { return false; } final FieldValueQuery that = (FieldValueQuery) obj; - return field.equals(that.field) && getBoost() == that.getBoost(); + return super.equals(obj) && field.equals(that.field); } @Override diff --git a/lucene/core/src/java/org/apache/lucene/search/Filter.java b/lucene/core/src/java/org/apache/lucene/search/Filter.java index 72dbe057e92..387c2e15783 100644 --- a/lucene/core/src/java/org/apache/lucene/search/Filter.java +++ b/lucene/core/src/java/org/apache/lucene/search/Filter.java @@ -63,7 +63,7 @@ public abstract class Filter extends Query { @Override public boolean equals(Object that) { - // Query's default impl only compares boots but they do not matter in the + // Query's default impl only compares boost but they do not matter in the // case of filters since it does not influence scores return this == that; } diff --git a/lucene/core/src/java/org/apache/lucene/search/MatchAllDocsQuery.java b/lucene/core/src/java/org/apache/lucene/search/MatchAllDocsQuery.java index 5cd2a8f4412..4089bc050e8 100644 --- a/lucene/core/src/java/org/apache/lucene/search/MatchAllDocsQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/MatchAllDocsQuery.java @@ -144,17 +144,4 @@ public class MatchAllDocsQuery extends Query { buffer.append(ToStringUtils.boost(getBoost())); return buffer.toString(); } - - @Override - public boolean equals(Object o) { - if (!(o instanceof MatchAllDocsQuery)) - return false; - MatchAllDocsQuery other = (MatchAllDocsQuery) o; - return this.getBoost() == other.getBoost(); - } - - @Override - public int hashCode() { - return Float.floatToIntBits(getBoost()) ^ 0x1AA71190; - } } diff --git a/lucene/core/src/java/org/apache/lucene/search/MultiPhraseQuery.java b/lucene/core/src/java/org/apache/lucene/search/MultiPhraseQuery.java index 363d7dacb9d..f6b9ff57482 100644 --- a/lucene/core/src/java/org/apache/lucene/search/MultiPhraseQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/MultiPhraseQuery.java @@ -357,7 +357,7 @@ public class MultiPhraseQuery extends Query { public boolean equals(Object o) { if (!(o instanceof MultiPhraseQuery)) return false; MultiPhraseQuery other = (MultiPhraseQuery)o; - return this.getBoost() == other.getBoost() + return super.equals(o) && this.slop == other.slop && termArraysEquals(this.termArrays, other.termArrays) && this.positions.equals(other.positions); @@ -366,11 +366,10 @@ public class MultiPhraseQuery extends Query { /** Returns a hash code value for this object.*/ @Override public int hashCode() { - return Float.floatToIntBits(getBoost()) + return super.hashCode() ^ slop ^ termArraysHashCode() - ^ positions.hashCode() - ^ 0x4AC65113; + ^ positions.hashCode(); } // Breakout calculation of the termArrays hashcode diff --git a/lucene/core/src/java/org/apache/lucene/search/MultiTermQuery.java b/lucene/core/src/java/org/apache/lucene/search/MultiTermQuery.java index 68862f36dde..b4753942c27 100644 --- a/lucene/core/src/java/org/apache/lucene/search/MultiTermQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/MultiTermQuery.java @@ -288,7 +288,7 @@ public abstract class MultiTermQuery extends Query { if (getClass() != obj.getClass()) return false; MultiTermQuery other = (MultiTermQuery) obj; - if (Float.floatToIntBits(getBoost()) != Float.floatToIntBits(other.getBoost())) + if (!super.equals(obj)) return false; if (!rewriteMethod.equals(other.rewriteMethod)) { return false; diff --git a/lucene/core/src/java/org/apache/lucene/search/PhraseQuery.java b/lucene/core/src/java/org/apache/lucene/search/PhraseQuery.java index 5b2e661352e..20d255145be 100644 --- a/lucene/core/src/java/org/apache/lucene/search/PhraseQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/PhraseQuery.java @@ -428,7 +428,7 @@ public class PhraseQuery extends Query { if (!(o instanceof PhraseQuery)) return false; PhraseQuery other = (PhraseQuery)o; - return (this.getBoost() == other.getBoost()) + return super.equals(o) && (this.slop == other.slop) && this.terms.equals(other.terms) && this.positions.equals(other.positions); @@ -437,7 +437,7 @@ public class PhraseQuery extends Query { /** Returns a hash code value for this object.*/ @Override public int hashCode() { - return Float.floatToIntBits(getBoost()) + return super.hashCode() ^ slop ^ terms.hashCode() ^ positions.hashCode(); diff --git a/lucene/core/src/java/org/apache/lucene/search/TermQuery.java b/lucene/core/src/java/org/apache/lucene/search/TermQuery.java index 69a6a4197ed..46b96ef9d69 100644 --- a/lucene/core/src/java/org/apache/lucene/search/TermQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/TermQuery.java @@ -210,14 +210,11 @@ public class TermQuery extends Query { public boolean equals(Object o) { if (!(o instanceof TermQuery)) return false; TermQuery other = (TermQuery) o; - return (this.getBoost() == other.getBoost()) - && this.term.equals(other.term); + return super.equals(o) && this.term.equals(other.term); } - - /** Returns a hash code value for this object. */ + @Override public int hashCode() { - return Float.floatToIntBits(getBoost()) ^ term.hashCode(); + return super.hashCode() ^ term.hashCode(); } - } diff --git a/lucene/core/src/test/org/apache/lucene/search/TestQueryRescorer.java b/lucene/core/src/test/org/apache/lucene/search/TestQueryRescorer.java index 17e12d995f3..3b78985dee8 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestQueryRescorer.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestQueryRescorer.java @@ -509,7 +509,7 @@ public class TestQueryRescorer extends LuceneTestCase { return false; } FixedScoreQuery other = (FixedScoreQuery) o; - return Float.floatToIntBits(getBoost()) == Float.floatToIntBits(other.getBoost()) && + return super.equals(o) && reverse == other.reverse && Arrays.equals(idToNum, other.idToNum); } diff --git a/lucene/queries/src/java/org/apache/lucene/queries/TermsQuery.java b/lucene/queries/src/java/org/apache/lucene/queries/TermsQuery.java index ca6746ac055..93350422997 100644 --- a/lucene/queries/src/java/org/apache/lucene/queries/TermsQuery.java +++ b/lucene/queries/src/java/org/apache/lucene/queries/TermsQuery.java @@ -192,13 +192,9 @@ public class TermsQuery extends Query implements Accountable { @Override public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if ((obj == null) || (obj.getClass() != this.getClass())) { + if (!super.equals(obj)) { return false; } - TermsQuery that = (TermsQuery) obj; // first check the fields before even comparing the bytes if (that.hashCode == hashCode && getBoost() == that.getBoost() && Arrays.equals(termsAndFields, that.termsAndFields)) { @@ -212,11 +208,6 @@ public class TermsQuery extends Query implements Accountable { return false; } - @Override - public int hashCode() { - return hashCode ^ Float.floatToIntBits(getBoost()); - } - @Override public String toString(String defaultField) { StringBuilder builder = new StringBuilder(); diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/FunctionQuery.java b/lucene/queries/src/java/org/apache/lucene/queries/function/FunctionQuery.java index b29b336ca94..2743ab9f0f0 100644 --- a/lucene/queries/src/java/org/apache/lucene/queries/function/FunctionQuery.java +++ b/lucene/queries/src/java/org/apache/lucene/queries/function/FunctionQuery.java @@ -203,14 +203,12 @@ public class FunctionQuery extends Query { public boolean equals(Object o) { if (!FunctionQuery.class.isInstance(o)) return false; FunctionQuery other = (FunctionQuery)o; - return this.getBoost() == other.getBoost() + return super.equals(o) && this.func.equals(other.func); } - /** Returns a hash code value for this object. */ @Override public int hashCode() { - return func.hashCode()*31 + Float.floatToIntBits(getBoost()); + return super.hashCode() ^ func.hashCode(); } - } diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/surround/query/RewriteQuery.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/surround/query/RewriteQuery.java index 33a0e1ea2aa..d036a0427be 100644 --- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/surround/query/RewriteQuery.java +++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/surround/query/RewriteQuery.java @@ -49,7 +49,7 @@ abstract class RewriteQuery extends Query { @Override public int hashCode() { - return getClass().hashCode() + return super.hashCode() ^ fieldName.hashCode() ^ qf.hashCode() ^ srndQuery.hashCode(); @@ -62,9 +62,10 @@ abstract class RewriteQuery extends Query { if (! getClass().equals(obj.getClass())) return false; RewriteQuery other = (RewriteQuery)obj; - return fieldName.equals(other.fieldName) - && qf.equals(other.qf) - && srndQuery.equals(other.srndQuery); + return super.equals(obj) + && fieldName.equals(other.fieldName) + && qf.equals(other.qf) + && srndQuery.equals(other.srndQuery); } /** diff --git a/lucene/sandbox/src/java/org/apache/lucene/search/TermAutomatonQuery.java b/lucene/sandbox/src/java/org/apache/lucene/search/TermAutomatonQuery.java index 85a33f14135..b52cd159367 100644 --- a/lucene/sandbox/src/java/org/apache/lucene/search/TermAutomatonQuery.java +++ b/lucene/sandbox/src/java/org/apache/lucene/search/TermAutomatonQuery.java @@ -258,7 +258,7 @@ public class TermAutomatonQuery extends Query { // NOTE: not quite correct, because if terms were added in different // order in each query but the language is the same, we return false: - return (this.getBoost() == other.getBoost()) + return super.equals(o) && this.termToID.equals(other.termToID) && Operations.sameLanguage(det, other.det); } @@ -269,7 +269,7 @@ public class TermAutomatonQuery extends Query { if (det == null) { throw new IllegalStateException("please call finish first"); } - return Float.floatToIntBits(getBoost()) ^ termToID.hashCode() + det.toDot().hashCode(); + return super.hashCode() ^ termToID.hashCode() + det.toDot().hashCode(); } /** Returns the dot (graphviz) representation of this automaton.