Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return unmodifiable collections in DefaultCollectionsTypeConverterFactory #691

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.function.Function;
import java.util.function.Supplier;

public final class DefaultCollectionsTypeConverterFactory implements TypeConverter.Factory {
Expand All @@ -27,7 +28,7 @@ private DefaultCollectionsTypeConverterFactory() {}
@Override
public Optional<TypeConverter<?>> get(Type type, TypeConverter.Registry registry) {
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType)type;
ParameterizedType parameterizedType = (ParameterizedType) type;
if (parameterizedType.getRawType().equals(Map.class)) {
return Optional.of(createMapTypeConverter(
registry.get(parameterizedType.getActualTypeArguments()[0]).orElseThrow(() -> new ConverterNotFoundException("No converter found")),
Expand All @@ -37,50 +38,70 @@ public Optional<TypeConverter<?>> get(Type type, TypeConverter.Registry registry
return Optional.of(createCollectionTypeConverter(
parameterizedType.getActualTypeArguments()[0],
registry,
LinkedHashSet::new));
LinkedHashSet::new,
Collections::emptySet,
Collections::unmodifiableSet));
} else if (parameterizedType.getRawType().equals(SortedSet.class)) {
return Optional.of(createCollectionTypeConverter(
parameterizedType.getActualTypeArguments()[0],
registry,
TreeSet::new));
TreeSet::new,
Collections::emptySortedSet,
Collections::unmodifiableSortedSet));
} else if (parameterizedType.getRawType().equals(List.class)) {
return Optional.of(createCollectionTypeConverter(
parameterizedType.getActualTypeArguments()[0],
registry,
ArrayList::new));
ArrayList::new,
Collections::emptyList,
Collections::unmodifiableList));
} else if (parameterizedType.getRawType().equals(LinkedList.class)) {
return Optional.of(createCollectionTypeConverter(
parameterizedType.getActualTypeArguments()[0],
registry,
LinkedList::new));
LinkedList::new,
LinkedList::new,
Function.identity()));
}
}

return Optional.empty();
}

@SuppressWarnings({ "rawtypes", "unchecked" })
private <T> TypeConverter<?> createCollectionTypeConverter(final Type type, TypeConverter.Registry registry, final Supplier<Collection<T>> collectionFactory) {
TypeConverter elementConverter = registry.get(type).orElseThrow(() -> new ConverterNotFoundException("No converter found"));
private static <E, T extends Collection<E>> TypeConverter<T> createCollectionTypeConverter(final Type elementType,
final TypeConverter.Registry registry,
final Supplier<T> collectionFactory,
final Supplier<T> emptyCollectionFactory,
final Function<T, T> finisher) {
@SuppressWarnings("unchecked")
TypeConverter<E> elementConverter = (TypeConverter<E>) registry.get(elementType)
.orElseThrow(() -> new ConverterNotFoundException("No converter found"));

boolean ignoreEmpty = !String.class.equals(type);
boolean ignoreEmpty = !String.class.equals(elementType);

return value -> {
final Collection collection = collectionFactory.get();
if (!value.isEmpty()) {
Arrays.asList(value.split("\\s*,\\s*")).forEach(v -> {
if (!v.isEmpty() || !ignoreEmpty) {
collection.add(elementConverter.convert(v));
}
});
if (value.isEmpty()) {
return emptyCollectionFactory.get();
}
return collection;
final T collection = collectionFactory.get();
for (String item : value.split("\\s*,\\s*")) {
if (!item.isEmpty() || !ignoreEmpty) {
collection.add(elementConverter.convert(item));
}
}
return finisher.apply(collection);
};
}

private TypeConverter<?> createMapTypeConverter(final TypeConverter<?> keyConverter, final TypeConverter<?> valueConverter, final Supplier<Map> mapFactory) {
private static <K, V> TypeConverter<Map<K, V>> createMapTypeConverter(final TypeConverter<K> keyConverter,
final TypeConverter<V> valueConverter,
final Supplier<Map<K, V>> mapFactory) {
return s -> {
Map result = mapFactory.get();
if (s.isEmpty()) {
return Collections.emptyMap();
}
Map<K, V> result = mapFactory.get();

Arrays
.stream(s.split("\\s*,\\s*"))
.filter(pair -> !pair.isEmpty())
Expand All @@ -91,4 +112,4 @@ private TypeConverter<?> createMapTypeConverter(final TypeConverter<?> keyConver
return Collections.unmodifiableMap(result);
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.math.BigInteger;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -167,6 +168,43 @@ public void testCollectionTypes() {
// Test proper handling of unset properties
Property<Map<CustomType, CustomType>> emptyProperty = factory.getMap("fubar", CustomType.class, CustomType.class);
Assert.assertNull(emptyProperty.get());

config.setProperty("fubar", "");
Property<List<String>> emptyListProperty = factory.getList("fubar", String.class);
Assert.assertEquals(Collections.emptyList(), emptyListProperty.get());
Copy link
Contributor

@rgallardo-netflix rgallardo-netflix Nov 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since all the default empty collections are value types, this assertion is really just equivalent to assertTrue(emptyList.isEmpty()), isn't it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's equivalent.


Property<Set<String>> emptySetProperty = factory.getSet("fubar", String.class);
Assert.assertEquals(Collections.emptySet(), emptySetProperty.get());

Property<Map<String, String>> emptyMapProperty = factory.getMap("fubar", String.class, String.class);
Assert.assertEquals(Collections.emptyMap(), emptyMapProperty.get());
}

@Test
public void testCollectionTypesImmutability() {
SettableConfig config = new DefaultSettableConfig();
DefaultPropertyFactory factory = DefaultPropertyFactory.from(config);
config.setProperty("foo", "10,13,13,20");
config.setProperty("bar", "");
config.setProperty("baz", "a=1,b=2");

List<Integer> list = factory.getList("foo", Integer.class).get();
Assert.assertThrows(UnsupportedOperationException.class, () -> list.add(100));

Set<Integer> set = factory.getSet("foo", Integer.class).get();
Assert.assertThrows(UnsupportedOperationException.class, () -> set.add(100));

Map<String, Integer> map = factory.getMap("baz", String.class, Integer.class).get();
Assert.assertThrows(UnsupportedOperationException.class, () -> map.put("c", 3));

List<Integer> emptyList = factory.getList("bar", Integer.class).get();
Assert.assertThrows(UnsupportedOperationException.class, () -> emptyList.add(100));

Set<Integer> emptySet = factory.getSet("bar", Integer.class).get();
Assert.assertThrows(UnsupportedOperationException.class, () -> emptySet.add(100));

Map<String, Integer> emptyMap = factory.getMap("bar", String.class, Integer.class).get();
Assert.assertThrows(UnsupportedOperationException.class, () -> emptyMap.put("c", 3));
}

@Test
Expand Down