Restore creation of plain HashSet/HashMap for direct HashSet/HashMap type

Closes gh-30596
This commit is contained in:
Juergen Hoeller 2023-06-05 13:57:59 +02:00
parent 6cc084dbde
commit cdc4497664
2 changed files with 14 additions and 8 deletions

View File

@ -179,7 +179,7 @@ public final class CollectionFactory {
@SuppressWarnings("unchecked")
public static <E> Collection<E> createCollection(Class<?> collectionType, @Nullable Class<?> elementType, int capacity) {
Assert.notNull(collectionType, "Collection type must not be null");
if (LinkedHashSet.class == collectionType || HashSet.class == collectionType ||
if (LinkedHashSet.class == collectionType ||
Set.class == collectionType || Collection.class == collectionType) {
return new LinkedHashSet<>(capacity);
}
@ -197,6 +197,9 @@ public final class CollectionFactory {
Assert.notNull(elementType, "Cannot create EnumSet for unknown element type");
return EnumSet.noneOf(asEnumType(elementType));
}
else if (HashSet.class == collectionType) {
return new HashSet<>(capacity);
}
else {
if (collectionType.isInterface() || !Collection.class.isAssignableFrom(collectionType)) {
throw new IllegalArgumentException("Unsupported Collection type: " + collectionType.getName());
@ -297,7 +300,7 @@ public final class CollectionFactory {
@SuppressWarnings({"rawtypes", "unchecked"})
public static <K, V> Map<K, V> createMap(Class<?> mapType, @Nullable Class<?> keyType, int capacity) {
Assert.notNull(mapType, "Map type must not be null");
if (LinkedHashMap.class == mapType || HashMap.class == mapType || Map.class == mapType) {
if (LinkedHashMap.class == mapType || Map.class == mapType) {
return new LinkedHashMap<>(capacity);
}
else if (LinkedMultiValueMap.class == mapType || MultiValueMap.class == mapType) {
@ -310,6 +313,9 @@ public final class CollectionFactory {
Assert.notNull(keyType, "Cannot create EnumMap for unknown key type");
return new EnumMap(asEnumType(keyType));
}
else if (HashMap.class == mapType) {
return new HashMap<>(capacity);
}
else {
if (mapType.isInterface() || !Map.class.isAssignableFrom(mapType)) {
throw new IllegalArgumentException("Unsupported Map type: " + mapType.getName());

View File

@ -217,15 +217,15 @@ class CollectionFactoryTests {
// concrete types
testCollection(ArrayList.class, ArrayList.class);
testCollection(HashSet.class, LinkedHashSet.class);
testCollection(HashSet.class, HashSet.class);
testCollection(LinkedHashSet.class, LinkedHashSet.class);
testCollection(TreeSet.class, TreeSet.class);
}
private void testCollection(Class<?> collectionType, Class<?> resultType) {
assertThat(CollectionFactory.isApproximableCollectionType(collectionType)).isTrue();
assertThat(createCollection(collectionType, 0)).isInstanceOf(resultType);
assertThat(createCollection(collectionType, String.class, 0)).isInstanceOf(resultType);
assertThat(createCollection(collectionType, 0)).isExactlyInstanceOf(resultType);
assertThat(createCollection(collectionType, String.class, 0)).isExactlyInstanceOf(resultType);
}
@Test
@ -266,7 +266,7 @@ class CollectionFactoryTests {
testMap(MultiValueMap.class, LinkedMultiValueMap.class);
// concrete types
testMap(HashMap.class, LinkedHashMap.class);
testMap(HashMap.class, HashMap.class);
testMap(LinkedHashMap.class, LinkedHashMap.class);
testMap(TreeMap.class, TreeMap.class);
testMap(LinkedMultiValueMap.class, LinkedMultiValueMap.class);
@ -274,8 +274,8 @@ class CollectionFactoryTests {
private void testMap(Class<?> mapType, Class<?> resultType) {
assertThat(CollectionFactory.isApproximableMapType(mapType)).isTrue();
assertThat(createMap(mapType, 0)).isInstanceOf(resultType);
assertThat(createMap(mapType, String.class, 0)).isInstanceOf(resultType);
assertThat(createMap(mapType, 0)).isExactlyInstanceOf(resultType);
assertThat(createMap(mapType, String.class, 0)).isExactlyInstanceOf(resultType);
}
@Test