From 6cd5f7c0524165bc1d31c8625c4dbea5bde6f858 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 22 Jun 2017 12:25:34 +0200 Subject: [PATCH] DATACMNS-1096 - Replaced references to Spring's Converter to JDK 8's Function in domain package. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Switched to use Optional to make nullability explicit. Keep the original ….convert(…) method around as deprecated default method so that clients can be migrated gradually. --- .../springframework/data/domain/Chunk.java | 5 +-- .../data/domain/ExampleMatcher.java | 39 ++++++++++++++++--- ...ExampleSpecificationAccessorUnitTests.java | 2 +- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/springframework/data/domain/Chunk.java b/src/main/java/org/springframework/data/domain/Chunk.java index 5a0c0aaa7..83436ea99 100644 --- a/src/main/java/org/springframework/data/domain/Chunk.java +++ b/src/main/java/org/springframework/data/domain/Chunk.java @@ -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 implements Slice, 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 List getConvertedContent(Function 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()); } diff --git a/src/main/java/org/springframework/data/domain/ExampleMatcher.java b/src/main/java/org/springframework/data/domain/ExampleMatcher.java index f0f349372..ff7b27b15 100644 --- a/src/main/java/org/springframework/data/domain/ExampleMatcher.java +++ b/src/main/java/org/springframework/data/domain/ExampleMatcher.java @@ -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 {} + public static interface PropertyValueTransformer extends Function, Optional> { + + /** + * 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 apply(Optional 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 transformValue(Optional source) { + return getPropertyValueTransformer().apply(source); } } diff --git a/src/test/java/org/springframework/data/repository/core/support/ExampleSpecificationAccessorUnitTests.java b/src/test/java/org/springframework/data/repository/core/support/ExampleSpecificationAccessorUnitTests.java index c617f435a..f60da4cb8 100755 --- a/src/test/java/org/springframework/data/repository/core/support/ExampleSpecificationAccessorUnitTests.java +++ b/src/test/java/org/springframework/data/repository/core/support/ExampleSpecificationAccessorUnitTests.java @@ -218,7 +218,7 @@ public class ExampleSpecificationAccessorUnitTests { @Test // DATACMNS-810 public void getValueTransformerForPathReturnsConfigurtedTransformerForPath() { - PropertyValueTransformer transformer = source -> source.toString(); + PropertyValueTransformer transformer = source -> source.map(Object::toString); specification = ExampleMatcher.matching().// withTransformer("firstname", transformer);