LANG-670 Add notEqual() method to ObjectUtils

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1057410 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Niall Pemberton 2011-01-10 23:10:16 +00:00
parent 8d24c1fc62
commit 66e42dc8b4
2 changed files with 31 additions and 0 deletions

View File

@ -158,6 +158,29 @@ public class ObjectUtils {
return object1.equals(object2);
}
/**
* <p>Compares two objects for inequality, where either one or both
* objects may be <code>null</code>.</p>
*
* <pre>
* ObjectUtils.notEqual(null, null) = false
* ObjectUtils.notEqual(null, "") = true
* ObjectUtils.notEqual("", null) = true
* ObjectUtils.notEqual("", "") = false
* ObjectUtils.notEqual(Boolean.TRUE, null) = true
* ObjectUtils.notEqual(Boolean.TRUE, "true") = true
* ObjectUtils.notEqual(Boolean.TRUE, Boolean.TRUE) = false
* ObjectUtils.notEqual(Boolean.TRUE, Boolean.FALSE) = true
* </pre>
*
* @param object1 the first object, may be <code>null</code>
* @param object2 the second object, may be <code>null</code>
* @return <code>false</code> if the values of both objects are the same
*/
public static boolean notEqual(Object object1, Object object2) {
return ObjectUtils.equals(object1, object2) == false;
}
/**
* <p>Gets the hash code of an object returning zero when the
* object is <code>null</code>.</p>

View File

@ -88,6 +88,14 @@ public class ObjectUtilsTest extends TestCase {
assertTrue("ObjectUtils.equals(\"foo\", \"foo\") returned false", ObjectUtils.equals(FOO, FOO));
}
public void testNotEqual() {
assertFalse("ObjectUtils.notEqual(null, null) returned false", ObjectUtils.notEqual(null, null));
assertTrue("ObjectUtils.notEqual(\"foo\", null) returned true", ObjectUtils.notEqual(FOO, null));
assertTrue("ObjectUtils.notEqual(null, \"bar\") returned true", ObjectUtils.notEqual(null, BAR));
assertTrue("ObjectUtils.notEqual(\"foo\", \"bar\") returned true", ObjectUtils.notEqual(FOO, BAR));
assertFalse("ObjectUtils.notEqual(\"foo\", \"foo\") returned false", ObjectUtils.notEqual(FOO, FOO));
}
public void testHashCode() {
assertEquals(0, ObjectUtils.hashCode(null));
assertEquals("a".hashCode(), ObjectUtils.hashCode("a"));