Apache Lucene open-source search software
LUCENE-5220: ExpressionSortField has crappy toString/bad equals/hashcode
Robert Muir95629b4f65a0
diff --git a/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionSortField.java b/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionSortField.javaindex 79cc8215f56..1daa435e7d1 100644--- a/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionSortField.java+++ b/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionSortField.java@@ -36,6 +36,41 @@ class ExpressionSortField extends SortField {return new ExpressionComparator(source, numHits);}+ @Override+ public int hashCode() {+ final int prime = 31;+ int result = super.hashCode();+ result = prime * result + ((source == null) ? 0 : source.hashCode());+ return result;+ }++ @Override+ public boolean equals(Object obj) {+ if (this == obj) return true;+ if (!super.equals(obj)) return false;+ if (getClass() != obj.getClass()) return false;+ ExpressionSortField other = (ExpressionSortField) obj;+ if (source == null) {+ if (other.source != null) return false;+ } else if (!source.equals(other.source)) return false;+ return true;+ }++ @Override+ public String toString() {+ StringBuilder buffer = new StringBuilder();++ buffer.append("<expr \"");+ buffer.append(getField());+ buffer.append("\">");++ if (getReverse()) {+ buffer.append('!');+ }++ return buffer.toString();+ }+@Overridepublic boolean needsScores() {return true; // TODO: maybe we can optimize by "figuring this out" somehow...diff --git a/lucene/expressions/src/test/org/apache/lucene/expressions/TestExpressionSortField.java b/lucene/expressions/src/test/org/apache/lucene/expressions/TestExpressionSortField.javanew file mode 100644index 00000000000..c3b68d87f6b--- /dev/null+++ b/lucene/expressions/src/test/org/apache/lucene/expressions/TestExpressionSortField.java@@ -0,0 +1,69 @@+package org.apache.lucene.expressions;++/*+ * 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.+ */++import org.apache.lucene.expressions.js.JavascriptCompiler;+import org.apache.lucene.search.SortField;+import org.apache.lucene.util.LuceneTestCase;++public class TestExpressionSortField extends LuceneTestCase {++ public void testToString() throws Exception {+ Expression expr = JavascriptCompiler.compile("sqrt(_score) + ln(popularity)");++ SimpleBindings bindings = new SimpleBindings();+ bindings.add(new SortField("_score", SortField.Type.SCORE));+ bindings.add(new SortField("popularity", SortField.Type.INT));++ SortField sf = expr.getSortField(bindings, true);+ assertEquals("<expr \"sqrt(_score) + ln(popularity)\">!", sf.toString());+ }++ public void testEquals() throws Exception {+ Expression expr = JavascriptCompiler.compile("sqrt(_score) + ln(popularity)");++ SimpleBindings bindings = new SimpleBindings();+ bindings.add(new SortField("_score", SortField.Type.SCORE));+ bindings.add(new SortField("popularity", SortField.Type.INT));++ SimpleBindings otherBindings = new SimpleBindings();+ bindings.add(new SortField("_score", SortField.Type.LONG));+ bindings.add(new SortField("popularity", SortField.Type.INT));++ SortField sf1 = expr.getSortField(bindings, true);++ // different order+ SortField sf2 = expr.getSortField(bindings, false);+ assertFalse(sf1.equals(sf2));++ // different bindings+ sf2 = expr.getSortField(otherBindings, true);+ assertFalse(sf1.equals(sf2));++ // different expression+ Expression other = JavascriptCompiler.compile("popularity/2");+ sf2 = other.getSortField(bindings, true);+ assertFalse(sf1.equals(sf2));++ // null+ assertFalse(sf1.equals(null));++ // same instance:+ assertEquals(sf1, sf1);+ }+}