DATACMNS-1096 - Replaced references to Spring's Converter to JDK 8's Function in domain package.

Switched to use Optional<Object> to make nullability explicit. Keep the original ….convert(…) method around as deprecated default method so that clients can be migrated gradually.
This commit is contained in:
Oliver Gierke
2017-06-22 12:25:34 +02:00
parent a605e168fc
commit 6cd5f7c052
3 changed files with 37 additions and 9 deletions

View File

@@ -25,7 +25,6 @@ import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.Assert;
/**
@@ -155,14 +154,14 @@ abstract class Chunk<T> implements Slice<T>, Serializable {
}
/**
* Applies the given {@link Converter} to the content of the {@link Chunk}.
* Applies the given {@link Function} to the content of the {@link Chunk}.
*
* @param converter must not be {@literal null}.
* @return
*/
protected <U> List<U> getConvertedContent(Function<? super T, ? extends U> converter) {
Assert.notNull(converter, "Converter must not be null!");
Assert.notNull(converter, "Function must not be null!");
return this.stream().map(converter::apply).collect(Collectors.toList());
}

View File

@@ -28,9 +28,10 @@ import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.Assert;
/**
@@ -661,24 +662,40 @@ public class ExampleMatcher {
* Treats strings as regular expression patterns
*/
REGEX;
}
/**
* Allows to transform the property value before it is used in the query.
*/
public static interface PropertyValueTransformer extends Converter<Object, Object> {}
public static interface PropertyValueTransformer extends Function<Optional<Object>, Optional<Object>> {
/**
* For backwards compatibility of clients used to invoke Spring's Converter interface.
*
* @param source
* @return
*/
@Deprecated
default Object convert(Object source) {
return apply(Optional.ofNullable(source)).orElse(null);
}
}
/**
* @author Christoph Strobl
* @author Oliver Gierke
* @since 1.12
*/
public static enum NoOpPropertyValueTransformer implements ExampleMatcher.PropertyValueTransformer {
INSTANCE;
/*
* (non-Javadoc)
* @see java.util.function.Function#apply(java.lang.Object)
*/
@Override
public Object convert(Object source) {
public Optional<Object> apply(Optional<Object> source) {
return source;
}
}
@@ -791,9 +808,21 @@ public class ExampleMatcher {
*
* @param source
* @return
* @deprecated since 2.0, use {@link #transformValue(Optional)} instead.
*/
@Deprecated
public Object transformValue(Object source) {
return getPropertyValueTransformer().convert(source);
return transformValue(Optional.ofNullable(source)).orElse(null);
}
/**
* Transforms a given source using the {@link PropertyValueTransformer}.
*
* @param source
* @return
*/
public Optional<Object> transformValue(Optional<Object> source) {
return getPropertyValueTransformer().apply(source);
}
}