DATACMNS-1065 - Added support for Vavr (successor of Javaslang).

Basically duplicated the Javaslang support to now work on Vavr types as well.
This commit is contained in:
Oliver Gierke
2017-05-11 12:36:23 +02:00
parent 258da5b378
commit 3d17ae5d7c
6 changed files with 373 additions and 4 deletions

View File

@@ -18,6 +18,7 @@
<properties>
<dist.key>DATACMNS</dist.key>
<javaslang>2.0.4</javaslang>
<vavr>0.9.0</vavr>
<scala>2.11.7</scala>
<xmlbeam>1.4.8</xmlbeam>
</properties>
@@ -150,6 +151,12 @@
<version>${javaslang}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>${vavr}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>javax.el</groupId>

View File

@@ -66,7 +66,7 @@ class JavaslangCollections {
return ((javaslang.collection.Set<?>) source).toJavaSet();
}
throw new IllegalArgumentException("Unsupported Javaslang collection " + source);
throw new IllegalArgumentException("Unsupported Javaslang collection " + source.getClass());
}
}

View File

@@ -16,7 +16,6 @@
package org.springframework.data.repository.util;
import javaslang.collection.Seq;
import javaslang.collection.Traversable;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.Value;
@@ -62,6 +61,7 @@ import com.google.common.base.Optional;
* <li>{@code javaslang.control.Option} - as of 1.13</li>
* <li>{@code javaslang.collection.Seq}, {@code javaslang.collection.Map}, {@code javaslang.collection.Set} - as of
* 1.13</li>
* <li>{@code io.vavr.collection.Seq}, {@code io.vavr.collection.Map}, {@code io.vavr.collection.Set} - as of 2.0</li>
* </ul>
*
* @author Oliver Gierke
@@ -79,6 +79,8 @@ public abstract class QueryExecutionConverters {
QueryExecutionConverters.class.getClassLoader());
private static final boolean JAVASLANG_PRESENT = ClassUtils.isPresent("javaslang.control.Option",
QueryExecutionConverters.class.getClassLoader());
private static final boolean VAVR_PRESENT = ClassUtils.isPresent("io.vavr.control.Option",
QueryExecutionConverters.class.getClassLoader());
private static final Set<WrapperType> WRAPPER_TYPES = new HashSet<WrapperType>();
private static final Set<Converter<Object, Object>> UNWRAPPERS = new HashSet<Converter<Object, Object>>();
@@ -121,6 +123,16 @@ public abstract class QueryExecutionConverters {
ALLOWED_PAGEABLE_TYPES.add(Seq.class);
}
if (VAVR_PRESENT) {
WRAPPER_TYPES.add(NullableWrapperToVavrOptionConverter.getWrapperType());
WRAPPER_TYPES.add(VavrCollections.ToJavaConverter.INSTANCE.getWrapperType());
UNWRAPPERS.add(VavrOptionUnwrapper.INSTANCE);
ALLOWED_PAGEABLE_TYPES.add(io.vavr.collection.Seq.class);
}
}
private QueryExecutionConverters() {}
@@ -194,6 +206,11 @@ public abstract class QueryExecutionConverters {
conversionService.addConverter(JavaslangCollections.FromJavaConverter.INSTANCE);
}
if (VAVR_PRESENT) {
conversionService.addConverter(new NullableWrapperToVavrOptionConverter(conversionService));
conversionService.addConverter(VavrCollections.FromJavaConverter.INSTANCE);
}
conversionService.addConverter(new NullableWrapperToFutureConverter(conversionService));
}
@@ -468,7 +485,7 @@ public abstract class QueryExecutionConverters {
@Override
@SuppressWarnings("unchecked")
protected Object wrap(Object source) {
return (javaslang.control.Option<Object>) ReflectionUtils.invokeMethod(OF_METHOD, null, source);
return ReflectionUtils.invokeMethod(OF_METHOD, null, source);
}
@SuppressWarnings("unchecked")
@@ -477,6 +494,50 @@ public abstract class QueryExecutionConverters {
}
}
/**
* Converter to convert from {@link NullableWrapper} into JavaSlang's {@link io.vavr.control.Option}.
*
* @author Oliver Gierke
* @since 2.0
*/
private static class NullableWrapperToVavrOptionConverter extends AbstractWrapperTypeConverter {
private static final Method OF_METHOD;
private static final Method NONE_METHOD;
static {
OF_METHOD = ReflectionUtils.findMethod(io.vavr.control.Option.class, "of", Object.class);
NONE_METHOD = ReflectionUtils.findMethod(io.vavr.control.Option.class, "none");
}
/**
* Creates a new {@link NullableWrapperToJavaslangOptionConverter} using the given {@link ConversionService}.
*
* @param conversionService must not be {@literal null}.
*/
public NullableWrapperToVavrOptionConverter(ConversionService conversionService) {
super(conversionService, createEmptyOption(), io.vavr.control.Option.class);
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.util.QueryExecutionConverters.AbstractWrapperTypeConverter#wrap(java.lang.Object)
*/
@Override
protected Object wrap(Object source) {
return ReflectionUtils.invokeMethod(OF_METHOD, source);
}
public static WrapperType getWrapperType() {
return WrapperType.singleValue(io.vavr.control.Option.class);
}
@SuppressWarnings("unchecked")
private static io.vavr.control.Option<Object> createEmptyOption() {
return (io.vavr.control.Option<Object>) ReflectionUtils.invokeMethod(NONE_METHOD, null);
}
}
/**
* A {@link Converter} to unwrap Guava {@link Optional} instances.
*
@@ -583,7 +644,7 @@ public abstract class QueryExecutionConverters {
return ((javaslang.control.Option<Object>) source).getOrElse(NULL_SUPPLIER);
}
if (source instanceof Traversable) {
if (source instanceof javaslang.collection.Traversable) {
return JavaslangCollections.ToJavaConverter.INSTANCE.convert(source);
}
@@ -591,6 +652,47 @@ public abstract class QueryExecutionConverters {
}
}
/**
* Converter to unwrap Vavr {@link io.vavr.control.Option} instances.
*
* @author Oliver Gierke
* @since 2.0
*/
private static enum VavrOptionUnwrapper implements Converter<Object, Object> {
INSTANCE;
private static final Supplier<Object> NULL_SUPPLIER = new Supplier<Object>() {
/*
* (non-Javadoc)
* @see java.util.function.Supplier#get()
*/
public Object get() {
return null;
}
};
/*
* (non-Javadoc)
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
*/
@Override
@SuppressWarnings("unchecked")
public Object convert(Object source) {
if (source instanceof io.vavr.control.Option) {
return ((io.vavr.control.Option<Object>) source).getOrElse(NULL_SUPPLIER);
}
if (source instanceof io.vavr.collection.Traversable) {
return VavrCollections.ToJavaConverter.INSTANCE.convert(source);
}
return source;
}
}
@Value
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public static class WrapperType {

View File

@@ -0,0 +1,149 @@
/*
* Copyright 2016-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.repository.util;
import io.vavr.collection.Traversable;
import java.lang.reflect.Method;
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 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.util.ReflectionUtils;
/**
* Converter implementations to map from and to Vavr collections.
*
* @author Oliver Gierke
* @author Christoph Strobl
* @since 1.13
*/
class VavrCollections {
public enum ToJavaConverter implements Converter<Object, Object> {
INSTANCE;
public WrapperType getWrapperType() {
return WrapperType.multiValue(io.vavr.collection.Traversable.class);
}
/*
* (non-Javadoc)
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
*/
@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 {
/*
* (non-Javadoc)
* @see org.springframework.core.convert.converter.GenericConverter#getConvertibleTypes()
*/
@Override
public java.util.Set<ConvertiblePair> getConvertibleTypes() {
return CONVERTIBLE_PAIRS;
}
/*
* (non-Javadoc)
* @see org.springframework.core.convert.converter.ConditionalConverter#matches(org.springframework.core.convert.TypeDescriptor, org.springframework.core.convert.TypeDescriptor)
*/
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
// Prevent collections to be mapped to maps
if (sourceType.isCollection() && io.vavr.collection.Map.class.isAssignableFrom(targetType.getType())) {
return false;
}
// Prevent maps to be mapped to collections
if (sourceType.isMap() && !(io.vavr.collection.Map.class.isAssignableFrom(targetType.getType())
|| targetType.getType().equals(Traversable.class))) {
return false;
}
return true;
}
/*
* (non-Javadoc)
* @see org.springframework.core.convert.converter.GenericConverter#convert(java.lang.Object, org.springframework.core.convert.TypeDescriptor, org.springframework.core.convert.TypeDescriptor)
*/
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source instanceof List) {
return ReflectionUtils.invokeMethod(LIST_FACTORY_METHOD, null, source);
}
if (source instanceof java.util.Set) {
return ReflectionUtils.invokeMethod(SET_FACTORY_METHOD, null, source);
}
if (source instanceof java.util.Map) {
return ReflectionUtils.invokeMethod(MAP_FACTORY_METHOD, null, source);
}
return source;
}
};
private static final Set<ConvertiblePair> CONVERTIBLE_PAIRS;
private static final Method LIST_FACTORY_METHOD;
private static final Method SET_FACTORY_METHOD;
private static final Method MAP_FACTORY_METHOD;
static {
Set<ConvertiblePair> pairs = new HashSet<ConvertiblePair>();
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);
MAP_FACTORY_METHOD = ReflectionUtils.findMethod(io.vavr.collection.LinkedHashMap.class, "ofAll", Map.class);
LIST_FACTORY_METHOD = ReflectionUtils.findMethod(io.vavr.collection.List.class, "ofAll", Iterable.class);
SET_FACTORY_METHOD = ReflectionUtils.findMethod(io.vavr.collection.LinkedHashSet.class, "ofAll", Iterable.class);
}
}
}

View File

@@ -231,6 +231,116 @@ public class QueryExecutionConvertersUnitTests {
assertThat(allowedPageableTypes, Matchers.<Class<?>> hasItems(Page.class, Slice.class, List.class, Seq.class));
}
@Test // DATACMNS-1065
public void unwrapsEmptyVavrOption() {
assertThat(QueryExecutionConverters.unwrap(vavrOptionNone()), is(nullValue()));
}
@Test // DATACMNS-1065
public void unwrapsVavrOption() {
assertThat(QueryExecutionConverters.unwrap(vavrOption("string")), is((Object) "string"));
}
@Test // DATACMNS-1065
public void conversListToVavr() {
assertThat(conversionService.canConvert(List.class, io.vavr.collection.Traversable.class), is(true));
assertThat(conversionService.canConvert(List.class, io.vavr.collection.List.class), is(true));
assertThat(conversionService.canConvert(List.class, io.vavr.collection.Set.class), is(true));
assertThat(conversionService.canConvert(List.class, io.vavr.collection.Map.class), is(false));
List<Integer> integers = Arrays.asList(1, 2, 3);
io.vavr.collection.Traversable<?> result = conversionService.convert(integers,
io.vavr.collection.Traversable.class);
assertThat(result, is(instanceOf(io.vavr.collection.List.class)));
}
@Test // DATACMNS-1065
public void convertsSetToVavr() {
assertThat(conversionService.canConvert(Set.class, io.vavr.collection.Traversable.class), is(true));
assertThat(conversionService.canConvert(Set.class, io.vavr.collection.Set.class), is(true));
assertThat(conversionService.canConvert(Set.class, io.vavr.collection.List.class), is(true));
assertThat(conversionService.canConvert(Set.class, io.vavr.collection.Map.class), is(false));
Set<Integer> integers = Collections.singleton(1);
io.vavr.collection.Traversable<?> result = conversionService.convert(integers,
io.vavr.collection.Traversable.class);
assertThat(result, is(instanceOf(io.vavr.collection.Set.class)));
}
@Test // DATACMNS-1065
public void convertsMapToVavr() {
assertThat(conversionService.canConvert(Map.class, io.vavr.collection.Traversable.class), is(true));
assertThat(conversionService.canConvert(Map.class, io.vavr.collection.Map.class), is(true));
assertThat(conversionService.canConvert(Map.class, io.vavr.collection.Set.class), is(false));
assertThat(conversionService.canConvert(Map.class, io.vavr.collection.List.class), is(false));
Map<String, String> map = Collections.singletonMap("key", "value");
io.vavr.collection.Traversable<?> result = conversionService.convert(map, io.vavr.collection.Traversable.class);
assertThat(result, is(instanceOf(io.vavr.collection.Map.class)));
}
@Test // DATACMNS-1065
public void unwrapsVavrCollectionsToJavaOnes() {
assertThat(unwrap(vavrList(1, 2, 3)), is(instanceOf(List.class)));
assertThat(unwrap(vavrSet(1, 2, 3)), is(instanceOf(Set.class)));
assertThat(unwrap(vavrMap("key", "value")), is(instanceOf(Map.class)));
}
@Test // DATACMNS-1065
public void vavrSeqIsASupportedPageableType() {
Set<Class<?>> allowedPageableTypes = QueryExecutionConverters.getAllowedPageableTypes();
assertThat(allowedPageableTypes, hasItem(io.vavr.collection.Seq.class));
}
// Vavr
@SuppressWarnings("unchecked")
private static io.vavr.control.Option<Object> vavrOptionNone() {
Method method = ReflectionUtils.findMethod(io.vavr.control.Option.class, "none");
return (io.vavr.control.Option<Object>) ReflectionUtils.invokeMethod(method, null);
}
@SuppressWarnings("unchecked")
private static <T> io.vavr.control.Option<T> vavrOption(T source) {
Method method = ReflectionUtils.findMethod(io.vavr.control.Option.class, "of", Object.class);
return (io.vavr.control.Option<T>) ReflectionUtils.invokeMethod(method, null, source);
}
@SuppressWarnings("unchecked")
private static <T> io.vavr.collection.List<T> vavrList(T... values) {
Method method = ReflectionUtils.findMethod(io.vavr.collection.List.class, "ofAll", Iterable.class);
return (io.vavr.collection.List<T>) ReflectionUtils.invokeMethod(method, null, Arrays.asList(values));
}
@SuppressWarnings("unchecked")
private static <T> io.vavr.collection.Set<T> vavrSet(T... values) {
Method method = ReflectionUtils.findMethod(io.vavr.collection.HashSet.class, "ofAll", Iterable.class);
return (io.vavr.collection.Set<T>) ReflectionUtils.invokeMethod(method, null, Arrays.asList(values));
}
@SuppressWarnings("unchecked")
private static <K, V> io.vavr.collection.Map<K, V> vavrMap(K key, V value) {
Method method = ReflectionUtils.findMethod(io.vavr.collection.HashMap.class, "ofAll", Map.class);
return (io.vavr.collection.Map<K, V>) ReflectionUtils.invokeMethod(method, null,
Collections.singletonMap(key, value));
}
@SuppressWarnings("unchecked")
private static javaslang.control.Option<Object> optionNone() {

View File

@@ -13,6 +13,7 @@ Import-Template:
com.google.common.*;version="${guava:[=.=.=,+1.0.0)}";resolution:=optional,
com.jayway.jsonpath.*;version="${jsonpath:[=.=.=,+1.0.0]}";resolution:=optional,
com.querydsl.*;version="${querydsl:[=.=.=,+1.0.0)}";resolution:=optional,
io.vavr.*;version="${vavr:[=.=.=,+1.0.0)}";resolution:=optional,
javaslang.*;version="${javaslang:[=.=.=,+1.0.0)}";resolution:=optional,
javax.enterprise.*;version="${cdi:[=.=.=,+1.0.0)}";resolution:=optional,
javax.inject.*;version="[1.0.0,2.0.0)";resolution:=optional,