Skip to content

Commit 3802aca

Browse files
committed
LinkedCaseInsensitiveMap exposes its locale for key conversion
Issue: SPR-15752 (cherry picked from commit 9b5132c)
1 parent a9a4d7c commit 3802aca

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

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

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,41 +47,42 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
4747

4848

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

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

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

7879
/**
7980
* Create a new LinkedCaseInsensitiveMap that wraps a {@link LinkedHashMap}
80-
* with the given initial capacity and stores lower-case keys according
81-
* to the given Locale.
81+
* with the given initial capacity and stores case-insensitive keys
82+
* according to the given Locale (by default in lower case).
8283
* @param initialCapacity the initial capacity
83-
* @param locale the Locale to use for lower-case conversion
84-
* @see java.lang.String#toLowerCase(java.util.Locale)
84+
* @param locale the Locale to use for case-insensitive key conversion
85+
* @see #convertKey(String)
8586
*/
8687
public LinkedCaseInsensitiveMap(int initialCapacity, Locale locale) {
8788
this.targetMap = new LinkedHashMap<String, V>(initialCapacity) {
@@ -113,6 +114,8 @@ private LinkedCaseInsensitiveMap(LinkedCaseInsensitiveMap<V> other) {
113114
}
114115

115116

117+
// Implementation of java.util.Map
118+
116119
@Override
117120
public int size() {
118121
return this.targetMap.size();
@@ -227,16 +230,29 @@ public String toString() {
227230
}
228231

229232

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

242258
/**

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)