-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#97 Make ArrayComponentType public, add ClassUtils#isInstance
- Loading branch information
Showing
6 changed files
with
196 additions
and
70 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
src/main/java/ch/jalu/typeresolver/array/ArrayComponentType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package ch.jalu.typeresolver.array; | ||
|
||
import ch.jalu.typeresolver.classutil.ClassUtils; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Represents the component <i>type</i> an array can have: this enum has all primitive types, and Object. | ||
*/ | ||
public enum ArrayComponentType { | ||
|
||
BOOLEAN(boolean.class), | ||
BYTE(byte.class), | ||
CHARACTER(char.class), | ||
SHORT(short.class), | ||
INTEGER(int.class), | ||
LONG(long.class), | ||
FLOAT(float.class), | ||
DOUBLE(double.class), | ||
/** Means any extension of Object, such as the component of {@code String[]}, or even {@code int[][]}. */ | ||
OBJECT(Object.class); | ||
|
||
private static final Map<Class<?>, ArrayComponentType> PRIMITIVE_ARRAY_TO_COMPONENT = | ||
createPrimitiveArrayTypesToComponentMap(); | ||
private final Class<?> componentClass; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param componentClass the base component type (primitive class, or Object.class) | ||
*/ | ||
ArrayComponentType(Class<?> componentClass) { | ||
this.componentClass = componentClass; | ||
} | ||
|
||
/** | ||
* @return The class that is the component type of the array. This is a primitive class like {@code int.class}, | ||
* or {@code Object.class} if the array's component type is a reference type. | ||
*/ | ||
public Class<?> getComponentClass() { | ||
return componentClass; | ||
} | ||
|
||
/** | ||
* Returns the appropriate {@link ArrayComponentType} entry for the given array. Throws an exception if | ||
* the argument is not an array. | ||
* | ||
* @param array the array to inspect | ||
* @return the array component type representing the enum's component type | ||
*/ | ||
public static ArrayComponentType getArrayComponentType(@Nullable Object array) { | ||
if (array != null) { | ||
Class<?> clazz = array.getClass(); | ||
ArrayComponentType componentType = PRIMITIVE_ARRAY_TO_COMPONENT.get(clazz); | ||
if (componentType != null) { | ||
return componentType; | ||
} else if (Object[].class.isAssignableFrom(clazz)) { | ||
return ArrayComponentType.OBJECT; | ||
} | ||
} | ||
|
||
throw new IllegalArgumentException("Expected an array but got an object of type: " | ||
+ ClassUtils.getClassName(array)); | ||
} | ||
|
||
private static Map<Class<?>, ArrayComponentType> createPrimitiveArrayTypesToComponentMap() { | ||
Map<Class<?>, ArrayComponentType> map = new HashMap<>(); | ||
map.put(boolean[].class, BOOLEAN); | ||
map.put(byte[].class, BYTE); | ||
map.put(char[].class, CHARACTER); | ||
map.put(short[].class, SHORT); | ||
map.put(int[].class, INTEGER); | ||
map.put(long[].class, LONG); | ||
map.put(float[].class, FLOAT); | ||
map.put(double[].class, DOUBLE); | ||
return Collections.unmodifiableMap(map); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/test/java/ch/jalu/typeresolver/array/ArrayComponentTypeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package ch.jalu.typeresolver.array; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
/** | ||
* Test for {@link ArrayComponentType}. | ||
*/ | ||
class ArrayComponentTypeTest { | ||
|
||
@ParameterizedTest | ||
@MethodSource("argsForDetermineArrayTypeTest") | ||
void shouldDetermineArrayType(Object array, ArrayComponentType expectedType) { | ||
// given / when | ||
ArrayComponentType result = ArrayComponentType.getArrayComponentType(array); | ||
|
||
// then | ||
assertThat(result, equalTo(expectedType)); | ||
if (result == ArrayComponentType.OBJECT) { | ||
assertThat(result.getComponentClass(), equalTo(Object.class)); | ||
} else { | ||
assertThat(result.getComponentClass(), equalTo(array.getClass().getComponentType())); | ||
} | ||
} | ||
|
||
static Stream<Arguments> argsForDetermineArrayTypeTest() { | ||
return Stream.of( | ||
Arguments.of(new boolean[]{true}, ArrayComponentType.BOOLEAN), | ||
Arguments.of(new byte[]{}, ArrayComponentType.BYTE), | ||
Arguments.of(new char[]{'ç'}, ArrayComponentType.CHARACTER), | ||
Arguments.of(new short[]{}, ArrayComponentType.SHORT), | ||
Arguments.of(new int[]{22}, ArrayComponentType.INTEGER), | ||
Arguments.of(new long[]{420}, ArrayComponentType.LONG), | ||
Arguments.of(new float[]{}, ArrayComponentType.FLOAT), | ||
Arguments.of(new double[]{3.4}, ArrayComponentType.DOUBLE), | ||
|
||
Arguments.of(new Object[]{'5'}, ArrayComponentType.OBJECT), | ||
Arguments.of(new Boolean[]{true}, ArrayComponentType.OBJECT), | ||
Arguments.of(new Double[]{6.2}, ArrayComponentType.OBJECT), | ||
Arguments.of(new String[]{"t"}, ArrayComponentType.OBJECT), | ||
Arguments.of(new String[][]{}, ArrayComponentType.OBJECT), | ||
Arguments.of(new double[][]{{54.2}}, ArrayComponentType.OBJECT), | ||
Arguments.of(new char[][][]{{{'ê'}}}, ArrayComponentType.OBJECT) | ||
); | ||
} | ||
|
||
@Test | ||
void shouldThrowForNull() { | ||
// given / when | ||
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, | ||
() -> ArrayComponentType.getArrayComponentType(null)); | ||
|
||
// then | ||
assertThat(ex.getMessage(), equalTo("Expected an array but got an object of type: null")); | ||
} | ||
|
||
@Test | ||
void shouldThrowForNonArrayValue() { | ||
// given / when | ||
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, | ||
() -> ArrayComponentType.getArrayComponentType("test")); | ||
|
||
// then | ||
assertThat(ex.getMessage(), equalTo("Expected an array but got an object of type: java.lang.String")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters