Skip to content

Commit

Permalink
Restore creation of plain HashSet/HashMap for direct HashSet/HashMap …
Browse files Browse the repository at this point in the history
…type

Closes gh-30596

(cherry picked from commit cdc4497)
  • Loading branch information
jhoeller committed Jun 5, 2023
1 parent 259bd52 commit 46d171a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public static <E> Collection<E> createCollection(Class<?> collectionType, int ca
@SuppressWarnings({"unchecked", "cast"})
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);
}
Expand All @@ -202,6 +202,9 @@ else if (EnumSet.class.isAssignableFrom(collectionType)) {
// Cast is necessary for compilation in Eclipse 4.4.1.
return (Collection<E>) 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());
Expand Down Expand Up @@ -302,7 +305,7 @@ public static <K, V> Map<K, V> createMap(Class<?> mapType, int capacity) {
@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) {
Expand All @@ -315,6 +318,9 @@ else if (EnumMap.class == mapType) {
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,15 @@ void createsCollectionsCorrectly() {

// 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
Expand Down Expand Up @@ -266,16 +266,16 @@ void createsMapsCorrectly() {
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);
}

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
Expand Down

0 comments on commit 46d171a

Please sign in to comment.