diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 22f86b2c404..2760474871e 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -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) diff --git a/lucene/core/src/java/org/apache/lucene/search/Explanation.java b/lucene/core/src/java/org/apache/lucene/search/Explanation.java index 818f83f77e2..42e3ce70624 100644 --- a/lucene/core/src/java/org/apache/lucene/search/Explanation.java +++ b/lucene/core/src/java/org/apache/lucene/search/Explanation.java @@ -16,7 +16,6 @@ */ package org.apache.lucene.search; - import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -120,5 +119,21 @@ 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); + } + } diff --git a/lucene/core/src/test/org/apache/lucene/search/TestSimpleExplanations.java b/lucene/core/src/test/org/apache/lucene/search/TestSimpleExplanations.java index 422245a8a49..9f8c589499d 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestSimpleExplanations.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestSimpleExplanations.java @@ -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()); + + } + }