Top Description Inners Fields Constructors Methods
org.apache.commons.lang3

public Class ObjectUtils

extends Object
Class Inheritance
Annotations
@SuppressWarnings:deprecation
Imports
java.io.IOException, .Serializable, java.lang.reflect.Array, java.time.Duration, java.util.ArrayList, .Collection, .Collections, .Comparator, .HashMap, .Hashtable, .Map, .Objects, .Optional, .TreeSet, java.util.function.Consumer, .Supplier, java.util.stream.Stream, org.apache.commons.lang3.exception.CloneFailedException, org.apache.commons.lang3.function.Consumers, .Suppliers, org.apache.commons.lang3.mutable.MutableInt, org.apache.commons.lang3.stream.Streams, org.apache.commons.lang3.text.StrBuilder, org.apache.commons.lang3.time.DurationUtils

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#

Since
1.0
See Also
Consumers, Suppliers

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
ObjectUtils.Null

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

Field Summary

Modifier and TypeField and Description
private static final char
public static final ObjectUtils.Null
NULL

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

Constructor Summary

AccessConstructor and Description
public
ObjectUtils()
Deprecated TODO Make private in 4.0.

ObjectUtils instances should NOT be constructed in standard programming.

Method Summary

Modifier and TypeMethod and Description
public static boolean

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.
allNotNull
(final Object...
the values to test, may be null or empty.
values
)

Tests if all values in the array are not nulls.

public static boolean

Returns:

true if all values in the array are nulls, false if there is at least one non-null value in the array.
allNull
(final Object...
the values to test, may be null or empty.
values
)

Tests if all values in the given array are null.

public static boolean

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.
anyNotNull
(final Object...
the values to test, may be null or empty.
values
)

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

public static boolean

Returns:

true if there is at least one null value in the array, false if all the values are non-null. If the array is null or empty, true is also returned.
anyNull
(final Object...
the values to test, may be null or empty.
values
)

Tests if any value in the given array is null.

public static <
the type of the object.
T
>
T

Returns:

the clone if the object implements Cloneable otherwise null.
clone
(final T
the object to clone, null returns null.
obj
)

Clones an object.

public static <
the type of the object.
T
>
T

Returns:

the clone if the object implements Cloneable otherwise the object itself.
cloneIfPossible
(final T
the object to clone, null returns null.
obj
)

Clones an object if possible.

public static <
type of the values processed by this method.
T extends Comparable<? super T>
>
int

Returns:

a negative value if c1 < c2, zero if c1 = c2 and a positive value if c1 > c2.
compare
(final T
the first comparable, may be null.
c1
,
final T
the second comparable, may be null.
c2
)

Null safe comparison of Comparables.

public static <
type of the values processed by this method.
T extends Comparable<? super T>
>
int

Returns:

a negative value if c1 < c2, zero if c1 = c2 and a positive value if c1 > c2.
compare
(final T
the first comparable, may be null.
c1
,
final T
the second comparable, may be null.
c2
,
final boolean
if true null is considered greater than a non-null value or if false null is considered less than a Non-null value.
nullGreater
)

Null safe comparison of Comparables.

public static boolean

Returns:

the boolean v, unchanged.
CONST
(final boolean
the boolean value to return.
v
)

Returns the provided value unchanged.

public static byte

Returns:

the byte v, unchanged.
CONST
(final byte
the byte value to return.
v
)

Returns the provided value unchanged.

public static char

Returns:

the char v, unchanged.
CONST
(final char
the char value to return.
v
)

Returns the provided value unchanged.

public static double

Returns:

the double v, unchanged.
CONST
(final double
the double value to return.
v
)

Returns the provided value unchanged.

public static float

Returns:

the float v, unchanged.
CONST
(final float
the float value to return.
v
)

Returns the provided value unchanged.

public static int

Returns:

the int v, unchanged.
CONST
(final int
the int value to return.
v
)

Returns the provided value unchanged.

public static long

Returns:

the long v, unchanged.
CONST
(final long
the long value to return.
v
)

Returns the provided value unchanged.

public static short

Returns:

the short v, unchanged.
CONST
(final short
the short value to return.
v
)

Returns the provided value unchanged.

public static <
the Object type.
T
>
T

Returns:

the genericized Object v, unchanged (typically a String).
CONST
(final T
the genericized Object value to return (typically a String).
v
)

Returns the provided value unchanged.

public static byte

Returns:

the byte v, unchanged.
CONST_BYTE
(final int
the byte literal (as an int) value to return.
v
)

Returns the provided value unchanged.

public static short

Returns:

the byte v, unchanged.
CONST_SHORT
(final int
the short literal (as an int) value to return.
v
)

Returns the provided value unchanged.

public static <
the type of the object.
T
>
T

Returns:

object if it is not null, defaultValue otherwise.
defaultIfNull
(final T
the Object to test, may be null.
object
,
final T
the default value to return, may be null.
defaultValue
)
Deprecated Use getIfNull(Object, Object).

Returns a default value if the object passed is null.

public static boolean

Returns:

true if the values of both objects are the same.
equals
(final Object
the first object, may be null.
object1
,
final Object
the second object, may be null.
object2
)
Deprecated this method has been 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.

public static <
the component type of the array.
T
>
T

Returns:

the first value from values which is not null, or null if there are no non-null values.
firstNonNull
(final T...
the values to test, may be null or empty.
values
)

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

public static <
The argument type or null.
T
>
Class<T>

Returns:

The argument's Class or null.
getClass
(final T
The argument.
object
)

Delegates to Object#getClass() using generics.

public static <
the type of the return values.
T
>
T

Returns:

the first return value from suppliers which is not null, or null if there are no non-null values.
getFirstNonNull
(final Supplier<T>...
the suppliers returning the values to test. null values are ignored. Suppliers may return null or a value of type T.
suppliers
)

Executes the given suppliers in order and returns the first return value where a value other than null is returned.

public static <
the type of the object.
T
>
T

Returns:

object if it is not null, defaultValueSupplier.get() otherwise.
getIfNull
(final T
the Object to test, may be null.
object
,
final Supplier<T>
the default value to return, may be null.
defaultSupplier
)

Returns the given object is it is non-null, otherwise returns the Supplier's Supplier#get() value.

public static <
the type of the object.
T
>
T

Returns:

object if it is not null, defaultValue otherwise.
getIfNull
(final T
the Object to test, may be null.
object
,
final T
the default value to return, may be null.
defaultValue
)

Returns a default value if the object passed is null.

public static int

Returns:

the hash code of the object, or zero if null.
hashCode
(final Object
the object to obtain the hash code of, may be null.
obj
)
Deprecated this method has been 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.

public static String

Returns:

Hash code in hexadecimal format.
hashCodeHex
(final Object
object for which the hashCode is to be calculated.
object
)

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

public static int

Returns:

the hash code of the objects, or zero if null.
hashCodeMulti
(final Object...
the objects to obtain the hash code of, may be null.
objects
)
Deprecated this method has been replaced by java.util.Objects.hash(Object...) in Java 7 and will be removed in future releases.

Gets the hash code for multiple objects.

public static String

Returns:

Hash code in hexadecimal format.
identityHashCodeHex
(final Object
object for which the hashCode is to be calculated.
object
)

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

public static void
identityToString(final Appendable
the appendable to append to.
appendable
,
final Object
the object to create a toString for.
object
)

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

public static String

Returns:

the default toString text, or null if null passed in.
identityToString
(final Object
the object to create a toString for, may be null.
object
)

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

public static void
identityToString(final StrBuilder
the builder to append to.
builder
,
final Object
the object to create a toString for.
object
)
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.

public static void
identityToString(final StringBuffer
the buffer to append to.
buffer
,
final Object
the object to create a toString for.
object
)

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

public static void
identityToString(final StringBuilder
the builder to append to.
builder
,
final Object
the object to create a toString for.
object
)

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

public static boolean

Returns:

true if the object is an array, false otherwise.
isArray
(final Object
the object to check, may be null.
object
)

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

public static boolean

Returns:

true if the object has a supported type and is empty or null, false otherwise.
isEmpty
(final Object
the Object to test, may be null.
object
)

Tests if an Object is empty or null.

public static boolean

Returns:

true if the object has an unsupported type or is not empty. and not null, false otherwise.
isNotEmpty
(final Object
the Object to test, may be null.
object
)

Tests if an Object is not empty and not null.

public static <
type of the values processed by this method.
T extends Comparable<? super T>
>
T

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.
max
(final T...
the set of comparable values, may be null.
values
)

Null safe comparison of Comparables.

public static <
type of values processed by this method.
T
>
T

Returns:

T at middle position.
median
(final Comparator<T>
to use for comparisons.
comparator
,
final T...
to compare.
items
)

Finds the "best guess" middle value among comparables.

public static <
type of values processed by this method.
T extends Comparable<? super T>
>
T

Returns:

T at middle position.
median
(final T...
to compare.
items
)

Finds the "best guess" middle value among comparables.

public static <
type of the values processed by this method
T extends Comparable<? super T>
>
T

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.
min
(final T...
the set of comparable values, may be null
values
)

Null safe comparison of Comparables.

public static <
type of values processed by this method.
T
>
T

Returns:

most populous T, null if non-unique or no items supplied.
mode
(final T...
to check.
items
)

Finds the most frequently occurring item.

public static boolean

Returns:

false if the values of both objects are the same.
notEqual
(final Object
the first object, may be null.
object1
,
final Object
the second object, may be null.
object2
)

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

public static <
the type of the reference.
T
>
T

Returns:

obj if not null.
requireNonEmpty
(final T
the object reference to check for nullity.
obj
)

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

public static <
the type of the reference.
T
>
T

Returns:

obj if not null.
requireNonEmpty
(final T
the object reference to check for nullity.
obj
,
final String
the exception message.
message
)

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

public static String

Returns:

the input's toString(), or "" if the input is null.
toString
(final Object
the Object to toString(), may be null.
obj
)

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

public static String

Returns:

the passed in Object's toString, or nullStr if null input.
toString
(final Object
the Object to toString, may be null.
obj
,
final String
the String to return if null input, may be null.
nullStr
)
Deprecated this method has been 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.

public static String

Returns:

the passed in Object's toString, or nullStr if null input.
toString
(final Supplier<Object>
the Object to toString, may be null.
obj
,
final Supplier<String>
the Supplier of String used on null input, may be null.
supplier
)

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

public static <
the obj type (used to provide better source compatibility in 3.14.0).
T
>
String

Returns:

the passed in Object's toString, or nullStr if null input.
toString
(final T
the Object to toString, may be null.
obj
,
final Supplier<String>
the Supplier of String used on null input, may be null.
supplier
)

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

public static void
wait(final Object
The receiver of the wait call.
obj
,
final Duration
How long to wait.
duration
)

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

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait