You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2002/09/18 21:49:08 UTC
cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang ObjectUtils.java
scolebourne 2002/09/18 12:49:08
Modified: lang/src/java/org/apache/commons/lang ObjectUtils.java
Log:
Add identityToString
Make constructor public in line with NumberUtils and StringUtils
Javadoc tidy
Revision Changes Path
1.2 +37 -15 jakarta-commons/lang/src/java/org/apache/commons/lang/ObjectUtils.java
Index: ObjectUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/ObjectUtils.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ObjectUtils.java 19 Jul 2002 03:35:54 -0000 1.1
+++ ObjectUtils.java 18 Sep 2002 19:49:08 -0000 1.2
@@ -66,17 +66,22 @@
public class ObjectUtils {
/**
- * Prevent construction of ObjectUtils instances
+ * ObjectUtils instances should NOT be constructed in standard programming.
+ * Instead, the class should be used as <code>ObjectUtils.defaultIfNull("a","b");</code>.
+ * This constructor is public to permit tools that require a JavaBean instance
+ * to operate.
*/
- private ObjectUtils() {
+ public ObjectUtils() {
}
+ //--------------------------------------------------------------------
+
/**
* Returns a default value if the object passed is null.
*
- * @param object the object to test.
- * @param defaultValue the default value to return.
- * @return object if it is not null, defaultValue otherwise.
+ * @param object the object to test
+ * @param defaultValue the default value to return
+ * @return object if it is not null, defaultValue otherwise
*/
public static Object defaultIfNull(Object object, Object defaultValue) {
return (object != null ? object : defaultValue);
@@ -86,19 +91,36 @@
* Compares two objects for equality, where either one or both
* objects may be <code>null</code>.
*
- * @param object1 the first object.
- * @param object2 the second object.
- * @return True if the values of both objects are the same.
+ * @param object1 the first object
+ * @param object2 the second object
+ * @return <code>true</code> if the values of both objects are the same
*/
public static boolean equals(Object object1, Object object2) {
- if (object1 == null) {
- return (object2 == null);
- } else if (object2 == null) {
- // object1 is not null
+ if (object1 == object2) {
+ return true;
+ }
+ if ((object1 == null) || (object2 == null)) {
return false;
- } else {
- return object1.equals(object2);
}
+ return object1.equals(object2);
+ }
+
+ /**
+ * Gets the toString that would be produced by Object if a class did not
+ * override toString itself. Null will return null.
+ *
+ * @param object the object to create a toString for, may be null
+ * @return the default toString text, or null if null passed in
+ */
+ public static String identityToString(Object object) {
+ if (object == null) {
+ return null;
+ }
+ return new StringBuffer()
+ .append(object.getClass().getName())
+ .append('@')
+ .append(Integer.toHexString(System.identityHashCode(object)))
+ .toString();
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>