|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2021 the original author or authors. |
|
|
|
* Copyright 2002-2022 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -180,26 +180,25 @@ public final class CollectionFactory { |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
public static <E> Collection<E> createCollection(Class<?> collectionType, @Nullable Class<?> elementType, int capacity) { |
|
|
|
public static <E> Collection<E> createCollection(Class<?> collectionType, @Nullable Class<?> elementType, int capacity) { |
|
|
|
Assert.notNull(collectionType, "Collection type must not be null"); |
|
|
|
Assert.notNull(collectionType, "Collection type must not be null"); |
|
|
|
if (collectionType.isInterface()) { |
|
|
|
if (LinkedHashSet.class == collectionType || HashSet.class == collectionType || |
|
|
|
if (Set.class == collectionType || Collection.class == collectionType) { |
|
|
|
Set.class == collectionType || Collection.class == collectionType) { |
|
|
|
return new LinkedHashSet<>(capacity); |
|
|
|
return new LinkedHashSet<>(capacity); |
|
|
|
} |
|
|
|
} |
|
|
|
else if (List.class == collectionType) { |
|
|
|
else if (ArrayList.class == collectionType || List.class == collectionType) { |
|
|
|
return new ArrayList<>(capacity); |
|
|
|
return new ArrayList<>(capacity); |
|
|
|
} |
|
|
|
} |
|
|
|
else if (SortedSet.class == collectionType || NavigableSet.class == collectionType) { |
|
|
|
else if (LinkedList.class == collectionType) { |
|
|
|
return new TreeSet<>(); |
|
|
|
return new LinkedList<>(); |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
else if (SortedSet.class == collectionType || NavigableSet.class == collectionType) { |
|
|
|
throw new IllegalArgumentException("Unsupported Collection interface: " + collectionType.getName()); |
|
|
|
return new TreeSet<>(); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
else if (EnumSet.class.isAssignableFrom(collectionType)) { |
|
|
|
else if (EnumSet.class.isAssignableFrom(collectionType)) { |
|
|
|
Assert.notNull(elementType, "Cannot create EnumSet for unknown element type"); |
|
|
|
Assert.notNull(elementType, "Cannot create EnumSet for unknown element type"); |
|
|
|
return EnumSet.noneOf(asEnumType(elementType)); |
|
|
|
return EnumSet.noneOf(asEnumType(elementType)); |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
else { |
|
|
|
if (!Collection.class.isAssignableFrom(collectionType)) { |
|
|
|
if (collectionType.isInterface() || !Collection.class.isAssignableFrom(collectionType)) { |
|
|
|
throw new IllegalArgumentException("Unsupported Collection type: " + collectionType.getName()); |
|
|
|
throw new IllegalArgumentException("Unsupported Collection type: " + collectionType.getName()); |
|
|
|
} |
|
|
|
} |
|
|
|
try { |
|
|
|
try { |
|
|
|
|