From a303646251526840329bfe81d0c5d7bc306f43c1 Mon Sep 17 00:00:00 2001
From: Stephen Colebourne Returns a default value if the object passed is
* Compares two objects for equality, where either one or both
* objects may be null
.
+ * ObjectUtils.defaultIfNull(null, null) = null
+ * ObjectUtils.defaultIfNull(null, "") = ""
+ * ObjectUtils.defaultIfNull(null, "zz") = "zz"
+ * ObjectUtils.defaultIfNull("abc", *) = "abc"
+ * ObjectUtils.defaultIfNull(Boolean.TRUE, *) = Boolean.TRUE
+ *
*
* @param object the Object
to test, may be null
* @param defaultValue the default value to return, may be null
@@ -118,6 +126,17 @@ public class ObjectUtils {
* null
.
+ * ObjectUtils.equals(null, null) = true
+ * ObjectUtils.equals(null, "") = false
+ * ObjectUtils.equals("", null) = false
+ * ObjectUtils.equals("", "") = true
+ * ObjectUtils.equals(Boolean.TRUE, null) = false
+ * ObjectUtils.equals(Boolean.TRUE, "true") = false
+ * ObjectUtils.equals(Boolean.TRUE, Boolean.TRUE) = true
+ * ObjectUtils.equals(Boolean.TRUE, Boolean.FALSE) = false
+ *
+ *
* @param object1 the first object, may be null
* @param object2 the second object, may be null
* @return true
if the values of both objects are the same
@@ -138,6 +157,12 @@ public class ObjectUtils {
* if a class did not override toString itself. null
* will return null
.
+ * ObjectUtils.identityToString(null) = null + * ObjectUtils.identityToString("") = "java.lang.String@1e23" + * ObjectUtils.identityToString(Boolean.TRUE) = "java.lang.Boolean@7fa" + *+ * * @param object the object to create a toString for, may be *
null
* @return the default toString text, or null
if
@@ -147,7 +172,7 @@ public class ObjectUtils {
if (object == null) {
return null;
}
- return appendIdentityToString(new StringBuffer(), object).toString();
+ return appendIdentityToString(null, object).toString();
}
/**
@@ -155,10 +180,15 @@ public class ObjectUtils {
* if a class did not override toString itself. null
* will return null
.
*
- * @param buffer the buffer to append to, may not be
- * null
- * @param object the object to create a toString for, may be
- * null
+ * + * ObjectUtils.appendIdentityToString(*, null) = null + * ObjectUtils.appendIdentityToString(null, "") = "java.lang.String@1e23" + * ObjectUtils.appendIdentityToString(null, Boolean.TRUE) = "java.lang.Boolean@7fa" + * ObjectUtils.appendIdentityToString(buf, Boolean.TRUE) = buf.append("java.lang.Boolean@7fa") + *+ * + * @param buffer the buffer to append to, may be
null
+ * @param object the object to create a toString for, may be null
* @return the default toString text, or null
if
* null
passed in
*/
@@ -166,6 +196,9 @@ public class ObjectUtils {
if (object == null) {
return null;
}
+ if (buffer == null) {
+ buffer = new StringBuffer();
+ }
return buffer
.append(object.getClass().getName())
.append('@')
diff --git a/src/test/org/apache/commons/lang/ObjectUtilsTest.java b/src/test/org/apache/commons/lang/ObjectUtilsTest.java
index f47575f38..04b6f9861 100644
--- a/src/test/org/apache/commons/lang/ObjectUtilsTest.java
+++ b/src/test/org/apache/commons/lang/ObjectUtilsTest.java
@@ -65,7 +65,7 @@ import junit.textui.TestRunner;
* @author Stephen Colebourne
* @author Ringo De Smet
* @author Gary Gregory
- * @version $Id: ObjectUtilsTest.java,v 1.6 2003/07/20 15:41:52 scolebourne Exp $
+ * @version $Id: ObjectUtilsTest.java,v 1.7 2003/07/25 22:37:59 scolebourne Exp $
*/
public class ObjectUtilsTest extends TestCase {
private static final String FOO = "foo";
@@ -111,6 +111,7 @@ public class ObjectUtilsTest extends TestCase {
}
public void testIdentityToString() {
+ assertEquals(null, ObjectUtils.identityToString(null));
assertEquals(
"java.lang.String@" + Integer.toHexString(System.identityHashCode(FOO)),
ObjectUtils.identityToString(FOO));
@@ -118,18 +119,24 @@ public class ObjectUtilsTest extends TestCase {
assertEquals(
"java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i)),
ObjectUtils.identityToString(i));
- assertEquals(null, ObjectUtils.identityToString(null));
}
public void testAppendIdentityToString() {
+ assertEquals(null, ObjectUtils.appendIdentityToString(null, null));
+ assertEquals(null, ObjectUtils.appendIdentityToString(new StringBuffer(), null));
+ assertEquals(
+ "java.lang.String@" + Integer.toHexString(System.identityHashCode(FOO)),
+ ObjectUtils.appendIdentityToString(null, FOO).toString());
assertEquals(
"java.lang.String@" + Integer.toHexString(System.identityHashCode(FOO)),
ObjectUtils.appendIdentityToString(new StringBuffer(), FOO).toString());
- Integer i = new Integer(90);
+ Integer val = new Integer(90);
assertEquals(
- "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i)),
- ObjectUtils.appendIdentityToString(new StringBuffer(), i).toString());
- assertEquals(null, ObjectUtils.appendIdentityToString(new StringBuffer(), null));
+ "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(val)),
+ ObjectUtils.appendIdentityToString(null, val).toString());
+ assertEquals(
+ "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(val)),
+ ObjectUtils.appendIdentityToString(new StringBuffer(), val).toString());
}
public void testToString_Object() {