diff --git a/src/java/org/apache/lucene/search/Query.java b/src/java/org/apache/lucene/search/Query.java index 7784d9f64f7..2d92fa5693f 100644 --- a/src/java/org/apache/lucene/search/Query.java +++ b/src/java/org/apache/lucene/search/Query.java @@ -213,4 +213,24 @@ public abstract class Query implements java.io.Serializable, Cloneable { throw new RuntimeException("Clone not supported: " + e.getMessage()); } } + + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + Float.floatToIntBits(boost); + return result; + } + + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Query other = (Query) obj; + if (Float.floatToIntBits(boost) != Float.floatToIntBits(other.boost)) + return false; + return true; + } } diff --git a/src/java/org/apache/lucene/search/payloads/AveragePayloadFunction.java b/src/java/org/apache/lucene/search/payloads/AveragePayloadFunction.java index aa05f63cbb4..9101d570033 100644 --- a/src/java/org/apache/lucene/search/payloads/AveragePayloadFunction.java +++ b/src/java/org/apache/lucene/search/payloads/AveragePayloadFunction.java @@ -1,6 +1,5 @@ package org.apache.lucene.search.payloads; -import org.apache.lucene.index.Term; /** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with @@ -35,4 +34,20 @@ public class AveragePayloadFunction extends PayloadFunction{ return numPayloadsSeen > 0 ? (payloadScore / numPayloadsSeen) : 1; } + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + this.getClass().hashCode(); + return result; + } + + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + return true; + } } diff --git a/src/java/org/apache/lucene/search/payloads/BoostingFunctionTermQuery.java b/src/java/org/apache/lucene/search/payloads/BoostingFunctionTermQuery.java index d326d02dc33..38da05d5213 100644 --- a/src/java/org/apache/lucene/search/payloads/BoostingFunctionTermQuery.java +++ b/src/java/org/apache/lucene/search/payloads/BoostingFunctionTermQuery.java @@ -170,11 +170,31 @@ public class BoostingFunctionTermQuery extends SpanTermQuery implements Payload } } - public boolean equals(Object o) { - if (!(o instanceof BoostingFunctionTermQuery)) - return false; - BoostingFunctionTermQuery other = (BoostingFunctionTermQuery) o; - return (this.getBoost() == other.getBoost()) - && this.term.equals(other.term) && this.function.equals(other.function); + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + ((function == null) ? 0 : function.hashCode()); + result = prime * result + (includeSpanScore ? 1231 : 1237); + return result; } + + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!super.equals(obj)) + return false; + if (getClass() != obj.getClass()) + return false; + BoostingFunctionTermQuery other = (BoostingFunctionTermQuery) obj; + if (function == null) { + if (other.function != null) + return false; + } else if (!function.equals(other.function)) + return false; + if (includeSpanScore != other.includeSpanScore) + return false; + return true; + } + + } diff --git a/src/java/org/apache/lucene/search/payloads/MaxPayloadFunction.java b/src/java/org/apache/lucene/search/payloads/MaxPayloadFunction.java index b0b50bedb77..ed8af9de1dc 100644 --- a/src/java/org/apache/lucene/search/payloads/MaxPayloadFunction.java +++ b/src/java/org/apache/lucene/search/payloads/MaxPayloadFunction.java @@ -1,6 +1,5 @@ package org.apache.lucene.search.payloads; -import org.apache.lucene.index.Term; /** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with @@ -25,7 +24,7 @@ import org.apache.lucene.index.Term; * Is thread safe and completely reusable. * **/ -public class MaxPayloadFunction extends PayloadFunction{ +public class MaxPayloadFunction extends PayloadFunction { public float currentScore(int docId, String field, int start, int end, int numPayloadsSeen, float currentScore, float currentPayloadScore) { return Math.max(currentPayloadScore, currentScore); } @@ -33,4 +32,21 @@ public class MaxPayloadFunction extends PayloadFunction{ public float docScore(int docId, String field, int numPayloadsSeen, float payloadScore) { return numPayloadsSeen > 0 ? payloadScore : 1; } + + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + this.getClass().hashCode(); + return result; + } + + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + return true; + } } diff --git a/src/java/org/apache/lucene/search/payloads/MinPayloadFunction.java b/src/java/org/apache/lucene/search/payloads/MinPayloadFunction.java index cd68469525b..bc7cbb2b4e5 100644 --- a/src/java/org/apache/lucene/search/payloads/MinPayloadFunction.java +++ b/src/java/org/apache/lucene/search/payloads/MinPayloadFunction.java @@ -1,7 +1,21 @@ package org.apache.lucene.search.payloads; -import org.apache.lucene.index.Term; - +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ /** * Calculates the miniumum payload seen @@ -16,5 +30,22 @@ public class MinPayloadFunction extends PayloadFunction { public float docScore(int docId, String field, int numPayloadsSeen, float payloadScore) { return numPayloadsSeen > 0 ? payloadScore : 1; } + + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + this.getClass().hashCode(); + return result; + } + + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + return true; + } } diff --git a/src/java/org/apache/lucene/search/spans/SpanTermQuery.java b/src/java/org/apache/lucene/search/spans/SpanTermQuery.java index e37d2d44890..8dc0bd6e574 100644 --- a/src/java/org/apache/lucene/search/spans/SpanTermQuery.java +++ b/src/java/org/apache/lucene/search/spans/SpanTermQuery.java @@ -61,18 +61,27 @@ public class SpanTermQuery extends SpanQuery { return buffer.toString(); } - /** Returns true iff o is equal to this. */ - public boolean equals(Object o) { - if (!(o instanceof SpanTermQuery)) - return false; - SpanTermQuery other = (SpanTermQuery)o; - return (this.getBoost() == other.getBoost()) - && this.term.equals(other.term); + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + ((term == null) ? 0 : term.hashCode()); + return result; } - /** Returns a hash code value for this object.*/ - public int hashCode() { - return Float.floatToIntBits(getBoost()) ^ term.hashCode() ^ 0xD23FE494; + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!super.equals(obj)) + return false; + if (getClass() != obj.getClass()) + return false; + SpanTermQuery other = (SpanTermQuery) obj; + if (term == null) { + if (other.term != null) + return false; + } else if (!term.equals(other.term)) + return false; + return true; } public Spans getSpans(final IndexReader reader) throws IOException { diff --git a/src/test/org/apache/lucene/search/payloads/BoostingFunctionTermQueryTest.java b/src/test/org/apache/lucene/search/payloads/BoostingFunctionTermQueryTest.java index 701bd97f55a..3265a34cc75 100644 --- a/src/test/org/apache/lucene/search/payloads/BoostingFunctionTermQueryTest.java +++ b/src/test/org/apache/lucene/search/payloads/BoostingFunctionTermQueryTest.java @@ -19,12 +19,14 @@ package org.apache.lucene.search.payloads; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.English; import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.QueryUtils; import org.apache.lucene.search.TopDocs; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.CheckHits; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.DefaultSimilarity; +import org.apache.lucene.search.spans.SpanTermQuery; import org.apache.lucene.search.spans.Spans; import org.apache.lucene.search.spans.TermSpans; import org.apache.lucene.analysis.Analyzer; @@ -151,6 +153,21 @@ public class BoostingFunctionTermQueryTest extends LuceneTestCase { }*/ } + + public void testQuery() { + BoostingFunctionTermQuery boostingFuncTermQuery = new BoostingFunctionTermQuery(new Term(PayloadHelper.MULTI_FIELD, "seventy"), + new MaxPayloadFunction()); + QueryUtils.check(boostingFuncTermQuery); + + SpanTermQuery spanTermQuery = new SpanTermQuery(new Term(PayloadHelper.MULTI_FIELD, "seventy")); + + assertTrue(boostingFuncTermQuery.equals(spanTermQuery) == spanTermQuery.equals(boostingFuncTermQuery)); + + BoostingFunctionTermQuery boostingFuncTermQuery2 = new BoostingFunctionTermQuery(new Term(PayloadHelper.MULTI_FIELD, "seventy"), + new AveragePayloadFunction()); + + QueryUtils.checkUnequal(boostingFuncTermQuery, boostingFuncTermQuery2); + } public void testMultipleMatchesPerDoc() throws Exception { BoostingFunctionTermQuery query = new BoostingFunctionTermQuery(new Term(PayloadHelper.MULTI_FIELD, "seventy"),