commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r711168 - in /commons/proper/collections/trunk/src: java/org/apache/commons/collections/map/CaseInsensitiveMap.java test/org/apache/commons/collections/map/TestCaseInsensitiveMap.java
Date Tue, 04 Nov 2008 03:58:38 GMT
Author: bayard
Date: Mon Nov  3 19:58:37 2008
New Revision: 711168

URL: http://svn.apache.org/viewvc?rev=711168&view=rev
Log:
Applying Benjamin Bentmann's second patch from COLLECTIONS-294, fixing the locale issue in
CaseInsensitiveMap by converting each character individually and not using toLowerCase

Modified:
    commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/CaseInsensitiveMap.java
    commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestCaseInsensitiveMap.java

Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/CaseInsensitiveMap.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/CaseInsensitiveMap.java?rev=711168&r1=711167&r2=711168&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/CaseInsensitiveMap.java
(original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/CaseInsensitiveMap.java
Mon Nov  3 19:58:37 2008
@@ -25,9 +25,9 @@
 /**
  * A case-insensitive <code>Map</code>.
  * <p>
- * As entries are added to the map, keys are converted to all lowercase. A new 
- * key is compared to existing keys by comparing <code>newKey.toString().toLower()</code>
- * to the lowercase values in the current <code>KeySet.</code>
+ * Before keys are added to the map or compared to other existing keys, they are converted
+ * to all lowercase in a locale-independent fashion by using information from the Unicode
+ * data file.
  * <p>
  * Null keys are supported.  
  * <p>
@@ -111,14 +111,18 @@
      * Overrides convertKey() from {@link AbstractHashedMap} to convert keys to 
      * lower case.
      * <p>
-     * Returns null if key is null.
+     * Returns {@link AbstractHashedMap#NULL} if key is null.
      * 
      * @param key  the key convert
      * @return the converted key
      */
     protected Object convertKey(Object key) {
         if (key != null) {
-            return key.toString().toLowerCase();
+            char[] chars = key.toString().toCharArray();
+            for (int i = chars.length - 1; i >= 0; i--) {
+                chars[i] = Character.toLowerCase(Character.toUpperCase(chars[i]));
+            }
+            return new String(chars);
         } else {
             return AbstractHashedMap.NULL;
         }

Modified: commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestCaseInsensitiveMap.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestCaseInsensitiveMap.java?rev=711168&r1=711167&r2=711168&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestCaseInsensitiveMap.java
(original)
+++ commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestCaseInsensitiveMap.java
Mon Nov  3 19:58:37 2008
@@ -17,6 +17,7 @@
 package org.apache.commons.collections.map;
 
 import java.util.HashMap;
+import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
 
@@ -117,4 +118,33 @@
         writeExternalFormToDisk((java.io.Serializable) map, "/home/phil/jakarta-commons/collections/data/test/CaseInsensitiveMap.fullCollection.version3.obj");
     }
      */
+
+    // COLLECTIONS-294
+    public void testLocaleIndependence() {
+        Locale orig = Locale.getDefault();
+
+        Locale[] locales = { Locale.ENGLISH, new Locale("tr"), Locale.getDefault() };
+
+        String[][] data = { 
+            { "i", "I" },
+            { "\u03C2", "\u03C3" },
+            { "\u03A3", "\u03C2" },
+            { "\u03A3", "\u03C3" },
+        };
+
+        try {
+            for (int i = 0; i < locales.length; i++) {
+                Locale.setDefault(locales[i]);
+                for (int j = 0; j < data.length; j++) {
+                    assertTrue("Test data corrupt: " + j, data[j][0].equalsIgnoreCase(data[j][1]));
+                    CaseInsensitiveMap map = new CaseInsensitiveMap();
+                    map.put(data[j][0], "value");
+                    assertEquals(Locale.getDefault() + ": " + j, "value", map.get(data[j][1]));
+                }
+            }
+        } finally {
+            Locale.setDefault(orig);
+        }
+    }
+
 }



Mime
View raw message