From e9a87de965c4bf35d2fff1afe3364232b93b5f6b Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 12 Jan 2017 21:14:07 +0100 Subject: [PATCH] LinkedCaseInsensitiveMap provides case-insensitive keySet again Issue: SPR-15026 (cherry picked from commit 50e5a65) --- .../util/LinkedCaseInsensitiveMap.java | 83 ++++++++++--------- .../util/LinkedCaseInsensitiveMapTests.java | 14 +++- 2 files changed, 56 insertions(+), 41 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/LinkedCaseInsensitiveMap.java b/spring-core/src/main/java/org/springframework/util/LinkedCaseInsensitiveMap.java index ce7dfe9818..8e8c2273e7 100644 --- a/spring-core/src/main/java/org/springframework/util/LinkedCaseInsensitiveMap.java +++ b/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"); * you may not use this file except in compliance with the License. @@ -85,6 +85,10 @@ public class LinkedCaseInsensitiveMap implements Map, Serializable */ public LinkedCaseInsensitiveMap(int initialCapacity, Locale locale) { this.targetMap = new LinkedHashMap(initialCapacity) { + @Override + public boolean containsKey(Object key) { + return LinkedCaseInsensitiveMap.this.containsKey(key); + } @Override protected boolean removeEldestEntry(Map.Entry eldest) { boolean doRemove = LinkedCaseInsensitiveMap.this.removeEldestEntry(eldest); @@ -119,50 +123,16 @@ public class LinkedCaseInsensitiveMap implements Map, Serializable return this.targetMap.isEmpty(); } - @Override - public boolean containsValue(Object value) { - return this.targetMap.containsValue(value); - } - - @Override - public Set keySet() { - return this.targetMap.keySet(); - } - - @Override - public Collection values() { - return this.targetMap.values(); - } - - @Override - public Set> 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 map) { - if (map.isEmpty()) { - return; - } - for (Map.Entry 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 implements Map, 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 map) { + if (map.isEmpty()) { + return; + } + for (Map.Entry 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 implements Map, Serializable this.targetMap.clear(); } + @Override + public Set keySet() { + return this.targetMap.keySet(); + } + + @Override + public Collection values() { + return this.targetMap.values(); + } + + @Override + public Set> entrySet() { + return this.targetMap.entrySet(); + } @Override public LinkedCaseInsensitiveMap clone() { diff --git a/spring-core/src/test/java/org/springframework/util/LinkedCaseInsensitiveMapTests.java b/spring-core/src/test/java/org/springframework/util/LinkedCaseInsensitiveMapTests.java index d9bd2b2d4f..369e1b5026 100644 --- a/spring-core/src/test/java/org/springframework/util/LinkedCaseInsensitiveMapTests.java +++ b/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"); * 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