Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-6946: SortField.equals now takes the missing value into account.

Adrien Grand939b22c08cff
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 9a2f890b9ac..5b6d72da9d3 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -172,6 +172,9 @@ Bug Fixes
* SOLR-7865: BlendedInfixSuggester was returning too many results
(Arcadius Ahouansou via Mike McCandless)
+* LUCENE-6946: SortField.equals now takes the missingValue parameter into
+ account. (Adrien Grand)
+
Other
* LUCENE-6924: Upgrade randomizedtesting to 2.3.2. (Dawid Weiss)
diff --git a/lucene/core/src/java/org/apache/lucene/search/SortField.java b/lucene/core/src/java/org/apache/lucene/search/SortField.java
index 9ff0678dae3..8b2c0e55a9b 100644
--- a/lucene/core/src/java/org/apache/lucene/search/SortField.java
+++ b/lucene/core/src/java/org/apache/lucene/search/SortField.java
@@ -19,9 +19,9 @@ package org.apache.lucene.search;
import java.io.IOException;
import java.util.Comparator;
+import java.util.Objects;
import org.apache.lucene.util.BytesRef;
-import org.apache.lucene.util.StringHelper;
/**
* Stores information about how to sort documents by terms in an individual
@@ -300,23 +300,20 @@ public class SortField {
if (!(o instanceof SortField)) return false;
final SortField other = (SortField)o;
return (
- StringHelper.equals(other.field, this.field)
+ Objects.equals(other.field, this.field)
&& other.type == this.type
&& other.reverse == this.reverse
- && (other.comparatorSource == null ? this.comparatorSource == null : other.comparatorSource.equals(this.comparatorSource))
+ && Objects.equals(this.comparatorSource, other.comparatorSource)
+ && Objects.equals(this.missingValue, other.missingValue)
);
}
- /** Returns true if <code>o</code> is equal to this. If a
+ /** Returns a hash code for this {@link SortField} instance. If a
* {@link FieldComparatorSource} was provided, it must properly
- * implement hashCode (unless a singleton is always
- * used). */
+ * implement hashCode (unless a singleton is always used). */
@Override
public int hashCode() {
- int hash = type.hashCode() ^ 0x346565dd + Boolean.valueOf(reverse).hashCode() ^ 0xaf5998bb;
- if (field != null) hash += field.hashCode()^0xff5685dd;
- if (comparatorSource != null) hash += comparatorSource.hashCode();
- return hash;
+ return Objects.hash(field, type, reverse, comparatorSource, missingValue);
}
private Comparator<BytesRef> bytesComparator = BytesRef.getUTF8SortedAsUnicodeComparator();
diff --git a/lucene/core/src/java/org/apache/lucene/util/StringHelper.java b/lucene/core/src/java/org/apache/lucene/util/StringHelper.java
index 2d4f3a8bdd2..bbbcdcd2337 100644
--- a/lucene/core/src/java/org/apache/lucene/util/StringHelper.java
+++ b/lucene/core/src/java/org/apache/lucene/util/StringHelper.java
@@ -71,14 +71,6 @@ public abstract class StringHelper {
private StringHelper() {
}
- public static boolean equals(String s1, String s2) {
- if (s1 == null) {
- return s2 == null;
- } else {
- return s1.equals(s2);
- }
- }
-
/**
* Returns <code>true</code> iff the ref starts with the given prefix.
* Otherwise <code>false</code>.
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestSort.java b/lucene/core/src/test/org/apache/lucene/search/TestSort.java
index 3fb598c6e2b..d6311ada869 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestSort.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestSort.java
@@ -48,7 +48,41 @@ import org.apache.lucene.util.LuceneTestCase;
* \./
*/
public class TestSort extends LuceneTestCase {
-
+
+ private void assertEquals(Sort a, Sort b) {
+ LuceneTestCase.assertEquals(a, b);
+ LuceneTestCase.assertEquals(b, a);
+ LuceneTestCase.assertEquals(a.hashCode(), b.hashCode());
+ }
+
+ private void assertDifferent(Sort a, Sort b) {
+ assertFalse(a.equals(b));
+ assertFalse(b.equals(a));
+ assertFalse(a.hashCode() == b.hashCode());
+ }
+
+ public void testEquals() {
+ SortField sortField1 = new SortField("foo", SortField.Type.STRING);
+ SortField sortField2 = new SortField("foo", SortField.Type.STRING);
+ assertEquals(new Sort(sortField1), new Sort(sortField2));
+
+ sortField2 = new SortField("bar", SortField.Type.STRING);
+ assertDifferent(new Sort(sortField1), new Sort(sortField2));
+
+ sortField2 = new SortField("foo", SortField.Type.LONG);
+ assertDifferent(new Sort(sortField1), new Sort(sortField2));
+
+ sortField2 = new SortField("foo", SortField.Type.STRING);
+ sortField2.setMissingValue(SortField.STRING_FIRST);
+ assertDifferent(new Sort(sortField1), new Sort(sortField2));
+
+ sortField2 = new SortField("foo", SortField.Type.STRING, false);
+ assertEquals(new Sort(sortField1), new Sort(sortField2));
+
+ sortField2 = new SortField("foo", SortField.Type.STRING, true);
+ assertDifferent(new Sort(sortField1), new Sort(sortField2));
+ }
+
/** Tests sorting on type string */
public void testString() throws IOException {
Directory dir = newDirectory();
diff --git a/lucene/core/src/test/org/apache/lucene/util/TestStringHelper.java b/lucene/core/src/test/org/apache/lucene/util/TestStringHelper.java
index 20bf2a494fd..be425e0d6fa 100644
--- a/lucene/core/src/test/org/apache/lucene/util/TestStringHelper.java
+++ b/lucene/core/src/test/org/apache/lucene/util/TestStringHelper.java
@@ -24,13 +24,6 @@ public class TestStringHelper extends LuceneTestCase {
BytesRef right = new BytesRef("foozo");
assertEquals(3, StringHelper.bytesDifference(left, right));
}
-
- public void testEquals() {
- assertTrue(StringHelper.equals("foo", "foo"));
- assertFalse(StringHelper.equals("foo", null));
- assertFalse(StringHelper.equals(null, "foo"));
- assertTrue(StringHelper.equals(null, null));
- }
public void testStartsWith() {
BytesRef ref = new BytesRef("foobar");