mirror of
https://github.com/apache/commons-lang.git
synced 2025-03-08 17:20:01 +00:00
Add identityToString
Make constructor public in line with NumberUtils and StringUtils Javadoc tidy git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137038 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
593e15d717
commit
a6dfea71c6
@ -61,22 +61,27 @@
|
|||||||
* @author <a href="mailto:janekdb@yahoo.co.uk">Janek Bogucki</a>
|
* @author <a href="mailto:janekdb@yahoo.co.uk">Janek Bogucki</a>
|
||||||
* @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
|
* @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
|
||||||
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
|
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
|
||||||
* @version $Id: ObjectUtils.java,v 1.1 2002/07/19 03:35:54 bayard Exp $
|
* @version $Id: ObjectUtils.java,v 1.2 2002/09/18 19:49:08 scolebourne Exp $
|
||||||
*/
|
*/
|
||||||
public class ObjectUtils {
|
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.
|
* Returns a default value if the object passed is null.
|
||||||
*
|
*
|
||||||
* @param object the object to test.
|
* @param object the object to test
|
||||||
* @param defaultValue the default value to return.
|
* @param defaultValue the default value to return
|
||||||
* @return object if it is not null, defaultValue otherwise.
|
* @return object if it is not null, defaultValue otherwise
|
||||||
*/
|
*/
|
||||||
public static Object defaultIfNull(Object object, Object defaultValue) {
|
public static Object defaultIfNull(Object object, Object defaultValue) {
|
||||||
return (object != null ? object : defaultValue);
|
return (object != null ? object : defaultValue);
|
||||||
@ -86,19 +91,36 @@ public static Object defaultIfNull(Object object, Object defaultValue) {
|
|||||||
* Compares two objects for equality, where either one or both
|
* Compares two objects for equality, where either one or both
|
||||||
* objects may be <code>null</code>.
|
* objects may be <code>null</code>.
|
||||||
*
|
*
|
||||||
* @param object1 the first object.
|
* @param object1 the first object
|
||||||
* @param object2 the second object.
|
* @param object2 the second object
|
||||||
* @return True if the values of both objects are the same.
|
* @return <code>true</code> if the values of both objects are the same
|
||||||
*/
|
*/
|
||||||
public static boolean equals(Object object1, Object object2) {
|
public static boolean equals(Object object1, Object object2) {
|
||||||
if (object1 == null) {
|
if (object1 == object2) {
|
||||||
return (object2 == null);
|
return true;
|
||||||
} else if (object2 == null) {
|
}
|
||||||
// object1 is not null
|
if ((object1 == null) || (object2 == null)) {
|
||||||
return false;
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user