package org.springframework.data.repository.util;
import io.vavr.collection.LinkedHashMap;
import io.vavr.collection.LinkedHashSet;
import io.vavr.collection.Traversable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nonnull;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.repository.util.QueryExecutionConverters.WrapperType;
import org.springframework.lang.Nullable;
class VavrCollections {
public enum ToJavaConverter implements Converter<Object, Object> {
INSTANCE;
public WrapperType getWrapperType() {
return WrapperType.multiValue(io.vavr.collection.Traversable.class);
}
@Nonnull
@Override
public Object convert(Object source) {
if (source instanceof io.vavr.collection.Seq) {
return ((io.vavr.collection.Seq<?>) source).toJavaList();
}
if (source instanceof io.vavr.collection.Map) {
return ((io.vavr.collection.Map<?, ?>) source).toJavaMap();
}
if (source instanceof io.vavr.collection.Set) {
return ((io.vavr.collection.Set<?>) source).toJavaSet();
}
throw new IllegalArgumentException("Unsupported Javaslang collection " + source.getClass());
}
}
public enum FromJavaConverter implements ConditionalGenericConverter {
INSTANCE {
@Nonnull
@Override
public java.util.Set<ConvertiblePair> getConvertibleTypes() {
return CONVERTIBLE_PAIRS;
}
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
if (sourceType.isCollection() && io.vavr.collection.Map.class.isAssignableFrom(targetType.getType())) {
return false;
}
if (sourceType.isMap() && !(io.vavr.collection.Map.class.isAssignableFrom(targetType.getType())
|| targetType.getType().equals(Traversable.class))) {
return false;
}
return true;
}
@Nullable
@Override
public Object convert(@Nullable Object source, TypeDescriptor sourceDescriptor, TypeDescriptor targetDescriptor) {
Class<?> targetType = targetDescriptor.getType();
if (io.vavr.collection.Seq.class.isAssignableFrom(targetType)) {
return io.vavr.collection.List.ofAll((Iterable<?>) source);
}
if (io.vavr.collection.Set.class.isAssignableFrom(targetType)) {
return LinkedHashSet.ofAll((Iterable<?>) source);
}
if (io.vavr.collection.Map.class.isAssignableFrom(targetType)) {
return LinkedHashMap.ofAll((Map<?, ?>) source);
}
if (source instanceof List) {
return io.vavr.collection.List.ofAll((Iterable<?>) source);
}
if (source instanceof Set) {
return LinkedHashSet.ofAll((Iterable<?>) source);
}
if (source instanceof Map) {
return LinkedHashMap.ofAll((Map<?, ?>) source);
}
return source;
}
};
private static final Set<ConvertiblePair> CONVERTIBLE_PAIRS;
static {
Set<ConvertiblePair> pairs = new HashSet<>();
pairs.add(new ConvertiblePair(Collection.class, io.vavr.collection.Traversable.class));
pairs.add(new ConvertiblePair(Map.class, io.vavr.collection.Traversable.class));
CONVERTIBLE_PAIRS = Collections.unmodifiableSet(pairs);
}
}
}