Browse Source

Consistent handling of empty List entries in LinkedMultiValueMap

Closes gh-22912
pull/25598/head
Juergen Hoeller 7 years ago
parent
commit
b69a31de85
  1. 12
      spring-core/src/main/java/org/springframework/util/LinkedMultiValueMap.java
  2. 51
      spring-core/src/test/java/org/springframework/util/LinkedMultiValueMapTests.java

12
spring-core/src/main/java/org/springframework/util/LinkedMultiValueMap.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 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.
@ -36,6 +36,8 @@ import org.springframework.lang.Nullable; @@ -36,6 +36,8 @@ import org.springframework.lang.Nullable;
* @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 {
@ -79,7 +81,7 @@ public class LinkedMultiValueMap<K, V> implements MultiValueMap<K, V>, Serializa @@ -79,7 +81,7 @@ public class LinkedMultiValueMap<K, V> implements MultiValueMap<K, V>, Serializa
@Nullable
public V getFirst(K key) {
List<V> values = this.targetMap.get(key);
return (values != null ? values.get(0) : null);
return (values != null && !values.isEmpty() ? values.get(0) : null);
}
@Override
@ -116,7 +118,11 @@ public class LinkedMultiValueMap<K, V> implements MultiValueMap<K, V>, Serializa @@ -116,7 +118,11 @@ public class LinkedMultiValueMap<K, V> implements MultiValueMap<K, V>, Serializa
@Override
public Map<K, V> toSingleValueMap() {
LinkedHashMap<K, V> singleValueMap = new LinkedHashMap<>(this.targetMap.size());
this.targetMap.forEach((key, value) -> singleValueMap.put(key, value.get(0)));
this.targetMap.forEach((key, values) -> {
if (values != null && !values.isEmpty()) {
singleValueMap.put(key, values.get(0));
}
});
return singleValueMap;
}

51
spring-core/src/test/java/org/springframework/util/LinkedMultiValueMapTests.java

@ -1,5 +1,5 @@ @@ -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.
@ -29,10 +29,11 @@ import static org.junit.Assert.*; @@ -29,10 +29,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
@ -47,7 +48,15 @@ public class LinkedMultiValueMapTests { @@ -47,7 +48,15 @@ public class LinkedMultiValueMapTests {
}
@Test
public void addAll() throws Exception {
public void set() {
map.set("key", "value1");
map.set("key", "value2");
assertEquals(1, map.size());
assertEquals(Collections.singletonList("value2"), map.get("key"));
}
@Test
public void addAll() {
map.add("key", "value1");
map.addAll("key", Arrays.asList("value2", "value3"));
assertEquals(1, map.size());
@ -58,6 +67,14 @@ public class LinkedMultiValueMapTests { @@ -58,6 +67,14 @@ public class LinkedMultiValueMapTests {
assertEquals(expected, map.get("key"));
}
@Test
public void addAllWithEmptyList() {
map.addAll("key", Collections.emptyList());
assertEquals(1, map.size());
assertEquals(Collections.emptyList(), map.get("key"));
assertNull(map.getFirst("key"));
}
@Test
public void getFirst() {
List<String> values = new ArrayList<>(2);
@ -69,11 +86,29 @@ public class LinkedMultiValueMapTests { @@ -69,11 +86,29 @@ public class LinkedMultiValueMapTests {
}
@Test
public void set() {
map.set("key", "value1");
map.set("key", "value2");
assertEquals(1, map.size());
assertEquals(Collections.singletonList("value2"), map.get("key"));
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

Loading…
Cancel
Save