lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rm...@apache.org
Subject svn commit: r1524588 - in /lucene/dev/trunk/lucene/expressions/src: java/org/apache/lucene/expressions/ExpressionSortField.java test/org/apache/lucene/expressions/TestExpressionSortField.java
Date Wed, 18 Sep 2013 21:51:24 GMT
Author: rmuir
Date: Wed Sep 18 21:51:24 2013
New Revision: 1524588

URL: http://svn.apache.org/r1524588
Log:
LUCENE-5220: ExpressionSortField has crappy toString/bad equals/hashcode

Added:
    lucene/dev/trunk/lucene/expressions/src/test/org/apache/lucene/expressions/TestExpressionSortField.java
  (with props)
Modified:
    lucene/dev/trunk/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionSortField.java

Modified: lucene/dev/trunk/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionSortField.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionSortField.java?rev=1524588&r1=1524587&r2=1524588&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionSortField.java
(original)
+++ lucene/dev/trunk/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionSortField.java
Wed Sep 18 21:51:24 2013
@@ -37,6 +37,41 @@ class ExpressionSortField extends SortFi
   }
 
   @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();
+  }
+
+  @Override
   public boolean needsScores() {
     return true; // TODO: maybe we can optimize by "figuring this out" somehow...
   }

Added: lucene/dev/trunk/lucene/expressions/src/test/org/apache/lucene/expressions/TestExpressionSortField.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/expressions/src/test/org/apache/lucene/expressions/TestExpressionSortField.java?rev=1524588&view=auto
==============================================================================
--- lucene/dev/trunk/lucene/expressions/src/test/org/apache/lucene/expressions/TestExpressionSortField.java
(added)
+++ lucene/dev/trunk/lucene/expressions/src/test/org/apache/lucene/expressions/TestExpressionSortField.java
Wed Sep 18 21:51:24 2013
@@ -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);
+  }
+}



Mime
View raw message