Skip to content

Commit cab4cd4

Browse files
committed
Fix #94 Create TypeInfo#equalTo(Class) and TypeInfo#isAssignableFrom(Class)
1 parent dbfac74 commit cab4cd4

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/main/java/ch/jalu/typeresolver/TypeInfo.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,40 @@ public Class<?> getSafeToReadClass() {
155155
}
156156

157157
/**
158-
* Returns a type info of the enclosing type (i.e. the outer class if this type is an nested class).
158+
* Returns whether this type info, as class ({@link #toClass()}), is equal to the given class.
159+
* <p>Examples:<ul>
160+
* <li>{@code new TypeInfo(String.class).equalTo(String.class) // true}</li>
161+
* <li>{@code new TypeReference<List<String>>() { }.equalTo(List.class) // true}</li>
162+
* <li>{@code new TypeReference<List<String>>() { }.equalTo(ArrayList.class) // false}</li>
163+
* </ul>
164+
*
165+
* @param clazz the class to check for equality
166+
* @return true if this type info converts to the same class as the given one, false otherwise
167+
*/
168+
public boolean equalTo(Class<?> clazz) {
169+
return clazz.equals(toClass());
170+
}
171+
172+
/**
173+
* Returns whether the class represented by this type info is equal or assignable from the given class. In other
174+
* words, this TypeInfo's {@link #toClass()} checks with {@link Class#isAssignableFrom} whether the given class
175+
* is equal or an extension of this type info's class.
176+
* <p>Examples:<ul>
177+
* <li>{@code new TypeInfo(String.class).isAssignableFrom(Double.class) // false}</li>
178+
* <li>{@code new TypeReference<List<String>>() { }.isAssignableFrom(List.class) // true}</li>
179+
* <li>{@code new TypeReference<List<String>>() { }.isAssignableFrom(ArrayList.class) // true}</li>
180+
* </ul>
181+
*
182+
* @param clazz the class to check with
183+
* @return true if this type info's class is equal or a supertype of the given class, false otherwise
184+
*/
185+
public boolean isAssignableFrom(Class<?> clazz) {
186+
Class<?> thisClass = toClass();
187+
return thisClass != null && thisClass.isAssignableFrom(clazz);
188+
}
189+
190+
/**
191+
* Returns a type info of the enclosing type (i.e. the outer class if this type is a nested class).
159192
* Returns null if not applicable.
160193
*
161194
* @return the enclosing type, or null if this type is not a nested type

src/test/java/ch/jalu/typeresolver/TypeInfoTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import ch.jalu.typeresolver.samples.typeinheritance.IntegerDoubleArgProcessorExtension;
1313
import ch.jalu.typeresolver.samples.typeinheritance.OneArgProcessor;
1414
import ch.jalu.typeresolver.samples.typeinheritance.StringArgProcessorExtension;
15+
import ch.jalu.typeresolver.typeimpl.WildcardTypeImpl;
1516
import org.junit.jupiter.api.Test;
1617

1718
import java.io.Serializable;
@@ -370,6 +371,36 @@ void shouldThrowForConstructorWithNotOverriddenMethod() {
370371
assertThrows(UnsupportedOperationException.class, TypeInfo::new);
371372
}
372373

374+
@Test
375+
void shouldReturnWhetherIsEqualToClass() {
376+
// given / when / then
377+
assertThat(new TypeInfo(String.class).equalTo(String.class), equalTo(true));
378+
assertThat(getType("stringList").equalTo(List.class), equalTo(true));
379+
assertThat(new NestedTypeReference<List<? super Number>>() { }.equalTo(Number.class), equalTo(true));
380+
381+
assertThat(new TypeInfo(String.class).equalTo(double.class), equalTo(false));
382+
assertThat(getType("stringList").equalTo(ArrayList.class), equalTo(false));
383+
assertThat(getType("stringList").equalTo(Collection.class), equalTo(false));
384+
assertThat(new TypeInfo(WildcardTypeImpl.newUnboundedWildcard()).equalTo(Object.class), equalTo(false));
385+
}
386+
387+
@Test
388+
void shouldReturnWhetherIsAssignableFromClass() {
389+
// given / when / then
390+
assertThat(new TypeInfo(String.class).isAssignableFrom(String.class), equalTo(true));
391+
assertThat(new TypeInfo(CharSequence.class).isAssignableFrom(String.class), equalTo(true));
392+
393+
assertThat(getType("stringList").isAssignableFrom(List.class), equalTo(true));
394+
assertThat(getType("stringList").isAssignableFrom(ArrayList.class), equalTo(true));
395+
assertThat(new NestedTypeReference<List<? super Number>>() { }.isAssignableFrom(Number.class), equalTo(true));
396+
assertThat(new NestedTypeReference<List<? super Number>>() { }.isAssignableFrom(Integer.class), equalTo(true));
397+
398+
assertThat(new TypeInfo(String.class).isAssignableFrom(double.class), equalTo(false));
399+
assertThat(getType("stringList").isAssignableFrom(Collection.class), equalTo(false));
400+
assertThat(new NestedTypeReference<List<? super Number>>() { }.isAssignableFrom(int.class), equalTo(false));
401+
assertThat(new TypeInfo(WildcardTypeImpl.newUnboundedWildcard()).isAssignableFrom(Object.class), equalTo(false));
402+
}
403+
373404
private static TypeInfo getType(String fieldName) {
374405
try {
375406
return new TypeInfo(ParameterizedTypes.class.getDeclaredField(fieldName).getGenericType());

0 commit comments

Comments
 (0)