Skip to content

Commit

Permalink
Support for SequencedCollection/Set/Map (on JDK 21)
Browse files Browse the repository at this point in the history
Includes consistent support for MultiValueMap.

Closes gh-30239
  • Loading branch information
jhoeller committed May 3, 2023
1 parent dd871e0 commit 87942ed
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -73,11 +73,13 @@ public final class CollectionFactory {
private static final Set<Class<?>> approximableMapTypes = Set.of(
// Standard map interfaces
Map.class,
MultiValueMap.class,
SortedMap.class,
NavigableMap.class,
// Common concrete map classes
HashMap.class,
LinkedHashMap.class,
LinkedMultiValueMap.class,
TreeMap.class,
EnumMap.class);

Expand All @@ -93,7 +95,9 @@ private CollectionFactory() {
* @return {@code true} if the type is <em>approximable</em>
*/
public static boolean isApproximableCollectionType(@Nullable Class<?> collectionType) {
return (collectionType != null && approximableCollectionTypes.contains(collectionType));
return (collectionType != null && (approximableCollectionTypes.contains(collectionType) ||
collectionType.getName().equals("java.util.SequencedSet") ||
collectionType.getName().equals("java.util.SequencedCollection")));
}

/**
Expand All @@ -118,20 +122,20 @@ public static boolean isApproximableCollectionType(@Nullable Class<?> collection
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public static <E> Collection<E> createApproximateCollection(@Nullable Object collection, int capacity) {
if (collection instanceof LinkedList) {
return new LinkedList<>();
}
else if (collection instanceof List) {
return new ArrayList<>(capacity);
}
else if (collection instanceof EnumSet enumSet) {
if (collection instanceof EnumSet enumSet) {
Collection<E> copy = EnumSet.copyOf(enumSet);
copy.clear();
return copy;
}
else if (collection instanceof SortedSet sortedSet) {
return new TreeSet<>(sortedSet.comparator());
}
else if (collection instanceof LinkedList) {
return new LinkedList<>();
}
else if (collection instanceof List) {
return new ArrayList<>(capacity);
}
else {
return new LinkedHashSet<>(capacity);
}
Expand Down Expand Up @@ -178,7 +182,9 @@ public static <E> Collection<E> createCollection(Class<?> collectionType, int ca
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 ||
Set.class == collectionType || Collection.class == collectionType) {
Set.class == collectionType || Collection.class == collectionType ||
collectionType.getName().equals("java.util.SequencedSet") ||
collectionType.getName().equals("java.util.SequencedCollection")) {
return new LinkedHashSet<>(capacity);
}
else if (ArrayList.class == collectionType || List.class == collectionType) {
Expand All @@ -187,8 +193,8 @@ else if (ArrayList.class == collectionType || List.class == collectionType) {
else if (LinkedList.class == collectionType) {
return new LinkedList<>();
}
else if (TreeSet.class == collectionType || NavigableSet.class == collectionType
|| SortedSet.class == collectionType) {
else if (TreeSet.class == collectionType || NavigableSet.class == collectionType ||
SortedSet.class == collectionType) {
return new TreeSet<>();
}
else if (EnumSet.class.isAssignableFrom(collectionType)) {
Expand Down Expand Up @@ -216,7 +222,8 @@ else if (EnumSet.class.isAssignableFrom(collectionType)) {
* @return {@code true} if the type is <em>approximable</em>
*/
public static boolean isApproximableMapType(@Nullable Class<?> mapType) {
return (mapType != null && approximableMapTypes.contains(mapType));
return (mapType != null && (approximableMapTypes.contains(mapType) ||
mapType.getName().equals("java.util.SequencedMap")));
}

/**
Expand Down Expand Up @@ -246,6 +253,9 @@ public static <K, V> Map<K, V> createApproximateMap(@Nullable Object map, int ca
else if (map instanceof SortedMap sortedMap) {
return new TreeMap<>(sortedMap.comparator());
}
else if (map instanceof MultiValueMap) {
return new LinkedMultiValueMap(capacity);
}
else {
return new LinkedHashMap<>(capacity);
}
Expand Down Expand Up @@ -292,26 +302,22 @@ 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 (mapType.isInterface()) {
if (Map.class == mapType) {
return new LinkedHashMap<>(capacity);
}
else if (SortedMap.class == mapType || NavigableMap.class == mapType) {
return new TreeMap<>();
}
else if (MultiValueMap.class == mapType) {
return new LinkedMultiValueMap();
}
else {
throw new IllegalArgumentException("Unsupported Map interface: " + mapType.getName());
}
if (LinkedHashMap.class == mapType || HashMap.class == mapType || Map.class == mapType ||
mapType.getName().equals("java.util.SequencedMap")) {
return new LinkedHashMap<>(capacity);
}
else if (LinkedMultiValueMap.class == mapType || MultiValueMap.class == mapType) {
return new LinkedMultiValueMap();
}
else if (TreeMap.class == mapType || SortedMap.class == mapType || NavigableMap.class == mapType) {
return new TreeMap<>();
}
else if (EnumMap.class == mapType) {
Assert.notNull(keyType, "Cannot create EnumMap for unknown key type");
return new EnumMap(asEnumType(keyType));
}
else {
if (!Map.class.isAssignableFrom(mapType)) {
if (mapType.isInterface() || !Map.class.isAssignableFrom(mapType)) {
throw new IllegalArgumentException("Unsupported Map type: " + mapType.getName());
}
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -209,21 +209,25 @@ void createApproximateMapFromNonEmptyEnumMap() {
@Test
void createsCollectionsCorrectly() {
// interfaces
assertThat(createCollection(List.class, 0)).isInstanceOf(ArrayList.class);
assertThat(createCollection(Set.class, 0)).isInstanceOf(LinkedHashSet.class);
assertThat(createCollection(Collection.class, 0)).isInstanceOf(LinkedHashSet.class);
assertThat(createCollection(SortedSet.class, 0)).isInstanceOf(TreeSet.class);
assertThat(createCollection(NavigableSet.class, 0)).isInstanceOf(TreeSet.class);

assertThat(createCollection(List.class, String.class, 0)).isInstanceOf(ArrayList.class);
assertThat(createCollection(Set.class, String.class, 0)).isInstanceOf(LinkedHashSet.class);
assertThat(createCollection(Collection.class, String.class, 0)).isInstanceOf(LinkedHashSet.class);
assertThat(createCollection(SortedSet.class, String.class, 0)).isInstanceOf(TreeSet.class);
assertThat(createCollection(NavigableSet.class, String.class, 0)).isInstanceOf(TreeSet.class);
testCollection(List.class, ArrayList.class);
testCollection(Set.class, LinkedHashSet.class);
testCollection(Collection.class, LinkedHashSet.class);
// on JDK 21: testCollection(SequencedSet.class, LinkedHashSet.class);
// on JDK 21: testCollection(SequencedCollection.class, LinkedHashSet.class);
testCollection(SortedSet.class, TreeSet.class);
testCollection(NavigableSet.class, TreeSet.class);

// concrete types
assertThat(createCollection(HashSet.class, 0)).isInstanceOf(HashSet.class);
assertThat(createCollection(HashSet.class, String.class, 0)).isInstanceOf(HashSet.class);
testCollection(ArrayList.class, ArrayList.class);
testCollection(HashSet.class, LinkedHashSet.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);
}

@Test
Expand Down Expand Up @@ -258,20 +262,23 @@ void rejectsNullCollectionType() {
@Test
void createsMapsCorrectly() {
// interfaces
assertThat(createMap(Map.class, 0)).isInstanceOf(LinkedHashMap.class);
assertThat(createMap(SortedMap.class, 0)).isInstanceOf(TreeMap.class);
assertThat(createMap(NavigableMap.class, 0)).isInstanceOf(TreeMap.class);
assertThat(createMap(MultiValueMap.class, 0)).isInstanceOf(LinkedMultiValueMap.class);

assertThat(createMap(Map.class, String.class, 0)).isInstanceOf(LinkedHashMap.class);
assertThat(createMap(SortedMap.class, String.class, 0)).isInstanceOf(TreeMap.class);
assertThat(createMap(NavigableMap.class, String.class, 0)).isInstanceOf(TreeMap.class);
assertThat(createMap(MultiValueMap.class, String.class, 0)).isInstanceOf(LinkedMultiValueMap.class);
testMap(Map.class, LinkedHashMap.class);
// on JDK 21: testMap(SequencedMap.class, LinkedHashMap.class);
testMap(SortedMap.class, TreeMap.class);
testMap(NavigableMap.class, TreeMap.class);
testMap(MultiValueMap.class, LinkedMultiValueMap.class);

// concrete types
assertThat(createMap(HashMap.class, 0)).isInstanceOf(HashMap.class);
testMap(HashMap.class, LinkedHashMap.class);
testMap(LinkedHashMap.class, LinkedHashMap.class);
testMap(TreeMap.class, TreeMap.class);
testMap(LinkedMultiValueMap.class, LinkedMultiValueMap.class);
}

assertThat(createMap(HashMap.class, String.class, 0)).isInstanceOf(HashMap.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);
}

@Test
Expand Down

0 comments on commit 87942ed

Please sign in to comment.