Skip to content

Commit c09055b

Browse files
committed
Consistent support for MultiValueMap and common Map implementations
Closes gh-30440
1 parent 2c15dcc commit c09055b

File tree

2 files changed

+53
-49
lines changed

2 files changed

+53
-49
lines changed

spring-core/src/main/java/org/springframework/core/CollectionFactory.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -73,11 +73,13 @@ public final class CollectionFactory {
7373
private static final Set<Class<?>> approximableMapTypes = Set.of(
7474
// Standard map interfaces
7575
Map.class,
76+
MultiValueMap.class,
7677
SortedMap.class,
7778
NavigableMap.class,
7879
// Common concrete map classes
7980
HashMap.class,
8081
LinkedHashMap.class,
82+
LinkedMultiValueMap.class,
8183
TreeMap.class,
8284
EnumMap.class);
8385

@@ -118,20 +120,20 @@ public static boolean isApproximableCollectionType(@Nullable Class<?> collection
118120
*/
119121
@SuppressWarnings({"rawtypes", "unchecked"})
120122
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) {
123+
if (collection instanceof EnumSet enumSet) {
128124
Collection<E> copy = EnumSet.copyOf(enumSet);
129125
copy.clear();
130126
return copy;
131127
}
132128
else if (collection instanceof SortedSet sortedSet) {
133129
return new TreeSet<>(sortedSet.comparator());
134130
}
131+
else if (collection instanceof LinkedList) {
132+
return new LinkedList<>();
133+
}
134+
else if (collection instanceof List) {
135+
return new ArrayList<>(capacity);
136+
}
135137
else {
136138
return new LinkedHashSet<>(capacity);
137139
}
@@ -187,8 +189,8 @@ else if (ArrayList.class == collectionType || List.class == collectionType) {
187189
else if (LinkedList.class == collectionType) {
188190
return new LinkedList<>();
189191
}
190-
else if (TreeSet.class == collectionType || NavigableSet.class == collectionType
191-
|| SortedSet.class == collectionType) {
192+
else if (TreeSet.class == collectionType || NavigableSet.class == collectionType ||
193+
SortedSet.class == collectionType) {
192194
return new TreeSet<>();
193195
}
194196
else if (EnumSet.class.isAssignableFrom(collectionType)) {
@@ -246,6 +248,9 @@ public static <K, V> Map<K, V> createApproximateMap(@Nullable Object map, int ca
246248
else if (map instanceof SortedMap sortedMap) {
247249
return new TreeMap<>(sortedMap.comparator());
248250
}
251+
else if (map instanceof MultiValueMap) {
252+
return new LinkedMultiValueMap(capacity);
253+
}
249254
else {
250255
return new LinkedHashMap<>(capacity);
251256
}
@@ -292,26 +297,21 @@ public static <K, V> Map<K, V> createMap(Class<?> mapType, int capacity) {
292297
@SuppressWarnings({"rawtypes", "unchecked"})
293298
public static <K, V> Map<K, V> createMap(Class<?> mapType, @Nullable Class<?> keyType, int capacity) {
294299
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-
}
300+
if (LinkedHashMap.class == mapType || HashMap.class == mapType || Map.class == mapType) {
301+
return new LinkedHashMap<>(capacity);
302+
}
303+
else if (LinkedMultiValueMap.class == mapType || MultiValueMap.class == mapType) {
304+
return new LinkedMultiValueMap();
305+
}
306+
else if (TreeMap.class == mapType || SortedMap.class == mapType || NavigableMap.class == mapType) {
307+
return new TreeMap<>();
308308
}
309309
else if (EnumMap.class == mapType) {
310310
Assert.notNull(keyType, "Cannot create EnumMap for unknown key type");
311311
return new EnumMap(asEnumType(keyType));
312312
}
313313
else {
314-
if (!Map.class.isAssignableFrom(mapType)) {
314+
if (mapType.isInterface() || !Map.class.isAssignableFrom(mapType)) {
315315
throw new IllegalArgumentException("Unsupported Map type: " + mapType.getName());
316316
}
317317
try {

spring-core/src/test/java/org/springframework/core/CollectionFactoryTests.java

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -209,21 +209,23 @@ void createApproximateMapFromNonEmptyEnumMap() {
209209
@Test
210210
void createsCollectionsCorrectly() {
211211
// interfaces
212-
assertThat(createCollection(List.class, 0)).isInstanceOf(ArrayList.class);
213-
assertThat(createCollection(Set.class, 0)).isInstanceOf(LinkedHashSet.class);
214-
assertThat(createCollection(Collection.class, 0)).isInstanceOf(LinkedHashSet.class);
215-
assertThat(createCollection(SortedSet.class, 0)).isInstanceOf(TreeSet.class);
216-
assertThat(createCollection(NavigableSet.class, 0)).isInstanceOf(TreeSet.class);
217-
218-
assertThat(createCollection(List.class, String.class, 0)).isInstanceOf(ArrayList.class);
219-
assertThat(createCollection(Set.class, String.class, 0)).isInstanceOf(LinkedHashSet.class);
220-
assertThat(createCollection(Collection.class, String.class, 0)).isInstanceOf(LinkedHashSet.class);
221-
assertThat(createCollection(SortedSet.class, String.class, 0)).isInstanceOf(TreeSet.class);
222-
assertThat(createCollection(NavigableSet.class, String.class, 0)).isInstanceOf(TreeSet.class);
212+
testCollection(List.class, ArrayList.class);
213+
testCollection(Set.class, LinkedHashSet.class);
214+
testCollection(Collection.class, LinkedHashSet.class);
215+
testCollection(SortedSet.class, TreeSet.class);
216+
testCollection(NavigableSet.class, TreeSet.class);
223217

224218
// concrete types
225-
assertThat(createCollection(HashSet.class, 0)).isInstanceOf(HashSet.class);
226-
assertThat(createCollection(HashSet.class, String.class, 0)).isInstanceOf(HashSet.class);
219+
testCollection(ArrayList.class, ArrayList.class);
220+
testCollection(HashSet.class, LinkedHashSet.class);
221+
testCollection(LinkedHashSet.class, LinkedHashSet.class);
222+
testCollection(TreeSet.class, TreeSet.class);
223+
}
224+
225+
private void testCollection(Class<?> collectionType, Class<?> resultType) {
226+
assertThat(CollectionFactory.isApproximableCollectionType(collectionType)).isTrue();
227+
assertThat(createCollection(collectionType, 0)).isInstanceOf(resultType);
228+
assertThat(createCollection(collectionType, String.class, 0)).isInstanceOf(resultType);
227229
}
228230

229231
@Test
@@ -258,20 +260,22 @@ void rejectsNullCollectionType() {
258260
@Test
259261
void createsMapsCorrectly() {
260262
// interfaces
261-
assertThat(createMap(Map.class, 0)).isInstanceOf(LinkedHashMap.class);
262-
assertThat(createMap(SortedMap.class, 0)).isInstanceOf(TreeMap.class);
263-
assertThat(createMap(NavigableMap.class, 0)).isInstanceOf(TreeMap.class);
264-
assertThat(createMap(MultiValueMap.class, 0)).isInstanceOf(LinkedMultiValueMap.class);
265-
266-
assertThat(createMap(Map.class, String.class, 0)).isInstanceOf(LinkedHashMap.class);
267-
assertThat(createMap(SortedMap.class, String.class, 0)).isInstanceOf(TreeMap.class);
268-
assertThat(createMap(NavigableMap.class, String.class, 0)).isInstanceOf(TreeMap.class);
269-
assertThat(createMap(MultiValueMap.class, String.class, 0)).isInstanceOf(LinkedMultiValueMap.class);
263+
testMap(Map.class, LinkedHashMap.class);
264+
testMap(SortedMap.class, TreeMap.class);
265+
testMap(NavigableMap.class, TreeMap.class);
266+
testMap(MultiValueMap.class, LinkedMultiValueMap.class);
270267

271268
// concrete types
272-
assertThat(createMap(HashMap.class, 0)).isInstanceOf(HashMap.class);
269+
testMap(HashMap.class, LinkedHashMap.class);
270+
testMap(LinkedHashMap.class, LinkedHashMap.class);
271+
testMap(TreeMap.class, TreeMap.class);
272+
testMap(LinkedMultiValueMap.class, LinkedMultiValueMap.class);
273+
}
273274

274-
assertThat(createMap(HashMap.class, String.class, 0)).isInstanceOf(HashMap.class);
275+
private void testMap(Class<?> mapType, Class<?> resultType) {
276+
assertThat(CollectionFactory.isApproximableMapType(mapType)).isTrue();
277+
assertThat(createMap(mapType, 0)).isInstanceOf(resultType);
278+
assertThat(createMap(mapType, String.class, 0)).isInstanceOf(resultType);
275279
}
276280

277281
@Test

0 commit comments

Comments
 (0)