Skip to content

Commit 9129ec2

Browse files
committed
LinkedCaseInsensitiveMap provides reliable getOrDefault implementation
Issue: SPR-13981 (cherry picked from commit 7a32ce3)
1 parent 2cbc5ff commit 9129ec2

File tree

2 files changed

+49
-16
lines changed

2 files changed

+49
-16
lines changed

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -114,21 +114,35 @@ public boolean containsKey(Object key) {
114114
@Override
115115
public V get(Object key) {
116116
if (key instanceof String) {
117-
return super.get(this.caseInsensitiveKeys.get(convertKey((String) key)));
117+
String caseInsensitiveKey = this.caseInsensitiveKeys.get(convertKey((String) key));
118+
if (caseInsensitiveKey != null) {
119+
return super.get(caseInsensitiveKey);
120+
}
118121
}
119-
else {
120-
return null;
122+
return null;
123+
}
124+
125+
// Overridden to avoid LinkedHashMap's own hash computation in its getOrDefault impl
126+
@Override
127+
public V getOrDefault(Object key, V defaultValue) {
128+
if (key instanceof String) {
129+
String caseInsensitiveKey = this.caseInsensitiveKeys.get(convertKey((String) key));
130+
if (caseInsensitiveKey != null) {
131+
return super.get(caseInsensitiveKey);
132+
}
121133
}
134+
return defaultValue;
122135
}
123136

124137
@Override
125138
public V remove(Object key) {
126-
if (key instanceof String ) {
127-
return super.remove(this.caseInsensitiveKeys.remove(convertKey((String) key)));
128-
}
129-
else {
130-
return null;
139+
if (key instanceof String) {
140+
String caseInsensitiveKey = this.caseInsensitiveKeys.remove(convertKey((String) key));
141+
if (caseInsensitiveKey != null) {
142+
return super.remove(caseInsensitiveKey);
143+
}
131144
}
145+
return null;
132146
}
133147

134148
@Override

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

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.util;
1818

19-
import org.junit.Before;
2019
import org.junit.Test;
2120

2221
import static org.junit.Assert.*;
@@ -26,12 +25,8 @@
2625
*/
2726
public class LinkedCaseInsensitiveMapTests {
2827

29-
private LinkedCaseInsensitiveMap<String> map;
28+
private final LinkedCaseInsensitiveMap<String> map = new LinkedCaseInsensitiveMap<String>();
3029

31-
@Before
32-
public void setUp() {
33-
map = new LinkedCaseInsensitiveMap<String>();
34-
}
3530

3631
@Test
3732
public void putAndGet() {
@@ -55,4 +50,28 @@ public void putWithOverlappingKeys() {
5550
assertEquals("value3", map.get("Key"));
5651
}
5752

53+
@Test
54+
public void getOrDefault() {
55+
map.put("key", "value1");
56+
map.put("KEY", "value2");
57+
map.put("Key", "value3");
58+
assertEquals("value3", map.getOrDefault("key", "N"));
59+
assertEquals("value3", map.getOrDefault("KEY", "N"));
60+
assertEquals("value3", map.getOrDefault("Key", "N"));
61+
assertEquals("N", map.getOrDefault("keeeey", "N"));
62+
assertEquals("N", map.getOrDefault(new Object(), "N"));
63+
}
64+
65+
@Test
66+
public void getOrDefaultWithNullValue() {
67+
map.put("key", null);
68+
map.put("KEY", null);
69+
map.put("Key", null);
70+
assertNull(map.getOrDefault("key", "N"));
71+
assertNull(map.getOrDefault("KEY", "N"));
72+
assertNull(map.getOrDefault("Key", "N"));
73+
assertEquals("N", map.getOrDefault("keeeey", "N"));
74+
assertEquals("N", map.getOrDefault(new Object(), "N"));
75+
}
76+
5877
}

0 commit comments

Comments
 (0)