Browse Source

LinkedCaseInsensitiveMap exposes its locale for key conversion

Issue: SPR-15752
(cherry picked from commit 9b5132c)
pull/1479/head
Juergen Hoeller 9 years ago
parent
commit
3802aca474
  1. 46
      spring-core/src/main/java/org/springframework/util/LinkedCaseInsensitiveMap.java
  2. 3
      spring-core/src/test/java/org/springframework/util/LinkedCaseInsensitiveMapTests.java

46
spring-core/src/main/java/org/springframework/util/LinkedCaseInsensitiveMap.java

@ -47,18 +47,19 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable @@ -47,18 +47,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(Locale locale) {
this(16, locale);
@ -66,10 +67,10 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable @@ -66,10 +67,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);
@ -77,11 +78,11 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable @@ -77,11 +78,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, Locale locale) {
this.targetMap = new LinkedHashMap<String, V>(initialCapacity) {
@ -113,6 +114,8 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable @@ -113,6 +114,8 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
}
// Implementation of java.util.Map
@Override
public int size() {
return this.targetMap.size();
@ -227,16 +230,29 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable @@ -227,16 +230,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());
}
/**

3
spring-core/src/test/java/org/springframework/util/LinkedCaseInsensitiveMapTests.java

@ -108,12 +108,15 @@ public class LinkedCaseInsensitiveMapTests { @@ -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());

Loading…
Cancel
Save