ObjectUtils

@SuppressWarnings({"deprecation"}) public class org.apache.commons.lang3.ObjectUtils[source]

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 behavior in more detail.

#ThreadSafe#

See also:
Since:

1.0

Constructors

ObjectUtils()

Deprecated. ObjectUtils instances should NOT be constructed in standard programming.

@Deprecated public ObjectUtils()[source]

Deprecated: TODO Make private in 4.0.

ObjectUtils instances should NOT be constructed in standard programming. Instead, the static methods on the class should be used, such as ObjectUtils.defaultIfNull("a","b");.

This constructor is public to permit tools that require a JavaBean instance to operate.

Fields and constants

NULL

Singleton used as a null placeholder where null has another meaning.

public static final ObjectUtils.Null NULL[source]

Singleton used as a null placeholder where null has another meaning.

For example, in a HashMap the java.util.HashMap#get(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.

Methods

CONST(boolean)

Returns the provided value unchanged.

CONST(byte)

Returns the provided value unchanged.

CONST(char)

Returns the provided value unchanged.

CONST(double)

Returns the provided value unchanged.

CONST(float)

Returns the provided value unchanged.

CONST(int)

Returns the provided value unchanged.

CONST(Object)

Returns the provided value unchanged.

CONST(long)

Returns the provided value unchanged.

CONST(short)

Returns the provided value unchanged.

CONST_BYTE(int)

Returns the provided value unchanged.

CONST_SHORT(int)

Returns the provided value unchanged.

allNotNull(Object[])

Tests if all values in the array are not nulls.

allNull(Object[])

Tests if all values in the given array are null.

anyNotNull(Object[])

Tests if any value in the given array is not null.

anyNull(Object[])

Tests if any value in the given array is null.

clone(Object)

Clones an object.

cloneIfPossible(Object)

Clones an object if possible.

compare(Comparable, Comparable)

Null safe comparison of Comparables.

compare(Comparable, Comparable, boolean)

Null safe comparison of Comparables.

defaultIfNull(Object, Object)

Deprecated. Returns a default value if the object passed is null.

equals(Object, Object)

Deprecated. Compares two objects for equality, where either one or both objects may be null.

firstNonNull(Object[])

Returns the first value in the array which is not null.

getClass(Object)

Gets the object’s class using Object#getClass() with generics.

getFirstNonNull(Supplier[])

Gets the first non-null result from the given suppliers.

getIfNull(Object, Object)

Gets the given object, or the default value if the object is null.

getIfNull(Object, Supplier)

Gets the given object if it is non-null; otherwise, gets the value from Supplier#get().

hashCode(Object)

Deprecated. Gets the hash code of an object returning zero when the object is null.

hashCodeHex(Object)

Returns the hexadecimal hash code for the given object per Objects#hashCode(Object).

hashCodeMulti(Object[])

Deprecated. Gets the hash code for multiple objects.

identityHashCodeHex(Object)

Returns the hexadecimal hash code for the given object per System#identityHashCode(Object).

identityToString(Appendable, Object)

Appends the toString that would be produced by Object if a class did not override toString itself.

identityToString(Object)

Gets the toString that would be produced by Object if a class did not override toString itself.

identityToString(StringBuffer, Object)

Appends the toString that would be produced by Object if a class did not override toString itself.

identityToString(StringBuilder, Object)

Appends the toString that would be produced by Object if a class did not override toString itself.

identityToString(StrBuilder, Object)

Deprecated. Appends the toString that would be produced by Object if a class did not override toString itself.

isArray(Object)

Tests whether the given object is an Object array or a primitive array in a null-safe manner.

isEmpty(Object)

Tests if an Object is empty or null.

isNotEmpty(Object)

Tests if an Object is not empty and not null.

max(Comparable[])

Null safe comparison of Comparables.

median(Comparable[])

Finds the “best guess” middle value among comparables.

median(Comparator, Object[])

Finds the “best guess” middle value among comparables.

min(Comparable[])

Null safe comparison of Comparables.

mode(Object[])

Finds the most frequently occurring item.

notEqual(Object, Object)

Compares two objects for inequality, where either one or both objects may be null.

requireNonEmpty(Object)

Checks that the specified object reference is not null or empty per #isEmpty(Object).

requireNonEmpty(Object, String)

Checks that the specified object reference is not null or empty per #isEmpty(Object).

toString(Object)

Gets the toString() of an Object or the empty string ("") if the input is null.

toString(Object, String)

Deprecated. Gets the toString of an Object returning a specified text if null input.

toString(Object, Supplier)

Gets the toString of an Object returning a specified text if null input.

toString(Supplier, Supplier)

Gets the toString of an Supplier’s Supplier#get() returning a specified text if null input.

wait(Object, Duration)

Calls Object#wait(long, int) for the given Duration.

public static boolean CONST(boolean v)[source]

Returns the provided value unchanged. This can prevent javac from inlining a constant field, e.g.,

public final static boolean MAGIC_FLAG = ObjectUtils.CONST(true);

This way any jars that refer to this field do not have to recompile themselves if the field’s value changes at some future date.

Parameters:

v – The boolean value to return.

Returns:

The boolean v, unchanged.

Since:

3.2

public static byte CONST(byte v)[source]

Returns the provided value unchanged. This can prevent javac from inlining a constant field, e.g.,

public final static byte MAGIC_BYTE = ObjectUtils.CONST((byte) 127);

This way any jars that refer to this field do not have to recompile themselves if the field’s value changes at some future date.

Parameters:

v – The byte value to return.

Returns:

The byte v, unchanged.

Since:

3.2

public static char CONST(char v)[source]

Returns the provided value unchanged. This can prevent javac from inlining a constant field, e.g.,

public final static char MAGIC_CHAR = ObjectUtils.CONST('a');

This way any jars that refer to this field do not have to recompile themselves if the field’s value changes at some future date.

Parameters:

v – The char value to return.

Returns:

The char v, unchanged.

Since:

3.2

public static double CONST(double v)[source]

Returns the provided value unchanged. This can prevent javac from inlining a constant field, e.g.,

public final static double MAGIC_DOUBLE = ObjectUtils.CONST(1.0);

This way any jars that refer to this field do not have to recompile themselves if the field’s value changes at some future date.

Parameters:

v – The double value to return.

Returns:

The double v, unchanged.

Since:

3.2

public static float CONST(float v)[source]

Returns the provided value unchanged. This can prevent javac from inlining a constant field, e.g.,

public final static float MAGIC_FLOAT = ObjectUtils.CONST(1.0f);

This way any jars that refer to this field do not have to recompile themselves if the field’s value changes at some future date.

Parameters:

v – The float value to return.

Returns:

The float v, unchanged.

Since:

3.2

public static int CONST(int v)[source]

Returns the provided value unchanged. This can prevent javac from inlining a constant field, e.g.,

public final static int MAGIC_INT = ObjectUtils.CONST(123);

This way any jars that refer to this field do not have to recompile themselves if the field’s value changes at some future date.

Parameters:

v – The int value to return.

Returns:

The int v, unchanged.

Since:

3.2

public static <T> T CONST(T v)[source]

Returns the provided value unchanged. This can prevent javac from inlining a constant field, e.g.,

public final static String MAGIC_STRING = ObjectUtils.CONST("abc");

This way any jars that refer to this field do not have to recompile themselves if the field’s value changes at some future date.

Type parameters:

<T> – The Object type.

Parameters:

v – The genericized Object value to return (typically a String).

Returns:

The genericized Object v, unchanged (typically a String).

Since:

3.2

public static long CONST(long v)[source]

Returns the provided value unchanged. This can prevent javac from inlining a constant field, e.g.,

public final static long MAGIC_LONG = ObjectUtils.CONST(123L);

This way any jars that refer to this field do not have to recompile themselves if the field’s value changes at some future date.

Parameters:

v – The long value to return.

Returns:

The long v, unchanged.

Since:

3.2

public static short CONST(short v)[source]

Returns the provided value unchanged. This can prevent javac from inlining a constant field, e.g.,

public final static short MAGIC_SHORT = ObjectUtils.CONST((short) 123);

This way any jars that refer to this field do not have to recompile themselves if the field’s value changes at some future date.

Parameters:

v – The short value to return.

Returns:

The short v, unchanged.

Since:

3.2

public static byte CONST_BYTE(int v)[source]

Returns the provided value unchanged. This can prevent javac from inlining a constant field, e.g.,

public final static byte MAGIC_BYTE = ObjectUtils.CONST_BYTE(127);

This way any jars that refer to this field do not have to recompile themselves if the field’s value changes at some future date.

Parameters:

v – The byte literal (as an int) value to return.

Throws:

java.lang.IllegalArgumentException – Thrown if the value passed to v is larger than a byte, that is, smaller than -128 or larger than 127.

Returns:

The byte v, unchanged.

Since:

3.2

public static short CONST_SHORT(int v)[source]

Returns the provided value unchanged. This can prevent javac from inlining a constant field, e.g.,

public final static short MAGIC_SHORT = ObjectUtils.CONST_SHORT(127);

This way any jars that refer to this field do not have to recompile themselves if the field’s value changes at some future date.

Parameters:

v – The short literal (as an int) value to return.

Throws:

java.lang.IllegalArgumentException – Thrown if the value passed to v is larger than a short, that is, smaller than -32768 or larger than 32767.

Returns:

The byte v, unchanged.

Since:

3.2

public static boolean allNotNull(Object... values)[source]

Tests if all values in the array are not nulls.

If any value is null or the array is null then false is returned. If all elements in array are not null or the array is empty (contains no elements) true is returned.

ObjectUtils.allNotNull(*)             = true
ObjectUtils.allNotNull(*, *)          = true
ObjectUtils.allNotNull(null)          = false
ObjectUtils.allNotNull(null, null)    = false
ObjectUtils.allNotNull(null, *)       = false
ObjectUtils.allNotNull(*, null)       = false
ObjectUtils.allNotNull(*, *, null, *) = false
Parameters:

values – The values to test, may be null or empty.

Returns:

false if there is at least one null value in the array or the array is null, true if all values in the array are not nulls or array contains no elements.

Since:

3.5

public static boolean allNull(Object... values)[source]

Tests if all values in the given array are null.

If all the values are null or the array is null or empty, then true is returned, otherwise false is returned.

ObjectUtils.allNull(*)                = false
ObjectUtils.allNull(*, null)          = false
ObjectUtils.allNull(null, *)          = false
ObjectUtils.allNull(null, null, *, *) = false
ObjectUtils.allNull(null)             = true
ObjectUtils.allNull(null, null)       = true
Parameters:

values – The values to test, may be null or empty.

Returns:

true if all values in the array are nulls, false if there is at least one non-null value in the array.

Since:

3.11

public static boolean anyNotNull(Object... values)[source]

Tests if any value in the given array is not null.

If all the values are null or the array is null or empty then false is returned. Otherwise true is returned.

ObjectUtils.anyNotNull(*)                = true
ObjectUtils.anyNotNull(*, null)          = true
ObjectUtils.anyNotNull(null, *)          = true
ObjectUtils.anyNotNull(null, null, *, *) = true
ObjectUtils.anyNotNull(null)             = false
ObjectUtils.anyNotNull(null, null)       = false
Parameters:

values – The values to test, may be null or empty.

Returns:

true if there is at least one non-null value in the array, false if all values in the array are nulls. If the array is null or empty false is also returned.

Since:

3.5

public static boolean anyNull(Object... values)[source]

Tests if any value in the given array is null.

If any of the values are null or the array is null, then true is returned, otherwise false is returned.

ObjectUtils.anyNull(*)             = false
ObjectUtils.anyNull(*, *)          = false
ObjectUtils.anyNull(null)          = true
ObjectUtils.anyNull(null, null)    = true
ObjectUtils.anyNull(null, *)       = true
ObjectUtils.anyNull(*, null)       = true
ObjectUtils.anyNull(*, *, null, *) = true
Parameters:

values – The values to test, may be null or empty.

Returns:

true if there is at least one null value in the array, false if all the values are non-null or the array is empty. If the array is null, true is also returned.

Since:

3.11

public static <T> T clone(T obj)[source]

Clones an object.

Type parameters:

<T> – The type of the object.

Parameters:

obj – The object to clone, null returns null.

Returns:

The clone if the object implements Cloneable otherwise null.

Throws:

org.apache.commons.lang3.exception.CloneFailedException – Thrown if the object is cloneable and the clone operation fails.

Since:

3.0

public static <T> T cloneIfPossible(T obj)[source]

Clones an object if possible.

This method is similar to #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.

Type parameters:

<T> – The type of the object.

Parameters:

obj – The object to clone, null returns null.

Returns:

The clone if the object implements Cloneable otherwise the object itself.

Throws:

org.apache.commons.lang3.exception.CloneFailedException – Thrown if the object is cloneable and the clone operation fails.

Since:

3.0

public static <T extends Comparable<? super T>> int compare(T c1, T c2)[source]

Null safe comparison of Comparables. null is assumed to be less than a non-null value.

TODO Move to ComparableUtils.

Type parameters:

<T> – type of the values processed by this method.

Parameters:
  • c1 – The first comparable, may be null.

  • c2 – The second comparable, may be null.

Returns:

A negative value if c1 < c2, zero if c1 = c2 and a positive value if c1 > c2.

public static <T extends Comparable<? super T>> int compare(T c1, T c2, boolean nullGreater)[source]

Null safe comparison of Comparables.

TODO Move to ComparableUtils.

Type parameters:

<T> – type of the values processed by this method.

Parameters:
  • c1 – The first comparable, may be null.

  • c2 – The second comparable, may be null.

  • nullGreater – if true null is considered greater than a non-null value or if false null 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.

See also:

java.util.Comparator#compare(Object, Object)

@Deprecated public static <T> T defaultIfNull(T object, T defaultValue)[source]

Deprecated: Use #getIfNull(Object, Object).

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
Type parameters:

<T> – The type of the object.

Parameters:
  • object – The Object to test, may be null.

  • defaultValue – The default value to return, may be null.

Returns:

object if it is not null, defaultValue otherwise.

See also:
@Deprecated public static boolean equals(Object object1, Object object2)[source]

Deprecated: Replaced by java.util.Objects.equals(Object, Object) in Java 7 and will be removed from future releases.

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 be null.

  • object2 – The second object, may be null.

Returns:

true if the values of both objects are the same.

@SafeVarargs public static <T> T firstNonNull(T... values)[source]

Returns the first value in the array which is not null. If all the values are null or the array is null or empty then 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
Type parameters:

<T> – The component type of the array.

Parameters:

values – The values to test, may be null or empty.

Returns:

The first value from values which is not null, or null if there are no non-null values.

Since:

3.0

@SuppressWarnings({"unchecked"}) public static <T> Class<T> getClass(T object)[source]

Gets the object’s class using Object#getClass() with generics.

Type parameters:

<T> – The argument type or null.

Parameters:

object – The argument.

Returns:

The argument’s Class or null.

Since:

3.13.0

@SafeVarargs public static <T> T getFirstNonNull(Supplier<T>... suppliers)[source]

Gets the first non-null result from the given suppliers. Suppliers are invoked in order until a non-null result is found. If all results are null, returns null.

ObjectUtils.firstNonNullLazy(null, () -> null)                                  = null
ObjectUtils.firstNonNullLazy(() -> null, () -> "")                              = ""
ObjectUtils.firstNonNullLazy(() -> "", () -> throw new IllegalStateException()) = ""
ObjectUtils.firstNonNullLazy(() -> null, () -> "zz)                             = "zz"
ObjectUtils.firstNonNullLazy()                                                  = null

See also Consumers#accept(Consumer, Object) and Suppliers#get(Supplier).

Type parameters:

<T> – the type of the return values.

Parameters:

suppliers – The suppliers returning the values to test. null values are ignored. Suppliers may return null or a value of type T.

Returns:

The first return value from suppliers which is not null, or null if there are no non-null values.

See also:
Since:

3.10

public static <T> T getIfNull(T object, T defaultValue)[source]

Gets the given object, or the default value if the object is null.

ObjectUtils.getIfNull(null, null)      = null
ObjectUtils.getIfNull(null, "")        = ""
ObjectUtils.getIfNull(null, "zz")      = "zz"
ObjectUtils.getIfNull("abc", *)        = "abc"
ObjectUtils.getIfNull(Boolean.TRUE, *) = Boolean.TRUE

See also Consumers#accept(Consumer, Object) and Suppliers#get(Supplier).

Type parameters:

<T> – The type of the object.

Parameters:
  • object – The Object to test, may be null.

  • defaultValue – The default value to return, may be null.

Returns:

object if it is not null, defaultValue otherwise.

See also:
Since:

3.18.0

public static <T> T getIfNull(T object, Supplier<T> defaultSupplier)[source]

Gets the given object if it is non-null; otherwise, gets the value from Supplier#get().

The caller is responsible for thread safety and exception handling for the default value supplier.

ObjectUtils.getIfNull(null, () -> null)     = null
ObjectUtils.getIfNull(null, null)           = null
ObjectUtils.getIfNull(null, () -> "")       = ""
ObjectUtils.getIfNull(null, () -> "zz")     = "zz"
ObjectUtils.getIfNull("abc", *)             = "abc"
ObjectUtils.getIfNull(Boolean.TRUE, *)      = Boolean.TRUE

See also Consumers#accept(Consumer, Object) and Suppliers#get(Supplier).

Type parameters:

<T> – The type of the object.

Parameters:
  • object – The Object to test, may be null.

  • defaultSupplier – The default value to return, may be null.

Returns:

object if it is not null, defaultValueSupplier.get() otherwise.

See also:
Since:

3.10

@Deprecated public static int hashCode(Object obj)[source]

Deprecated: Replaced by java.util.Objects.hashCode(Object) in Java 7 and will be removed in future releases.

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 be null.

Returns:

The hash code of the object, or zero if null.

Since:

2.1

public static String hashCodeHex(Object object)[source]

Returns the hexadecimal hash code for the given object per Objects#hashCode(Object).

Short hand for Integer.toHexString(Objects.hashCode(object)).

Parameters:

object – object for which the hashCode is to be calculated.

Returns:

Hash code in hexadecimal format.

Since:

3.13.0

@Deprecated public static int hashCodeMulti(Object... objects)[source]

Deprecated: Replaced by java.util.Objects.hash(Object...) in Java 7 and will be removed in future releases.

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(Object). The hash code for multiple objects is the same as that calculated by an 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 null.

Returns:

The hash code of the objects, or zero if null.

Since:

3.0

public static String identityHashCodeHex(Object object)[source]

Returns the hexadecimal hash code for the given object per System#identityHashCode(Object).

Short hand for Integer.toHexString(System.identityHashCode(object)).

Parameters:

object – object for which the hashCode is to be calculated.

Returns:

Hash code in hexadecimal format.

Since:

3.13.0

public static void identityToString(Appendable appendable, Object object) throws IOException[source]

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(appendable, "")            = appendable.append("java.lang.String@1e23")
ObjectUtils.identityToString(appendable, Boolean.TRUE)  = appendable.append("java.lang.Boolean@7fa")
ObjectUtils.identityToString(appendable, Boolean.TRUE)  = appendable.append("java.lang.Boolean@7fa")
Parameters:
  • appendable – The appendable to append to.

  • object – The object to create a toString for.

Throws:

java.io.IOException – Thrown if an I/O error occurs.

Since:

3.2

public static String identityToString(Object object)[source]

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"
Parameters:

object – The object to create a toString for, may be null.

Returns:

The default toString text, or null if null passed in.

public static void identityToString(StringBuffer buffer, Object object)[source]

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 to.

  • object – The object to create a toString for.

Since:

2.4

public static void identityToString(StringBuilder builder, Object object)[source]

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(builder, "")            = builder.append("java.lang.String@1e23")
ObjectUtils.identityToString(builder, Boolean.TRUE)  = builder.append("java.lang.Boolean@7fa")
ObjectUtils.identityToString(builder, Boolean.TRUE)  = builder.append("java.lang.Boolean@7fa")
Parameters:
  • builder – The builder to append to.

  • object – The object to create a toString for.

Since:

3.2

@Deprecated public static void identityToString(StrBuilder builder, Object object)[source]

Deprecated: as of 3.6, because StrBuilder was moved to commons-text, use one of the other identityToString methods instead.

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(builder, "")            = builder.append("java.lang.String@1e23")
ObjectUtils.identityToString(builder, Boolean.TRUE)  = builder.append("java.lang.Boolean@7fa")
ObjectUtils.identityToString(builder, Boolean.TRUE)  = builder.append("java.lang.Boolean@7fa")
Parameters:
  • builder – The builder to append to.

  • object – The object to create a toString for.

Since:

3.2

public static boolean isArray(Object object)[source]

Tests whether the given object is an Object array or a primitive array in a null-safe manner.

A null object Object will return false.

ObjectUtils.isArray(null)             = false
ObjectUtils.isArray("")               = false
ObjectUtils.isArray("ab")             = false
ObjectUtils.isArray(new int[]{})      = true
ObjectUtils.isArray(new int[]{1,2,3}) = true
ObjectUtils.isArray(1234)             = false
Parameters:

object – The object to check, may be null.

Returns:

true if the object is an array, false otherwise.

Since:

3.13.0

public static boolean isEmpty(Object object)[source]

Tests if an Object is empty or null.

The following types are supported:

  • CharSequence: Considered empty if its length is zero.

  • Array: Considered empty if its length is zero.

  • Collection: Considered empty if it has zero elements.

  • Map: Considered empty if it has zero key-value mappings.

  • Optional: Considered empty if Optional#isPresent returns false, regardless of the “emptiness” of the contents.

ObjectUtils.isEmpty(null)             = true
ObjectUtils.isEmpty("")               = true
ObjectUtils.isEmpty("ab")             = false
ObjectUtils.isEmpty(new int[]{})      = true
ObjectUtils.isEmpty(new int[]{1,2,3}) = false
ObjectUtils.isEmpty(1234)             = false
ObjectUtils.isEmpty(1234)             = false
ObjectUtils.isEmpty(Optional.of(""))  = false
ObjectUtils.isEmpty(Optional.empty()) = true
Parameters:

object – The Object to test, may be null.

Returns:

true if the object has a supported type and is empty or null, false otherwise.

Since:

3.9

public static boolean isNotEmpty(Object object)[source]

Tests if an Object is not empty and not null.

The following types are supported:

  • CharSequence: Considered empty if its length is zero.

  • Array: Considered empty if its length is zero.

  • Collection: Considered empty if it has zero elements.

  • Map: Considered empty if it has zero key-value mappings.

  • Optional: Considered empty if Optional#isPresent returns false, regardless of the “emptiness” of the contents.

ObjectUtils.isNotEmpty(null)             = false
ObjectUtils.isNotEmpty("")               = false
ObjectUtils.isNotEmpty("ab")             = true
ObjectUtils.isNotEmpty(new int[]{})      = false
ObjectUtils.isNotEmpty(new int[]{1,2,3}) = true
ObjectUtils.isNotEmpty(1234)             = true
ObjectUtils.isNotEmpty(Optional.of(""))  = true
ObjectUtils.isNotEmpty(Optional.empty()) = false
Parameters:

object – The Object to test, may be null.

Returns:

true if the object has an unsupported type or is not empty. and not null, false otherwise.

Since:

3.9

@SafeVarargs public static <T extends Comparable<? super T>> T max(T... values)[source]

Null safe comparison of Comparables.

TODO Move to ComparableUtils.

Type parameters:

<T> – type of the values processed by this method.

Parameters:

values – The set of comparable values, may be null.

Returns:
  • If any objects are non-null and unequal, the greater object.

  • If all objects are non-null and equal, the first.

  • If any of the comparables are null, the greater of the non-null objects.

  • If all the comparables are null, null is returned.

@SafeVarargs public static <T extends Comparable<? super T>> T median(T... items)[source]

Finds the “best guess” middle value among comparables. If there is an even number of total values, the lower of the two middle values will be returned.

Type parameters:

<T> – type of values processed by this method.

Parameters:

items – to compare.

Returns:

T at middle position.

Throws:
  • java.lang.NullPointerException – Thrown if items is null.

  • java.lang.IllegalArgumentException – Thrown if items is empty or contains null values.

Since:

3.0.1

@SafeVarargs public static <T> T median(Comparator<T> comparator, T... items)[source]

Finds the “best guess” middle value among comparables. If there is an even number of total values, the lower of the two middle values will be returned.

Type parameters:

<T> – type of values processed by this method.

Parameters:
  • comparator – to use for comparisons.

  • items – to compare.

Returns:

T at middle position.

Throws:
  • java.lang.NullPointerException – Thrown if items or comparator is null.

  • java.lang.IllegalArgumentException – Thrown if items is empty or contains null values.

Since:

3.0.1

@SafeVarargs public static <T extends Comparable<? super T>> T min(T... values)[source]

Null safe comparison of Comparables.

TODO Move to ComparableUtils.

Type parameters:

<T> – type of the values processed by this method

Parameters:

values – The set of comparable values, may be null

Returns:
  • If any objects are non-null and unequal, the lesser object.

  • If all objects are non-null and equal, the first.

  • If any of the comparables are null, the lesser of the non-null objects.

  • If all the comparables are null, null is returned.

@SafeVarargs public static <T> T mode(T... items)[source]

Finds the most frequently occurring item.

Type parameters:

<T> – type of values processed by this method.

Parameters:

items – to check.

Returns:

most populous T, null if non-unique or no items supplied.

Since:

3.0.1

public static boolean notEqual(Object object1, Object object2)[source]

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 be null.

  • object2 – The second object, may be null.

Returns:

false if the values of both objects are the same.

public static <T> T requireNonEmpty(T obj)[source]

Checks that the specified object reference is not null or empty per #isEmpty(Object). Use this method for validation, for example:

public Foo(Bar bar) {
    this.bar = Objects.requireNonEmpty(bar);
}
Type parameters:

<T> – The type of the reference.

Parameters:

obj – The object reference to check for nullity.

Returns:

obj if not null.

Throws:
  • java.lang.NullPointerException – Thrown if obj is null.

  • java.lang.IllegalArgumentException – Thrown if obj is empty per #isEmpty(Object).

See also:

#isEmpty(Object)

Since:

3.12.0

public static <T> T requireNonEmpty(T obj, String message)[source]

Checks that the specified object reference is not null or empty per #isEmpty(Object). Use this method for validation, for example:

public Foo(Bar bar) {
    this.bar = Objects.requireNonEmpty(bar, "bar");
}
Type parameters:

<T> – The type of the reference.

Parameters:
  • obj – The object reference to check for nullity.

  • message – The exception message.

Returns:

obj if not null.

Throws:
  • java.lang.NullPointerException – Thrown if obj is null.

  • java.lang.IllegalArgumentException – Thrown if obj is empty per #isEmpty(Object).

See also:

#isEmpty(Object)

Since:

3.12.0

public static String toString(Object obj)[source]

Gets the toString() of an Object or the empty string ("") if the input is null.

ObjectUtils.toString(null)         = ""
ObjectUtils.toString("")           = ""
ObjectUtils.toString("bat")        = "bat"
ObjectUtils.toString(Boolean.TRUE) = "true"
Parameters:

obj – The Object to toString(), may be null.

Returns:

The input’s toString(), or "" if the input is null.

See also:
Since:

2.0

@Deprecated public static String toString(Object obj, String nullStr)[source]

Deprecated: Replaced by java.util.Objects.toString(Object, String) in Java 7 and will be removed in future releases.

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"
Parameters:
  • obj – The Object to toString, may be null.

  • nullStr – The String to return if null input, may be null.

Returns:

The passed in Object’s toString, or nullStr if null input.

See also:
Since:

2.0

public static <T> String toString(T obj, Supplier<String> supplier)[source]

Gets the toString of an Object returning a specified text if null input.

ObjectUtils.toString(obj, () -> expensive())
ObjectUtils.toString(null, () -> expensive())         = result of expensive()
ObjectUtils.toString(null, () -> expensive())         = result of expensive()
ObjectUtils.toString("", () -> expensive())           = ""
ObjectUtils.toString("bat", () -> expensive())        = "bat"
ObjectUtils.toString(Boolean.TRUE, () -> expensive()) = "true"
Type parameters:

<T> – The obj type (used to provide better source compatibility in 3.14.0).

Parameters:
  • obj – The Object to toString, may be null.

  • supplier – The Supplier of String used on null input, may be null.

Returns:

The passed in Object’s toString, or nullStr if null input.

Since:

3.11

public static String toString(Supplier<Object> obj, Supplier<String> supplier)[source]

Gets the toString of an Supplier’s Supplier#get() returning a specified text if null input.

ObjectUtils.toString(() -> obj, () -> expensive())
</pre>
<pre>
ObjectUtils.toString(() -> null, () -> expensive())         = result of expensive()
ObjectUtils.toString(() -> null, () -> expensive())         = result of expensive()
ObjectUtils.toString(() -> "", () -> expensive())           = ""
ObjectUtils.toString(() -> "bat", () -> expensive())        = "bat"
ObjectUtils.toString(() -> Boolean.TRUE, () -> expensive()) = "true"
Parameters:
  • obj – The Object to toString, may be null.

  • supplier – The Supplier of String used on null input, may be null.

Returns:

The passed in Object’s toString, or nullStr if null input.

Since:

3.14.0

public static void wait(Object obj, Duration duration) throws InterruptedException[source]

Calls Object#wait(long, int) for the given Duration.

Parameters:
  • obj – The receiver of the wait call.

  • duration – How long to wait.

Throws:
  • java.lang.IllegalArgumentException – Thrown if the timeout duration is negative.

  • java.lang.IllegalMonitorStateException – Thrown if the current thread is not the owner of the obj’s monitor.

  • java.lang.InterruptedException – Thrown if any thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.

See also:

Object#wait(long, int)

Since:

3.12.0