LUCENE-7359: Add equals() and hashCode() to Explanation

This commit is contained in:
Alan Woodward 2016-06-29 10:36:28 +01:00
parent 4ea95bf8f1
commit 6a703bebf7
3 changed files with 48 additions and 2 deletions

View File

@ -68,6 +68,8 @@ Improvements
and empty boolean queries now rewrite to MatchNoDocsQuery instead of
vice/versa (Jim Ferenczi via Mike McCandless)
* LUCENE-7359: Add equals() and hashCode() to Explanation (Alan Woodward)
Optimizations
* LUCENE-7330, LUCENE-7339: Speed up conjunction queries. (Adrien Grand)

View File

@ -16,7 +16,6 @@
*/
package org.apache.lucene.search;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@ -121,4 +120,20 @@ public final class Explanation {
return buffer.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Explanation that = (Explanation) o;
return match == that.match &&
Float.compare(that.value, value) == 0 &&
Objects.equals(description, that.description) &&
Objects.equals(details, that.details);
}
@Override
public int hashCode() {
return Objects.hash(match, value, description, details);
}
}

View File

@ -20,6 +20,7 @@ package org.apache.lucene.search;
import java.util.Arrays;
import org.apache.lucene.index.Term;
import org.junit.Test;
/**
* TestExplanations subclass focusing on basic query types
@ -715,4 +716,32 @@ public class TestSimpleExplanations extends BaseExplanationTestCase {
qtest(query, new int[] { 0,1,2,3 });
}
@Test
public void testEquality() {
Explanation e1 = Explanation.match(1f, "an explanation");
Explanation e2 = Explanation.match(1f, "an explanation", Explanation.match(1f, "a subexplanation"));
Explanation e25 = Explanation.match(1f, "an explanation",
Explanation.match(1f, "a subexplanation", Explanation.match(1f, "a subsubexplanation")));
Explanation e3 = Explanation.match(1f, "an explanation");
Explanation e4 = Explanation.match(2f, "an explanation");
Explanation e5 = Explanation.noMatch("an explanation");
Explanation e6 = Explanation.noMatch("an explanation", Explanation.match(1f, "a subexplanation"));
Explanation e7 = Explanation.noMatch("an explanation");
Explanation e8 = Explanation.match(1f, "another explanation");
assertEquals(e1, e3);
assertFalse(e1.equals(e2));
assertFalse(e2.equals(e25));
assertFalse(e1.equals(e4));
assertFalse(e1.equals(e5));
assertEquals(e5, e7);
assertFalse(e5.equals(e6));
assertFalse(e1.equals(e8));
assertEquals(e1.hashCode(), e3.hashCode());
assertEquals(e5.hashCode(), e7.hashCode());
}
}