Skip to content

Commit 9b5132c

Browse files
committed
LinkedCaseInsensitiveMap exposes its locale for key conversion
Issue: SPR-15752
1 parent fac35eb commit 9b5132c

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

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

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,41 +49,42 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
4949

5050

5151
/**
52-
* Create a new LinkedCaseInsensitiveMap for the default Locale.
53-
* @see java.lang.String#toLowerCase()
52+
* Create a new LinkedCaseInsensitiveMap that stores case-insensitive keys
53+
* according to the default Locale (by default in lower case).
54+
* @see #convertKey(String)
5455
*/
5556
public LinkedCaseInsensitiveMap() {
5657
this((Locale) null);
5758
}
5859

5960
/**
60-
* Create a new LinkedCaseInsensitiveMap that stores lower-case keys
61-
* according to the given Locale.
62-
* @param locale the Locale to use for lower-case conversion
63-
* @see java.lang.String#toLowerCase(java.util.Locale)
61+
* Create a new LinkedCaseInsensitiveMap that stores case-insensitive keys
62+
* according to the given Locale (by default in lower case).
63+
* @param locale the Locale to use for case-insensitive key conversion
64+
* @see #convertKey(String)
6465
*/
6566
public LinkedCaseInsensitiveMap(@Nullable Locale locale) {
6667
this(16, locale);
6768
}
6869

6970
/**
7071
* Create a new LinkedCaseInsensitiveMap that wraps a {@link LinkedHashMap}
71-
* with the given initial capacity and stores lower-case keys according
72-
* to the default Locale.
72+
* with the given initial capacity and stores case-insensitive keys
73+
* according to the default Locale (by default in lower case).
7374
* @param initialCapacity the initial capacity
74-
* @see java.lang.String#toLowerCase()
75+
* @see #convertKey(String)
7576
*/
7677
public LinkedCaseInsensitiveMap(int initialCapacity) {
7778
this(initialCapacity, null);
7879
}
7980

8081
/**
8182
* Create a new LinkedCaseInsensitiveMap that wraps a {@link LinkedHashMap}
82-
* with the given initial capacity and stores lower-case keys according
83-
* to the given Locale.
83+
* with the given initial capacity and stores case-insensitive keys
84+
* according to the given Locale (by default in lower case).
8485
* @param initialCapacity the initial capacity
85-
* @param locale the Locale to use for lower-case conversion
86-
* @see java.lang.String#toLowerCase(java.util.Locale)
86+
* @param locale the Locale to use for case-insensitive key conversion
87+
* @see #convertKey(String)
8788
*/
8889
public LinkedCaseInsensitiveMap(int initialCapacity, @Nullable Locale locale) {
8990
this.targetMap = new LinkedHashMap<String, V>(initialCapacity) {
@@ -115,6 +116,8 @@ private LinkedCaseInsensitiveMap(LinkedCaseInsensitiveMap<V> other) {
115116
}
116117

117118

119+
// Implementation of java.util.Map
120+
118121
@Override
119122
public int size() {
120123
return this.targetMap.size();
@@ -159,7 +162,7 @@ public V getOrDefault(Object key, V defaultValue) {
159162
}
160163

161164
@Override
162-
public V put(String key, V value) {
165+
public V put(String key, @Nullable V value) {
163166
String oldKey = this.caseInsensitiveKeys.put(convertKey(key), key);
164167
if (oldKey != null && !oldKey.equals(key)) {
165168
this.targetMap.remove(oldKey);
@@ -229,16 +232,29 @@ public String toString() {
229232
}
230233

231234

235+
// Specific to LinkedCaseInsensitiveMap
236+
237+
/**
238+
* Return the locale used by this {@code LinkedCaseInsensitiveMap}.
239+
* Used for case-insensitive key conversion.
240+
* @since 4.3.10
241+
* @see #LinkedCaseInsensitiveMap(Locale)
242+
* @see #convertKey(String)
243+
*/
244+
public Locale getLocale() {
245+
return this.locale;
246+
}
247+
232248
/**
233249
* Convert the given key to a case-insensitive key.
234250
* <p>The default implementation converts the key
235251
* to lower-case according to this Map's Locale.
236252
* @param key the user-specified key
237253
* @return the key to use for storing
238-
* @see java.lang.String#toLowerCase(java.util.Locale)
254+
* @see String#toLowerCase(Locale)
239255
*/
240256
protected String convertKey(String key) {
241-
return key.toLowerCase(this.locale);
257+
return key.toLowerCase(getLocale());
242258
}
243259

244260
/**

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,15 @@ public void computeIfAbsentWithComputedValue() {
108108
public void mapClone() {
109109
map.put("key", "value1");
110110
LinkedCaseInsensitiveMap<String> copy = map.clone();
111+
112+
assertEquals(map.getLocale(), copy.getLocale());
111113
assertEquals("value1", map.get("key"));
112114
assertEquals("value1", map.get("KEY"));
113115
assertEquals("value1", map.get("Key"));
114116
assertEquals("value1", copy.get("key"));
115117
assertEquals("value1", copy.get("KEY"));
116118
assertEquals("value1", copy.get("Key"));
119+
117120
copy.put("Key", "value2");
118121
assertEquals(1, map.size());
119122
assertEquals(1, copy.size());

0 commit comments

Comments
 (0)