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.
@@ -73,11 +73,13 @@ public final class CollectionFactory {
73
73
private static final Set <Class <?>> approximableMapTypes = Set .of (
74
74
// Standard map interfaces
75
75
Map .class ,
76
+ MultiValueMap .class ,
76
77
SortedMap .class ,
77
78
NavigableMap .class ,
78
79
// Common concrete map classes
79
80
HashMap .class ,
80
81
LinkedHashMap .class ,
82
+ LinkedMultiValueMap .class ,
81
83
TreeMap .class ,
82
84
EnumMap .class );
83
85
@@ -93,7 +95,9 @@ private CollectionFactory() {
93
95
* @return {@code true} if the type is <em>approximable</em>
94
96
*/
95
97
public static boolean isApproximableCollectionType (@ Nullable Class <?> collectionType ) {
96
- return (collectionType != null && approximableCollectionTypes .contains (collectionType ));
98
+ return (collectionType != null && (approximableCollectionTypes .contains (collectionType ) ||
99
+ collectionType .getName ().equals ("java.util.SequencedSet" ) ||
100
+ collectionType .getName ().equals ("java.util.SequencedCollection" )));
97
101
}
98
102
99
103
/**
@@ -118,20 +122,20 @@ public static boolean isApproximableCollectionType(@Nullable Class<?> collection
118
122
*/
119
123
@ SuppressWarnings ({"rawtypes" , "unchecked" })
120
124
public static <E > Collection <E > createApproximateCollection (@ Nullable Object collection , int capacity ) {
121
- if (collection instanceof LinkedList ) {
122
- return new LinkedList <>();
123
- }
124
- else if (collection instanceof List ) {
125
- return new ArrayList <>(capacity );
126
- }
127
- else if (collection instanceof EnumSet enumSet ) {
125
+ if (collection instanceof EnumSet enumSet ) {
128
126
Collection <E > copy = EnumSet .copyOf (enumSet );
129
127
copy .clear ();
130
128
return copy ;
131
129
}
132
130
else if (collection instanceof SortedSet sortedSet ) {
133
131
return new TreeSet <>(sortedSet .comparator ());
134
132
}
133
+ else if (collection instanceof LinkedList ) {
134
+ return new LinkedList <>();
135
+ }
136
+ else if (collection instanceof List ) {
137
+ return new ArrayList <>(capacity );
138
+ }
135
139
else {
136
140
return new LinkedHashSet <>(capacity );
137
141
}
@@ -178,7 +182,9 @@ public static <E> Collection<E> createCollection(Class<?> collectionType, int ca
178
182
public static <E > Collection <E > createCollection (Class <?> collectionType , @ Nullable Class <?> elementType , int capacity ) {
179
183
Assert .notNull (collectionType , "Collection type must not be null" );
180
184
if (LinkedHashSet .class == collectionType || HashSet .class == collectionType ||
181
- Set .class == collectionType || Collection .class == collectionType ) {
185
+ Set .class == collectionType || Collection .class == collectionType ||
186
+ collectionType .getName ().equals ("java.util.SequencedSet" ) ||
187
+ collectionType .getName ().equals ("java.util.SequencedCollection" )) {
182
188
return new LinkedHashSet <>(capacity );
183
189
}
184
190
else if (ArrayList .class == collectionType || List .class == collectionType ) {
@@ -187,8 +193,8 @@ else if (ArrayList.class == collectionType || List.class == collectionType) {
187
193
else if (LinkedList .class == collectionType ) {
188
194
return new LinkedList <>();
189
195
}
190
- else if (TreeSet .class == collectionType || NavigableSet .class == collectionType
191
- || SortedSet .class == collectionType ) {
196
+ else if (TreeSet .class == collectionType || NavigableSet .class == collectionType ||
197
+ SortedSet .class == collectionType ) {
192
198
return new TreeSet <>();
193
199
}
194
200
else if (EnumSet .class .isAssignableFrom (collectionType )) {
@@ -216,7 +222,8 @@ else if (EnumSet.class.isAssignableFrom(collectionType)) {
216
222
* @return {@code true} if the type is <em>approximable</em>
217
223
*/
218
224
public static boolean isApproximableMapType (@ Nullable Class <?> mapType ) {
219
- return (mapType != null && approximableMapTypes .contains (mapType ));
225
+ return (mapType != null && (approximableMapTypes .contains (mapType ) ||
226
+ mapType .getName ().equals ("java.util.SequencedMap" )));
220
227
}
221
228
222
229
/**
@@ -246,6 +253,9 @@ public static <K, V> Map<K, V> createApproximateMap(@Nullable Object map, int ca
246
253
else if (map instanceof SortedMap sortedMap ) {
247
254
return new TreeMap <>(sortedMap .comparator ());
248
255
}
256
+ else if (map instanceof MultiValueMap ) {
257
+ return new LinkedMultiValueMap (capacity );
258
+ }
249
259
else {
250
260
return new LinkedHashMap <>(capacity );
251
261
}
@@ -292,26 +302,22 @@ public static <K, V> Map<K, V> createMap(Class<?> mapType, int capacity) {
292
302
@ SuppressWarnings ({"rawtypes" , "unchecked" })
293
303
public static <K , V > Map <K , V > createMap (Class <?> mapType , @ Nullable Class <?> keyType , int capacity ) {
294
304
Assert .notNull (mapType , "Map type must not be null" );
295
- if (mapType .isInterface ()) {
296
- if (Map .class == mapType ) {
297
- return new LinkedHashMap <>(capacity );
298
- }
299
- else if (SortedMap .class == mapType || NavigableMap .class == mapType ) {
300
- return new TreeMap <>();
301
- }
302
- else if (MultiValueMap .class == mapType ) {
303
- return new LinkedMultiValueMap ();
304
- }
305
- else {
306
- throw new IllegalArgumentException ("Unsupported Map interface: " + mapType .getName ());
307
- }
305
+ if (LinkedHashMap .class == mapType || HashMap .class == mapType || Map .class == mapType ||
306
+ mapType .getName ().equals ("java.util.SequencedMap" )) {
307
+ return new LinkedHashMap <>(capacity );
308
+ }
309
+ else if (LinkedMultiValueMap .class == mapType || MultiValueMap .class == mapType ) {
310
+ return new LinkedMultiValueMap ();
311
+ }
312
+ else if (TreeMap .class == mapType || SortedMap .class == mapType || NavigableMap .class == mapType ) {
313
+ return new TreeMap <>();
308
314
}
309
315
else if (EnumMap .class == mapType ) {
310
316
Assert .notNull (keyType , "Cannot create EnumMap for unknown key type" );
311
317
return new EnumMap (asEnumType (keyType ));
312
318
}
313
319
else {
314
- if (!Map .class .isAssignableFrom (mapType )) {
320
+ if (mapType . isInterface () || !Map .class .isAssignableFrom (mapType )) {
315
321
throw new IllegalArgumentException ("Unsupported Map type: " + mapType .getName ());
316
322
}
317
323
try {
0 commit comments