@Test public void testCompareEqual() { assertTrue(ObjectUtils.compareEqual(null, null)); Object object = new Object(); assertTrue(ObjectUtils.compareEqual(object, object)); assertTrue(ObjectUtils.compareEqual("", "")); assertTrue(ObjectUtils.compareEqual("abc", String.valueOf("abc"))); assertTrue(ObjectUtils.compareEqual("abc", new String("abc"))); assertTrue(ObjectUtils.compareEqual("", null)); assertTrue(ObjectUtils.compareEqual(null, "")); assertFalse(ObjectUtils.compareEqual("", " ")); assertTrue(ObjectUtils.compareEqual(Long.valueOf(100), Integer.valueOf(100))); assertTrue(ObjectUtils.compareEqual(Float.valueOf(100), Integer.valueOf(100))); assertTrue(ObjectUtils.compareEqual(Float.valueOf(100.0123f), Double.valueOf(100.0123f))); assertFalse(ObjectUtils.compareEqual(Float.valueOf(100.0123f), Long.valueOf(100))); Date date = new Date(); Date dateEq = new Date(date.getTime()); Date dateDiff = new Date(123); assertTrue(ObjectUtils.compareEqual(date, dateEq)); assertFalse(ObjectUtils.compareEqual(date, dateDiff)); Object o1 = new Object(){ @Override public String toString() { return "JUnit"; } }; Object oEq = new Object(){ @Override public String toString() { return "JUnit"; } }; Object oDiff = new Object(){ @Override public String toString() { return "JUnitDiff"; } }; assertTrue(ObjectUtils.compareEqual(o1, oEq)); assertFalse(ObjectUtils.compareEqual(o1, oDiff)); }
@SuppressWarnings("unchecked") public static boolean compareEqual(Object o1, Object o2) { if (o1==o2) return true; if (isEmpty(o1)) return isEmpty(o2); if (isEmpty(o2)) return isEmpty(o1); if (o1.getClass().equals(o2.getClass())) { if (o1 instanceof Comparable) return (((Comparable<Object>)o1).compareTo(o2)==0); else return o1.equals(o2); } if (o1.equals(o2) || o2.equals(o1)) return true; if (o1 instanceof Number && o2 instanceof Number) { double d1 = ((Number)o1).doubleValue(); double d2 = ((Number)o2).doubleValue(); return (d1==d2); } if (o1.getClass().isEnum()) { if (o2 instanceof Number) return ((Enum<?>)o1).ordinal()==((Number)o2).intValue(); else if (o2 instanceof Character) return ((Enum<?>)o1).name().equals(o2.toString()); else return ((Enum<?>)o1).name().equals(o2); } else if (o2.getClass().isEnum()) { if (o1 instanceof Number) return ((Enum<?>)o2).ordinal()==((Number)o1).intValue(); else if (o1 instanceof Character) return ((Enum<?>)o2).name().equals(o1.toString()); else return ((Enum<?>)o2).name().equals(o1); } return o1.toString().equals(o2.toString()); }
ObjectUtils { @SuppressWarnings("unchecked") public static boolean compareEqual(Object o1, Object o2) { if (o1==o2) return true; if (isEmpty(o1)) return isEmpty(o2); if (isEmpty(o2)) return isEmpty(o1); if (o1.getClass().equals(o2.getClass())) { if (o1 instanceof Comparable) return (((Comparable<Object>)o1).compareTo(o2)==0); else return o1.equals(o2); } if (o1.equals(o2) || o2.equals(o1)) return true; if (o1 instanceof Number && o2 instanceof Number) { double d1 = ((Number)o1).doubleValue(); double d2 = ((Number)o2).doubleValue(); return (d1==d2); } if (o1.getClass().isEnum()) { if (o2 instanceof Number) return ((Enum<?>)o1).ordinal()==((Number)o2).intValue(); else if (o2 instanceof Character) return ((Enum<?>)o1).name().equals(o2.toString()); else return ((Enum<?>)o1).name().equals(o2); } else if (o2.getClass().isEnum()) { if (o1 instanceof Number) return ((Enum<?>)o2).ordinal()==((Number)o1).intValue(); else if (o1 instanceof Character) return ((Enum<?>)o2).name().equals(o1.toString()); else return ((Enum<?>)o2).name().equals(o1); } return o1.toString().equals(o2.toString()); } }
ObjectUtils { @SuppressWarnings("unchecked") public static boolean compareEqual(Object o1, Object o2) { if (o1==o2) return true; if (isEmpty(o1)) return isEmpty(o2); if (isEmpty(o2)) return isEmpty(o1); if (o1.getClass().equals(o2.getClass())) { if (o1 instanceof Comparable) return (((Comparable<Object>)o1).compareTo(o2)==0); else return o1.equals(o2); } if (o1.equals(o2) || o2.equals(o1)) return true; if (o1 instanceof Number && o2 instanceof Number) { double d1 = ((Number)o1).doubleValue(); double d2 = ((Number)o2).doubleValue(); return (d1==d2); } if (o1.getClass().isEnum()) { if (o2 instanceof Number) return ((Enum<?>)o1).ordinal()==((Number)o2).intValue(); else if (o2 instanceof Character) return ((Enum<?>)o1).name().equals(o2.toString()); else return ((Enum<?>)o1).name().equals(o2); } else if (o2.getClass().isEnum()) { if (o1 instanceof Number) return ((Enum<?>)o2).ordinal()==((Number)o1).intValue(); else if (o1 instanceof Character) return ((Enum<?>)o2).name().equals(o1.toString()); else return ((Enum<?>)o2).name().equals(o1); } return o1.toString().equals(o2.toString()); } private  ObjectUtils(); }
ObjectUtils { @SuppressWarnings("unchecked") public static boolean compareEqual(Object o1, Object o2) { if (o1==o2) return true; if (isEmpty(o1)) return isEmpty(o2); if (isEmpty(o2)) return isEmpty(o1); if (o1.getClass().equals(o2.getClass())) { if (o1 instanceof Comparable) return (((Comparable<Object>)o1).compareTo(o2)==0); else return o1.equals(o2); } if (o1.equals(o2) || o2.equals(o1)) return true; if (o1 instanceof Number && o2 instanceof Number) { double d1 = ((Number)o1).doubleValue(); double d2 = ((Number)o2).doubleValue(); return (d1==d2); } if (o1.getClass().isEnum()) { if (o2 instanceof Number) return ((Enum<?>)o1).ordinal()==((Number)o2).intValue(); else if (o2 instanceof Character) return ((Enum<?>)o1).name().equals(o2.toString()); else return ((Enum<?>)o1).name().equals(o2); } else if (o2.getClass().isEnum()) { if (o1 instanceof Number) return ((Enum<?>)o2).ordinal()==((Number)o1).intValue(); else if (o1 instanceof Character) return ((Enum<?>)o2).name().equals(o1.toString()); else return ((Enum<?>)o2).name().equals(o1); } return o1.toString().equals(o2.toString()); } private  ObjectUtils(); static boolean isEmpty(Object o); static int lengthOf(Object o); @SuppressWarnings("unchecked") static boolean compareEqual(Object o1, Object o2); static boolean compareEqual(Object[] array1, Object[] array2); static T coalesce(T preferred, T alternative); static int toInteger(Object v); static int getInteger(Object v, int defValue); static int getInteger(Object v); static long toLong(Object v); static long getLong(Object v, long defValue); static long getLong(Object v); static double toDouble(Object v); static double getDouble(Object v, double defValue); static double getDouble(Object v); static BigDecimal toDecimal(Object v); static BigDecimal getDecimal(Object v, BigDecimal defValue); static BigDecimal getDecimal(Object v); static boolean getBoolean(Object v); @SuppressWarnings("unchecked") static T getEnum(Class<T> enumType, Object value); static Date getDate(Object v, Locale locale); static Date getDate(Object v); static String formatDate(Date date, boolean withTime); @SuppressWarnings("unchecked") static T convert(Class<T> c, Object v); static boolean isAssignmentCompatible(Class<?> target, Class<?> source); static List<T> convert(Class<T> t, Collection<? extends T> source); static String[] toStringArray(Object[] objArray, String defValue); static boolean contains(T[] array, T item); static synchronized Object getFieldValue(Class<?> clazz, Object object, String property, boolean includePrivateFields); static Object getFieldValue(Object object, String property); static Object getPrivateFieldValue(Object object, String property); static synchronized Object invokeSimpleMethod(Class<?> clazz, Object object, String methodName, boolean includePrivateMethods); static Object invokeSimpleMethod(Object object, String methodName); static Object invokeSimplePrivateMethod(Object object, String methodName); }
ObjectUtils { @SuppressWarnings("unchecked") public static boolean compareEqual(Object o1, Object o2) { if (o1==o2) return true; if (isEmpty(o1)) return isEmpty(o2); if (isEmpty(o2)) return isEmpty(o1); if (o1.getClass().equals(o2.getClass())) { if (o1 instanceof Comparable) return (((Comparable<Object>)o1).compareTo(o2)==0); else return o1.equals(o2); } if (o1.equals(o2) || o2.equals(o1)) return true; if (o1 instanceof Number && o2 instanceof Number) { double d1 = ((Number)o1).doubleValue(); double d2 = ((Number)o2).doubleValue(); return (d1==d2); } if (o1.getClass().isEnum()) { if (o2 instanceof Number) return ((Enum<?>)o1).ordinal()==((Number)o2).intValue(); else if (o2 instanceof Character) return ((Enum<?>)o1).name().equals(o2.toString()); else return ((Enum<?>)o1).name().equals(o2); } else if (o2.getClass().isEnum()) { if (o1 instanceof Number) return ((Enum<?>)o2).ordinal()==((Number)o1).intValue(); else if (o1 instanceof Character) return ((Enum<?>)o2).name().equals(o1.toString()); else return ((Enum<?>)o2).name().equals(o1); } return o1.toString().equals(o2.toString()); } private  ObjectUtils(); static boolean isEmpty(Object o); static int lengthOf(Object o); @SuppressWarnings("unchecked") static boolean compareEqual(Object o1, Object o2); static boolean compareEqual(Object[] array1, Object[] array2); static T coalesce(T preferred, T alternative); static int toInteger(Object v); static int getInteger(Object v, int defValue); static int getInteger(Object v); static long toLong(Object v); static long getLong(Object v, long defValue); static long getLong(Object v); static double toDouble(Object v); static double getDouble(Object v, double defValue); static double getDouble(Object v); static BigDecimal toDecimal(Object v); static BigDecimal getDecimal(Object v, BigDecimal defValue); static BigDecimal getDecimal(Object v); static boolean getBoolean(Object v); @SuppressWarnings("unchecked") static T getEnum(Class<T> enumType, Object value); static Date getDate(Object v, Locale locale); static Date getDate(Object v); static String formatDate(Date date, boolean withTime); @SuppressWarnings("unchecked") static T convert(Class<T> c, Object v); static boolean isAssignmentCompatible(Class<?> target, Class<?> source); static List<T> convert(Class<T> t, Collection<? extends T> source); static String[] toStringArray(Object[] objArray, String defValue); static boolean contains(T[] array, T item); static synchronized Object getFieldValue(Class<?> clazz, Object object, String property, boolean includePrivateFields); static Object getFieldValue(Object object, String property); static Object getPrivateFieldValue(Object object, String property); static synchronized Object invokeSimpleMethod(Class<?> clazz, Object object, String methodName, boolean includePrivateMethods); static Object invokeSimpleMethod(Object object, String methodName); static Object invokeSimplePrivateMethod(Object object, String methodName); static final NoValue NO_VALUE; }