Browse Source

LinkedCaseInsensitiveMap provides case-insensitive keySet again

Issue: SPR-15026
pull/1296/head
Juergen Hoeller 8 years ago
parent
commit
50e5a65b2d
  1. 75
      spring-core/src/main/java/org/springframework/util/LinkedCaseInsensitiveMap.java
  2. 14
      spring-core/src/test/java/org/springframework/util/LinkedCaseInsensitiveMapTests.java

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

@ -1,5 +1,5 @@ @@ -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 @@ -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);
@ -120,23 +124,35 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable @@ -120,23 +124,35 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
}
@Override
public boolean containsValue(Object value) {
return this.targetMap.containsValue(value);
public boolean containsKey(Object key) {
return (key instanceof String && this.caseInsensitiveKeys.containsKey(convertKey((String) key)));
}
@Override
public Set<String> keySet() {
return this.targetMap.keySet();
public boolean containsValue(Object value) {
return this.targetMap.containsValue(value);
}
@Override
public Collection<V> values() {
return this.targetMap.values();
public V get(Object key) {
if (key instanceof String) {
String caseInsensitiveKey = this.caseInsensitiveKeys.get(convertKey((String) key));
if (caseInsensitiveKey != null) {
return this.targetMap.get(caseInsensitiveKey);
}
}
return null;
}
@Override
public Set<Entry<String, V>> entrySet() {
return this.targetMap.entrySet();
public V getOrDefault(Object key, V defaultValue) {
if (key instanceof String) {
String caseInsensitiveKey = this.caseInsensitiveKeys.get(convertKey((String) key));
if (caseInsensitiveKey != null) {
return this.targetMap.get(caseInsensitiveKey);
}
}
return defaultValue;
}
@Override
@ -158,33 +174,6 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable @@ -158,33 +174,6 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
}
}
@Override
public boolean containsKey(Object key) {
return (key instanceof String && this.caseInsensitiveKeys.containsKey(convertKey((String) key)));
}
@Override
public V get(Object key) {
if (key instanceof String) {
String caseInsensitiveKey = this.caseInsensitiveKeys.get(convertKey((String) key));
if (caseInsensitiveKey != null) {
return this.targetMap.get(caseInsensitiveKey);
}
}
return null;
}
@Override
public V getOrDefault(Object key, V defaultValue) {
if (key instanceof String) {
String caseInsensitiveKey = this.caseInsensitiveKeys.get(convertKey((String) key));
if (caseInsensitiveKey != null) {
return this.targetMap.get(caseInsensitiveKey);
}
}
return defaultValue;
}
@Override
public V remove(Object key) {
if (key instanceof String) {
@ -202,6 +191,20 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable @@ -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() {

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

@ -1,5 +1,5 @@ @@ -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 { @@ -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 { @@ -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

Loading…
Cancel
Save