From 0e86470e363f3a17fdeacfe5c671986bb76a00a1 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 3 Jul 2017 14:23:05 +0200 Subject: [PATCH] =?UTF-8?q?DATACMNS-1101=20-=20Remove=20Alias.ofOptional(?= =?UTF-8?q?=E2=80=A6).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refactored type alias detection invocation in BasicPersistentEntity into separate method and avoid the factory method Alias.ofOptional(…) so that it can be removed. --- .../springframework/data/mapping/Alias.java | 16 --------- .../mapping/model/BasicPersistentEntity.java | 35 +++++++++++++++---- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/springframework/data/mapping/Alias.java b/src/main/java/org/springframework/data/mapping/Alias.java index 5cef57691..b1ddd672e 100644 --- a/src/main/java/org/springframework/data/mapping/Alias.java +++ b/src/main/java/org/springframework/data/mapping/Alias.java @@ -19,8 +19,6 @@ import lombok.AccessLevel; import lombok.RequiredArgsConstructor; import lombok.Value; -import java.util.Optional; - import org.springframework.util.Assert; /** @@ -71,20 +69,6 @@ public class Alias { return alias == null ? NONE : new Alias(alias); } - /** - * Create an {@link Alias} from an {@link Optional}. Returns an {@link Alias} object of the optional value is present, - * otherwise {@link #empty()}. - * - * @param optional must not be {@literal null}. - * @return the {@link Alias} for {@code alias} or {@link #empty()} if the given alias was {@literal null}. - */ - public static Alias ofOptional(Optional optional) { - - Assert.notNull(optional, "Optional must not be null!"); - - return optional.map(Alias::new).orElse(NONE); - } - /** * Returns an empty {@code Alias} instance. No value is present for this Alias. * diff --git a/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java b/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java index c80418fa5..751b6cbf0 100644 --- a/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java +++ b/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java @@ -32,7 +32,18 @@ import java.util.TreeSet; import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.data.annotation.TypeAlias; -import org.springframework.data.mapping.*; +import org.springframework.data.mapping.Alias; +import org.springframework.data.mapping.Association; +import org.springframework.data.mapping.AssociationHandler; +import org.springframework.data.mapping.IdentifierAccessor; +import org.springframework.data.mapping.PersistentEntity; +import org.springframework.data.mapping.PersistentProperty; +import org.springframework.data.mapping.PersistentPropertyAccessor; +import org.springframework.data.mapping.PreferredConstructor; +import org.springframework.data.mapping.PropertyHandler; +import org.springframework.data.mapping.SimpleAssociationHandler; +import org.springframework.data.mapping.SimplePropertyHandler; +import org.springframework.data.mapping.TargetAwareIdentifierAccessor; import org.springframework.data.util.Lazy; import org.springframework.data.util.TypeInformation; import org.springframework.util.Assert; @@ -99,11 +110,7 @@ public class BasicPersistentEntity> implement this.propertyCache = new HashMap<>(); this.annotationCache = new HashMap<>(); this.propertyAccessorFactory = BeanWrapperPropertyAccessorFactory.INSTANCE; - - this.typeAlias = Lazy.of(() -> Alias - .ofOptional(Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(getType(), TypeAlias.class))// - .map(TypeAlias::value)// - .filter(StringUtils::hasText))); + this.typeAlias = Lazy.of(() -> getAliasFromAnnotation(getType())); } /* @@ -458,6 +465,22 @@ public class BasicPersistentEntity> implement return hasIdProperty() ? new IdPropertyIdentifierAccessor(this, bean) : new AbsentIdentifierAccessor(bean); } + /** + * Calculates the {@link Alias} to be used for the given type. + * + * @param type must not be {@literal null}. + * @return + */ + private static Alias getAliasFromAnnotation(Class type) { + + Optional typeAliasValue = Optional + .ofNullable(AnnotatedElementUtils.findMergedAnnotation(type, TypeAlias.class))// + .map(TypeAlias::value)// + .filter(StringUtils::hasText); + + return Alias.ofNullable(typeAliasValue.orElse(null)); + } + /** * A null-object implementation of {@link IdentifierAccessor} to be able to return an accessor for entities that do * not have an identifier property.