Skip to content

Apache Lucene open-source search software

fix bug in CategoryPath.compareTo

Shai Erera076b6de0a3c9
diff --git a/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/CategoryPath.java b/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/CategoryPath.java
index 4e14fcf23cc..f277c054d68 100644
--- a/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/CategoryPath.java
+++ b/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/CategoryPath.java
@@ -92,8 +92,8 @@ public class CategoryPath implements Comparable<CategoryPath> {
*/
@Override
public int compareTo(CategoryPath other) {
- int length = this.length < other.length ? this.length : other.length;
- for (int i = 0, j = 0; i < length; i++, j++) {
+ final int len = length < other.length ? length : other.length;
+ for (int i = 0, j = 0; i < len; i++, j++) {
int cmp = components[i].compareTo(other.components[j]);
if (cmp < 0) return -1; // this is 'before'
if (cmp > 0) return 1; // this is 'after'
diff --git a/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/TestCategoryPath.java b/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/TestCategoryPath.java
index 5ea3c111b8f..ccaaf4d33ec 100644
--- a/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/TestCategoryPath.java
+++ b/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/TestCategoryPath.java
@@ -163,16 +163,22 @@ public class TestCategoryPath extends LuceneTestCase {
CategoryPath p = new CategoryPath("a/b/c/d", '/');
CategoryPath pother = new CategoryPath("a/b/c/d", '/');
assertEquals(0, pother.compareTo(p));
+ assertEquals(0, p.compareTo(pother));
pother = new CategoryPath("", '/');
assertTrue(pother.compareTo(p) < 0);
+ assertTrue(p.compareTo(pother) > 0);
pother = new CategoryPath("a/b_/c/d", '/');
assertTrue(pother.compareTo(p) > 0);
+ assertTrue(p.compareTo(pother) < 0);
pother = new CategoryPath("a/b/c", '/');
assertTrue(pother.compareTo(p) < 0);
+ assertTrue(p.compareTo(pother) > 0);
pother = new CategoryPath("a/b/c/e", '/');
assertTrue(pother.compareTo(p) > 0);
+ assertTrue(p.compareTo(pother) < 0);
pother = new CategoryPath("a/b/c//e", '/');
assertTrue(pother.compareTo(p) < 0);
+ assertTrue(p.compareTo(pother) > 0);
}
}