Consistent MultiValueMap behavior for empty list values

This commit extracts MultiValueMapAdapter from CollectionUtils and reuses its implementation as base class of LinkedMultiValueMap.

Closes gh-25140
This commit is contained in:
Juergen Hoeller 2020-05-29 15:48:19 +02:00
parent 768257567d
commit bec89dba4c
4 changed files with 202 additions and 299 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@ -16,7 +16,6 @@
package org.springframework.util;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@ -24,7 +23,6 @@ import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
@ -412,25 +410,28 @@ public abstract class CollectionUtils {
/**
* Adapt a {@code Map<K, List<V>>} to an {@code MultiValueMap<K, V>}.
* @param map the original map
* @return the multi-value map
* @param targetMap the original map
* @return the adapted multi-value map (wrapping the original map)
* @since 3.1
*/
public static <K, V> MultiValueMap<K, V> toMultiValueMap(Map<K, List<V>> map) {
return new MultiValueMapAdapter<>(map);
public static <K, V> MultiValueMap<K, V> toMultiValueMap(Map<K, List<V>> targetMap) {
Assert.notNull(targetMap, "'targetMap' must not be null");
return new MultiValueMapAdapter<>(targetMap);
}
/**
* Return an unmodifiable view of the specified multi-value map.
* @param map the map for which an unmodifiable view is to be returned.
* @return an unmodifiable view of the specified multi-value map.
* @param targetMap the map for which an unmodifiable view is to be returned.
* @return an unmodifiable view of the specified multi-value map
* @since 3.1
*/
@SuppressWarnings("unchecked")
public static <K, V> MultiValueMap<K, V> unmodifiableMultiValueMap(MultiValueMap<? extends K, ? extends V> map) {
Assert.notNull(map, "'map' must not be null");
Map<K, List<V>> result = new LinkedHashMap<>(map.size());
map.forEach((key, value) -> {
public static <K, V> MultiValueMap<K, V> unmodifiableMultiValueMap(
MultiValueMap<? extends K, ? extends V> targetMap) {
Assert.notNull(targetMap, "'targetMap' must not be null");
Map<K, List<V>> result = new LinkedHashMap<>(targetMap.size());
targetMap.forEach((key, value) -> {
List<? extends V> values = Collections.unmodifiableList(value);
result.put(key, (List<V>) values);
});
@ -467,141 +468,4 @@ public abstract class CollectionUtils {
}
/**
* Adapts a Map to the MultiValueMap contract.
*/
@SuppressWarnings("serial")
private static class MultiValueMapAdapter<K, V> implements MultiValueMap<K, V>, Serializable {
private final Map<K, List<V>> map;
public MultiValueMapAdapter(Map<K, List<V>> map) {
Assert.notNull(map, "'map' must not be null");
this.map = map;
}
@Override
@Nullable
public V getFirst(K key) {
List<V> values = this.map.get(key);
return (values != null ? values.get(0) : null);
}
@Override
public void add(K key, @Nullable V value) {
List<V> values = this.map.computeIfAbsent(key, k -> new LinkedList<>());
values.add(value);
}
@Override
public void addAll(K key, List<? extends V> values) {
List<V> currentValues = this.map.computeIfAbsent(key, k -> new LinkedList<>());
currentValues.addAll(values);
}
@Override
public void addAll(MultiValueMap<K, V> values) {
for (Entry<K, List<V>> entry : values.entrySet()) {
addAll(entry.getKey(), entry.getValue());
}
}
@Override
public void set(K key, @Nullable V value) {
List<V> values = new LinkedList<>();
values.add(value);
this.map.put(key, values);
}
@Override
public void setAll(Map<K, V> values) {
values.forEach(this::set);
}
@Override
public Map<K, V> toSingleValueMap() {
LinkedHashMap<K, V> singleValueMap = new LinkedHashMap<>(this.map.size());
this.map.forEach((key, value) -> singleValueMap.put(key, value.get(0)));
return singleValueMap;
}
@Override
public int size() {
return this.map.size();
}
@Override
public boolean isEmpty() {
return this.map.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return this.map.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return this.map.containsValue(value);
}
@Override
public List<V> get(Object key) {
return this.map.get(key);
}
@Override
public List<V> put(K key, List<V> value) {
return this.map.put(key, value);
}
@Override
public List<V> remove(Object key) {
return this.map.remove(key);
}
@Override
public void putAll(Map<? extends K, ? extends List<V>> map) {
this.map.putAll(map);
}
@Override
public void clear() {
this.map.clear();
}
@Override
public Set<K> keySet() {
return this.map.keySet();
}
@Override
public Collection<List<V>> values() {
return this.map.values();
}
@Override
public Set<Entry<K, List<V>>> entrySet() {
return this.map.entrySet();
}
@Override
public boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
return this.map.equals(other);
}
@Override
public int hashCode() {
return this.map.hashCode();
}
@Override
public String toString() {
return this.map.toString();
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@ -273,8 +273,8 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
}
@Override
public boolean equals(@Nullable Object obj) {
return this.targetMap.equals(obj);
public boolean equals(@Nullable Object other) {
return (this == other || this.targetMap.equals(other));
}
@Override

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@ -17,14 +17,10 @@
package org.springframework.util;
import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.lang.Nullable;
/**
* Simple implementation of {@link MultiValueMap} that wraps a {@link LinkedHashMap},
@ -39,18 +35,16 @@ import org.springframework.lang.Nullable;
* @param <K> the key type
* @param <V> the value element type
*/
public class LinkedMultiValueMap<K, V> implements MultiValueMap<K, V>, Serializable, Cloneable {
public class LinkedMultiValueMap<K, V> extends MultiValueMapAdapter<K, V> implements Serializable, Cloneable {
private static final long serialVersionUID = 3801124242820219131L;
private final Map<K, List<V>> targetMap;
/**
* Create a new LinkedMultiValueMap that wraps a {@link LinkedHashMap}.
*/
public LinkedMultiValueMap() {
this.targetMap = new LinkedHashMap<>();
super(new LinkedHashMap<>());
}
/**
@ -59,7 +53,7 @@ public class LinkedMultiValueMap<K, V> implements MultiValueMap<K, V>, Serializa
* @param initialCapacity the initial capacity
*/
public LinkedMultiValueMap(int initialCapacity) {
this.targetMap = new LinkedHashMap<>(initialCapacity);
super(new LinkedHashMap<>(initialCapacity));
}
/**
@ -71,125 +65,7 @@ public class LinkedMultiValueMap<K, V> implements MultiValueMap<K, V>, Serializa
* @see #deepCopy()
*/
public LinkedMultiValueMap(Map<K, List<V>> otherMap) {
this.targetMap = new LinkedHashMap<>(otherMap);
}
// MultiValueMap implementation
@Override
@Nullable
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, @Nullable V value) {
List<V> values = this.targetMap.computeIfAbsent(key, k -> new LinkedList<>());
values.add(value);
}
@Override
public void addAll(K key, List<? extends V> values) {
List<V> currentValues = this.targetMap.computeIfAbsent(key, k -> new LinkedList<>());
currentValues.addAll(values);
}
@Override
public void addAll(MultiValueMap<K, V> values) {
for (Entry<K, List<V>> entry : values.entrySet()) {
addAll(entry.getKey(), entry.getValue());
}
}
@Override
public void set(K key, @Nullable V value) {
List<V> values = new LinkedList<>();
values.add(value);
this.targetMap.put(key, values);
}
@Override
public void setAll(Map<K, V> values) {
values.forEach(this::set);
}
@Override
public Map<K, V> toSingleValueMap() {
LinkedHashMap<K, V> singleValueMap = new LinkedHashMap<>(this.targetMap.size());
this.targetMap.forEach((key, values) -> {
if (values != null && !values.isEmpty()) {
singleValueMap.put(key, values.get(0));
}
});
return singleValueMap;
}
// Map implementation
@Override
public int size() {
return this.targetMap.size();
}
@Override
public boolean isEmpty() {
return this.targetMap.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return this.targetMap.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return this.targetMap.containsValue(value);
}
@Override
@Nullable
public List<V> get(Object key) {
return this.targetMap.get(key);
}
@Override
@Nullable
public List<V> put(K key, List<V> value) {
return this.targetMap.put(key, value);
}
@Override
@Nullable
public List<V> remove(Object key) {
return this.targetMap.remove(key);
}
@Override
public void putAll(Map<? extends K, ? extends List<V>> map) {
this.targetMap.putAll(map);
}
@Override
public void clear() {
this.targetMap.clear();
}
@Override
public Set<K> keySet() {
return this.targetMap.keySet();
}
@Override
public Collection<List<V>> values() {
return this.targetMap.values();
}
@Override
public Set<Entry<K, List<V>>> entrySet() {
return this.targetMap.entrySet();
super(new LinkedHashMap<>(otherMap));
}
@ -203,8 +79,8 @@ public class LinkedMultiValueMap<K, V> implements MultiValueMap<K, V>, Serializa
* @see #clone()
*/
public LinkedMultiValueMap<K, V> deepCopy() {
LinkedMultiValueMap<K, V> copy = new LinkedMultiValueMap<>(this.targetMap.size());
this.targetMap.forEach((key, value) -> copy.put(key, new LinkedList<>(value)));
LinkedMultiValueMap<K, V> copy = new LinkedMultiValueMap<>(size());
forEach((key, values) -> copy.put(key, new LinkedList<>(values)));
return copy;
}
@ -224,19 +100,4 @@ public class LinkedMultiValueMap<K, V> implements MultiValueMap<K, V>, Serializa
return new LinkedMultiValueMap<>(this);
}
@Override
public boolean equals(@Nullable Object obj) {
return this.targetMap.equals(obj);
}
@Override
public int hashCode() {
return this.targetMap.hashCode();
}
@Override
public String toString() {
return this.targetMap.toString();
}
}

View File

@ -0,0 +1,178 @@
/*
* Copyright 2002-2020 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.util;
import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.lang.Nullable;
/**
* Adapts a given {@link Map} to the {@link MultiValueMap} contract.
*
* @author Arjen Poutsma
* @author Juergen Hoeller
* @since 3.1
* @param <K> the key type
* @param <V> the value element type
* @see CollectionUtils#toMultiValueMap
* @see LinkedMultiValueMap
*/
@SuppressWarnings("serial")
class MultiValueMapAdapter<K, V> implements MultiValueMap<K, V>, Serializable {
private final Map<K, List<V>> targetMap;
MultiValueMapAdapter(Map<K, List<V>> targetMap) {
this.targetMap = targetMap;
}
@Override
@Nullable
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, @Nullable V value) {
List<V> values = this.targetMap.computeIfAbsent(key, k -> new LinkedList<>());
values.add(value);
}
@Override
public void addAll(K key, List<? extends V> values) {
List<V> currentValues = this.targetMap.computeIfAbsent(key, k -> new LinkedList<>());
currentValues.addAll(values);
}
@Override
public void addAll(MultiValueMap<K, V> values) {
for (Entry<K, List<V>> entry : values.entrySet()) {
addAll(entry.getKey(), entry.getValue());
}
}
@Override
public void set(K key, @Nullable V value) {
List<V> values = new LinkedList<>();
values.add(value);
this.targetMap.put(key, values);
}
@Override
public void setAll(Map<K, V> values) {
values.forEach(this::set);
}
@Override
public Map<K, V> toSingleValueMap() {
Map<K, V> singleValueMap = new LinkedHashMap<>(this.targetMap.size());
this.targetMap.forEach((key, values) -> {
if (values != null && !values.isEmpty()) {
singleValueMap.put(key, values.get(0));
}
});
return singleValueMap;
}
@Override
public int size() {
return this.targetMap.size();
}
@Override
public boolean isEmpty() {
return this.targetMap.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return this.targetMap.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return this.targetMap.containsValue(value);
}
@Override
@Nullable
public List<V> get(Object key) {
return this.targetMap.get(key);
}
@Override
@Nullable
public List<V> put(K key, List<V> value) {
return this.targetMap.put(key, value);
}
@Override
@Nullable
public List<V> remove(Object key) {
return this.targetMap.remove(key);
}
@Override
public void putAll(Map<? extends K, ? extends List<V>> map) {
this.targetMap.putAll(map);
}
@Override
public void clear() {
this.targetMap.clear();
}
@Override
public Set<K> keySet() {
return this.targetMap.keySet();
}
@Override
public Collection<List<V>> values() {
return this.targetMap.values();
}
@Override
public Set<Entry<K, List<V>>> entrySet() {
return this.targetMap.entrySet();
}
@Override
public boolean equals(@Nullable Object other) {
return (this == other || this.targetMap.equals(other));
}
@Override
public int hashCode() {
return this.targetMap.hashCode();
}
@Override
public String toString() {
return this.targetMap.toString();
}
}