LinkedCaseInsensitiveMap provides case-insensitive keySet again

Issue: SPR-15026
This commit is contained in:
Juergen Hoeller 2017-01-12 21:14:07 +01:00
parent 60882ceb4e
commit 50e5a65b2d
2 changed files with 56 additions and 41 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -85,6 +85,10 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
*/
public LinkedCaseInsensitiveMap(int initialCapacity, Locale locale) {
this.targetMap = new LinkedHashMap<String, V>(initialCapacity) {
@Override
public boolean containsKey(Object key) {
return LinkedCaseInsensitiveMap.this.containsKey(key);
}
@Override
protected boolean removeEldestEntry(Map.Entry<String, V> eldest) {
boolean doRemove = LinkedCaseInsensitiveMap.this.removeEldestEntry(eldest);
@ -119,50 +123,16 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
return this.targetMap.isEmpty();
}
@Override
public boolean containsValue(Object value) {
return this.targetMap.containsValue(value);
}
@Override
public Set<String> keySet() {
return this.targetMap.keySet();
}
@Override
public Collection<V> values() {
return this.targetMap.values();
}
@Override
public Set<Entry<String, V>> entrySet() {
return this.targetMap.entrySet();
}
@Override
public V put(String key, V value) {
String oldKey = this.caseInsensitiveKeys.put(convertKey(key), key);
if (oldKey != null && !oldKey.equals(key)) {
this.targetMap.remove(oldKey);
}
return this.targetMap.put(key, value);
}
@Override
public void putAll(Map<? extends String, ? extends V> map) {
if (map.isEmpty()) {
return;
}
for (Map.Entry<? extends String, ? extends V> entry : map.entrySet()) {
put(entry.getKey(), entry.getValue());
}
}
@Override
public boolean containsKey(Object key) {
return (key instanceof String && this.caseInsensitiveKeys.containsKey(convertKey((String) key)));
}
@Override
public boolean containsValue(Object value) {
return this.targetMap.containsValue(value);
}
@Override
public V get(Object key) {
if (key instanceof String) {
@ -185,6 +155,25 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
return defaultValue;
}
@Override
public V put(String key, V value) {
String oldKey = this.caseInsensitiveKeys.put(convertKey(key), key);
if (oldKey != null && !oldKey.equals(key)) {
this.targetMap.remove(oldKey);
}
return this.targetMap.put(key, value);
}
@Override
public void putAll(Map<? extends String, ? extends V> map) {
if (map.isEmpty()) {
return;
}
for (Map.Entry<? extends String, ? extends V> entry : map.entrySet()) {
put(entry.getKey(), entry.getValue());
}
}
@Override
public V remove(Object key) {
if (key instanceof String) {
@ -202,6 +191,20 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
this.targetMap.clear();
}
@Override
public Set<String> keySet() {
return this.targetMap.keySet();
}
@Override
public Collection<V> values() {
return this.targetMap.values();
}
@Override
public Set<Entry<String, V>> entrySet() {
return this.targetMap.entrySet();
}
@Override
public LinkedCaseInsensitiveMap<V> clone() {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -37,6 +37,12 @@ public class LinkedCaseInsensitiveMapTests {
assertEquals("value3", map.get("key"));
assertEquals("value3", map.get("KEY"));
assertEquals("value3", map.get("Key"));
assertTrue(map.containsKey("key"));
assertTrue(map.containsKey("KEY"));
assertTrue(map.containsKey("Key"));
assertTrue(map.keySet().contains("key"));
assertTrue(map.keySet().contains("KEY"));
assertTrue(map.keySet().contains("Key"));
}
@Test
@ -48,6 +54,12 @@ public class LinkedCaseInsensitiveMapTests {
assertEquals("value3", map.get("key"));
assertEquals("value3", map.get("KEY"));
assertEquals("value3", map.get("Key"));
assertTrue(map.containsKey("key"));
assertTrue(map.containsKey("KEY"));
assertTrue(map.containsKey("Key"));
assertTrue(map.keySet().contains("key"));
assertTrue(map.keySet().contains("KEY"));
assertTrue(map.keySet().contains("Key"));
}
@Test