|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2021 the original author or authors. |
| 2 | + * Copyright 2002-2022 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.
|
@@ -181,27 +181,26 @@ public static <E> Collection<E> createCollection(Class<?> collectionType, int ca
|
181 | 181 | @SuppressWarnings({"unchecked", "cast"})
|
182 | 182 | public static <E> Collection<E> createCollection(Class<?> collectionType, @Nullable Class<?> elementType, int capacity) {
|
183 | 183 | Assert.notNull(collectionType, "Collection type must not be null");
|
184 |
| - if (collectionType.isInterface()) { |
185 |
| - if (Set.class == collectionType || Collection.class == collectionType) { |
186 |
| - return new LinkedHashSet<>(capacity); |
187 |
| - } |
188 |
| - else if (List.class == collectionType) { |
189 |
| - return new ArrayList<>(capacity); |
190 |
| - } |
191 |
| - else if (SortedSet.class == collectionType || NavigableSet.class == collectionType) { |
192 |
| - return new TreeSet<>(); |
193 |
| - } |
194 |
| - else { |
195 |
| - throw new IllegalArgumentException("Unsupported Collection interface: " + collectionType.getName()); |
196 |
| - } |
| 184 | + if (LinkedHashSet.class == collectionType || HashSet.class == collectionType || |
| 185 | + Set.class == collectionType || Collection.class == collectionType) { |
| 186 | + return new LinkedHashSet<>(capacity); |
| 187 | + } |
| 188 | + else if (ArrayList.class == collectionType || List.class == collectionType) { |
| 189 | + return new ArrayList<>(capacity); |
| 190 | + } |
| 191 | + else if (LinkedList.class == collectionType) { |
| 192 | + return new LinkedList<>(); |
| 193 | + } |
| 194 | + else if (SortedSet.class == collectionType || NavigableSet.class == collectionType) { |
| 195 | + return new TreeSet<>(); |
197 | 196 | }
|
198 | 197 | else if (EnumSet.class.isAssignableFrom(collectionType)) {
|
199 | 198 | Assert.notNull(elementType, "Cannot create EnumSet for unknown element type");
|
200 | 199 | // Cast is necessary for compilation in Eclipse 4.4.1.
|
201 | 200 | return (Collection<E>) EnumSet.noneOf(asEnumType(elementType));
|
202 | 201 | }
|
203 | 202 | else {
|
204 |
| - if (!Collection.class.isAssignableFrom(collectionType)) { |
| 203 | + if (collectionType.isInterface() || !Collection.class.isAssignableFrom(collectionType)) { |
205 | 204 | throw new IllegalArgumentException("Unsupported Collection type: " + collectionType.getName());
|
206 | 205 | }
|
207 | 206 | try {
|
|
0 commit comments