Consistent handling of empty List entries in LinkedMultiValueMap

Closes gh-22912
This commit is contained in:
Juergen Hoeller
2019-05-07 14:02:25 +02:00
parent 0ef611707d
commit d777c73f78
2 changed files with 55 additions and 23 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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.
@@ -34,6 +34,8 @@ import java.util.Set;
* @author Arjen Poutsma
* @author Juergen Hoeller
* @since 3.0
* @param <K> the key type
* @param <V> the value element type
*/
public class LinkedMultiValueMap<K, V> implements MultiValueMap<K, V>, Serializable, Cloneable {
@@ -73,6 +75,12 @@ public class LinkedMultiValueMap<K, V> implements MultiValueMap<K, V>, Serializa
// MultiValueMap implementation
@Override
public V getFirst(K key) {
List<V> values = this.targetMap.get(key);
return (values != null && !values.isEmpty() ? values.get(0) : null);
}
@Override
public void add(K key, V value) {
List<V> values = this.targetMap.get(key);
@@ -83,12 +91,6 @@ public class LinkedMultiValueMap<K, V> implements MultiValueMap<K, V>, Serializa
values.add(value);
}
@Override
public V getFirst(K key) {
List<V> values = this.targetMap.get(key);
return (values != null ? values.get(0) : null);
}
@Override
public void set(K key, V value) {
List<V> values = new LinkedList<V>();
@@ -107,7 +109,10 @@ public class LinkedMultiValueMap<K, V> implements MultiValueMap<K, V>, Serializa
public Map<K, V> toSingleValueMap() {
LinkedHashMap<K, V> singleValueMap = new LinkedHashMap<K,V>(this.targetMap.size());
for (Entry<K, List<V>> entry : this.targetMap.entrySet()) {
singleValueMap.put(entry.getKey(), entry.getValue().get(0));
List<V> values = entry.getValue();
if (values != null && !values.isEmpty()) {
singleValueMap.put(entry.getKey(), values.get(0));
}
}
return singleValueMap;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 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.
@@ -28,10 +28,11 @@ import static org.junit.Assert.*;
/**
* @author Arjen Poutsma
* @author Juergen Hoeller
*/
public class LinkedMultiValueMapTests {
private final LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
private final LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<>();
@Test
@@ -39,22 +40,12 @@ public class LinkedMultiValueMapTests {
map.add("key", "value1");
map.add("key", "value2");
assertEquals(1, map.size());
List<String> expected = new ArrayList<String>(2);
List<String> expected = new ArrayList<>(2);
expected.add("value1");
expected.add("value2");
assertEquals(expected, map.get("key"));
}
@Test
public void getFirst() {
List<String> values = new ArrayList<String>(2);
values.add("value1");
values.add("value2");
map.put("key", values);
assertEquals("value1", map.getFirst("key"));
assertNull(map.getFirst("other"));
}
@Test
public void set() {
map.set("key", "value1");
@@ -63,15 +54,51 @@ public class LinkedMultiValueMapTests {
assertEquals(Collections.singletonList("value2"), map.get("key"));
}
@Test
public void getFirst() {
List<String> values = new ArrayList<>(2);
values.add("value1");
values.add("value2");
map.put("key", values);
assertEquals("value1", map.getFirst("key"));
assertNull(map.getFirst("other"));
}
@Test
public void getFirstWithEmptyList() {
map.put("key", Collections.emptyList());
assertNull(map.getFirst("key"));
assertNull(map.getFirst("other"));
}
@Test
public void toSingleValueMap() {
List<String> values = new ArrayList<>(2);
values.add("value1");
values.add("value2");
map.put("key", values);
Map<String, String> svm = map.toSingleValueMap();
assertEquals(1, svm.size());
assertEquals("value1", svm.get("key"));
}
@Test
public void toSingleValueMapWithEmptyList() {
map.put("key", Collections.emptyList());
Map<String, String> svm = map.toSingleValueMap();
assertEquals(0, svm.size());
assertNull(svm.get("key"));
}
@Test
public void equals() {
map.set("key1", "value1");
assertEquals(map, map);
MultiValueMap<String, String> o1 = new LinkedMultiValueMap<String, String>();
MultiValueMap<String, String> o1 = new LinkedMultiValueMap<>();
o1.set("key1", "value1");
assertEquals(map, o1);
assertEquals(o1, map);
Map<String, List<String>> o2 = new HashMap<String, List<String>>();
Map<String, List<String>> o2 = new HashMap<>();
o2.put("key1", Collections.singletonList("value1"));
assertEquals(map, o2);
assertEquals(o2, map);