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;
}