TypeUtils { @Nonnull public static <T> T convertValue(@Nonnull Class<T> targetType, @Nonnull Object value) { return convertValue(targetType, value, null); } private  TypeUtils(); static boolean castToBoolean(@Nonnull Object value); static char castToChar(@Nonnull Object value); static byte castToByte(@Nonnull Object value); static short castToShort(@Nonnull Object value); static int castToInt(@Nonnull Object value); static long castToLong(@Nonnull Object value); static float castToFloat(@Nonnull Object value); static double castToDouble(@Nonnull Object value); static BigInteger castToBigInteger(@Nonnull Object value); static BigDecimal castToBigDecimal(@Nonnull Object value); @Nullable static Number castToNumber(@Nonnull Object value); static boolean castToBoolean(@Nullable Object value, boolean defaultValue); static char castToChar(@Nullable Object value, char defaultValue); static byte castToByte(@Nullable Object value, byte defaultValue); static short castToShort(@Nullable Object value, short defaultValue); static int castToInt(@Nullable Object value, int defaultValue); static long castToLong(@Nullable Object value, long defaultValue); static float castToFloat(@Nullable Object value, float defaultValue); static double castToDouble(@Nullable Object value, double defaultValue); @Nullable static Number castToNumber(@Nullable Object value, @Nullable Number defaultValue); @Nullable static BigInteger castToBigInteger(@Nullable Object value, @Nullable BigInteger defaultValue); @Nullable static BigDecimal castToBigDecimal(@Nullable Object value, @Nullable BigDecimal defaultValue); @Nonnull static T convertValue(@Nonnull Class<T> targetType, @Nonnull Object value); @Nonnull @SuppressWarnings("unchecked") static T convertValue(@Nonnull Class<T> targetType, @Nonnull Object value, @Nullable String format); static boolean isBoolean(@Nonnull Class<?> type); static boolean isCharacter(@Nonnull Class<?> type); static boolean isByte(@Nonnull Class<?> type); static boolean isShort(@Nonnull Class<?> type); static boolean isInteger(@Nonnull Class<?> type); static boolean isLong(@Nonnull Class<?> type); static boolean isFloat(@Nonnull Class<?> type); static boolean isDouble(@Nonnull Class<?> type); static boolean isBigInteger(@Nonnull Class<?> type); static boolean isBigDecimal(@Nonnull Class<?> type); static boolean isNumber(@Nonnull Class<?> type); static boolean equals(@Nullable Object left, @Nullable Object right); static boolean arrayEqual(@Nullable Object left, @Nullable Object right); static boolean equals(int[] left, int[] right); static boolean equals(Object[] left, List right); static boolean equals(List left, Object[] right); static boolean equals(List<T> left, List<T> right); static boolean equals(Set<T> left, Set<T> right); static boolean equals(Map<K, V> left, Map<K, V> right); static List primitiveArrayToList(Object array); @SuppressWarnings("unchecked") static int compareTo(Object left, Object right); static int compareTo(Character left, Number right); static int compareTo(Number left, Character right); static int compareTo(Character left, Character right); static int compareTo(Number left, Number right); static boolean isFloatingPoint(Number number); static boolean isInteger(Number number); static boolean isLong(Number number); static boolean isBigDecimal(Number number); static boolean isBigInteger(Number number);  }
@Test public void convertNumbersIntoWrapperByte() { assertEquals(Byte.valueOf("10"), TypeUtils.convertValue(Byte.class, Byte.valueOf("10"))); assertEquals(Byte.valueOf("10"), TypeUtils.convertValue(Byte.class, Short.valueOf("10"))); assertEquals(Byte.valueOf("10"), TypeUtils.convertValue(Byte.class, Integer.valueOf("10"))); assertEquals(Byte.valueOf("10"), TypeUtils.convertValue(Byte.class, Long.valueOf("10"))); assertEquals(Byte.valueOf("10"), TypeUtils.convertValue(Byte.class, Float.valueOf("10"))); assertEquals(Byte.valueOf("10"), TypeUtils.convertValue(Byte.class, Double.valueOf("10"))); assertEquals(Byte.valueOf("10"), TypeUtils.convertValue(Byte.class, new BigInteger("10"))); assertEquals(Byte.valueOf("10"), TypeUtils.convertValue(Byte.class, new BigDecimal("10"))); }
@Test public void convertNumbersIntoPrimitiveShort() { assertEquals((short) 10, (short) TypeUtils.convertValue(Short.TYPE, Byte.valueOf("10"))); assertEquals((short) 10, (short) TypeUtils.convertValue(Short.TYPE, Short.valueOf("10"))); assertEquals((short) 10, (short) TypeUtils.convertValue(Short.TYPE, Integer.valueOf("10"))); assertEquals((short) 10, (short) TypeUtils.convertValue(Short.TYPE, Long.valueOf("10"))); assertEquals((short) 10, (short) TypeUtils.convertValue(Short.TYPE, Float.valueOf("10"))); assertEquals((short) 10, (short) TypeUtils.convertValue(Short.TYPE, Double.valueOf("10"))); assertEquals((short) 10, (short) TypeUtils.convertValue(Short.TYPE, new BigInteger("10"))); assertEquals((short) 10, (short) TypeUtils.convertValue(Short.TYPE, new BigDecimal("10"))); }
@Test public void convertNumbersIntoWrapperShort() { assertEquals(Short.valueOf("10"), TypeUtils.convertValue(Short.class, Byte.valueOf("10"))); assertEquals(Short.valueOf("10"), TypeUtils.convertValue(Short.class, Short.valueOf("10"))); assertEquals(Short.valueOf("10"), TypeUtils.convertValue(Short.class, Integer.valueOf("10"))); assertEquals(Short.valueOf("10"), TypeUtils.convertValue(Short.class, Long.valueOf("10"))); assertEquals(Short.valueOf("10"), TypeUtils.convertValue(Short.class, Float.valueOf("10"))); assertEquals(Short.valueOf("10"), TypeUtils.convertValue(Short.class, Double.valueOf("10"))); assertEquals(Short.valueOf("10"), TypeUtils.convertValue(Short.class, new BigInteger("10"))); assertEquals(Short.valueOf("10"), TypeUtils.convertValue(Short.class, new BigDecimal("10"))); }
@Test public void convertNumbersIntoPrimitiveInteger() { assertEquals((int) 10, (int) TypeUtils.convertValue(Integer.TYPE, Byte.valueOf("10"))); assertEquals((int) 10, (int) TypeUtils.convertValue(Integer.TYPE, Short.valueOf("10"))); assertEquals((int) 10, (int) TypeUtils.convertValue(Integer.TYPE, Integer.valueOf("10"))); assertEquals((int) 10, (int) TypeUtils.convertValue(Integer.TYPE, Long.valueOf("10"))); assertEquals((int) 10, (int) TypeUtils.convertValue(Integer.TYPE, Float.valueOf("10"))); assertEquals((int) 10, (int) TypeUtils.convertValue(Integer.TYPE, Double.valueOf("10"))); assertEquals((int) 10, (int) TypeUtils.convertValue(Integer.TYPE, new BigInteger("10"))); assertEquals((int) 10, (int) TypeUtils.convertValue(Integer.TYPE, new BigDecimal("10"))); }
@Test public void convertNumbersIntoWrapperInteger() { assertEquals(Integer.valueOf("10"), TypeUtils.convertValue(Integer.class, Byte.valueOf("10"))); assertEquals(Integer.valueOf("10"), TypeUtils.convertValue(Integer.class, Short.valueOf("10"))); assertEquals(Integer.valueOf("10"), TypeUtils.convertValue(Integer.class, Integer.valueOf("10"))); assertEquals(Integer.valueOf("10"), TypeUtils.convertValue(Integer.class, Long.valueOf("10"))); assertEquals(Integer.valueOf("10"), TypeUtils.convertValue(Integer.class, Float.valueOf("10"))); assertEquals(Integer.valueOf("10"), TypeUtils.convertValue(Integer.class, Double.valueOf("10"))); assertEquals(Integer.valueOf("10"), TypeUtils.convertValue(Integer.class, new BigInteger("10"))); assertEquals(Integer.valueOf("10"), TypeUtils.convertValue(Integer.class, new BigDecimal("10"))); }
@Test public void convertNumbersIntoPrimitiveLong() { assertEquals((long) 10, (long) TypeUtils.convertValue(Long.TYPE, Byte.valueOf("10"))); assertEquals((long) 10, (long) TypeUtils.convertValue(Long.TYPE, Short.valueOf("10"))); assertEquals((long) 10, (long) TypeUtils.convertValue(Long.TYPE, Integer.valueOf("10"))); assertEquals((long) 10, (long) TypeUtils.convertValue(Long.TYPE, Long.valueOf("10"))); assertEquals((long) 10, (long) TypeUtils.convertValue(Long.TYPE, Float.valueOf("10"))); assertEquals((long) 10, (long) TypeUtils.convertValue(Long.TYPE, Double.valueOf("10"))); assertEquals((long) 10, (long) TypeUtils.convertValue(Long.TYPE, new BigInteger("10"))); assertEquals((long) 10, (long) TypeUtils.convertValue(Long.TYPE, new BigDecimal("10"))); }
@Test public void convertNumbersIntoWrapperLong() { assertEquals(Long.valueOf("10"), TypeUtils.convertValue(Long.class, Byte.valueOf("10"))); assertEquals(Long.valueOf("10"), TypeUtils.convertValue(Long.class, Short.valueOf("10"))); assertEquals(Long.valueOf("10"), TypeUtils.convertValue(Long.class, Integer.valueOf("10"))); assertEquals(Long.valueOf("10"), TypeUtils.convertValue(Long.class, Long.valueOf("10"))); assertEquals(Long.valueOf("10"), TypeUtils.convertValue(Long.class, Float.valueOf("10"))); assertEquals(Long.valueOf("10"), TypeUtils.convertValue(Long.class, Double.valueOf("10"))); assertEquals(Long.valueOf("10"), TypeUtils.convertValue(Long.class, new BigInteger("10"))); assertEquals(Long.valueOf("10"), TypeUtils.convertValue(Long.class, new BigDecimal("10"))); }
@Test public void convertNumbersIntoPrimitiveFloat() { assertEquals((float) 10, (float) TypeUtils.convertValue(Float.TYPE, Byte.valueOf("10"))); assertEquals((float) 10, (float) TypeUtils.convertValue(Float.TYPE, Short.valueOf("10"))); assertEquals((float) 10, (float) TypeUtils.convertValue(Float.TYPE, Integer.valueOf("10"))); assertEquals((float) 10, (float) TypeUtils.convertValue(Float.TYPE, Long.valueOf("10"))); assertEquals((float) 10, (float) TypeUtils.convertValue(Float.TYPE, Float.valueOf("10"))); assertEquals((float) 10, (float) TypeUtils.convertValue(Float.TYPE, Double.valueOf("10"))); assertEquals((float) 10, (float) TypeUtils.convertValue(Float.TYPE, new BigInteger("10"))); assertEquals((float) 10, (float) TypeUtils.convertValue(Float.TYPE, new BigDecimal("10"))); }
@Test public void convertNumbersIntoWrapperFloat() { assertEquals(Float.valueOf("10"), TypeUtils.convertValue(Float.class, Byte.valueOf("10"))); assertEquals(Float.valueOf("10"), TypeUtils.convertValue(Float.class, Short.valueOf("10"))); assertEquals(Float.valueOf("10"), TypeUtils.convertValue(Float.class, Integer.valueOf("10"))); assertEquals(Float.valueOf("10"), TypeUtils.convertValue(Float.class, Long.valueOf("10"))); assertEquals(Float.valueOf("10"), TypeUtils.convertValue(Float.class, Float.valueOf("10"))); assertEquals(Float.valueOf("10"), TypeUtils.convertValue(Float.class, Double.valueOf("10"))); assertEquals(Float.valueOf("10"), TypeUtils.convertValue(Float.class, new BigInteger("10"))); assertEquals(Float.valueOf("10"), TypeUtils.convertValue(Float.class, new BigDecimal("10"))); }
@Test public void convertNumbersIntoPrimitiveDouble() { assertEquals((double) 10, (double) TypeUtils.convertValue(Double.TYPE, Byte.valueOf("10"))); assertEquals((double) 10, (double) TypeUtils.convertValue(Double.TYPE, Short.valueOf("10"))); assertEquals((double) 10, (double) TypeUtils.convertValue(Double.TYPE, Integer.valueOf("10"))); assertEquals((double) 10, (double) TypeUtils.convertValue(Double.TYPE, Long.valueOf("10"))); assertEquals((double) 10, (double) TypeUtils.convertValue(Double.TYPE, Float.valueOf("10"))); assertEquals((double) 10, (double) TypeUtils.convertValue(Double.TYPE, Double.valueOf("10"))); assertEquals((double) 10, (double) TypeUtils.convertValue(Double.TYPE, new BigInteger("10"))); assertEquals((double) 10, (double) TypeUtils.convertValue(Double.TYPE, new BigDecimal("10"))); }
@Test public void convertNumbersIntoWrapperDouble() { assertEquals(Double.valueOf("10"), TypeUtils.convertValue(Double.class, Byte.valueOf("10"))); assertEquals(Double.valueOf("10"), TypeUtils.convertValue(Double.class, Short.valueOf("10"))); assertEquals(Double.valueOf("10"), TypeUtils.convertValue(Double.class, Integer.valueOf("10"))); assertEquals(Double.valueOf("10"), TypeUtils.convertValue(Double.class, Long.valueOf("10"))); assertEquals(Double.valueOf("10"), TypeUtils.convertValue(Double.class, Float.valueOf("10"))); assertEquals(Double.valueOf("10"), TypeUtils.convertValue(Double.class, Double.valueOf("10"))); assertEquals(Double.valueOf("10"), TypeUtils.convertValue(Double.class, new BigInteger("10"))); assertEquals(Double.valueOf("10"), TypeUtils.convertValue(Double.class, new BigDecimal("10"))); }
@Test public void convertNumbersIntoWrapperBigInteger() { assertEquals(BigInteger.valueOf(10L), TypeUtils.convertValue(BigInteger.class, Byte.valueOf("10"))); assertEquals(BigInteger.valueOf(10L), TypeUtils.convertValue(BigInteger.class, Short.valueOf("10"))); assertEquals(BigInteger.valueOf(10L), TypeUtils.convertValue(BigInteger.class, Integer.valueOf("10"))); assertEquals(BigInteger.valueOf(10L), TypeUtils.convertValue(BigInteger.class, Long.valueOf("10"))); assertEquals(BigInteger.valueOf(10L), TypeUtils.convertValue(BigInteger.class, Float.valueOf("10"))); assertEquals(BigInteger.valueOf(10L), TypeUtils.convertValue(BigInteger.class, Double.valueOf("10"))); assertEquals(BigInteger.valueOf(10L), TypeUtils.convertValue(BigInteger.class, new BigInteger("10"))); assertEquals(BigInteger.valueOf(10L), TypeUtils.convertValue(BigInteger.class, new BigDecimal("10"))); }
@Test public void convertNumbersIntoWrapperBigDecimal() { assertEquals(BigDecimal.valueOf(10d), TypeUtils.convertValue(BigDecimal.class, Byte.valueOf("10"))); assertEquals(BigDecimal.valueOf(10d), TypeUtils.convertValue(BigDecimal.class, Short.valueOf("10"))); assertEquals(BigDecimal.valueOf(10d), TypeUtils.convertValue(BigDecimal.class, Integer.valueOf("10"))); assertEquals(BigDecimal.valueOf(10d), TypeUtils.convertValue(BigDecimal.class, Long.valueOf("10"))); assertEquals(BigDecimal.valueOf(10d), TypeUtils.convertValue(BigDecimal.class, Float.valueOf("10"))); assertEquals(BigDecimal.valueOf(10d), TypeUtils.convertValue(BigDecimal.class, Double.valueOf("10"))); assertEquals(BigDecimal.valueOf(10d), TypeUtils.convertValue(BigDecimal.class, new BigInteger("10"))); assertEquals(BigDecimal.valueOf(10d), TypeUtils.convertValue(BigDecimal.class, new BigDecimal("10.0"))); }
@Test public void convertBooleans() { assertEquals(true, (boolean) TypeUtils.convertValue(Boolean.TYPE, Boolean.TRUE)); assertEquals(Boolean.TRUE, TypeUtils.convertValue(Boolean.class, Boolean.TRUE)); }
@Test @SuppressWarnings("UnnecessaryBoxing") public void convertCharacters() { assertEquals('A', (char) TypeUtils.convertValue(Character.TYPE, Character.valueOf('A'))); assertEquals(Character.valueOf('A'), TypeUtils.convertValue(Character.class, Character.valueOf('A'))); }
@Test public void convertNumbersIntoPrimitiveByte() { assertEquals((byte) 10, (byte) TypeUtils.convertValue(Byte.TYPE, Byte.valueOf("10"))); assertEquals((byte) 10, (byte) TypeUtils.convertValue(Byte.TYPE, Short.valueOf("10"))); assertEquals((byte) 10, (byte) TypeUtils.convertValue(Byte.TYPE, Integer.valueOf("10"))); assertEquals((byte) 10, (byte) TypeUtils.convertValue(Byte.TYPE, Long.valueOf("10"))); assertEquals((byte) 10, (byte) TypeUtils.convertValue(Byte.TYPE, Float.valueOf("10"))); assertEquals((byte) 10, (byte) TypeUtils.convertValue(Byte.TYPE, Double.valueOf("10"))); assertEquals((byte) 10, (byte) TypeUtils.convertValue(Byte.TYPE, new BigInteger("10"))); assertEquals((byte) 10, (byte) TypeUtils.convertValue(Byte.TYPE, new BigDecimal("10"))); }