From 9d1673994369ef7577ef2be9c08e53892c08a7c8 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Mon, 19 Nov 2018 19:21:33 +0100 Subject: [PATCH] =?UTF-8?q?DATACMNS-1419=20-=20Added=20TypeInformation.isS?= =?UTF-8?q?ubtypeOf(=E2=80=A6).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springframework/data/util/TypeInformation.java | 11 +++++++++++ .../data/util/TypeDiscovererUnitTests.java | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/main/java/org/springframework/data/util/TypeInformation.java b/src/main/java/org/springframework/data/util/TypeInformation.java index de16747df..a158c4f53 100644 --- a/src/main/java/org/springframework/data/util/TypeInformation.java +++ b/src/main/java/org/springframework/data/util/TypeInformation.java @@ -262,4 +262,15 @@ public interface TypeInformation { * @return will never be {@literal null}. */ TypeInformation specialize(ClassTypeInformation type); + + /** + * Returns whether the current type is a sub type of the given one, i.e. whether it's assignable but not the same one. + * + * @param type must not be {@literal null}. + * @return + * @since 2.2 + */ + default boolean isSubTypeOf(Class type) { + return !type.equals(getType()) && type.isAssignableFrom(getType()); + } } diff --git a/src/test/java/org/springframework/data/util/TypeDiscovererUnitTests.java b/src/test/java/org/springframework/data/util/TypeDiscovererUnitTests.java index 6196cbec2..1feb998c4 100755 --- a/src/test/java/org/springframework/data/util/TypeDiscovererUnitTests.java +++ b/src/test/java/org/springframework/data/util/TypeDiscovererUnitTests.java @@ -27,6 +27,7 @@ import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Set; import org.junit.Test; import org.junit.runner.RunWith; @@ -168,6 +169,16 @@ public class TypeDiscovererUnitTests { assertThat(type.getRequiredProperty("streamable").isCollectionLike()).isTrue(); } + @Test // DATACMNS-1419 + public void detectsSubTypes() { + + ClassTypeInformation type = from(Set.class); + + assertThat(type.isSubTypeOf(Collection.class)).isTrue(); + assertThat(type.isSubTypeOf(Set.class)).isFalse(); + assertThat(type.isSubTypeOf(String.class)).isFalse(); + } + class Person { Addresses addresses;