ObjectUtil { public static boolean isEqual(final Object... objects) { if (objects == null || objects.length < 2) { return false; } boolean previousNull = false; boolean previousNotNull = false; Object previousObject = null; for (final Object object : objects) { if (object == null) { if (previousNotNull) { return false; } previousNull = true; } else if (previousNull) { return false; } else { if (previousNotNull && !object.equals(previousObject)) { return false; } previousNotNull = true; previousObject = object; } } return true; } private  ObjectUtil(); static boolean isEqual(final Object... objects); static boolean isEqual(final Object object1, final Object object2);  }
@Test public void testIsEqualObjectArray() { assertFalse(ObjectUtil.isEqual(null)); assertFalse(ObjectUtil.isEqual(new Object[]{null})); assertFalse(ObjectUtil.isEqual(new Object[]{"d"})); assertFalse(ObjectUtil.isEqual("dfd")); assertTrue(ObjectUtil.isEqual(null, null, null)); assertFalse(ObjectUtil.isEqual("d", null, null)); assertFalse(ObjectUtil.isEqual(null, "dfd", null)); assertFalse(ObjectUtil.isEqual(null, null, "dfd")); String obj1 = "hi"; String obj2 = "hi"; String obj3 = "hi"; assertTrue(ObjectUtil.isEqual(obj1, obj2, obj3)); obj2 = "hidfd"; assertFalse(ObjectUtil.isEqual(obj1, obj2, obj3)); obj2 = obj1; obj3 = "dfdf"; assertFalse(ObjectUtil.isEqual(obj1, obj2, obj3)); obj3 = obj1; obj1 = "dfdasdfsdf"; assertFalse(ObjectUtil.isEqual(obj1, obj2, obj3)); obj3 = "dfdfdfdfddddddddd"; assertFalse(ObjectUtil.isEqual(obj1, obj2, obj3)); }
@Test public void testIsEqualObjectObject() { assertTrue(ObjectUtil.isEqual(null, null)); assertFalse(ObjectUtil.isEqual("df", null)); assertFalse(ObjectUtil.isEqual(null, "df")); String obj1 = "hi"; String obj2 = "hi"; assertTrue(ObjectUtil.isEqual(obj1, obj2)); assertTrue(ObjectUtil.isEqual("hi", "hi")); }