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.
* @see java.lang.String#toLowerCase()
* Create a new LinkedCaseInsensitiveMap that stores case-insensitive keys
* according to the default Locale (by default in lower case).
* @see #convertKey(String)
*/
public LinkedCaseInsensitiveMap() {
this((Locale) null);
}
/**
* Create a new LinkedCaseInsensitiveMap that stores lower-case keys
* according to the given Locale.
* @param locale the Locale to use for lower-case conversion
* @see java.lang.String#toLowerCase(java.util.Locale)
* Create a new LinkedCaseInsensitiveMap that stores case-insensitive keys
* according to the given Locale (by default in lower case).
* @param locale the Locale to use for case-insensitive key conversion
* @see #convertKey(String)
*/
public LinkedCaseInsensitiveMap(@Nullable Locale 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}
* with the given initial capacity and stores lower-case keys according
* to the default Locale.
* with the given initial capacity and stores case-insensitive keys
* according to the default Locale (by default in lower case).
* @param initialCapacity the initial capacity
* @see java.lang.String#toLowerCase()
* @see #convertKey(String)
*/
public LinkedCaseInsensitiveMap(int initialCapacity) {
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}
* with the given initial capacity and stores lower-case keys according
* to the given Locale.
* with the given initial capacity and stores case-insensitive keys
* according to the given Locale (by default in lower case).
* @param initialCapacity the initial capacity
* @param locale the Locale to use for lower-case conversion
* @see java.lang.String#toLowerCase(java.util.Locale)
* @param locale the Locale to use for case-insensitive key conversion
* @see #convertKey(String)
*/
public LinkedCaseInsensitiveMap(int initialCapacity, @Nullable Locale locale) {
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
public int size() {
return this.targetMap.size();
@ -159,7 +162,7 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
}
@Override
public V put(String key, V value) {
public V put(String key, @Nullable V value) {
String oldKey = this.caseInsensitiveKeys.put(convertKey(key), key);
if (oldKey != null && !oldKey.equals(key)) {
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.
* <p>The default implementation converts the key
* to lower-case according to this Map's Locale.
* @param key the user-specified key
* @return the key to use for storing
* @see java.lang.String#toLowerCase(java.util.Locale)
* @see String#toLowerCase(Locale)
*/
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() {
map.put("key", "value1");
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", copy.get("key"));
assertEquals("value1", copy.get("KEY"));
assertEquals("value1", copy.get("Key"));
copy.put("Key", "value2");
assertEquals(1, map.size());
assertEquals(1, copy.size());