commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r601797 - in /commons/proper/io/trunk/src: java/org/apache/commons/io/IOCase.java test/org/apache/commons/io/IOCaseTestCase.java
Date Thu, 06 Dec 2007 18:06:11 GMT
Author: niallp
Date: Thu Dec  6 10:06:10 2007
New Revision: 601797

URL: http://svn.apache.org/viewvc?rev=601797&view=rev
Log:
IO-144 Add a compare method

Modified:
    commons/proper/io/trunk/src/java/org/apache/commons/io/IOCase.java
    commons/proper/io/trunk/src/test/org/apache/commons/io/IOCaseTestCase.java

Modified: commons/proper/io/trunk/src/java/org/apache/commons/io/IOCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/IOCase.java?rev=601797&r1=601796&r2=601797&view=diff
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/IOCase.java (original)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/IOCase.java Thu Dec  6 10:06:10
2007
@@ -128,6 +128,24 @@
     /**
      * Compares two strings using the case-sensitivity rule.
      * <p>
+     * This method mimics {@link String#compareTo} but takes case-sensitivity
+     * into account.
+     * 
+     * @param str1  the first string to compare, not null
+     * @param str2  the second string to compare, not null
+     * @return true if equal using the case rules
+     * @throws NullPointerException if either string is null
+     */
+    public int checkCompareTo(String str1, String str2) {
+        if (str1 == null || str2 == null) {
+            throw new NullPointerException("The strings must not be null");
+        }
+        return sensitive ? str1.compareTo(str2) : str1.compareToIgnoreCase(str2);
+    }
+
+    /**
+     * Compares two strings using the case-sensitivity rule.
+     * <p>
      * This method mimics {@link String#equals} but takes case-sensitivity
      * into account.
      * 

Modified: commons/proper/io/trunk/src/test/org/apache/commons/io/IOCaseTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/IOCaseTestCase.java?rev=601797&r1=601796&r2=601797&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/org/apache/commons/io/IOCaseTestCase.java (original)
+++ commons/proper/io/trunk/src/test/org/apache/commons/io/IOCaseTestCase.java Thu Dec  6
10:06:10 2007
@@ -96,6 +96,43 @@
         assertEquals(false, IOCase.INSENSITIVE.isCaseSensitive());
         assertEquals(!WINDOWS, IOCase.SYSTEM.isCaseSensitive());
     }
+    //-----------------------------------------------------------------------
+    public void test_checkCompare_functionality() throws Exception {
+        assertTrue(IOCase.SENSITIVE.checkCompareTo("ABC", "") > 0);
+        assertTrue(IOCase.SENSITIVE.checkCompareTo("", "ABC") < 0);
+        assertTrue(IOCase.SENSITIVE.checkCompareTo("ABC", "DEF") < 0);
+        assertTrue(IOCase.SENSITIVE.checkCompareTo("DEF", "ABC") > 0);
+        assertEquals(0, IOCase.SENSITIVE.checkCompareTo("ABC", "ABC"));
+        assertEquals(0, IOCase.SENSITIVE.checkCompareTo("", ""));
+        
+        try {
+            IOCase.SENSITIVE.checkCompareTo("ABC", null);
+            fail();
+        } catch (NullPointerException ex) {}
+        try {
+            IOCase.SENSITIVE.checkCompareTo(null, "ABC");
+            fail();
+        } catch (NullPointerException ex) {}
+        try {
+            IOCase.SENSITIVE.checkCompareTo(null, null);
+            fail();
+        } catch (NullPointerException ex) {}
+    }
+
+    public void test_checkCompare_case() throws Exception {
+        assertEquals(0, IOCase.SENSITIVE.checkCompareTo("ABC", "ABC"));
+        assertTrue(IOCase.SENSITIVE.checkCompareTo("ABC", "abc") < 0);
+        assertTrue(IOCase.SENSITIVE.checkCompareTo("abc", "ABC") > 0);
+        
+        assertEquals(0, IOCase.INSENSITIVE.checkCompareTo("ABC", "ABC"));
+        assertEquals(0, IOCase.INSENSITIVE.checkCompareTo("ABC", "abc"));
+        assertEquals(0, IOCase.INSENSITIVE.checkCompareTo("abc", "ABC"));
+
+        assertEquals(0, IOCase.SYSTEM.checkCompareTo("ABC", "ABC"));
+        assertEquals(WINDOWS, IOCase.SYSTEM.checkCompareTo("ABC", "abc") == 0);
+        assertEquals(WINDOWS, IOCase.SYSTEM.checkCompareTo("abc", "ABC") == 0);
+    }
+
 
     //-----------------------------------------------------------------------
     public void test_checkEquals_functionality() throws Exception {



Mime
View raw message