LinkedCaseInsensitiveMap exposes its locale for key conversion

Issue: SPR-15752
This commit is contained in:
Juergen Hoeller 2017-07-10 14:24:28 +02:00
parent fac35ebec2
commit 9b5132ce53
2 changed files with 35 additions and 16 deletions

View File

@ -49,18 +49,19 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
/** /**
* Create a new LinkedCaseInsensitiveMap for the default Locale. * Create a new LinkedCaseInsensitiveMap that stores case-insensitive keys
* @see java.lang.String#toLowerCase() * according to the default Locale (by default in lower case).
* @see #convertKey(String)
*/ */
public LinkedCaseInsensitiveMap() { public LinkedCaseInsensitiveMap() {
this((Locale) null); this((Locale) null);
} }
/** /**
* Create a new LinkedCaseInsensitiveMap that stores lower-case keys * Create a new LinkedCaseInsensitiveMap that stores case-insensitive keys
* according to the given Locale. * according to the given Locale (by default in lower case).
* @param locale the Locale to use for lower-case conversion * @param locale the Locale to use for case-insensitive key conversion
* @see java.lang.String#toLowerCase(java.util.Locale) * @see #convertKey(String)
*/ */
public LinkedCaseInsensitiveMap(@Nullable Locale locale) { public LinkedCaseInsensitiveMap(@Nullable Locale locale) {
this(16, locale); this(16, locale);
@ -68,10 +69,10 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
/** /**
* Create a new LinkedCaseInsensitiveMap that wraps a {@link LinkedHashMap} * Create a new LinkedCaseInsensitiveMap that wraps a {@link LinkedHashMap}
* with the given initial capacity and stores lower-case keys according * with the given initial capacity and stores case-insensitive keys
* to the default Locale. * according to the default Locale (by default in lower case).
* @param initialCapacity the initial capacity * @param initialCapacity the initial capacity
* @see java.lang.String#toLowerCase() * @see #convertKey(String)
*/ */
public LinkedCaseInsensitiveMap(int initialCapacity) { public LinkedCaseInsensitiveMap(int initialCapacity) {
this(initialCapacity, null); this(initialCapacity, null);
@ -79,11 +80,11 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
/** /**
* Create a new LinkedCaseInsensitiveMap that wraps a {@link LinkedHashMap} * Create a new LinkedCaseInsensitiveMap that wraps a {@link LinkedHashMap}
* with the given initial capacity and stores lower-case keys according * with the given initial capacity and stores case-insensitive keys
* to the given Locale. * according to the given Locale (by default in lower case).
* @param initialCapacity the initial capacity * @param initialCapacity the initial capacity
* @param locale the Locale to use for lower-case conversion * @param locale the Locale to use for case-insensitive key conversion
* @see java.lang.String#toLowerCase(java.util.Locale) * @see #convertKey(String)
*/ */
public LinkedCaseInsensitiveMap(int initialCapacity, @Nullable Locale locale) { public LinkedCaseInsensitiveMap(int initialCapacity, @Nullable Locale locale) {
this.targetMap = new LinkedHashMap<String, V>(initialCapacity) { this.targetMap = new LinkedHashMap<String, V>(initialCapacity) {
@ -115,6 +116,8 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
} }
// Implementation of java.util.Map
@Override @Override
public int size() { public int size() {
return this.targetMap.size(); return this.targetMap.size();
@ -159,7 +162,7 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
} }
@Override @Override
public V put(String key, V value) { public V put(String key, @Nullable V value) {
String oldKey = this.caseInsensitiveKeys.put(convertKey(key), key); String oldKey = this.caseInsensitiveKeys.put(convertKey(key), key);
if (oldKey != null && !oldKey.equals(key)) { if (oldKey != null && !oldKey.equals(key)) {
this.targetMap.remove(oldKey); this.targetMap.remove(oldKey);
@ -229,16 +232,29 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
} }
// Specific to LinkedCaseInsensitiveMap
/**
* Return the locale used by this {@code LinkedCaseInsensitiveMap}.
* Used for case-insensitive key conversion.
* @since 4.3.10
* @see #LinkedCaseInsensitiveMap(Locale)
* @see #convertKey(String)
*/
public Locale getLocale() {
return this.locale;
}
/** /**
* Convert the given key to a case-insensitive key. * Convert the given key to a case-insensitive key.
* <p>The default implementation converts the key * <p>The default implementation converts the key
* to lower-case according to this Map's Locale. * to lower-case according to this Map's Locale.
* @param key the user-specified key * @param key the user-specified key
* @return the key to use for storing * @return the key to use for storing
* @see java.lang.String#toLowerCase(java.util.Locale) * @see String#toLowerCase(Locale)
*/ */
protected String convertKey(String key) { protected String convertKey(String key) {
return key.toLowerCase(this.locale); return key.toLowerCase(getLocale());
} }
/** /**

View File

@ -108,12 +108,15 @@ public class LinkedCaseInsensitiveMapTests {
public void mapClone() { public void mapClone() {
map.put("key", "value1"); map.put("key", "value1");
LinkedCaseInsensitiveMap<String> copy = map.clone(); LinkedCaseInsensitiveMap<String> copy = map.clone();
assertEquals(map.getLocale(), copy.getLocale());
assertEquals("value1", map.get("key")); assertEquals("value1", map.get("key"));
assertEquals("value1", map.get("KEY")); assertEquals("value1", map.get("KEY"));
assertEquals("value1", map.get("Key")); assertEquals("value1", map.get("Key"));
assertEquals("value1", copy.get("key")); assertEquals("value1", copy.get("key"));
assertEquals("value1", copy.get("KEY")); assertEquals("value1", copy.get("KEY"));
assertEquals("value1", copy.get("Key")); assertEquals("value1", copy.get("Key"));
copy.put("Key", "value2"); copy.put("Key", "value2");
assertEquals(1, map.size()); assertEquals(1, map.size());
assertEquals(1, copy.size()); assertEquals(1, copy.size());