Lior Vernia has posted comments on this change.

Change subject: core: Added LexoNumericComparator utility
......................................................................


Patch Set 1: (6 inline comments)

....................................................
File 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/LexoNumericComparator.java
Line 14:  * <br>
Line 15:  * It is assumed that a string always begins with a nondigit sequence; 
if it actually begins with a digit sequence, the
Line 16:  * behaviour is as if it started with an empty nondigit sequence.
Line 17:  */
Line 18: public class LexoNumericComparator implements Comparator<String> {
Done
Line 19: 
Line 20:     @Override
Line 21:     public int compare(String str1, String str2) {
Line 22:         return comp(str1, str2);


Line 21:     public int compare(String str1, String str2) {
Line 22:         return comp(str1, str2);
Line 23:     }
Line 24: 
Line 25:     public static int comp(String str1, String str2) {
Because in later patches I use it statically from different places throughout 
the project, where it would be ugly to instantiate an object for the sake of 
one comparison.
Line 26:         if (str1 == null) {
Line 27:             return (str2 == null) ? 0 : -1;
Line 28:         } else if (str2 == null) {
Line 29:             return 1;


Line 28:         } else if (str2 == null) {
Line 29:             return 1;
Line 30:         }
Line 31: 
Line 32:         boolean isDigitTurn = false;
Done
Line 33:         int begSeq1 = 0, begSeq2 = 0, endSeq1, endSeq2;
Line 34: 
Line 35:         while ((endSeq1 = findEndOfSequence(str1, begSeq1, 
isDigitTurn)) != begSeq1) {
Line 36:             if ((endSeq2 = findEndOfSequence(str2, begSeq2, 
isDigitTurn)) == begSeq2) {


Line 29:             return 1;
Line 30:         }
Line 31: 
Line 32:         boolean isDigitTurn = false;
Line 33:         int begSeq1 = 0, begSeq2 = 0, endSeq1, endSeq2;
Because it might be more readable for people in general, and specifically those 
who have programmed in other programming languages and don't remember the 
convention of each language as to assigning default values to uninitialized 
variables.
Line 34: 
Line 35:         while ((endSeq1 = findEndOfSequence(str1, begSeq1, 
isDigitTurn)) != begSeq1) {
Line 36:             if ((endSeq2 = findEndOfSequence(str2, begSeq2, 
isDigitTurn)) == begSeq2) {
Line 37:                 return 1; // str1 and str2 have the same prefix but 
str1 has an extra sequence => str1 > str2


Line 61:     }
Line 62: 
Line 63:     private static int compDigitSequence(String seq1, String seq2) {
Line 64:         int compRes = ((Integer) 
Integer.parseInt(seq1)).compareTo(Integer.parseInt(seq2));
Line 65:         return compRes != 0 ? compRes : compNonDigitSequence(seq1, 
seq2);
Sounds pretty arbitrary to me, but alright.
Line 66:     }
Line 67: 
Line 68:     private static int compNonDigitSequence(String seq1, String seq2) {
Line 69:         return seq1.compareTo(seq2);


....................................................
File 
backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/utils/LexoNumericComparatorTest.java
Line 12:     private final String NONDIGIT_OTHER = "def";
Line 13:     private LexoNumericComparator comparator = new 
LexoNumericComparator();
Line 14: 
Line 15:     private void verifyResult(String str1, String str2, int 
expectedResult) {
Line 16:         assertEquals(Integer.signum(expectedResult), 
Integer.signum(comparator.compare(str1, str2)));
Done
Line 17:         assertEquals(-Integer.signum(expectedResult), 
Integer.signum(comparator.compare(str2, str1)));
Line 18:     }
Line 19: 
Line 20:     @Test


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

Gerrit-MessageType: comment
Gerrit-Change-Id: I8c68446ea6f1865a51455a0a359df339bfb15bba
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Lior Vernia <[email protected]>
Gerrit-Reviewer: Lior Vernia <[email protected]>
Gerrit-Reviewer: Mike Kolesnik <[email protected]>
Gerrit-Reviewer: Moti Asayag <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to