Class ObjectUtils
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#
- Since:
- 1.0
- Version:
- $Id: ObjectUtils.java 1057434 2011-01-11 01:27:37Z niallp $
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classClass used as a null placeholder wherenullhas another meaning. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ObjectUtils.NullSingleton used as anullplaceholder wherenullhas another meaning. -
Constructor Summary
ConstructorsConstructorDescriptionObjectUtilsinstances should NOT be constructed in standard programming. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringBufferappendIdentityToString(StringBuffer buffer, Object object) Deprecated.The design of this method is bad - see LANG-360.static ObjectClone an object.static ObjectClone an object if possible.static intcompare(Comparable c1, Comparable c2) Null safe comparison of Comparables.static intcompare(Comparable c1, Comparable c2, boolean nullGreater) Null safe comparison of Comparables.static ObjectdefaultIfNull(Object object, Object defaultValue) Returns a default value if the object passed isnull.static booleanCompares two objects for equality, where either one or both objects may benull.static intGets the hash code of an object returning zero when the object isnull.static StringidentityToString(Object object) Gets the toString that would be produced byObjectif a class did not override toString itself.static voididentityToString(StringBuffer buffer, Object object) Appends the toString that would be produced byObjectif a class did not override toString itself.static Objectmax(Comparable c1, Comparable c2) Null safe comparison of Comparables.static Objectmin(Comparable c1, Comparable c2) Null safe comparison of Comparables.static booleanCompares two objects for inequality, where either one or both objects may benull.static StringGets thetoStringof anObjectreturning an empty string ("") ifnullinput.static StringGets thetoStringof anObjectreturning a specified text ifnullinput.
-
Field Details
-
NULL
Singleton used as a
nullplaceholder wherenullhas another meaning.For example, in a
HashMaptheHashMap.get(java.lang.Object)method returnsnullif theMapcontainsnullor if there is no matching key. TheNullplaceholder can be used to distinguish between these two cases.Another example is
Hashtable, wherenullcannot be stored.This instance is Serializable.
-
-
Constructor Details
-
ObjectUtils
public ObjectUtils()ObjectUtilsinstances should NOT be constructed in standard programming. Instead, the class should be used asObjectUtils.defaultIfNull("a","b");.This constructor is public to permit tools that require a JavaBean instance to operate.
-
-
Method Details
-
defaultIfNull
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- theObjectto test, may benulldefaultValue- the default value to return, may benull- Returns:
objectif it is notnull, defaultValue otherwise
-
equals
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 benullobject2- the second object, may benull- Returns:
trueif the values of both objects are the same
-
notEqual
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 benullobject2- the second object, may benull- Returns:
falseif the values of both objects are the same- Since:
- 2.6
-
hashCode
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
Gets the toString that would be produced by
Objectif a class did not override toString itself.nullwill 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
nullifnullpassed in
-
identityToString
Appends the toString that would be produced by
Objectif a class did not override toString itself.nullwill 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
Deprecated.The design of this method is bad - see LANG-360. Instead, use identityToString(StringBuffer, Object).Appends the toString that would be produced by
Objectif a class did not override toString itself.nullwill 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 benullobject- the object to create a toString for, may benull- Returns:
- the default toString text, or
nullifnullpassed in - Since:
- 2.0
-
toString
Gets the
toStringof anObjectreturning an empty string ("") ifnullinput.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
nullinput - Since:
- 2.0
- See Also:
-
toString
Gets the
toStringof anObjectreturning a specified text ifnullinput.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 ifnullinput, may be null- Returns:
- the passed in Object's toString, or nullStr if
nullinput - Since:
- 2.0
- See Also:
-
min
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
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
Null safe comparison of Comparables.nullis assumed to be less than a non-nullvalue.- Parameters:
c1- the first comparable, may be nullc2- the second comparable, may be null- Returns:
- a negative value if c1 invalid input: '<' c2, zero if c1 = c2 and a positive value if c1 > c2
- Since:
- 2.6
-
compare
Null safe comparison of Comparables.- Parameters:
c1- the first comparable, may be nullc2- the second comparable, may be nullnullGreater- if truenullis considered greater than a Non-nullvalue or if falsenullis considered less than a Non-nullvalue- Returns:
- a negative value if c1 invalid input: '<' c2, zero if c1 = c2 and a positive value if c1 > c2
- Since:
- 2.6
- See Also:
-
clone
Clone an object.- Parameters:
o- the object to clone- Returns:
- the clone if the object implements
Cloneableotherwisenull - Throws:
CloneFailedException- if the object is cloneable and the clone operation fails- Since:
- 2.6
-
cloneIfPossible
Clone an object if possible. This method is similar toclone(Object), but will return the provided instance as the return value instead ofnullif 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
Cloneableotherwise the object itself - Throws:
CloneFailedException- if the object is cloneable and the clone operation fails- Since:
- 2.6
-