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

This commit is contained in:
Oliver Drotbohm
2018-11-19 19:21:33 +01:00
parent caaec1b862
commit 9d16739943
2 changed files with 22 additions and 0 deletions

View File

@@ -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());
}
}

View File

@@ -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<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;