From 66e42dc8b43d37ff011de69586f8e6764c4b3164 Mon Sep 17 00:00:00 2001 From: Niall Pemberton Date: Mon, 10 Jan 2011 23:10:16 +0000 Subject: [PATCH] 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 --- .../org/apache/commons/lang3/ObjectUtils.java | 23 +++++++++++++++++++ .../apache/commons/lang3/ObjectUtilsTest.java | 8 +++++++ 2 files changed, 31 insertions(+) diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java b/src/main/java/org/apache/commons/lang3/ObjectUtils.java index a0c132954..015a829ad 100644 --- a/src/main/java/org/apache/commons/lang3/ObjectUtils.java +++ b/src/main/java/org/apache/commons/lang3/ObjectUtils.java @@ -158,6 +158,29 @@ public class ObjectUtils { return object1.equals(object2); } + /** + *

Compares two objects for inequality, where either one or both + * objects may be null.

+ * + *
+     * 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
+     * 
+ * + * @param object1 the first object, may be null + * @param object2 the second object, may be null + * @return false if the values of both objects are the same + */ + public static boolean notEqual(Object object1, Object object2) { + return ObjectUtils.equals(object1, object2) == false; + } + /** *

Gets the hash code of an object returning zero when the * object is null.

diff --git a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java index 01ba19721..2e22a9f91 100644 --- a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java @@ -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"));