Skip to content

Commit b790bd1

Browse files
committed
LinkedCaseInsensitiveMap explicitly implements put/computeIfAbsent
Issue: SPR-16926
1 parent 646d7f9 commit b790bd1

File tree

2 files changed

+45
-20
lines changed

2 files changed

+45
-20
lines changed

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -23,6 +23,7 @@
2323
import java.util.Locale;
2424
import java.util.Map;
2525
import java.util.Set;
26+
import java.util.function.Function;
2627

2728
import org.springframework.lang.Nullable;
2829

@@ -151,6 +152,7 @@ public V get(Object key) {
151152
}
152153

153154
@Override
155+
@Nullable
154156
public V getOrDefault(Object key, V defaultValue) {
155157
if (key instanceof String) {
156158
String caseInsensitiveKey = this.caseInsensitiveKeys.get(convertKey((String) key));
@@ -162,12 +164,15 @@ public V getOrDefault(Object key, V defaultValue) {
162164
}
163165

164166
@Override
167+
@Nullable
165168
public V put(String key, @Nullable V value) {
166169
String oldKey = this.caseInsensitiveKeys.put(convertKey(key), key);
170+
V oldKeyValue = null;
167171
if (oldKey != null && !oldKey.equals(key)) {
168-
this.targetMap.remove(oldKey);
172+
oldKeyValue = this.targetMap.remove(oldKey);
169173
}
170-
return this.targetMap.put(key, value);
174+
V oldValue = this.targetMap.put(key, value);
175+
return (oldKeyValue != null ? oldKeyValue : oldValue);
171176
}
172177

173178
@Override
@@ -178,6 +183,26 @@ public void putAll(Map<? extends String, ? extends V> map) {
178183
map.forEach(this::put);
179184
}
180185

186+
@Override
187+
@Nullable
188+
public V putIfAbsent(String key, @Nullable V value) {
189+
String oldKey = this.caseInsensitiveKeys.putIfAbsent(convertKey(key), key);
190+
if (oldKey != null) {
191+
return this.targetMap.get(oldKey);
192+
}
193+
return this.targetMap.putIfAbsent(key, value);
194+
}
195+
196+
@Override
197+
@Nullable
198+
public V computeIfAbsent(String key, Function<? super String, ? extends V> mappingFunction) {
199+
String oldKey = this.caseInsensitiveKeys.putIfAbsent(convertKey(key), key);
200+
if (oldKey != null) {
201+
return this.targetMap.get(oldKey);
202+
}
203+
return this.targetMap.computeIfAbsent(key, mappingFunction);
204+
}
205+
181206
@Override
182207
@Nullable
183208
public V remove(Object key) {

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -30,9 +30,9 @@ public class LinkedCaseInsensitiveMapTests {
3030

3131
@Test
3232
public void putAndGet() {
33-
map.put("key", "value1");
34-
map.put("key", "value2");
35-
map.put("key", "value3");
33+
assertNull(map.put("key", "value1"));
34+
assertEquals("value1", map.put("key", "value2"));
35+
assertEquals("value2", map.put("key", "value3"));
3636
assertEquals(1, map.size());
3737
assertEquals("value3", map.get("key"));
3838
assertEquals("value3", map.get("KEY"));
@@ -47,9 +47,9 @@ public void putAndGet() {
4747

4848
@Test
4949
public void putWithOverlappingKeys() {
50-
map.put("key", "value1");
51-
map.put("KEY", "value2");
52-
map.put("Key", "value3");
50+
assertNull(map.put("key", "value1"));
51+
assertEquals("value1", map.put("KEY", "value2"));
52+
assertEquals("value2", map.put("Key", "value3"));
5353
assertEquals(1, map.size());
5454
assertEquals("value3", map.get("key"));
5555
assertEquals("value3", map.get("KEY"));
@@ -64,9 +64,9 @@ public void putWithOverlappingKeys() {
6464

6565
@Test
6666
public void getOrDefault() {
67-
map.put("key", "value1");
68-
map.put("KEY", "value2");
69-
map.put("Key", "value3");
67+
assertNull(map.put("key", "value1"));
68+
assertEquals("value1", map.put("KEY", "value2"));
69+
assertEquals("value2", map.put("Key", "value3"));
7070
assertEquals("value3", map.getOrDefault("key", "N"));
7171
assertEquals("value3", map.getOrDefault("KEY", "N"));
7272
assertEquals("value3", map.getOrDefault("Key", "N"));
@@ -76,9 +76,9 @@ public void getOrDefault() {
7676

7777
@Test
7878
public void getOrDefaultWithNullValue() {
79-
map.put("key", null);
80-
map.put("KEY", null);
81-
map.put("Key", null);
79+
assertNull(map.put("key", null));
80+
assertNull(map.put("KEY", null));
81+
assertNull(map.put("Key", null));
8282
assertNull(map.getOrDefault("key", "N"));
8383
assertNull(map.getOrDefault("KEY", "N"));
8484
assertNull(map.getOrDefault("Key", "N"));
@@ -88,9 +88,9 @@ public void getOrDefaultWithNullValue() {
8888

8989
@Test
9090
public void computeIfAbsentWithExistingValue() {
91-
map.put("key", "value1");
92-
map.put("KEY", "value2");
93-
map.put("Key", "value3");
91+
assertNull(map.putIfAbsent("key", "value1"));
92+
assertEquals("value1", map.putIfAbsent("KEY", "value2"));
93+
assertEquals("value1", map.put("Key", "value3"));
9494
assertEquals("value3", map.computeIfAbsent("key", key -> "value1"));
9595
assertEquals("value3", map.computeIfAbsent("KEY", key -> "value2"));
9696
assertEquals("value3", map.computeIfAbsent("Key", key -> "value3"));
@@ -105,7 +105,7 @@ public void computeIfAbsentWithComputedValue() {
105105

106106
@Test
107107
public void mapClone() {
108-
map.put("key", "value1");
108+
assertNull(map.put("key", "value1"));
109109
LinkedCaseInsensitiveMap<String> copy = map.clone();
110110

111111
assertEquals(map.getLocale(), copy.getLocale());

0 commit comments

Comments
 (0)