@Test @SuppressWarnings("UnnecessaryBoxing") public void equality() { Integer[] integers1 = new Integer[]{Integer.valueOf(1), Integer.valueOf(2)}; Integer[] integers2 = new Integer[]{Integer.valueOf(1), Integer.valueOf(2), new Integer(3)}; int[] ints1 = {1, 2}; int[] ints2 = {1, 2, 3}; List<Integer> lintegers = asList(Integer.valueOf(1), Integer.valueOf(2)); assertTrue(TypeUtils.equals(integers1, integers1)); assertTrue(TypeUtils.equals(ints1, ints1)); assertFalse(TypeUtils.equals(integers1, ints2)); assertFalse(TypeUtils.equals(ints1, integers2)); assertTrue(TypeUtils.equals(integers1, lintegers)); assertTrue(TypeUtils.equals(lintegers, integers1)); assertTrue(TypeUtils.equals(ints1, lintegers)); assertTrue(TypeUtils.equals(lintegers, ints1)); assertTrue(TypeUtils.equals((Object) integers1, (Object) integers1)); assertTrue(TypeUtils.equals((Object) ints1, (Object) ints1)); assertFalse(TypeUtils.equals((Object) integers1, (Object) ints2)); assertFalse(TypeUtils.equals((Object) ints1, (Object) integers2)); assertTrue(TypeUtils.equals((Object) integers1, (Object) lintegers)); assertTrue(TypeUtils.equals((Object) lintegers, (Object) integers1)); assertTrue(TypeUtils.equals((Object) ints1, (Object) lintegers)); assertTrue(TypeUtils.equals((Object) lintegers, (Object) ints1)); }
public static boolean equals(@Nullable Object left, @Nullable Object right) { if (left == right) return true; if (left == null || right == null) return false; Class<?> leftClass = left.getClass(); Class<?> rightClass = right.getClass(); if (leftClass.isArray() && rightClass.isArray()) { return arrayEqual(left, right); } if (leftClass.isArray() && leftClass.getComponentType().isPrimitive()) { left = primitiveArrayToList(left); } if (rightClass.isArray() && rightClass.getComponentType().isPrimitive()) { right = primitiveArrayToList(right); } if (left instanceof Object[] && right instanceof List) { return equals((Object[]) left, (List) right); } if (left instanceof List && right instanceof Object[]) { return equals((List) left, (Object[]) right); } if (left instanceof List && right instanceof List) { return equals((List) left, (List) right); } if (left instanceof Map.Entry && right instanceof Map.Entry) { Object k1 = ((Map.Entry) left).getKey(); Object k2 = ((Map.Entry) right).getKey(); if (k1 == k2 || (k1 != null && k1.equals(k2))) { Object v1 = ((Map.Entry) left).getValue(); Object v2 = ((Map.Entry) right).getValue(); if (v1 == v2 || (v1 != null && equals(v1, v2))) return true; } return false; } return left.equals(right); }
TypeUtils { public static boolean equals(@Nullable Object left, @Nullable Object right) { if (left == right) return true; if (left == null || right == null) return false; Class<?> leftClass = left.getClass(); Class<?> rightClass = right.getClass(); if (leftClass.isArray() && rightClass.isArray()) { return arrayEqual(left, right); } if (leftClass.isArray() && leftClass.getComponentType().isPrimitive()) { left = primitiveArrayToList(left); } if (rightClass.isArray() && rightClass.getComponentType().isPrimitive()) { right = primitiveArrayToList(right); } if (left instanceof Object[] && right instanceof List) { return equals((Object[]) left, (List) right); } if (left instanceof List && right instanceof Object[]) { return equals((List) left, (Object[]) right); } if (left instanceof List && right instanceof List) { return equals((List) left, (List) right); } if (left instanceof Map.Entry && right instanceof Map.Entry) { Object k1 = ((Map.Entry) left).getKey(); Object k2 = ((Map.Entry) right).getKey(); if (k1 == k2 || (k1 != null && k1.equals(k2))) { Object v1 = ((Map.Entry) left).getValue(); Object v2 = ((Map.Entry) right).getValue(); if (v1 == v2 || (v1 != null && equals(v1, v2))) return true; } return false; } return left.equals(right); } }
TypeUtils { public static boolean equals(@Nullable Object left, @Nullable Object right) { if (left == right) return true; if (left == null || right == null) return false; Class<?> leftClass = left.getClass(); Class<?> rightClass = right.getClass(); if (leftClass.isArray() && rightClass.isArray()) { return arrayEqual(left, right); } if (leftClass.isArray() && leftClass.getComponentType().isPrimitive()) { left = primitiveArrayToList(left); } if (rightClass.isArray() && rightClass.getComponentType().isPrimitive()) { right = primitiveArrayToList(right); } if (left instanceof Object[] && right instanceof List) { return equals((Object[]) left, (List) right); } if (left instanceof List && right instanceof Object[]) { return equals((List) left, (Object[]) right); } if (left instanceof List && right instanceof List) { return equals((List) left, (List) right); } if (left instanceof Map.Entry && right instanceof Map.Entry) { Object k1 = ((Map.Entry) left).getKey(); Object k2 = ((Map.Entry) right).getKey(); if (k1 == k2 || (k1 != null && k1.equals(k2))) { Object v1 = ((Map.Entry) left).getValue(); Object v2 = ((Map.Entry) right).getValue(); if (v1 == v2 || (v1 != null && equals(v1, v2))) return true; } return false; } return left.equals(right); } private  TypeUtils(); }
TypeUtils { public static boolean equals(@Nullable Object left, @Nullable Object right) { if (left == right) return true; if (left == null || right == null) return false; Class<?> leftClass = left.getClass(); Class<?> rightClass = right.getClass(); if (leftClass.isArray() && rightClass.isArray()) { return arrayEqual(left, right); } if (leftClass.isArray() && leftClass.getComponentType().isPrimitive()) { left = primitiveArrayToList(left); } if (rightClass.isArray() && rightClass.getComponentType().isPrimitive()) { right = primitiveArrayToList(right); } if (left instanceof Object[] && right instanceof List) { return equals((Object[]) left, (List) right); } if (left instanceof List && right instanceof Object[]) { return equals((List) left, (Object[]) right); } if (left instanceof List && right instanceof List) { return equals((List) left, (List) right); } if (left instanceof Map.Entry && right instanceof Map.Entry) { Object k1 = ((Map.Entry) left).getKey(); Object k2 = ((Map.Entry) right).getKey(); if (k1 == k2 || (k1 != null && k1.equals(k2))) { Object v1 = ((Map.Entry) left).getValue(); Object v2 = ((Map.Entry) right).getValue(); if (v1 == v2 || (v1 != null && equals(v1, v2))) return true; } return false; } return left.equals(right); } 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); }
TypeUtils { public static boolean equals(@Nullable Object left, @Nullable Object right) { if (left == right) return true; if (left == null || right == null) return false; Class<?> leftClass = left.getClass(); Class<?> rightClass = right.getClass(); if (leftClass.isArray() && rightClass.isArray()) { return arrayEqual(left, right); } if (leftClass.isArray() && leftClass.getComponentType().isPrimitive()) { left = primitiveArrayToList(left); } if (rightClass.isArray() && rightClass.getComponentType().isPrimitive()) { right = primitiveArrayToList(right); } if (left instanceof Object[] && right instanceof List) { return equals((Object[]) left, (List) right); } if (left instanceof List && right instanceof Object[]) { return equals((List) left, (Object[]) right); } if (left instanceof List && right instanceof List) { return equals((List) left, (List) right); } if (left instanceof Map.Entry && right instanceof Map.Entry) { Object k1 = ((Map.Entry) left).getKey(); Object k2 = ((Map.Entry) right).getKey(); if (k1 == k2 || (k1 != null && k1.equals(k2))) { Object v1 = ((Map.Entry) left).getValue(); Object v2 = ((Map.Entry) right).getValue(); if (v1 == v2 || (v1 != null && equals(v1, v2))) return true; } return false; } return left.equals(right); } 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);  }