LinkedMultiValueMap.java 5.9 KB
Newer Older
1
/*
P
Phillip Webb 已提交
2
 * Copyright 2002-2018 the original author or authors.
3 4 5 6 7
 *
 * 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
 *
S
Spring Operator 已提交
8
 *      https://www.apache.org/licenses/LICENSE-2.0
9 10 11 12 13 14 15 16 17 18
 *
 * 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;

19
import java.io.Serializable;
20 21 22 23 24 25 26
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

27 28
import org.springframework.lang.Nullable;

29
/**
30 31
 * Simple implementation of {@link MultiValueMap} that wraps a {@link LinkedHashMap},
 * storing multiple values in a {@link LinkedList}.
32 33 34
 *
 * <p>This Map implementation is generally not thread-safe. It is primarily designed
 * for data structures exposed from request objects, for use in a single thread only.
35
 *
36 37 38
 * @author Arjen Poutsma
 * @author Juergen Hoeller
 * @since 3.0
P
Phillip Webb 已提交
39 40
 * @param <K> the key type
 * @param <V> the value element type
41
 */
42
public class LinkedMultiValueMap<K, V> implements MultiValueMap<K, V>, Serializable, Cloneable {
43 44

	private static final long serialVersionUID = 3801124242820219131L;
45 46 47

	private final Map<K, List<V>> targetMap;

48

49
	/**
50
	 * Create a new LinkedMultiValueMap that wraps a {@link LinkedHashMap}.
51 52
	 */
	public LinkedMultiValueMap() {
53
		this.targetMap = new LinkedHashMap<>();
54 55
	}

A
Arjen Poutsma 已提交
56
	/**
57 58
	 * Create a new LinkedMultiValueMap that wraps a {@link LinkedHashMap}
	 * with the given initial capacity.
A
Arjen Poutsma 已提交
59 60 61
	 * @param initialCapacity the initial capacity
	 */
	public LinkedMultiValueMap(int initialCapacity) {
62
		this.targetMap = new LinkedHashMap<>(initialCapacity);
A
Arjen Poutsma 已提交
63 64
	}

65
	/**
66 67 68
	 * Copy constructor: Create a new LinkedMultiValueMap with the same mappings as
	 * the specified Map. Note that this will be a shallow copy; its value-holding
	 * List entries will get reused and therefore cannot get modified independently.
69
	 * @param otherMap the Map whose mappings are to be placed in this Map
70 71
	 * @see #clone()
	 * @see #deepCopy()
72
	 */
73
	public LinkedMultiValueMap(Map<K, List<V>> otherMap) {
74
		this.targetMap = new LinkedHashMap<>(otherMap);
75 76
	}

77

78 79
	// MultiValueMap implementation

80
	@Override
81
	@Nullable
82 83
	public V getFirst(K key) {
		List<V> values = this.targetMap.get(key);
84
		return (values != null && !values.isEmpty() ? values.get(0) : null);
85 86
	}

87
	@Override
88
	public void add(K key, @Nullable V value) {
89
		List<V> values = this.targetMap.computeIfAbsent(key, k -> new LinkedList<>());
90 91 92
		values.add(value);
	}

93
	@Override
94
	public void addAll(K key, List<? extends V> values) {
95 96 97 98
		List<V> currentValues = this.targetMap.computeIfAbsent(key, k -> new LinkedList<>());
		currentValues.addAll(values);
	}

A
Arjen Poutsma 已提交
99 100 101 102 103 104 105
	@Override
	public void addAll(MultiValueMap<K, V> values) {
		for (Entry<K, List<V>> entry : values.entrySet()) {
			addAll(entry.getKey(), entry.getValue());
		}
	}

106
	@Override
107
	public void set(K key, @Nullable V value) {
108
		List<V> values = new LinkedList<>();
109 110 111 112
		values.add(value);
		this.targetMap.put(key, values);
	}

113
	@Override
114
	public void setAll(Map<K, V> values) {
115
		values.forEach(this::set);
116 117
	}

118
	@Override
119
	public Map<K, V> toSingleValueMap() {
120
		LinkedHashMap<K, V> singleValueMap = new LinkedHashMap<>(this.targetMap.size());
121
		this.targetMap.forEach((key, value) -> singleValueMap.put(key, value.get(0)));
122 123
		return singleValueMap;
	}
124

125

126 127
	// Map implementation

128
	@Override
129 130 131 132
	public int size() {
		return this.targetMap.size();
	}

133
	@Override
134 135 136 137
	public boolean isEmpty() {
		return this.targetMap.isEmpty();
	}

138
	@Override
139 140 141 142
	public boolean containsKey(Object key) {
		return this.targetMap.containsKey(key);
	}

143
	@Override
144 145 146 147
	public boolean containsValue(Object value) {
		return this.targetMap.containsValue(value);
	}

148
	@Override
149
	@Nullable
150 151 152 153
	public List<V> get(Object key) {
		return this.targetMap.get(key);
	}

154
	@Override
155
	@Nullable
156 157 158 159
	public List<V> put(K key, List<V> value) {
		return this.targetMap.put(key, value);
	}

160
	@Override
161
	@Nullable
162 163 164 165
	public List<V> remove(Object key) {
		return this.targetMap.remove(key);
	}

166
	@Override
167 168
	public void putAll(Map<? extends K, ? extends List<V>> map) {
		this.targetMap.putAll(map);
169 170
	}

171
	@Override
172 173 174 175
	public void clear() {
		this.targetMap.clear();
	}

176
	@Override
177 178 179 180
	public Set<K> keySet() {
		return this.targetMap.keySet();
	}

181
	@Override
182 183 184 185
	public Collection<List<V>> values() {
		return this.targetMap.values();
	}

186
	@Override
187 188 189 190
	public Set<Entry<K, List<V>>> entrySet() {
		return this.targetMap.entrySet();
	}

191

192 193 194
	/**
	 * Create a deep copy of this Map.
	 * @return a copy of this Map, including a copy of each value-holding List entry
195 196
	 * (consistently using an independent modifiable {@link LinkedList} for each entry)
	 * along the lines of {@code MultiValueMap.addAll} semantics
197
	 * @since 4.2
198
	 * @see #addAll(MultiValueMap)
199 200 201
	 * @see #clone()
	 */
	public LinkedMultiValueMap<K, V> deepCopy() {
202
		LinkedMultiValueMap<K, V> copy = new LinkedMultiValueMap<>(this.targetMap.size());
203
		this.targetMap.forEach((key, value) -> copy.put(key, new LinkedList<>(value)));
204 205 206
		return copy;
	}

207 208 209
	/**
	 * Create a regular copy of this Map.
	 * @return a shallow copy of this Map, reusing this Map's value-holding List entries
210 211
	 * (even if some entries are shared or unmodifiable) along the lines of standard
	 * {@code Map.put} semantics
212
	 * @since 4.2
213 214
	 * @see #put(Object, List)
	 * @see #putAll(Map)
215 216 217 218 219 220 221
	 * @see LinkedMultiValueMap#LinkedMultiValueMap(Map)
	 * @see #deepCopy()
	 */
	@Override
	public LinkedMultiValueMap<K, V> clone() {
		return new LinkedMultiValueMap<>(this);
	}
222

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
	@Override
	public boolean equals(Object obj) {
		return this.targetMap.equals(obj);
	}

	@Override
	public int hashCode() {
		return this.targetMap.hashCode();
	}

	@Override
	public String toString() {
		return this.targetMap.toString();
	}

}