diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/CustomConversions.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/CustomConversions.java index dd730bc37..1bdda18dd 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/CustomConversions.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/CustomConversions.java @@ -21,9 +21,9 @@ import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -import java.util.function.Supplier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -63,9 +63,9 @@ public class CustomConversions { private final List converters; - private final Map> customReadTargetTypes; - private final Map> customWriteTargetTypes; - private final Map, Class> rawWriteTargetTypes; + private final Map>> customReadTargetTypes; + private final Map>> customWriteTargetTypes; + private final Map, Optional>> rawWriteTargetTypes; /** * Creates an empty {@link CustomConversions} object. @@ -233,13 +233,8 @@ public class CustomConversions { */ public Class getCustomWriteTarget(final Class sourceType) { - return getOrCreateAndCache(sourceType, rawWriteTargetTypes, new Supplier>() { - - @Override - public Class get() { - return getCustomTarget(sourceType, null, writingPairs); - } - }); + return rawWriteTargetTypes.computeIfAbsent(sourceType, it -> getCustomTarget(sourceType, null, writingPairs)) + .orElse(null); } /** @@ -257,8 +252,8 @@ public class CustomConversions { return getCustomWriteTarget(sourceType); } - return getOrCreateAndCache(new ConvertiblePair(sourceType, requestedTargetType), customWriteTargetTypes, - () -> getCustomTarget(sourceType, requestedTargetType, writingPairs)); + return customWriteTargetTypes.computeIfAbsent(new ConvertiblePair(sourceType, requestedTargetType), + it -> getCustomTarget(sourceType, requestedTargetType, writingPairs)).orElse(null); } /** @@ -309,8 +304,9 @@ public class CustomConversions { return null; } - return getOrCreateAndCache(new ConvertiblePair(sourceType, requestedTargetType), customReadTargetTypes, - () -> getCustomTarget(sourceType, requestedTargetType, readingPairs)); + return customReadTargetTypes.computeIfAbsent(new ConvertiblePair(sourceType, requestedTargetType), + it -> getCustomTarget(sourceType, requestedTargetType, readingPairs)).orElse(null); + } /** @@ -322,33 +318,25 @@ public class CustomConversions { * @param pairs must not be {@literal null}. * @return */ - private static Class getCustomTarget(Class sourceType, Class requestedTargetType, + private static Optional> getCustomTarget(Class sourceType, Class requestedTargetType, Collection pairs) { Assert.notNull(sourceType, "Source Class must not be null"); Assert.notNull(pairs, "Collection of ConvertiblePair must not be null"); if (requestedTargetType != null && pairs.contains(new ConvertiblePair(sourceType, requestedTargetType))) { - return requestedTargetType; + return Optional.of(requestedTargetType); } - return pairs.stream() // - .filter(typePair -> typePair.getSourceType().isAssignableFrom(sourceType)) // - .map(ConvertiblePair::getTargetType) // - .filter(targetType -> requestedTargetType == null || targetType.isAssignableFrom(requestedTargetType)) // - .findFirst().orElse(null); - } + for (ConvertiblePair typePair : pairs) { + if (typePair.getSourceType().isAssignableFrom(sourceType)) { + Class targetType = typePair.getTargetType(); + if (requestedTargetType == null || targetType.isAssignableFrom(requestedTargetType)) { + return Optional.of(targetType); + } + } + } - /** - * Will try to find a value for the given key in the given cache or produce one using the given {@link Supplier} and - * store it in the cache. - * - * @param key the key to lookup a potentially existing value, must not be {@literal null}. - * @param cache the cache to find the value in, must not be {@literal null}. - * @param producer the {@link Supplier} to create values to cache, must not be {@literal null}. - * @return - */ - private static Class getOrCreateAndCache(T key, Map> cache, Supplier> producer) { - return cache.computeIfAbsent(key, t -> producer.get()); + return Optional.empty(); } }