You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by eh...@apache.org on 2005/04/12 14:34:57 UTC

svn commit: r161045 - in lucene/java/trunk/src: java/org/apache/lucene/search/RangeQuery.java test/org/apache/lucene/search/TestRangeQuery.java

Author: ehatcher
Date: Tue Apr 12 05:34:56 2005
New Revision: 161045

URL: http://svn.apache.org/viewcvs?view=rev&rev=161045
Log:
#34408: Add .equals/.hashCode to RangeQuery to facilitate use within hash maps and in unit test comparisons

Modified:
    lucene/java/trunk/src/java/org/apache/lucene/search/RangeQuery.java
    lucene/java/trunk/src/test/org/apache/lucene/search/TestRangeQuery.java

Modified: lucene/java/trunk/src/java/org/apache/lucene/search/RangeQuery.java
URL: http://svn.apache.org/viewcvs/lucene/java/trunk/src/java/org/apache/lucene/search/RangeQuery.java?view=diff&r1=161044&r2=161045
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/RangeQuery.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/RangeQuery.java Tue Apr 12 05:34:56 2005
@@ -144,4 +144,26 @@
         }
         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);
+    }
 }

Modified: lucene/java/trunk/src/test/org/apache/lucene/search/TestRangeQuery.java
URL: http://svn.apache.org/viewcvs/lucene/java/trunk/src/test/org/apache/lucene/search/TestRangeQuery.java?view=diff&r1=161044&r2=161045
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/search/TestRangeQuery.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/search/TestRangeQuery.java Tue Apr 12 05:34:56 2005
@@ -69,6 +69,51 @@
     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++) {