From 976501f6f3fe602ada85a6f2392d1bb23438a319 Mon Sep 17 00:00:00 2001 From: Alan Woodward Date: Wed, 29 Jun 2016 10:36:28 +0100 Subject: [PATCH] LUCENE-7359: Add equals() and hashCode() to Explanation --- lucene/CHANGES.txt | 2 ++ .../org/apache/lucene/search/Explanation.java | 19 ++++++++++-- .../lucene/search/TestSimpleExplanations.java | 29 +++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 912e601750f..53e18a566c8 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -56,6 +56,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 3e5951a5a0f..6af1019bdf8 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; @@ -121,7 +120,6 @@ public final class Explanation { return buffer.toString(); } - /** Render an explanation as HTML. */ @Deprecated public String toHtml() { @@ -142,4 +140,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()); + + } + }