diff --git a/src/java/org/apache/lucene/index/Term.java b/src/java/org/apache/lucene/index/Term.java
index 23d2569aa5f..3a6cce85d3e 100644
--- a/src/java/org/apache/lucene/index/Term.java
+++ b/src/java/org/apache/lucene/index/Term.java
@@ -71,22 +71,35 @@ public final class Term implements Comparable, java.io.Serializable {
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) {
diff --git a/src/java/org/apache/lucene/search/FuzzyQuery.java b/src/java/org/apache/lucene/search/FuzzyQuery.java
index c355ad2dc7f..0adc973512e 100644
--- a/src/java/org/apache/lucene/search/FuzzyQuery.java
+++ b/src/java/org/apache/lucene/search/FuzzyQuery.java
@@ -36,6 +36,8 @@ public class FuzzyQuery extends MultiTermQuery {
private int prefixLength;
private boolean termLongEnough = false;
+ protected Term term;
+
/**
* Create a new FuzzyQuery that will match terms with a similarity
* of at least minimumSimilarity
to term
.
@@ -53,7 +55,8 @@ public class FuzzyQuery extends MultiTermQuery {
* or if prefixLength < 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 @@ public class FuzzyQuery extends MultiTermQuery {
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 class FuzzyQuery extends MultiTermQuery {
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 class FuzzyQuery extends MultiTermQuery {
}
- 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;
- }
+
}
diff --git a/src/java/org/apache/lucene/search/MultiTermQuery.java b/src/java/org/apache/lucene/search/MultiTermQuery.java
index 36ef46cf816..651a101d925 100644
--- a/src/java/org/apache/lucene/search/MultiTermQuery.java
+++ b/src/java/org/apache/lucene/search/MultiTermQuery.java
@@ -39,6 +39,7 @@ import org.apache.lucene.util.ToStringUtils;
* 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 abstract class MultiTermQuery extends Query {
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 @@ public abstract class MultiTermQuery extends Query {
}
}
- /** 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 @@ public abstract class MultiTermQuery extends Query {
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");
- }
-
- if (this == o)
- return true;
- if (!(o instanceof MultiTermQuery))
- return false;
-
- final MultiTermQuery multiTermQuery = (MultiTermQuery) o;
-
- if (!term.equals(multiTermQuery.term))
- return false;
-
- return getBoost() == multiTermQuery.getBoost();
- }
-
+ //@Override
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());
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + Float.floatToIntBits(getBoost());
+ result = prime * result + (constantScoreRewrite ? 1231 : 1237);
+ return result;
}
+ //@Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ MultiTermQuery other = (MultiTermQuery) obj;
+ if (Float.floatToIntBits(getBoost()) != Float.floatToIntBits(other.getBoost()))
+ return false;
+ if (constantScoreRewrite != other.constantScoreRewrite)
+ return false;
+ return true;
+ }
+
}
diff --git a/src/java/org/apache/lucene/search/PrefixQuery.java b/src/java/org/apache/lucene/search/PrefixQuery.java
index 5eb5d9f648f..a229051c5f2 100644
--- a/src/java/org/apache/lucene/search/PrefixQuery.java
+++ b/src/java/org/apache/lucene/search/PrefixQuery.java
@@ -30,7 +30,7 @@ public class PrefixQuery extends MultiTermQuery {
/** Constructs a query for terms starting with prefix
. */
public PrefixQuery(Term prefix) {
- super(prefix);
+ super(prefix); //will be removed in 3.0
this.prefix = prefix;
}
@@ -38,14 +38,7 @@ public class PrefixQuery extends MultiTermQuery {
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 @@ public class PrefixQuery extends MultiTermQuery {
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;
+ }
+
}
diff --git a/src/java/org/apache/lucene/search/RangeQuery.java b/src/java/org/apache/lucene/search/RangeQuery.java
index c081907382b..dc74e017132 100644
--- a/src/java/org/apache/lucene/search/RangeQuery.java
+++ b/src/java/org/apache/lucene/search/RangeQuery.java
@@ -29,7 +29,7 @@ import org.apache.lucene.index.IndexReader;
* the useOldRangeQuery property set to true. The QueryParser default behaviour is to use
* the newer ConstantScore mode. This is generally preferable because:
*
o
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;
+ }
+
}
diff --git a/src/java/org/apache/lucene/search/WildcardQuery.java b/src/java/org/apache/lucene/search/WildcardQuery.java
index c24f3249f64..147db9c6b4e 100644
--- a/src/java/org/apache/lucene/search/WildcardQuery.java
+++ b/src/java/org/apache/lucene/search/WildcardQuery.java
@@ -19,6 +19,8 @@ package org.apache.lucene.search;
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 *
, which
@@ -32,21 +34,23 @@ import java.io.IOException;
*/
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 @@ public class WildcardQuery extends MultiTermQuery {
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;
+ }
+
}