lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From markrmil...@apache.org
Subject svn commit: r772156 - in /lucene/java/trunk/src/java/org/apache/lucene: index/Term.java search/FuzzyQuery.java search/MultiTermQuery.java search/PrefixQuery.java search/RangeQuery.java search/WildcardQuery.java
Date Wed, 06 May 2009 11:42:19 GMT
Author: markrmiller
Date: Wed May  6 11:42:05 2009
New Revision: 772156

URL: http://svn.apache.org/viewvc?rev=772156&view=rev
Log:
LUCENE-1621: deprecate MultiTermQuery:getTerm

Modified:
    lucene/java/trunk/src/java/org/apache/lucene/index/Term.java
    lucene/java/trunk/src/java/org/apache/lucene/search/FuzzyQuery.java
    lucene/java/trunk/src/java/org/apache/lucene/search/MultiTermQuery.java
    lucene/java/trunk/src/java/org/apache/lucene/search/PrefixQuery.java
    lucene/java/trunk/src/java/org/apache/lucene/search/RangeQuery.java
    lucene/java/trunk/src/java/org/apache/lucene/search/WildcardQuery.java

Modified: lucene/java/trunk/src/java/org/apache/lucene/index/Term.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/index/Term.java?rev=772156&r1=772155&r2=772156&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/index/Term.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/index/Term.java Wed May  6 11:42:05 2009
@@ -71,22 +71,35 @@
       return new Term(field,text,false);
   }
 
-  /** Compares two terms, returning true iff they have the same
-      field and text. */
-  public final boolean equals(Object o) {
-    if (o == this)
+  //@Override
+  public boolean equals(Object obj) {
+    if (this == obj)
       return true;
-    if (o == null)
+    if (obj == null)
       return false;
-    if (!(o instanceof Term))
+    if (getClass() != obj.getClass())
       return false;
-    Term other = (Term)o;
-    return field == other.field && text.equals(other.text);
+    Term other = (Term) obj;
+    if (field == null) {
+      if (other.field != null)
+        return false;
+    } else if (!field.equals(other.field))
+      return false;
+    if (text == null) {
+      if (other.text != null)
+        return false;
+    } else if (!text.equals(other.text))
+      return false;
+    return true;
   }
 
-  /** Combines the hashCode() of the field and the text. */
-  public final int hashCode() {
-    return field.hashCode() + text.hashCode();
+  //@Override
+  public int hashCode() {
+    final int prime = 31;
+    int result = 1;
+    result = prime * result + ((field == null) ? 0 : field.hashCode());
+    result = prime * result + ((text == null) ? 0 : text.hashCode());
+    return result;
   }
 
   public int compareTo(Object other) {

Modified: lucene/java/trunk/src/java/org/apache/lucene/search/FuzzyQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/FuzzyQuery.java?rev=772156&r1=772155&r2=772156&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/FuzzyQuery.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/FuzzyQuery.java Wed May  6 11:42:05
2009
@@ -36,6 +36,8 @@
   private int prefixLength;
   private boolean termLongEnough = false;
   
+  protected Term term;
+  
   /**
    * Create a new FuzzyQuery that will match terms with a similarity 
    * of at least <code>minimumSimilarity</code> to <code>term</code>.
@@ -53,7 +55,8 @@
    * or if prefixLength &lt; 0
    */
   public FuzzyQuery(Term term, float minimumSimilarity, int prefixLength) throws IllegalArgumentException
{
-    super(term);
+    super(term); // will be removed in 3.0
+    this.term = term;
     
     if (minimumSimilarity >= 1.0f)
       throw new IllegalArgumentException("minimumSimilarity >= 1");
@@ -104,6 +107,13 @@
   protected FilteredTermEnum getEnum(IndexReader reader) throws IOException {
     return new FuzzyTermEnum(reader, getTerm(), minimumSimilarity, prefixLength);
   }
+  
+  /**
+   * Returns the pattern term.
+   */
+  public Term getTerm() {
+    return term;
+  }
 
   public void setConstantScoreRewrite(boolean constantScoreRewrite) {
     throw new UnsupportedOperationException("FuzzyQuery cannot rewrite to a constant score
query");
@@ -158,7 +168,6 @@
     
   public String toString(String field) {
     StringBuffer buffer = new StringBuffer();
-    Term term = getTerm();
     if (!term.field().equals(field)) {
         buffer.append(term.field());
         buffer.append(":");
@@ -200,23 +209,35 @@
     
   }
 
-  public boolean equals(Object o) {
-    if (this == o) return true;
-    if (!(o instanceof FuzzyQuery)) return false;
-    if (!super.equals(o)) return false;
-
-    final FuzzyQuery fuzzyQuery = (FuzzyQuery) o;
-
-    if (minimumSimilarity != fuzzyQuery.minimumSimilarity) return false;
-    if (prefixLength != fuzzyQuery.prefixLength) return false;
+  public int hashCode() {
+    final int prime = 31;
+    int result = 1;
+    result = prime * result + Float.floatToIntBits(minimumSimilarity);
+    result = prime * result + prefixLength;
+    result = prime * result + ((term == null) ? 0 : term.hashCode());
+    return result;
+  }
 
+  public boolean equals(Object obj) {
+    if (this == obj)
+      return true;
+    if (obj == null)
+      return false;
+    if (getClass() != obj.getClass())
+      return false;
+    FuzzyQuery other = (FuzzyQuery) obj;
+    if (Float.floatToIntBits(minimumSimilarity) != Float
+        .floatToIntBits(other.minimumSimilarity))
+      return false;
+    if (prefixLength != other.prefixLength)
+      return false;
+    if (term == null) {
+      if (other.term != null)
+        return false;
+    } else if (!term.equals(other.term))
+      return false;
     return true;
   }
 
-  public int hashCode() {
-    int result = super.hashCode();
-    result = 29 * result + minimumSimilarity != +0.0f ? Float.floatToIntBits(minimumSimilarity)
: 0;
-    result = 29 * result + prefixLength;
-    return result;
-  }
+
 }

Modified: lucene/java/trunk/src/java/org/apache/lucene/search/MultiTermQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/MultiTermQuery.java?rev=772156&r1=772155&r2=772156&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/MultiTermQuery.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/MultiTermQuery.java Wed May  6 11:42:05
2009
@@ -39,6 +39,7 @@
  * override equals and hashcode.
  */
 public abstract class MultiTermQuery extends Query {
+  /* @deprecated move to sub class */
   protected Term term;
   protected boolean constantScoreRewrite = false;
   transient int numberOfTerms = 0;
@@ -55,7 +56,11 @@
   public MultiTermQuery() {
   }
 
-  /** Returns the pattern term. */
+  /**
+   * Returns the pattern term.
+   * @deprecated check sub class for possible term access - getTerm does not
+   * make sense for all MultiTermQuerys and will be removed.
+   */
   public Term getTerm() {
     return term;
   }
@@ -121,7 +126,11 @@
     }
   }
 
-  /** Prints a user-readable version of this query. */
+
+  /* Prints a user-readable version of this query.
+   * Implemented for back compat in case MultiTermQuery
+   * subclasses do no implement.
+   */
   public String toString(String field) {
     StringBuffer buffer = new StringBuffer();
     if (term != null) {
@@ -145,31 +154,29 @@
     this.constantScoreRewrite = constantScoreRewrite;
   }
 
-  public boolean equals(Object o) {
-    if (o == null || term == null) {
-      throw new UnsupportedOperationException(
-          "MultiTermQuerys that do not use a pattern term need to override equals/hashcode");
-    }
+  //@Override
+  public int hashCode() {
+    final int prime = 31;
+    int result = 1;
+    result = prime * result + Float.floatToIntBits(getBoost());
+    result = prime * result + (constantScoreRewrite ? 1231 : 1237);
+    return result;
+  }
 
-    if (this == o)
+  //@Override
+  public boolean equals(Object obj) {
+    if (this == obj)
       return true;
-    if (!(o instanceof MultiTermQuery))
+    if (obj == null)
       return false;
-
-    final MultiTermQuery multiTermQuery = (MultiTermQuery) o;
-
-    if (!term.equals(multiTermQuery.term))
+    if (getClass() != obj.getClass())
       return false;
-
-    return getBoost() == multiTermQuery.getBoost();
-  }
-
-  public int hashCode() {
-    if (term == null) {
-      throw new UnsupportedOperationException(
-          "MultiTermQuerys that do not use a pattern term need to override equals/hashcode");
-    }
-    return term.hashCode() + Float.floatToRawIntBits(getBoost());
+    MultiTermQuery other = (MultiTermQuery) obj;
+    if (Float.floatToIntBits(getBoost()) != Float.floatToIntBits(other.getBoost()))
+      return false;
+    if (constantScoreRewrite != other.constantScoreRewrite)
+      return false;
+    return true;
   }
-
+ 
 }

Modified: lucene/java/trunk/src/java/org/apache/lucene/search/PrefixQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/PrefixQuery.java?rev=772156&r1=772155&r2=772156&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/PrefixQuery.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/PrefixQuery.java Wed May  6 11:42:05
2009
@@ -30,7 +30,7 @@
 
   /** Constructs a query for terms starting with <code>prefix</code>. */
   public PrefixQuery(Term prefix) {
-    super(prefix);
+    super(prefix); //will be removed in 3.0
     this.prefix = prefix;
   }
 
@@ -38,14 +38,7 @@
   public Term getPrefix() { return prefix; }
   
   protected FilteredTermEnum getEnum(IndexReader reader) throws IOException {
-    return new PrefixTermEnum(reader, getTerm());
-  }
-
-  public boolean equals(Object o) {
-    if (o instanceof PrefixQuery)
-      return super.equals(o);
-
-    return false;
+    return new PrefixTermEnum(reader, prefix);
   }
 
   /** Prints a user-readable version of this query. */
@@ -60,4 +53,30 @@
     buffer.append(ToStringUtils.boost(getBoost()));
     return buffer.toString();
   }
+
+  //@Override
+  public int hashCode() {
+    final int prime = 31;
+    int result = super.hashCode();
+    result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
+    return result;
+  }
+
+  //@Override
+  public boolean equals(Object obj) {
+    if (this == obj)
+      return true;
+    if (!super.equals(obj))
+      return false;
+    if (getClass() != obj.getClass())
+      return false;
+    PrefixQuery other = (PrefixQuery) obj;
+    if (prefix == null) {
+      if (other.prefix != null)
+        return false;
+    } else if (!prefix.equals(other.prefix))
+      return false;
+    return true;
+  }
+
 }

Modified: lucene/java/trunk/src/java/org/apache/lucene/search/RangeQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/RangeQuery.java?rev=772156&r1=772155&r2=772156&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/RangeQuery.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/RangeQuery.java Wed May  6 11:42:05
2009
@@ -29,7 +29,7 @@
  * the useOldRangeQuery property set to true. The QueryParser default behaviour is to use
  * the newer ConstantScore mode. This is generally preferable because:
  * <ul>
- *  <li>It is faster than the standard RangeQuery mode</li>
+ *  <li>In certain situations, it may be faster than the standard RangeQuery mode</li>
  *  <li>Unlike the RangeQuery mode, it does not cause a BooleanQuery.TooManyClauses
exception if the range of values is large</li>
  *  <li>Unlike the RangeQuery mode, it does not influence scoring based on the scarcity
of individual terms that may match</li>
  * </ul>
@@ -195,40 +195,53 @@
       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;
-    RangeQuery other = (RangeQuery) o;
-
-    if (this.field != other.field  // interned comparison
-        || this.includeLower != other.includeLower
-        || this.includeUpper != other.includeUpper
-        || (this.collator != null && ! this.collator.equals(other.collator) || (this.collator
== null && other.collator != null))
-       ) { return false; }
-    String lowerVal = this.lowerTerm == null ? null : lowerTerm.text();
-    String upperVal = this.upperTerm == null ? null : upperTerm.text();
-    String olowerText = other.lowerTerm == null ? null : other.lowerTerm.text();
-    String oupperText = other.upperTerm == null ? null : other.upperTerm.text();
-    if (lowerVal != null ? !lowerVal.equals(olowerText) : olowerText != null) return false;
-    if (upperVal != null ? !upperVal.equals(oupperText) : oupperText != null) return false;
-    return this.getBoost() == other.getBoost();
-  }
-
-  /** Returns a hash code value for this object.*/
+  //@Override
   public int hashCode() {
-    int h = Float.floatToIntBits(getBoost()) ^ field.hashCode();
-    String lowerVal = this.lowerTerm == null ? null : lowerTerm.text();
-    String upperVal = this.upperTerm == null ? null : upperTerm.text();
-    // hashCode of "" is 0, so don't use that for null...
-    h ^= lowerVal != null ? lowerVal.hashCode() : 0x965a965a;
-    // don't just XOR upperVal with out mixing either it or h, as it will cancel
-    // out lowerVal if they are equal.
-    h ^= (h << 17) | (h >>> 16);  // a reversible (one to one) 32 bit mapping
mix
-    h ^= (upperVal != null ? (upperVal.hashCode()) : 0x5a695a69);
-    h ^= (includeLower ? 0x665599aa : 0)
-       ^ (includeUpper ? 0x99aa5566 : 0);
-    h ^= collator != null ? collator.hashCode() : 0;
-    return h;
+    final int prime = 31;
+    int result = super.hashCode();
+    result = prime * result + ((collator == null) ? 0 : collator.hashCode());
+    result = prime * result + ((field == null) ? 0 : field.hashCode());
+    result = prime * result + (includeLower ? 1231 : 1237);
+    result = prime * result + (includeUpper ? 1231 : 1237);
+    result = prime * result + ((lowerTerm == null) ? 0 : lowerTerm.hashCode());
+    result = prime * result + ((upperTerm == null) ? 0 : upperTerm.hashCode());
+    return result;
+  }
+
+  //@Override
+  public boolean equals(Object obj) {
+    if (this == obj)
+      return true;
+    if (!super.equals(obj))
+      return false;
+    if (getClass() != obj.getClass())
+      return false;
+    RangeQuery other = (RangeQuery) obj;
+    if (collator == null) {
+      if (other.collator != null)
+        return false;
+    } else if (!collator.equals(other.collator))
+      return false;
+    if (field == null) {
+      if (other.field != null)
+        return false;
+    } else if (!field.equals(other.field))
+      return false;
+    if (includeLower != other.includeLower)
+      return false;
+    if (includeUpper != other.includeUpper)
+      return false;
+    if (lowerTerm == null) {
+      if (other.lowerTerm != null)
+        return false;
+    } else if (!lowerTerm.equals(other.lowerTerm))
+      return false;
+    if (upperTerm == null) {
+      if (other.upperTerm != null)
+        return false;
+    } else if (!upperTerm.equals(other.upperTerm))
+      return false;
+    return true;
   }
+
 }

Modified: lucene/java/trunk/src/java/org/apache/lucene/search/WildcardQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/WildcardQuery.java?rev=772156&r1=772155&r2=772156&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/WildcardQuery.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/WildcardQuery.java Wed May  6 11:42:05
2009
@@ -19,6 +19,8 @@
 
 import org.apache.lucene.index.IndexReader;
 import org.apache.lucene.index.Term;
+import org.apache.lucene.util.ToStringUtils;
+
 import java.io.IOException;
 
 /** Implements the wildcard search query. Supported wildcards are <code>*</code>,
which
@@ -32,21 +34,23 @@
  */
 public class WildcardQuery extends MultiTermQuery {
   private boolean termContainsWildcard;
+  protected Term term;
     
   public WildcardQuery(Term term) {
-    super(term);
+    super(term); //will be removed in 3.0
+    this.term = term;
     this.termContainsWildcard = (term.text().indexOf('*') != -1) || (term.text().indexOf('?')
!= -1);
   }
 
   protected FilteredTermEnum getEnum(IndexReader reader) throws IOException {
     return new WildcardTermEnum(reader, getTerm());
   }
-
-  public boolean equals(Object o) {
-    if (o instanceof WildcardQuery)
-      return super.equals(o);
-
-    return false;
+  
+  /**
+   * Returns the pattern term.
+   */
+  public Term getTerm() {
+    return term;
   }
 
   public Query rewrite(IndexReader reader) throws IOException {
@@ -55,4 +59,42 @@
     else
       return super.rewrite(reader);
   }
+  
+  /** Prints a user-readable version of this query. */
+  public String toString(String field) {
+    StringBuffer buffer = new StringBuffer();
+    if (!term.field().equals(field)) {
+      buffer.append(term.field());
+      buffer.append(":");
+    }
+    buffer.append(term.text());
+    buffer.append(ToStringUtils.boost(getBoost()));
+    return buffer.toString();
+  }
+
+  //@Override
+  public int hashCode() {
+    final int prime = 31;
+    int result = super.hashCode();
+    result = prime * result + ((term == null) ? 0 : term.hashCode());
+    return result;
+  }
+
+  //@Override
+  public boolean equals(Object obj) {
+    if (this == obj)
+      return true;
+    if (!super.equals(obj))
+      return false;
+    if (getClass() != obj.getClass())
+      return false;
+    WildcardQuery other = (WildcardQuery) obj;
+    if (term == null) {
+      if (other.term != null)
+        return false;
+    } else if (!term.equals(other.term))
+      return false;
+    return true;
+  }
+
 }



Mime
View raw message