Skip to content

Commit e9a87de

Browse files
committed
LinkedCaseInsensitiveMap provides case-insensitive keySet again
Issue: SPR-15026 (cherry picked from commit 50e5a65)
1 parent db7b74f commit e9a87de

File tree

2 files changed

+52
-37
lines changed

2 files changed

+52
-37
lines changed

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

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -85,6 +85,10 @@ public LinkedCaseInsensitiveMap(int initialCapacity) {
8585
*/
8686
public LinkedCaseInsensitiveMap(int initialCapacity, Locale locale) {
8787
this.targetMap = new LinkedHashMap<String, V>(initialCapacity) {
88+
@Override
89+
public boolean containsKey(Object key) {
90+
return LinkedCaseInsensitiveMap.this.containsKey(key);
91+
}
8892
@Override
8993
protected boolean removeEldestEntry(Map.Entry<String, V> eldest) {
9094
boolean doRemove = LinkedCaseInsensitiveMap.this.removeEldestEntry(eldest);
@@ -120,23 +124,35 @@ public boolean isEmpty() {
120124
}
121125

122126
@Override
123-
public boolean containsValue(Object value) {
124-
return this.targetMap.containsValue(value);
127+
public boolean containsKey(Object key) {
128+
return (key instanceof String && this.caseInsensitiveKeys.containsKey(convertKey((String) key)));
125129
}
126130

127131
@Override
128-
public Set<String> keySet() {
129-
return this.targetMap.keySet();
132+
public boolean containsValue(Object value) {
133+
return this.targetMap.containsValue(value);
130134
}
131135

132136
@Override
133-
public Collection<V> values() {
134-
return this.targetMap.values();
137+
public V get(Object key) {
138+
if (key instanceof String) {
139+
String caseInsensitiveKey = this.caseInsensitiveKeys.get(convertKey((String) key));
140+
if (caseInsensitiveKey != null) {
141+
return this.targetMap.get(caseInsensitiveKey);
142+
}
143+
}
144+
return null;
135145
}
136146

137147
@Override
138-
public Set<Entry<String, V>> entrySet() {
139-
return this.targetMap.entrySet();
148+
public V getOrDefault(Object key, V defaultValue) {
149+
if (key instanceof String) {
150+
String caseInsensitiveKey = this.caseInsensitiveKeys.get(convertKey((String) key));
151+
if (caseInsensitiveKey != null) {
152+
return this.targetMap.get(caseInsensitiveKey);
153+
}
154+
}
155+
return defaultValue;
140156
}
141157

142158
@Override
@@ -158,33 +174,6 @@ public void putAll(Map<? extends String, ? extends V> map) {
158174
}
159175
}
160176

161-
@Override
162-
public boolean containsKey(Object key) {
163-
return (key instanceof String && this.caseInsensitiveKeys.containsKey(convertKey((String) key)));
164-
}
165-
166-
@Override
167-
public V get(Object key) {
168-
if (key instanceof String) {
169-
String caseInsensitiveKey = this.caseInsensitiveKeys.get(convertKey((String) key));
170-
if (caseInsensitiveKey != null) {
171-
return this.targetMap.get(caseInsensitiveKey);
172-
}
173-
}
174-
return null;
175-
}
176-
177-
@Override
178-
public V getOrDefault(Object key, V defaultValue) {
179-
if (key instanceof String) {
180-
String caseInsensitiveKey = this.caseInsensitiveKeys.get(convertKey((String) key));
181-
if (caseInsensitiveKey != null) {
182-
return this.targetMap.get(caseInsensitiveKey);
183-
}
184-
}
185-
return defaultValue;
186-
}
187-
188177
@Override
189178
public V remove(Object key) {
190179
if (key instanceof String) {
@@ -202,6 +191,20 @@ public void clear() {
202191
this.targetMap.clear();
203192
}
204193

194+
@Override
195+
public Set<String> keySet() {
196+
return this.targetMap.keySet();
197+
}
198+
199+
@Override
200+
public Collection<V> values() {
201+
return this.targetMap.values();
202+
}
203+
204+
@Override
205+
public Set<Entry<String, V>> entrySet() {
206+
return this.targetMap.entrySet();
207+
}
205208

206209
@Override
207210
public LinkedCaseInsensitiveMap<V> clone() {

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,6 +37,12 @@ public void putAndGet() {
3737
assertEquals("value3", map.get("key"));
3838
assertEquals("value3", map.get("KEY"));
3939
assertEquals("value3", map.get("Key"));
40+
assertTrue(map.containsKey("key"));
41+
assertTrue(map.containsKey("KEY"));
42+
assertTrue(map.containsKey("Key"));
43+
assertTrue(map.keySet().contains("key"));
44+
assertTrue(map.keySet().contains("KEY"));
45+
assertTrue(map.keySet().contains("Key"));
4046
}
4147

4248
@Test
@@ -48,6 +54,12 @@ public void putWithOverlappingKeys() {
4854
assertEquals("value3", map.get("key"));
4955
assertEquals("value3", map.get("KEY"));
5056
assertEquals("value3", map.get("Key"));
57+
assertTrue(map.containsKey("key"));
58+
assertTrue(map.containsKey("KEY"));
59+
assertTrue(map.containsKey("Key"));
60+
assertTrue(map.keySet().contains("key"));
61+
assertTrue(map.keySet().contains("KEY"));
62+
assertTrue(map.keySet().contains("Key"));
5163
}
5264

5365
@Test

0 commit comments

Comments
 (0)