Javadoc and tests for ObjectUtils

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137494 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-07-25 22:37:59 +00:00
parent 7ff6e3a30f
commit a303646251
2 changed files with 52 additions and 12 deletions

View File

@ -68,7 +68,7 @@ import java.io.Serializable;
* @author Stephen Colebourne
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @since 1.0
* @version $Id: ObjectUtils.java,v 1.13 2003/07/20 15:41:52 scolebourne Exp $
* @version $Id: ObjectUtils.java,v 1.14 2003/07/25 22:37:58 scolebourne Exp $
*/
public class ObjectUtils {
@ -105,6 +105,14 @@ public class ObjectUtils {
/**
* <p>Returns a default value if the object passed is
* <code>null</code>.</p>
*
* <pre>
* ObjectUtils.defaultIfNull(null, null) = null
* ObjectUtils.defaultIfNull(null, "") = ""
* ObjectUtils.defaultIfNull(null, "zz") = "zz"
* ObjectUtils.defaultIfNull("abc", *) = "abc"
* ObjectUtils.defaultIfNull(Boolean.TRUE, *) = Boolean.TRUE
* </pre>
*
* @param object the <code>Object</code> to test, may be <code>null</code>
* @param defaultValue the default value to return, may be <code>null</code>
@ -118,6 +126,17 @@ public class ObjectUtils {
* <p>Compares two objects for equality, where either one or both
* objects may be <code>null</code>.</p>
*
* <pre>
* 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
* </pre>
*
* @param object1 the first object, may be <code>null</code>
* @param object2 the second object, may be <code>null</code>
* @return <code>true</code> if the values of both objects are the same
@ -138,6 +157,12 @@ public class ObjectUtils {
* if a class did not override toString itself. <code>null</code>
* will return <code>null</code>.</p>
*
* <pre>
* ObjectUtils.identityToString(null) = null
* ObjectUtils.identityToString("") = "java.lang.String@1e23"
* ObjectUtils.identityToString(Boolean.TRUE) = "java.lang.Boolean@7fa"
* </pre>
*
* @param object the object to create a toString for, may be
* <code>null</code>
* @return the default toString text, or <code>null</code> 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. <code>null</code>
* will return <code>null</code>.</p>
*
* @param buffer the buffer to append to, may not be
* <code>null</code>
* @param object the object to create a toString for, may be
* <code>null</code>
* <pre>
* 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")
* </pre>
*
* @param buffer the buffer to append to, may be <code>null</code>
* @param object the object to create a toString for, may be <code>null</code>
* @return the default toString text, or <code>null</code> if
* <code>null</code> 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('@')

View File

@ -65,7 +65,7 @@ import junit.textui.TestRunner;
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @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() {