diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 8695e3359..66cee6864 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -29,6 +29,10 @@ Fixed cache assignment for "TreeBidiMap#entrySet". + + "CaseInsensitiveMap" will now convert input strings to lower-case in a + locale-independent manner. + "MultiKey" will now be correctly serialized/de-serialized. @@ -71,10 +75,6 @@ "SetUniqueList#set(int, Object)" will now correctly enforce the uniqueness constraint. - - "CaseInsensitiveMap" will now convert input strings to lower-case in a - locale-independant manner. - "Flat3Map#remove(Object)" will now return the correct value mapped to the removed key if the size of the map is less or equal 3. diff --git a/src/java/org/apache/commons/collections/map/CaseInsensitiveMap.java b/src/java/org/apache/commons/collections/map/CaseInsensitiveMap.java index ba9650d5f..237c05af6 100644 --- a/src/java/org/apache/commons/collections/map/CaseInsensitiveMap.java +++ b/src/java/org/apache/commons/collections/map/CaseInsensitiveMap.java @@ -25,9 +25,9 @@ import java.util.Map; /** * A case-insensitive Map. *

- * As entries are added to the map, keys are converted to all lowercase. A new - * key is compared to existing keys by comparing newKey.toString().toLower() - * to the lowercase values in the current KeySet. + * 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. *

* Null keys are supported. *

@@ -111,14 +111,18 @@ public class CaseInsensitiveMap extends AbstractHashedMap implements Serializabl * Overrides convertKey() from {@link AbstractHashedMap} to convert keys to * lower case. *

- * 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; } diff --git a/src/test/org/apache/commons/collections/map/TestCaseInsensitiveMap.java b/src/test/org/apache/commons/collections/map/TestCaseInsensitiveMap.java index b3f27a5ee..0b7266e75 100644 --- a/src/test/org/apache/commons/collections/map/TestCaseInsensitiveMap.java +++ b/src/test/org/apache/commons/collections/map/TestCaseInsensitiveMap.java @@ -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; @@ -108,7 +109,32 @@ public class TestCaseInsensitiveMap extends AbstractTestIterableMap { assertEquals(map.size(), cloned.size()); assertSame(map.get("1"), cloned.get("1")); } - + + // 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); + } + } + /* public void testCreate() throws Exception { resetEmpty();