Class ObjectUtils
- java.lang.Object
-
- org.apache.commons.lang.ObjectUtils
-
@Deprecated(since="2021-04-30") public class ObjectUtils extends java.lang.Object
Deprecated.Operations on
Object
.This class tries to handle
null
input gracefully. An exception will generally not be thrown for anull
input. Each method documents its behaviour in more detail.#ThreadSafe#
- Since:
- 1.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ObjectUtils.Null
Deprecated.Commons Lang 2 is in maintenance mode.
-
Field Summary
Fields Modifier and Type Field Description static ObjectUtils.Null
NULL
Deprecated.Singleton used as anull
placeholder wherenull
has another meaning.
-
Constructor Summary
Constructors Constructor Description ObjectUtils()
Deprecated.ObjectUtils
instances should NOT be constructed in standard programming.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method 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.static java.lang.Object
clone(java.lang.Object o)
Deprecated.Clone an object.static java.lang.Object
cloneIfPossible(java.lang.Object o)
Deprecated.Clone an object if possible.static int
compare(java.lang.Comparable c1, java.lang.Comparable c2)
Deprecated.Null safe comparison of Comparables.static int
compare(java.lang.Comparable c1, java.lang.Comparable c2, boolean nullGreater)
Deprecated.Null safe comparison of Comparables.static java.lang.Object
defaultIfNull(java.lang.Object object, java.lang.Object defaultValue)
Deprecated.Returns a default value if the object passed isnull
.static boolean
equals(java.lang.Object object1, java.lang.Object object2)
Deprecated.Compares two objects for equality, where either one or both objects may benull
.static int
hashCode(java.lang.Object obj)
Deprecated.Gets the hash code of an object returning zero when the object isnull
.static java.lang.String
identityToString(java.lang.Object object)
Deprecated.Gets the toString that would be produced byObject
if a class did not override toString itself.static void
identityToString(java.lang.StringBuffer buffer, java.lang.Object object)
Deprecated.Appends the toString that would be produced byObject
if a class did not override toString itself.static java.lang.Object
max(java.lang.Comparable c1, java.lang.Comparable c2)
Deprecated.Null safe comparison of Comparables.static java.lang.Object
min(java.lang.Comparable c1, java.lang.Comparable c2)
Deprecated.Null safe comparison of Comparables.static boolean
notEqual(java.lang.Object object1, java.lang.Object object2)
Deprecated.Compares two objects for inequality, where either one or both objects may benull
.static java.lang.String
toString(java.lang.Object obj)
Deprecated.Gets thetoString
of anObject
returning an empty string ("") ifnull
input.static java.lang.String
toString(java.lang.Object obj, java.lang.String nullStr)
Deprecated.Gets thetoString
of anObject
returning a specified text ifnull
input.
-
-
-
Field Detail
-
NULL
public static final ObjectUtils.Null NULL
Deprecated.Singleton used as a
null
placeholder wherenull
has another meaning.For example, in a
HashMap
theHashMap.get(java.lang.Object)
method returnsnull
if theMap
containsnull
or if there is no matching key. TheNull
placeholder can be used to distinguish between these two cases.Another example is
Hashtable
, wherenull
cannot be stored.This instance is Serializable.
-
-
Method Detail
-
defaultIfNull
public static java.lang.Object defaultIfNull(java.lang.Object object, java.lang.Object defaultValue)
Deprecated.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
- Parameters:
object
- theObject
to test, may benull
defaultValue
- the default value to return, may benull
- Returns:
object
if it is notnull
, defaultValue otherwise
-
equals
public static boolean equals(java.lang.Object object1, java.lang.Object object2)
Deprecated.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
- Parameters:
object1
- the first object, may benull
object2
- the second object, may benull
- Returns:
true
if the values of both objects are the same
-
notEqual
public static boolean notEqual(java.lang.Object object1, java.lang.Object object2)
Deprecated.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
- Parameters:
object1
- the first object, may benull
object2
- the second object, may benull
- Returns:
false
if the values of both objects are the same- Since:
- 2.6
-
hashCode
public static int hashCode(java.lang.Object obj)
Deprecated.Gets the hash code of an object returning zero when the object is
null
.ObjectUtils.hashCode(null) = 0 ObjectUtils.hashCode(obj) = obj.hashCode()
- Parameters:
obj
- the object to obtain the hash code of, may benull
- Returns:
- the hash code of the object, or zero if null
- Since:
- 2.1
-
identityToString
public static java.lang.String identityToString(java.lang.Object object)
Deprecated.Gets the toString that would be produced by
Object
if a class did not override toString itself.null
will returnnull
.ObjectUtils.identityToString(null) = null ObjectUtils.identityToString("") = "java.lang.String@1e23" ObjectUtils.identityToString(Boolean.TRUE) = "java.lang.Boolean@7fa"
- Parameters:
object
- the object to create a toString for, may benull
- Returns:
- the default toString text, or
null
ifnull
passed in
-
identityToString
public static void identityToString(java.lang.StringBuffer buffer, java.lang.Object object)
Deprecated.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")
- Parameters:
buffer
- the buffer to append toobject
- the object to create a toString for- Since:
- 2.4
-
appendIdentityToString
public 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).Appends the toString that would be produced by
Object
if a class did not override toString itself.null
will returnnull
.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")
- Parameters:
buffer
- the buffer to append to, may benull
object
- the object to create a toString for, may benull
- Returns:
- the default toString text, or
null
ifnull
passed in - Since:
- 2.0
-
toString
public static java.lang.String toString(java.lang.Object obj)
Deprecated.Gets the
toString
of anObject
returning an empty string ("") ifnull
input.ObjectUtils.toString(null) = "" ObjectUtils.toString("") = "" ObjectUtils.toString("bat") = "bat" ObjectUtils.toString(Boolean.TRUE) = "true"
- Parameters:
obj
- the Object totoString
, may be null- Returns:
- the passed in Object's toString, or nullStr if
null
input - Since:
- 2.0
- See Also:
StringUtils.defaultString(String)
,String.valueOf(Object)
-
toString
public static java.lang.String toString(java.lang.Object obj, java.lang.String nullStr)
Deprecated.Gets the
toString
of anObject
returning a specified text ifnull
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"
- Parameters:
obj
- the Object totoString
, may be nullnullStr
- the String to return ifnull
input, may be null- Returns:
- the passed in Object's toString, or nullStr if
null
input - Since:
- 2.0
- See Also:
StringUtils.defaultString(String,String)
,String.valueOf(Object)
-
min
public static java.lang.Object min(java.lang.Comparable c1, java.lang.Comparable c2)
Deprecated.Null safe comparison of Comparables.- Parameters:
c1
- the first comparable, may be nullc2
- the second comparable, may be null- Returns:
- If both objects are non-null and unequal, the lesser object.
- If both objects are non-null and equal, c1.
- If one of the comparables is null, the non-null object.
- If both the comparables are null, null is returned.
-
max
public static java.lang.Object max(java.lang.Comparable c1, java.lang.Comparable c2)
Deprecated.Null safe comparison of Comparables.- Parameters:
c1
- the first comparable, may be nullc2
- the second comparable, may be null- Returns:
- If both objects are non-null and unequal, the greater object.
- If both objects are non-null and equal, c1.
- If one of the comparables is null, the non-null object.
- If both the comparables are null, null is returned.
-
compare
public static int compare(java.lang.Comparable c1, java.lang.Comparable c2)
Deprecated.Null safe comparison of Comparables.null
is assumed to be less than a non-null
value.- Parameters:
c1
- the first comparable, may be nullc2
- the second comparable, may be null- Returns:
- a negative value if c1 < c2, zero if c1 = c2 and a positive value if c1 > c2
- Since:
- 2.6
-
compare
public static int compare(java.lang.Comparable c1, java.lang.Comparable c2, boolean nullGreater)
Deprecated.Null safe comparison of Comparables.- Parameters:
c1
- the first comparable, may be nullc2
- the second comparable, may be nullnullGreater
- if truenull
is considered greater than a Non-null
value or if falsenull
is considered less than a Non-null
value- Returns:
- a negative value if c1 < c2, zero if c1 = c2 and a positive value if c1 > c2
- Since:
- 2.6
- See Also:
Comparator.compare(Object, Object)
-
clone
public static java.lang.Object clone(java.lang.Object o)
Deprecated.Clone an object.- Parameters:
o
- the object to clone- Returns:
- the clone if the object implements
Cloneable
otherwisenull
- Throws:
CloneFailedException
- if the object is cloneable and the clone operation fails- Since:
- 2.6
-
cloneIfPossible
public static java.lang.Object cloneIfPossible(java.lang.Object o)
Deprecated.Clone an object if possible. This method is similar toclone(Object)
, but will return the provided instance as the return value instead ofnull
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.- Parameters:
o
- the object to clone- Returns:
- the clone if the object implements
Cloneable
otherwise the object itself - Throws:
CloneFailedException
- if the object is cloneable and the clone operation fails- Since:
- 2.6
-
-