Browse Source

DATACMNS-1419 - Added TypeInformation.isSubtypeOf(…).

pull/324/head
Oliver Drotbohm 7 years ago
parent
commit
9d16739943
No known key found for this signature in database
GPG Key ID: 6E42B5787543F690
  1. 11
      src/main/java/org/springframework/data/util/TypeInformation.java
  2. 11
      src/test/java/org/springframework/data/util/TypeDiscovererUnitTests.java

11
src/main/java/org/springframework/data/util/TypeInformation.java

@ -262,4 +262,15 @@ public interface TypeInformation<S> { @@ -262,4 +262,15 @@ public interface TypeInformation<S> {
* @return will never be {@literal null}.
*/
TypeInformation<? extends S> 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());
}
}

11
src/test/java/org/springframework/data/util/TypeDiscovererUnitTests.java

@ -27,6 +27,7 @@ import java.util.Iterator; @@ -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 { @@ -168,6 +169,16 @@ public class TypeDiscovererUnitTests {
assertThat(type.getRequiredProperty("streamable").isCollectionLike()).isTrue();
}
@Test // DATACMNS-1419
public void detectsSubTypes() {
ClassTypeInformation<Set> 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;

Loading…
Cancel
Save