-
public class ObjectUtilsOperations on
{@code Object}.This class tries to handle
{@code null}input gracefully. An exception will generally not be thrown for a{@code null}input. Each method documents its behaviour in more detail.#ThreadSafe#
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public classObjectUtils.NullClass used as a null placeholder where
{@code null}has another meaning.For example, in a
{@code HashMap}the get method returns{@code null}if the{@code Map}contains{@code null}or if there isno matching key. The{@code Null}placeholder can be used to distinguishbetween these two cases.Another example is
{@code Hashtable}, where{@code null}cannot be stored.
-
Field Summary
Fields Modifier and Type Field Description public final static ObjectUtils.NullNULL
-
Constructor Summary
Constructors Constructor Description ObjectUtils(){@code ObjectUtils}instances should NOT be constructed instandard programming.
-
Method Summary
Modifier and Type Method Description static <T> TdefaultIfNull(T object, T defaultValue)Returns a default value if the object passed is {@code null}.static <T> TfirstNonNull(Array<T> values)Returns the first value in the array which is not {@code null}.static booleanequals(Object object1, Object object2)Compares two objects for equality, where either one or bothobjects may be {@code null}.static booleannotEqual(Object object1, Object object2)Compares two objects for inequality, where either one or bothobjects may be {@code null}.static inthashCode(Object obj)Gets the hash code of an object returning zero when theobject is {@code null}.static inthashCodeMulti(Array<Object> objects)Gets the hash code for multiple objects. static StringidentityToString(Object object)Gets the toString that would be produced by {@code Object}if a class did not override toString itself.static voididentityToString(StringBuffer buffer, Object object)Appends the toString that would be produced by {@code Object}if a class did not override toString itself.static StringtoString(Object obj)Gets the {@code toString}of an{@code Object}returningan empty string ("") if{@code null}input.static StringtoString(Object obj, String nullStr)Gets the {@code toString}of an{@code Object}returninga specified text if{@code null}input.static <T extends Comparable<out Object>> Tmin(Array<T> values)Null safe comparison of Comparables. static <T extends Comparable<out Object>> Tmax(Array<T> values)Null safe comparison of Comparables. static <T extends Comparable<out Object>> intcompare(T c1, T c2)Null safe comparison of Comparables. static <T extends Comparable<out Object>> intcompare(T c1, T c2, boolean nullGreater)Null safe comparison of Comparables. static <T extends Comparable<out Object>> Tmedian(Array<T> items)Find the "best guess" middle value among comparables. static <T> Tmedian(Comparator<T> comparator, Array<T> items)Find the "best guess" middle value among comparables. static <T> Tmode(Array<T> items)Find the most frequently occurring item. static <T> Tclone(T obj)Clone an object. static <T> TcloneIfPossible(T obj)Clone an object if possible. -
-
Method Detail
-
defaultIfNull
static <T> T defaultIfNull(T object, T defaultValue)
Returns a default value if the object passed is
{@code 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- the{@code Object}to test, may be{@code null}defaultValue- the default value to return, may be{@code null}
-
firstNonNull
static <T> T firstNonNull(Array<T> values)
Returns the first value in the array which is not
{@code null}.If all the values are{@code null}or the array is{@code null}or empty then{@code null}is returned.ObjectUtils.firstNonNull(null, null) = null ObjectUtils.firstNonNull(null, "") = "" ObjectUtils.firstNonNull(null, null, "") = "" ObjectUtils.firstNonNull(null, "zz") = "zz" ObjectUtils.firstNonNull("abc", *) = "abc" ObjectUtils.firstNonNull(null, "xyz", *) = "xyz" ObjectUtils.firstNonNull(Boolean.TRUE, *) = Boolean.TRUE ObjectUtils.firstNonNull() = null- Parameters:
values- the values to test, may be{@code null}or empty
-
equals
static boolean equals(Object object1, Object object2)
Compares two objects for equality, where either one or bothobjects may be
{@code 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 be{@code null}object2- the second object, may be{@code null}
-
notEqual
static boolean notEqual(Object object1, Object object2)
Compares two objects for inequality, where either one or bothobjects may be
{@code 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 be{@code null}object2- the second object, may be{@code null}
-
hashCode
static int hashCode(Object obj)
Gets the hash code of an object returning zero when theobject is
{@code null}.ObjectUtils.hashCode(null) = 0 ObjectUtils.hashCode(obj) = obj.hashCode()- Parameters:
obj- the object to obtain the hash code of, may be{@code null}
-
hashCodeMulti
static int hashCodeMulti(Array<Object> objects)
Gets the hash code for multiple objects.
This allows a hash code to be rapidly calculated for a number of objects.The hash code for a single object is the not same as hashCode.The hash code for multiple objects is the same as that calculated by an
{@code ArrayList}containing the specified objects.ObjectUtils.hashCodeMulti() = 1 ObjectUtils.hashCodeMulti((Object[]) null) = 1 ObjectUtils.hashCodeMulti(a) = 31 + a.hashCode() ObjectUtils.hashCodeMulti(a,b) = (31 + a.hashCode()) * 31 + b.hashCode() ObjectUtils.hashCodeMulti(a,b,c) = ((31 + a.hashCode()) * 31 + b.hashCode()) * 31 + c.hashCode()- Parameters:
objects- the objects to obtain the hash code of, may be{@code null}
-
identityToString
static String identityToString(Object object)
Gets the toString that would be produced by
{@code Object}if a class did not override toString itself.{@code null}will return{@code null}.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 be{@code null}
-
identityToString
static void identityToString(StringBuffer buffer, Object object)
Appends the toString that would be produced by
{@code Object}if a class did not override toString itself.{@code 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
-
toString
static String toString(Object obj)
Gets the
{@code toString}of an{@code Object}returningan empty string ("") if{@code null}input.ObjectUtils.toString(null) = "" ObjectUtils.toString("") = "" ObjectUtils.toString("bat") = "bat" ObjectUtils.toString(Boolean.TRUE) = "true"- Parameters:
obj- the Object to{@code toString}, may be null
-
toString
static String toString(Object obj, String nullStr)
Gets the
{@code toString}of an{@code Object}returninga specified text if{@code 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"- Parameters:
obj- the Object to{@code toString}, may be nullnullStr- the String to return if{@code null}input, may be null
-
min
static <T extends Comparable<out Object>> T min(Array<T> values)
Null safe comparison of Comparables.
- Parameters:
values- the set of comparable values, may be null
-
max
static <T extends Comparable<out Object>> T max(Array<T> values)
Null safe comparison of Comparables.
- Parameters:
values- the set of comparable values, may be null
-
compare
static <T extends Comparable<out Object>> int compare(T c1, T c2)
Null safe comparison of Comparables.
{@code null}is assumed to be less than a non-{@code null}value.- Parameters:
c1- the first comparable, may be nullc2- the second comparable, may be null
-
compare
static <T extends Comparable<out Object>> int compare(T c1, T c2, boolean nullGreater)
Null safe comparison of Comparables.
- Parameters:
c1- the first comparable, may be nullc2- the second comparable, may be nullnullGreater- if true{@code null}is considered greaterthan a non-{@code null}value or if false{@code null}isconsidered less than a Non-{@code null}value
-
median
static <T extends Comparable<out Object>> T median(Array<T> items)
Find the "best guess" middle value among comparables. If there is an evennumber of total values, the lower of the two middle values will be returned.
- Parameters:
items- to compare
-
median
static <T> T median(Comparator<T> comparator, Array<T> items)
Find the "best guess" middle value among comparables. If there is an evennumber of total values, the lower of the two middle values will be returned.
- Parameters:
comparator- to use for comparisonsitems- to compare
-
mode
static <T> T mode(Array<T> items)
Find the most frequently occurring item.
- Parameters:
items- to check
-
clone
static <T> T clone(T obj)
Clone an object.
- Parameters:
obj- the object to clone, null returns null
-
cloneIfPossible
static <T> T cloneIfPossible(T obj)
Clone an object if possible.
This method is similar to clone, but will return the providedinstance as the return value instead of
{@code null}if the instanceis not cloneable. This is more convenient if the caller uses differentimplementations (e.g. of a service) and some of the implementations do not allow concurrentprocessing or have state. In such cases the implementation can simply provide a properclone implementation and the caller's code does not have to change.- Parameters:
obj- the object to clone, null returns null
-
-
-
-