From 7c33bcf98a5233f0266a4d4ce72482446e761bdf Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 21 Mar 2017 18:56:41 +0100 Subject: [PATCH] DATACMNS-867 - Let lookups for a property's component and map value type use Optional. --- .../data/mapping/PersistentProperty.java | 10 +++++----- .../data/mapping/model/AbstractPersistentProperty.java | 10 +++++----- .../data/mapping/MappingMetadataTests.java | 5 +++-- .../model/AbstractPersistentPropertyUnitTests.java | 2 +- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/springframework/data/mapping/PersistentProperty.java b/src/main/java/org/springframework/data/mapping/PersistentProperty.java index 68e9afcad..39e9e6897 100644 --- a/src/main/java/org/springframework/data/mapping/PersistentProperty.java +++ b/src/main/java/org/springframework/data/mapping/PersistentProperty.java @@ -171,10 +171,10 @@ public interface PersistentProperty

> { * Returns the component type of the type if it is a {@link java.util.Collection}. Will return the type of the key if * the property is a {@link java.util.Map}. * - * @return the component type, the map's key type or {@literal null} if neither {@link java.util.Collection} nor - * {@link java.util.Map}. + * @return the component type, the map's key type or {@link Optional#empty()} if neither {@link java.util.Collection} + * nor {@link java.util.Map}. */ - Class getComponentType(); + Optional> getComponentType(); /** * Returns the raw type as it's pulled from from the reflected property. @@ -186,9 +186,9 @@ public interface PersistentProperty

> { /** * Returns the type of the values if the property is a {@link java.util.Map}. * - * @return the map's value type or {@literal null} if no {@link java.util.Map} + * @return the map's value type or {@link Optional#empty()} if no {@link java.util.Map} */ - Class getMapValueType(); + Optional> getMapValueType(); /** * Returns the actual type of the property. This will be the original property type if no generics were used, the diff --git a/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java b/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java index b96943744..7a0893d74 100644 --- a/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java +++ b/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java @@ -250,13 +250,13 @@ public abstract class AbstractPersistentProperty

* @see org.springframework.data.mapping.PersistentProperty#getComponentType() */ @Override - public Class getComponentType() { + public Optional> getComponentType() { if (!isMap() && !isCollectionLike()) { - return null; + return Optional.empty(); } - return information.getRequiredComponentType().getType(); + return Optional.of(information.getRequiredComponentType().getType()); } /* @@ -264,8 +264,8 @@ public abstract class AbstractPersistentProperty

* @see org.springframework.data.mapping.PersistentProperty#getMapValueType() */ @Override - public Class getMapValueType() { - return isMap() ? information.getMapValueType().map(TypeInformation::getType).orElse(null) : null; + public Optional> getMapValueType() { + return isMap() ? information.getMapValueType().map(TypeInformation::getType) : Optional.empty(); } /* diff --git a/src/test/java/org/springframework/data/mapping/MappingMetadataTests.java b/src/test/java/org/springframework/data/mapping/MappingMetadataTests.java index 7be4bc1b3..1337fe091 100755 --- a/src/test/java/org/springframework/data/mapping/MappingMetadataTests.java +++ b/src/test/java/org/springframework/data/mapping/MappingMetadataTests.java @@ -50,7 +50,8 @@ public class MappingMetadataTests { PersistentEntity person = ctx.getRequiredPersistentEntity(PersonWithChildren.class); - person.doWithAssociations((AssociationHandler) association -> assertThat( - association.getInverse().getComponentType()).isEqualTo(Child.class)); + person.doWithAssociations((AssociationHandler) association -> { + assertThat(association.getInverse().getComponentType()).hasValue(Child.class); + }); } } diff --git a/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java b/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java index 9d3dcbdb6..1524f5a51 100755 --- a/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java @@ -61,7 +61,7 @@ public class AbstractPersistentPropertyUnitTests { @Test // DATACMNS-68 public void discoversComponentTypeCorrectly() throws Exception { - assertThat(getProperty(TestClassComplex.class, "testClassSet").getComponentType()).isEqualTo(Object.class); + assertThat(getProperty(TestClassComplex.class, "testClassSet").getComponentType()).hasValue(Object.class); } @Test // DATACMNS-101