Skip to content

Apache Lucene open-source search software

#34408: Add .equals/.hashCode to RangeQuery to facilitate use within hash maps and in unit test comparisons

Erik Hatchere676822c771b
diff --git a/src/java/org/apache/lucene/search/RangeQuery.java b/src/java/org/apache/lucene/search/RangeQuery.java
index 59502ad6e7c..bf082e7fe91 100644
--- a/src/java/org/apache/lucene/search/RangeQuery.java
+++ b/src/java/org/apache/lucene/search/RangeQuery.java
@@ -144,4 +144,26 @@ public class RangeQuery extends Query
}
return buffer.toString();
}
+
+ /** Returns true iff <code>o</code> is equal to this. */
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof RangeQuery)) return false;
+
+ final RangeQuery other = (RangeQuery) o;
+ if (this.getBoost() != other.getBoost()) return false;
+ if (this.inclusive != other.inclusive) return false;
+ // one of lowerTerm and upperTerm can be null
+ if (this.lowerTerm != null ? !this.lowerTerm.equals(other.lowerTerm) : other.lowerTerm != null) return false;
+ if (this.upperTerm != null ? !this.upperTerm.equals(other.upperTerm) : other.upperTerm != null) return false;
+ return true;
+ }
+
+ /** Returns a hash code value for this object.*/
+ public int hashCode() {
+ return Float.floatToIntBits(getBoost()) ^
+ (lowerTerm != null ? lowerTerm.hashCode() : 0) ^
+ (upperTerm != null ? upperTerm.hashCode() : 0) ^
+ (this.inclusive ? 1 : 0);
+ }
}
diff --git a/src/test/org/apache/lucene/search/TestRangeQuery.java b/src/test/org/apache/lucene/search/TestRangeQuery.java
index 5810d943054..3252733bed1 100644
--- a/src/test/org/apache/lucene/search/TestRangeQuery.java
+++ b/src/test/org/apache/lucene/search/TestRangeQuery.java
@@ -69,6 +69,51 @@ public class TestRangeQuery extends TestCase {
searcher.close();
}
+ public void testEqualsHashcode() {
+ Query query = new RangeQuery(new Term("content", "A"),
+ new Term("content", "C"),
+ true);
+ query.setBoost(1.0f);
+ Query other = new RangeQuery(new Term("content", "A"),
+ new Term("content", "C"),
+ true);
+ other.setBoost(1.0f);
+
+ assertEquals("query equals itself is true", query, query);
+ assertEquals("equivalent queries are equal", query, other);
+ assertEquals("hashcode must return same value when equals is true", query.hashCode(), other.hashCode());
+
+ other.setBoost(2.0f);
+ assertFalse("Different boost queries are not equal", query.equals(other));
+
+ other = new RangeQuery(new Term("notcontent", "A"), new Term("notcontent", "C"), true);
+ assertFalse("Different fields are not equal", query.equals(other));
+
+ other = new RangeQuery(new Term("content", "X"), new Term("content", "C"), true);
+ assertFalse("Different lower terms are not equal", query.equals(other));
+
+ other = new RangeQuery(new Term("content", "A"), new Term("content", "Z"), true);
+ assertFalse("Different upper terms are not equal", query.equals(other));
+
+ query = new RangeQuery(null, new Term("content", "C"), true);
+ other = new RangeQuery(null, new Term("content", "C"), true);
+ assertEquals("equivalent queries with null lowerterms are equal()", query, other);
+ assertEquals("hashcode must return same value when equals is true", query.hashCode(), other.hashCode());
+
+ query = new RangeQuery(new Term("content", "C"), null, true);
+ other = new RangeQuery(new Term("content", "C"), null, true);
+ assertEquals("equivalent queries with null upperterms are equal()", query, other);
+ assertEquals("hashcode returns same value", query.hashCode(), other.hashCode());
+
+ query = new RangeQuery(null, new Term("content", "C"), true);
+ other = new RangeQuery(new Term("content", "C"), null, true);
+ assertFalse("queries with different upper and lower terms are not equal", query.equals(other));
+
+ query = new RangeQuery(new Term("content", "A"), new Term("content", "C"), false);
+ other = new RangeQuery(new Term("content", "A"), new Term("content", "C"), true);
+ assertFalse("queries with different inclusive are not equal", query.equals(other));
+ }
+
private void initializeIndex(String[] values) throws IOException {
IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
for (int i = 0; i < values.length; i++) {