1
1
/*
2
- * Copyright 2002-2022 the original author or authors.
2
+ * Copyright 2002-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -68,6 +68,7 @@ public final class CollectionFactory {
68
68
approximableCollectionTypes .add (SortedSet .class );
69
69
approximableCollectionTypes .add (NavigableSet .class );
70
70
approximableMapTypes .add (Map .class );
71
+ approximableMapTypes .add (MultiValueMap .class );
71
72
approximableMapTypes .add (SortedMap .class );
72
73
approximableMapTypes .add (NavigableMap .class );
73
74
@@ -80,6 +81,7 @@ public final class CollectionFactory {
80
81
approximableCollectionTypes .add (EnumSet .class );
81
82
approximableMapTypes .add (HashMap .class );
82
83
approximableMapTypes .add (LinkedHashMap .class );
84
+ approximableMapTypes .add (LinkedMultiValueMap .class );
83
85
approximableMapTypes .add (TreeMap .class );
84
86
approximableMapTypes .add (EnumMap .class );
85
87
}
@@ -121,13 +123,7 @@ public static boolean isApproximableCollectionType(@Nullable Class<?> collection
121
123
*/
122
124
@ SuppressWarnings ({"rawtypes" , "unchecked" , "cast" })
123
125
public static <E > Collection <E > createApproximateCollection (@ Nullable Object collection , int capacity ) {
124
- if (collection instanceof LinkedList ) {
125
- return new LinkedList <>();
126
- }
127
- else if (collection instanceof List ) {
128
- return new ArrayList <>(capacity );
129
- }
130
- else if (collection instanceof EnumSet ) {
126
+ if (collection instanceof EnumSet ) {
131
127
// Cast is necessary for compilation in Eclipse 4.4.1.
132
128
Collection <E > enumSet = (Collection <E >) EnumSet .copyOf ((EnumSet ) collection );
133
129
enumSet .clear ();
@@ -136,6 +132,12 @@ else if (collection instanceof EnumSet) {
136
132
else if (collection instanceof SortedSet ) {
137
133
return new TreeSet <>(((SortedSet <E >) collection ).comparator ());
138
134
}
135
+ if (collection instanceof LinkedList ) {
136
+ return new LinkedList <>();
137
+ }
138
+ else if (collection instanceof List ) {
139
+ return new ArrayList <>(capacity );
140
+ }
139
141
else {
140
142
return new LinkedHashSet <>(capacity );
141
143
}
@@ -191,8 +193,8 @@ else if (ArrayList.class == collectionType || List.class == collectionType) {
191
193
else if (LinkedList .class == collectionType ) {
192
194
return new LinkedList <>();
193
195
}
194
- else if (TreeSet .class == collectionType || NavigableSet .class == collectionType
195
- || SortedSet .class == collectionType ) {
196
+ else if (TreeSet .class == collectionType || NavigableSet .class == collectionType ||
197
+ SortedSet .class == collectionType ) {
196
198
return new TreeSet <>();
197
199
}
198
200
else if (EnumSet .class .isAssignableFrom (collectionType )) {
@@ -251,6 +253,9 @@ public static <K, V> Map<K, V> createApproximateMap(@Nullable Object map, int ca
251
253
else if (map instanceof SortedMap ) {
252
254
return new TreeMap <>(((SortedMap <K , V >) map ).comparator ());
253
255
}
256
+ else if (map instanceof MultiValueMap ) {
257
+ return new LinkedMultiValueMap (capacity );
258
+ }
254
259
else {
255
260
return new LinkedHashMap <>(capacity );
256
261
}
@@ -297,26 +302,21 @@ public static <K, V> Map<K, V> createMap(Class<?> mapType, int capacity) {
297
302
@ SuppressWarnings ({"rawtypes" , "unchecked" })
298
303
public static <K , V > Map <K , V > createMap (Class <?> mapType , @ Nullable Class <?> keyType , int capacity ) {
299
304
Assert .notNull (mapType , "Map type must not be null" );
300
- if (mapType .isInterface ()) {
301
- if (Map .class == mapType ) {
302
- return new LinkedHashMap <>(capacity );
303
- }
304
- else if (SortedMap .class == mapType || NavigableMap .class == mapType ) {
305
- return new TreeMap <>();
306
- }
307
- else if (MultiValueMap .class == mapType ) {
308
- return new LinkedMultiValueMap ();
309
- }
310
- else {
311
- throw new IllegalArgumentException ("Unsupported Map interface: " + mapType .getName ());
312
- }
305
+ if (LinkedHashMap .class == mapType || HashMap .class == mapType || Map .class == mapType ) {
306
+ return new LinkedHashMap <>(capacity );
307
+ }
308
+ else if (LinkedMultiValueMap .class == mapType || MultiValueMap .class == mapType ) {
309
+ return new LinkedMultiValueMap ();
310
+ }
311
+ else if (TreeMap .class == mapType || SortedMap .class == mapType || NavigableMap .class == mapType ) {
312
+ return new TreeMap <>();
313
313
}
314
314
else if (EnumMap .class == mapType ) {
315
315
Assert .notNull (keyType , "Cannot create EnumMap for unknown key type" );
316
316
return new EnumMap (asEnumType (keyType ));
317
317
}
318
318
else {
319
- if (!Map .class .isAssignableFrom (mapType )) {
319
+ if (mapType . isInterface () || !Map .class .isAssignableFrom (mapType )) {
320
320
throw new IllegalArgumentException ("Unsupported Map type: " + mapType .getName ());
321
321
}
322
322
try {
0 commit comments