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

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@161045 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2005-04-12 12:34:56 +00:00
parent 2fe0a80189
commit e676822c77
2 changed files with 67 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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++) {