LinkedCaseInsensitiveMap provides reliable getOrDefault implementation
Issue: SPR-13981
This commit is contained in:
parent
b6dd8a9233
commit
7a32ce317c
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 the original author or authors.
|
* Copyright 2002-2016 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.
|
||||||
|
@ -114,21 +114,35 @@ public class LinkedCaseInsensitiveMap<V> extends LinkedHashMap<String, V> {
|
||||||
@Override
|
@Override
|
||||||
public V get(Object key) {
|
public V get(Object key) {
|
||||||
if (key instanceof String) {
|
if (key instanceof String) {
|
||||||
return super.get(this.caseInsensitiveKeys.get(convertKey((String) key)));
|
String caseInsensitiveKey = this.caseInsensitiveKeys.get(convertKey((String) key));
|
||||||
|
if (caseInsensitiveKey != null) {
|
||||||
|
return super.get(caseInsensitiveKey);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
return null;
|
||||||
return null;
|
}
|
||||||
|
|
||||||
|
// Overridden to avoid LinkedHashMap's own hash computation in its getOrDefault impl
|
||||||
|
@Override
|
||||||
|
public V getOrDefault(Object key, V defaultValue) {
|
||||||
|
if (key instanceof String) {
|
||||||
|
String caseInsensitiveKey = this.caseInsensitiveKeys.get(convertKey((String) key));
|
||||||
|
if (caseInsensitiveKey != null) {
|
||||||
|
return super.get(caseInsensitiveKey);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public V remove(Object key) {
|
public V remove(Object key) {
|
||||||
if (key instanceof String ) {
|
if (key instanceof String) {
|
||||||
return super.remove(this.caseInsensitiveKeys.remove(convertKey((String) key)));
|
String caseInsensitiveKey = this.caseInsensitiveKeys.remove(convertKey((String) key));
|
||||||
}
|
if (caseInsensitiveKey != null) {
|
||||||
else {
|
return super.remove(caseInsensitiveKey);
|
||||||
return null;
|
}
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 the original author or authors.
|
* Copyright 2002-2016 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.
|
||||||
|
@ -16,7 +16,6 @@
|
||||||
|
|
||||||
package org.springframework.util;
|
package org.springframework.util;
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
@ -26,12 +25,8 @@ import static org.junit.Assert.*;
|
||||||
*/
|
*/
|
||||||
public class LinkedCaseInsensitiveMapTests {
|
public class LinkedCaseInsensitiveMapTests {
|
||||||
|
|
||||||
private LinkedCaseInsensitiveMap<String> map;
|
private final LinkedCaseInsensitiveMap<String> map = new LinkedCaseInsensitiveMap<String>();
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() {
|
|
||||||
map = new LinkedCaseInsensitiveMap<String>();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void putAndGet() {
|
public void putAndGet() {
|
||||||
|
@ -55,4 +50,28 @@ public class LinkedCaseInsensitiveMapTests {
|
||||||
assertEquals("value3", map.get("Key"));
|
assertEquals("value3", map.get("Key"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getOrDefault() {
|
||||||
|
map.put("key", "value1");
|
||||||
|
map.put("KEY", "value2");
|
||||||
|
map.put("Key", "value3");
|
||||||
|
assertEquals("value3", map.getOrDefault("key", "N"));
|
||||||
|
assertEquals("value3", map.getOrDefault("KEY", "N"));
|
||||||
|
assertEquals("value3", map.getOrDefault("Key", "N"));
|
||||||
|
assertEquals("N", map.getOrDefault("keeeey", "N"));
|
||||||
|
assertEquals("N", map.getOrDefault(new Object(), "N"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getOrDefaultWithNullValue() {
|
||||||
|
map.put("key", null);
|
||||||
|
map.put("KEY", null);
|
||||||
|
map.put("Key", null);
|
||||||
|
assertNull(map.getOrDefault("key", "N"));
|
||||||
|
assertNull(map.getOrDefault("KEY", "N"));
|
||||||
|
assertNull(map.getOrDefault("Key", "N"));
|
||||||
|
assertEquals("N", map.getOrDefault("keeeey", "N"));
|
||||||
|
assertEquals("N", map.getOrDefault(new Object(), "N"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue