17
17
package org .springframework .util ;
18
18
19
19
import java .io .Serializable ;
20
+ import java .util .Collection ;
20
21
import java .util .LinkedHashMap ;
21
22
import java .util .LinkedList ;
22
23
import java .util .List ;
23
24
import java .util .Map ;
25
+ import java .util .Set ;
26
+
27
+ import org .springframework .lang .Nullable ;
24
28
25
29
/**
26
30
* Simple implementation of {@link MultiValueMap} that wraps a {@link LinkedHashMap},
35
39
* @param <K> the key type
36
40
* @param <V> the value element type
37
41
*/
38
- public class LinkedMultiValueMap <K , V > extends MultiValueMapAdapter <K , V > implements Serializable , Cloneable {
42
+ public class LinkedMultiValueMap <K , V > implements MultiValueMap <K , V >, Serializable , Cloneable {
39
43
40
44
private static final long serialVersionUID = 3801124242820219131L ;
41
45
46
+ private final Map <K , List <V >> targetMap ;
47
+
42
48
43
49
/**
44
50
* Create a new LinkedMultiValueMap that wraps a {@link LinkedHashMap}.
45
51
*/
46
52
public LinkedMultiValueMap () {
47
- super ( new LinkedHashMap <>() );
53
+ this . targetMap = new LinkedHashMap <>();
48
54
}
49
55
50
56
/**
@@ -53,7 +59,7 @@ public LinkedMultiValueMap() {
53
59
* @param initialCapacity the initial capacity
54
60
*/
55
61
public LinkedMultiValueMap (int initialCapacity ) {
56
- super ( new LinkedHashMap <>(initialCapacity ) );
62
+ this . targetMap = new LinkedHashMap <>(initialCapacity );
57
63
}
58
64
59
65
/**
@@ -65,7 +71,140 @@ public LinkedMultiValueMap(int initialCapacity) {
65
71
* @see #deepCopy()
66
72
*/
67
73
public LinkedMultiValueMap (Map <K , List <V >> otherMap ) {
68
- super (new LinkedHashMap <>(otherMap ));
74
+ this .targetMap = new LinkedHashMap <>(otherMap );
75
+ }
76
+
77
+
78
+ // MultiValueMap implementation
79
+
80
+ @ Override
81
+ @ Nullable
82
+ public V getFirst (K key ) {
83
+ 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
+ values .add (value );
91
+ }
92
+
93
+ @ Override
94
+ public void addAll (K key , List <? extends V > values ) {
95
+ List <V > currentValues = this .targetMap .computeIfAbsent (key , k -> new LinkedList <>());
96
+ currentValues .addAll (values );
97
+ }
98
+
99
+ @ Override
100
+ public void addAll (MultiValueMap <K , V > values ) {
101
+ for (Entry <K , List <V >> entry : values .entrySet ()) {
102
+ addAll (entry .getKey (), entry .getValue ());
103
+ }
104
+ }
105
+
106
+ @ Override
107
+ public void set (K key , @ Nullable V value ) {
108
+ List <V > values = new LinkedList <>();
109
+ values .add (value );
110
+ this .targetMap .put (key , values );
111
+ }
112
+
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
+ Map <K , V > singleValueMap = new LinkedHashMap <>(this .targetMap .size ());
121
+ this .targetMap .forEach ((key , values ) -> {
122
+ if (values != null && !values .isEmpty ()) {
123
+ singleValueMap .put (key , values .get (0 ));
124
+ }
125
+ });
126
+ return singleValueMap ;
127
+ }
128
+
129
+
130
+ // Map implementation
131
+
132
+ @ Override
133
+ public int size () {
134
+ return this .targetMap .size ();
135
+ }
136
+
137
+ @ Override
138
+ public boolean isEmpty () {
139
+ return this .targetMap .isEmpty ();
140
+ }
141
+
142
+ @ Override
143
+ public boolean containsKey (Object key ) {
144
+ return this .targetMap .containsKey (key );
145
+ }
146
+
147
+ @ Override
148
+ public boolean containsValue (Object value ) {
149
+ return this .targetMap .containsValue (value );
150
+ }
151
+
152
+ @ Override
153
+ @ Nullable
154
+ public List <V > get (Object key ) {
155
+ return this .targetMap .get (key );
156
+ }
157
+
158
+ @ Override
159
+ @ Nullable
160
+ public List <V > put (K key , List <V > value ) {
161
+ return this .targetMap .put (key , value );
162
+ }
163
+
164
+ @ Override
165
+ @ Nullable
166
+ public List <V > remove (Object key ) {
167
+ return this .targetMap .remove (key );
168
+ }
169
+
170
+ @ Override
171
+ public void putAll (Map <? extends K , ? extends List <V >> map ) {
172
+ this .targetMap .putAll (map );
173
+ }
174
+
175
+ @ Override
176
+ public void clear () {
177
+ this .targetMap .clear ();
178
+ }
179
+
180
+ @ Override
181
+ public Set <K > keySet () {
182
+ return this .targetMap .keySet ();
183
+ }
184
+
185
+ @ Override
186
+ public Collection <List <V >> values () {
187
+ return this .targetMap .values ();
188
+ }
189
+
190
+ @ Override
191
+ public Set <Entry <K , List <V >>> entrySet () {
192
+ return this .targetMap .entrySet ();
193
+ }
194
+
195
+ @ Override
196
+ public boolean equals (@ Nullable Object other ) {
197
+ return (this == other || this .targetMap .equals (other ));
198
+ }
199
+
200
+ @ Override
201
+ public int hashCode () {
202
+ return this .targetMap .hashCode ();
203
+ }
204
+
205
+ @ Override
206
+ public String toString () {
207
+ return this .targetMap .toString ();
69
208
}
70
209
71
210
0 commit comments