Browse Source

LinkedCaseInsensitiveMap provides case-insensitive keySet again

Issue: SPR-15026
pull/1296/head
Juergen Hoeller 9 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 @@
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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) { public LinkedCaseInsensitiveMap(int initialCapacity, Locale locale) {
this.targetMap = new LinkedHashMap<String, V>(initialCapacity) { this.targetMap = new LinkedHashMap<String, V>(initialCapacity) {
@Override
public boolean containsKey(Object key) {
return LinkedCaseInsensitiveMap.this.containsKey(key);
}
@Override @Override
protected boolean removeEldestEntry(Map.Entry<String, V> eldest) { protected boolean removeEldestEntry(Map.Entry<String, V> eldest) {
boolean doRemove = LinkedCaseInsensitiveMap.this.removeEldestEntry(eldest); boolean doRemove = LinkedCaseInsensitiveMap.this.removeEldestEntry(eldest);
@ -120,23 +124,35 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
} }
@Override @Override
public boolean containsValue(Object value) { public boolean containsKey(Object key) {
return this.targetMap.containsValue(value); return (key instanceof String && this.caseInsensitiveKeys.containsKey(convertKey((String) key)));
} }
@Override @Override
public Set<String> keySet() { public boolean containsValue(Object value) {
return this.targetMap.keySet(); return this.targetMap.containsValue(value);
} }
@Override @Override
public Collection<V> values() { public V get(Object key) {
return this.targetMap.values(); if (key instanceof String) {
String caseInsensitiveKey = this.caseInsensitiveKeys.get(convertKey((String) key));
if (caseInsensitiveKey != null) {
return this.targetMap.get(caseInsensitiveKey);
}
}
return null;
} }
@Override @Override
public Set<Entry<String, V>> entrySet() { public V getOrDefault(Object key, V defaultValue) {
return this.targetMap.entrySet(); if (key instanceof String) {
String caseInsensitiveKey = this.caseInsensitiveKeys.get(convertKey((String) key));
if (caseInsensitiveKey != null) {
return this.targetMap.get(caseInsensitiveKey);
}
}
return defaultValue;
} }
@Override @Override
@ -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 @Override
public V remove(Object key) { public V remove(Object key) {
if (key instanceof String) { if (key instanceof String) {
@ -202,6 +191,20 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
this.targetMap.clear(); 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 @Override
public LinkedCaseInsensitiveMap<V> clone() { public LinkedCaseInsensitiveMap<V> clone() {

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

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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")); 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 @Test
@ -48,6 +54,12 @@ public class LinkedCaseInsensitiveMapTests {
assertEquals("value3", map.get("key")); assertEquals("value3", map.get("key"));
assertEquals("value3", map.get("KEY")); 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 @Test

Loading…
Cancel
Save