public class ObjectUtils
extends java.lang.Object
Operations on Object
.
This class tries to handle null
input gracefully.
An exception will generally not be thrown for a null
input.
Each method documents its behaviour in more detail.
#ThreadSafe#
Modifier and Type | Class and Description |
---|---|
static class |
ObjectUtils.Null
Class used as a null placeholder where
null
has another meaning. |
Modifier and Type | Field and Description |
---|---|
static ObjectUtils.Null |
NULL
Singleton used as a
null placeholder where
null has another meaning. |
Constructor and Description |
---|
ObjectUtils()
ObjectUtils instances should NOT be constructed in
standard programming. |
Modifier and Type | Method and Description |
---|---|
static java.lang.StringBuffer |
appendIdentityToString(java.lang.StringBuffer buffer,
java.lang.Object object)
Deprecated.
The design of this method is bad - see LANG-360. Instead, use identityToString(StringBuffer, Object).
|
static java.lang.Object |
clone(java.lang.Object o)
Clone an object.
|
static java.lang.Object |
cloneIfPossible(java.lang.Object o)
Clone an object if possible.
|
static int |
compare(java.lang.Comparable c1,
java.lang.Comparable c2)
Null safe comparison of Comparables.
|
static int |
compare(java.lang.Comparable c1,
java.lang.Comparable c2,
boolean nullGreater)
Null safe comparison of Comparables.
|
static java.lang.Object |
defaultIfNull(java.lang.Object object,
java.lang.Object defaultValue)
Returns a default value if the object passed is
null . |
static boolean |
equals(java.lang.Object object1,
java.lang.Object object2)
Compares two objects for equality, where either one or both
objects may be
null . |
static int |
hashCode(java.lang.Object obj)
Gets the hash code of an object returning zero when the
object is
null . |
static java.lang.String |
identityToString(java.lang.Object object)
Gets the toString that would be produced by
Object
if a class did not override toString itself. |
static void |
identityToString(java.lang.StringBuffer buffer,
java.lang.Object object)
Appends the toString that would be produced by
Object
if a class did not override toString itself. |
static java.lang.Object |
max(java.lang.Comparable c1,
java.lang.Comparable c2)
Null safe comparison of Comparables.
|
static java.lang.Object |
min(java.lang.Comparable c1,
java.lang.Comparable c2)
Null safe comparison of Comparables.
|
static boolean |
notEqual(java.lang.Object object1,
java.lang.Object object2)
Compares two objects for inequality, where either one or both
objects may be
null . |
static java.lang.String |
toString(java.lang.Object obj)
Gets the
toString of an Object returning
an empty string ("") if null input. |
static java.lang.String |
toString(java.lang.Object obj,
java.lang.String nullStr)
Gets the
toString of an Object returning
a specified text if null input. |
public static final ObjectUtils.Null NULL
Singleton used as a null
placeholder where
null
has another meaning.
For example, in a HashMap
the
HashMap.get(java.lang.Object)
method returns
null
if the Map
contains
null
or if there is no matching key. The
Null
placeholder can be used to distinguish between
these two cases.
Another example is Hashtable
, where null
cannot be stored.
This instance is Serializable.
public ObjectUtils()
ObjectUtils
instances should NOT be constructed in
standard programming. Instead, the class should be used as
ObjectUtils.defaultIfNull("a","b");
.
This constructor is public to permit tools that require a JavaBean instance to operate.
public static java.lang.Object defaultIfNull(java.lang.Object object, java.lang.Object defaultValue)
Returns a default value if the object passed is
null
.
ObjectUtils.defaultIfNull(null, null) = null ObjectUtils.defaultIfNull(null, "") = "" ObjectUtils.defaultIfNull(null, "zz") = "zz" ObjectUtils.defaultIfNull("abc", *) = "abc" ObjectUtils.defaultIfNull(Boolean.TRUE, *) = Boolean.TRUE
object
- the Object
to test, may be null
defaultValue
- the default value to return, may be null
object
if it is not null
, defaultValue otherwisepublic static boolean equals(java.lang.Object object1, java.lang.Object object2)
Compares two objects for equality, where either one or both
objects may be 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
object1
- the first object, may be null
object2
- the second object, may be null
true
if the values of both objects are the samepublic static boolean notEqual(java.lang.Object object1, java.lang.Object 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
object1
- the first object, may be null
object2
- the second object, may be null
false
if the values of both objects are the samepublic static int hashCode(java.lang.Object obj)
Gets the hash code of an object returning zero when the
object is null
.
ObjectUtils.hashCode(null) = 0 ObjectUtils.hashCode(obj) = obj.hashCode()
obj
- the object to obtain the hash code of, may be null
public static java.lang.String identityToString(java.lang.Object object)
Gets the toString that would be produced by Object
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"
object
- the object to create a toString for, may be
null
null
if
null
passed inpublic static void identityToString(java.lang.StringBuffer buffer, java.lang.Object object)
Appends the toString that would be produced by Object
if a class did not override toString itself. null
will throw a NullPointerException for either of the two parameters.
ObjectUtils.identityToString(buf, "") = buf.append("java.lang.String@1e23" ObjectUtils.identityToString(buf, Boolean.TRUE) = buf.append("java.lang.Boolean@7fa" ObjectUtils.identityToString(buf, Boolean.TRUE) = buf.append("java.lang.Boolean@7fa")
buffer
- the buffer to append toobject
- the object to create a toString forpublic static java.lang.StringBuffer appendIdentityToString(java.lang.StringBuffer buffer, java.lang.Object object)
Appends the toString that would be produced by Object
if a class did not override toString itself. null
will return 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")
buffer
- the buffer to append to, may be null
object
- the object to create a toString for, may be null
null
if
null
passed inpublic static java.lang.String toString(java.lang.Object obj)
Gets the toString
of an Object
returning
an empty string ("") if null
input.
ObjectUtils.toString(null) = "" ObjectUtils.toString("") = "" ObjectUtils.toString("bat") = "bat" ObjectUtils.toString(Boolean.TRUE) = "true"
obj
- the Object to toString
, may be nullnull
inputStringUtils.defaultString(String)
,
String.valueOf(Object)
public static java.lang.String toString(java.lang.Object obj, java.lang.String nullStr)
Gets the toString
of an Object
returning
a specified text if null
input.
ObjectUtils.toString(null, null) = null ObjectUtils.toString(null, "null") = "null" ObjectUtils.toString("", "null") = "" ObjectUtils.toString("bat", "null") = "bat" ObjectUtils.toString(Boolean.TRUE, "null") = "true"
obj
- the Object to toString
, may be nullnullStr
- the String to return if null
input, may be nullnull
inputStringUtils.defaultString(String,String)
,
String.valueOf(Object)
public static java.lang.Object min(java.lang.Comparable c1, java.lang.Comparable c2)
c1
- the first comparable, may be nullc2
- the second comparable, may be nullpublic static java.lang.Object max(java.lang.Comparable c1, java.lang.Comparable c2)
c1
- the first comparable, may be nullc2
- the second comparable, may be nullpublic static int compare(java.lang.Comparable c1, java.lang.Comparable c2)
null
is assumed to be less than a non-null
value.c1
- the first comparable, may be nullc2
- the second comparable, may be nullpublic static int compare(java.lang.Comparable c1, java.lang.Comparable c2, boolean nullGreater)
c1
- the first comparable, may be nullc2
- the second comparable, may be nullnullGreater
- if true null
is considered greater
than a Non-null
value or if false null
is
considered less than a Non-null
valueComparator.compare(Object, Object)
public static java.lang.Object clone(java.lang.Object o)
o
- the object to cloneCloneable
otherwise null
CloneFailedException
- if the object is cloneable and the clone operation failspublic static java.lang.Object cloneIfPossible(java.lang.Object o)
clone(Object)
, but will
return the provided instance as the return value instead of null
if the instance
is not cloneable. This is more convenient if the caller uses different
implementations (e.g. of a service) and some of the implementations do not allow concurrent
processing or have state. In such cases the implementation can simply provide a proper
clone implementation and the caller's code does not have to change.o
- the object to cloneCloneable
otherwise the object itselfCloneFailedException
- if the object is cloneable and the clone operation fails"Copyright © 2010 - 2020 Adobe Systems Incorporated. All Rights Reserved"