Lior Vernia has uploaded a new change for review.

Change subject: core: Slight improvements to LexoNumericComparator
......................................................................

core: Slight improvements to LexoNumericComparator

Added ability to choose case-sensitiveness of comparison, while the
default behaviour is case-insensitive, and added a couple of
tests. Also ceased the opportunity to change the comparison's return
value to always be -1, 0 or 1 (it used to be possible to return other
numbers corresponding to String differences, which could have been
confusing).

Change-Id: If85552b7d07903321d08d55a7d6675c79692f385
Signed-off-by: Lior Vernia <[email protected]>
---
M 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/comparators/LexoNumericComparator.java
M 
backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/businessentities/comparators/LexoNumericComparatorTest.java
2 files changed, 62 insertions(+), 42 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/96/16196/1

diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/comparators/LexoNumericComparator.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/comparators/LexoNumericComparator.java
index fde7fc3..9b58c62 100644
--- 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/comparators/LexoNumericComparator.java
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/comparators/LexoNumericComparator.java
@@ -21,12 +21,26 @@
  */
 public class LexoNumericComparator implements Comparator<String>, Serializable 
{
 
+    private boolean caseSensitive;
+
+    public LexoNumericComparator(boolean caseSensitive) {
+        this.caseSensitive = caseSensitive;
+    }
+
+    public LexoNumericComparator() {
+        this(false);
+    }
+
     @Override
     public int compare(String str1, String str2) {
-        return comp(str1, str2);
+        return comp(str1, str2, caseSensitive);
     }
 
     public static int comp(String str1, String str2) {
+        return comp(str1, str2, false);
+    }
+
+    public static int comp(String str1, String str2, boolean caseSensitive) {
         if (str1 == null) {
             return (str2 == null) ? 0 : -1;
         } else if (str2 == null) {
@@ -48,7 +62,7 @@
             int endSeq2 = findEndOfSequence(str2, begSeq2, digitTurn);
             String seq1 = str1.substring(begSeq1, endSeq1);
             String seq2 = str2.substring(begSeq2, endSeq2);
-            int compRes = compareSequence(seq1, seq2, digitTurn);
+            int compRes = compareSequence(seq1, seq2, digitTurn, 
caseSensitive);
             if (compRes != 0) {
                 return compRes;
             }
@@ -66,17 +80,18 @@
         return 0;
     }
 
-    private static int compareSequence(String seq1, String seq2, boolean 
digitSequence) {
-        return digitSequence ? compDigitSequence(seq1, seq2) : 
compNonDigitSequence(seq1, seq2);
+    private static int compareSequence(String seq1, String seq2, boolean 
digitSequence, boolean caseSensitive) {
+        return digitSequence ? compDigitSequence(seq1, seq2, caseSensitive)
+                : compNonDigitSequence(seq1, seq2, caseSensitive);
     }
 
-    private static int compDigitSequence(String seq1, String seq2) {
+    private static int compDigitSequence(String seq1, String seq2, boolean 
caseSensitive) {
         int compRes = ((Integer) 
Integer.parseInt(seq1)).compareTo(Integer.parseInt(seq2));
-        return compRes == 0 ? compNonDigitSequence(seq1, seq2) : compRes;
+        return compRes == 0 ? compNonDigitSequence(seq1, seq2, caseSensitive) 
: compRes;
     }
 
-    private static int compNonDigitSequence(String seq1, String seq2) {
-        return seq1.compareTo(seq2);
+    private static int compNonDigitSequence(String seq1, String seq2, boolean 
caseSensitive) {
+        return Integer.signum(caseSensitive ? seq1.compareTo(seq2) : 
seq1.compareToIgnoreCase(seq2));
     }
 
     private static int findEndOfSequence(String seq, int startIndex, boolean 
digitSequence) {
diff --git 
a/backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/businessentities/comparators/LexoNumericComparatorTest.java
 
b/backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/businessentities/comparators/LexoNumericComparatorTest.java
index 7ae833a..3f6e401 100644
--- 
a/backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/businessentities/comparators/LexoNumericComparatorTest.java
+++ 
b/backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/businessentities/comparators/LexoNumericComparatorTest.java
@@ -12,13 +12,14 @@
 @RunWith(Parameterized.class)
 public class LexoNumericComparatorTest {
 
-    private LexoNumericComparator comparator = new LexoNumericComparator();
+    private LexoNumericComparator comparator;
 
     private String left;
     private String right;
     private int expectedResult;
 
-    public LexoNumericComparatorTest(String left, String right, int 
expectedResult) {
+    public LexoNumericComparatorTest(boolean caseSensitive, String left, 
String right, int expectedResult) {
+        comparator = new LexoNumericComparator(caseSensitive);
         this.left = left;
         this.right = right;
         this.expectedResult = expectedResult;
@@ -29,8 +30,8 @@
                 left,
                 right,
                 expectedResult == -1 ? "less than" : (expectedResult == 1 ? 
"greater than" : "equal to")),
-                Integer.signum(expectedResult),
-                Integer.signum(comparator.compare(left, right)));
+                expectedResult,
+                comparator.compare(left, right));
     }
 
     @Test
@@ -42,36 +43,40 @@
     @Parameterized.Parameters
     public static Collection<Object[]> comparisonParameters() {
         return Arrays.asList(new Object[][] {
-                { null, null, 0 },
-                { null, "", -1 },
-                { "", "", 0 },
-                { "", "123", -1 },
-                { "123", "123", 0 },
-                { "123", "456", -1 },
-                { "12", "123", -1 },
-                { "012", "12", -1 },
-                { "2", "10", -1 },
-                { "123abc", "123abc", 0 },
-                { "123abc", "456abc", -1 },
-                { "12abc", "123abc", -1 },
-                { "012abc", "12abc", -1 },
-                { "2abc", "10abc", -1 },
-                { "123", "abc", -1 },
-                { "abc", "abc", 0 },
-                { "abc", "def", -1 },
-                { "ab", "abc", -1 },
-                { "123abc", "123def", -1 },
-                { "123ab", "123abc", -1 },
-                { "abc123", "abc123", 0 },
-                { "abc123", "def123", -1 },
-                { "ab123", "abc123", -1 },
-                { "abc123", "abc456", -1 },
-                { "abc12", "abc123", -1 },
-                { "abc012", "abc12", -1 },
-                { "abc2", "abc10", -1 },
-                { "abc123def", "abc123def", 0 },
-                { "abc123def", "abc123ghi", -1 },
-                { "abc123de", "abc123def", -1 }
+                { false, null, null, 0 },
+                { false, null, "", -1 },
+                { false, "", "", 0 },
+                { false, "", "123", -1 },
+                { false, "123", "123", 0 },
+                { false, "123", "456", -1 },
+                { false, "12", "123", -1 },
+                { false, "012", "12", -1 },
+                { false, "2", "10", -1 },
+                { false, "123Abc", "123abc", 0 },
+                { true, "123Abc", "123abc", -1 },
+                { false, "123abc", "456abc", -1 },
+                { false, "12abc", "123abc", -1 },
+                { false, "012abc", "12abc", -1 },
+                { false, "2abc", "10abc", -1 },
+                { false, "123", "abc", -1 },
+                { false, "Abc", "abc", 0 },
+                { true, "Abc", "abc", -1 },
+                { false, "abc", "def", -1 },
+                { false, "ab", "abc", -1 },
+                { false, "123abc", "123def", -1 },
+                { false, "123ab", "123abc", -1 },
+                { false, "Abc123", "abc123", 0 },
+                { true, "Abc123", "abc123", -1 },
+                { false, "abc123", "def123", -1 },
+                { false, "ab123", "abc123", -1 },
+                { false, "abc123", "abc456", -1 },
+                { false, "abc12", "abc123", -1 },
+                { false, "abc012", "abc12", -1 },
+                { false, "abc2", "abc10", -1 },
+                { false, "Abc123def", "abc123def", 0 },
+                { true, "Abc123def", "abc123def", -1 },
+                { false, "abc123def", "abc123ghi", -1 },
+                { false, "abc123de", "abc123def", -1 }
         });
     }
 }


-- 
To view, visit http://gerrit.ovirt.org/16196
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If85552b7d07903321d08d55a7d6675c79692f385
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Lior Vernia <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to