Class ObjectUtils
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
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Class used as a null placeholder wherenull
has another meaning. -
Field Summary
Modifier and TypeFieldDescriptionstatic final ObjectUtils.Null
Singleton used as anull
placeholder wherenull
has another meaning. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
allNotNull
(Object... values) Tests if all values in the array are notnulls
.static boolean
Tests if all values in the given array arenull
.static boolean
anyNotNull
(Object... values) Tests if any value in the given array is notnull
.static boolean
Tests if any value in the given array isnull
.static <T> T
clone
(T obj) Clones an object.static <T> T
cloneIfPossible
(T obj) Clones an object if possible.static <T extends Comparable<? super T>>
intcompare
(T c1, T c2) Null safe comparison of Comparables.static <T extends Comparable<? super T>>
intcompare
(T c1, T c2, boolean nullGreater) Null safe comparison of Comparables.static boolean
CONST
(boolean v) Returns the provided value unchanged.static byte
CONST
(byte v) Returns the provided value unchanged.static char
CONST
(char v) Returns the provided value unchanged.static double
CONST
(double v) Returns the provided value unchanged.static float
CONST
(float v) Returns the provided value unchanged.static int
CONST
(int v) Returns the provided value unchanged.static long
CONST
(long v) Returns the provided value unchanged.static short
CONST
(short v) Returns the provided value unchanged.static <T> T
CONST
(T v) Returns the provided value unchanged.static byte
CONST_BYTE
(int v) Returns the provided value unchanged.static short
CONST_SHORT
(int v) Returns the provided value unchanged.static <T> T
defaultIfNull
(T object, T defaultValue) Returns a default value if the object passed isnull
.static boolean
Deprecated.this method has been replaced byjava.util.Objects.equals(Object, Object)
in Java 7 and will be removed from future releases.static <T> T
firstNonNull
(T... values) Returns the first value in the array which is notnull
.static <T> Class<T>
getClass
(T object) Delegates toObject.getClass()
using generics.static <T> T
getFirstNonNull
(Supplier<T>... suppliers) Executes the given suppliers in order and returns the first return value where a value other thannull
is returned.static <T> T
Returns the givenobject
is it is non-null, otherwise returns the Supplier'sSupplier.get()
value.static int
Deprecated.this method has been replaced byjava.util.Objects.hashCode(Object)
in Java 7 and will be removed in future releasesstatic String
hashCodeHex
(Object object) Returns the hexadecimal hash code for the given object perObjects.hashCode(Object)
.static int
hashCodeMulti
(Object... objects) Deprecated.this method has been replaced byjava.util.Objects.hash(Object...)
in Java 7 and will be removed in future releases.static String
identityHashCodeHex
(Object object) Returns the hexadecimal hash code for the given object perSystem.identityHashCode(Object)
.static void
identityToString
(Appendable appendable, Object object) Appends the toString that would be produced byObject
if a class did not override toString itself.static String
identityToString
(Object object) Gets the toString that would be produced byObject
if a class did not override toString itself.static void
identityToString
(StringBuffer buffer, Object object) Appends the toString that would be produced byObject
if a class did not override toString itself.static void
identityToString
(StringBuilder builder, Object object) Appends the toString that would be produced byObject
if a class did not override toString itself.static void
identityToString
(StrBuilder builder, Object object) Deprecated.as of 3.6, because StrBuilder was moved to commons-text, use one of the otheridentityToString
methods insteadstatic boolean
Tests whether the given object is an Object array or a primitive array in a null-safe manner.static boolean
Tests if an Object is empty or null.static boolean
isNotEmpty
(Object object) Tests if an Object is not empty and not null.static <T extends Comparable<? super T>>
Tmax
(T... values) Null safe comparison of Comparables.static <T> T
median
(Comparator<T> comparator, T... items) Find the "best guess" middle value among comparables.static <T extends Comparable<? super T>>
Tmedian
(T... items) Find the "best guess" middle value among comparables.static <T extends Comparable<? super T>>
Tmin
(T... values) Null safe comparison of Comparables.static <T> T
mode
(T... items) Find the most frequently occurring item.static boolean
Compares two objects for inequality, where either one or both objects may benull
.static <T> T
requireNonEmpty
(T obj) Checks that the specified object reference is notnull
or empty perisEmpty(Object)
.static <T> T
requireNonEmpty
(T obj, String message) Checks that the specified object reference is notnull
or empty perisEmpty(Object)
.static String
Deprecated.this method has been replaced byjava.util.Objects.toString(Object)
in Java 7 and will be removed in future releases.static String
Deprecated.this method has been replaced byjava.util.Objects.toString(Object, String)
in Java 7 and will be removed in future releases.static String
static <T> String
static void
CallsObject.wait(long, int)
for the given Duration.
-
Field Details
-
NULL
Singleton used as anull
placeholder wherenull
has another meaning.For example, in a
HashMap
theHashMap.get(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.
-
-
Constructor Details
-
ObjectUtils
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 asObjectUtils.defaultIfNull("a","b");
.This constructor is public to permit tools that require a JavaBean instance to operate.
-
-
Method Details
-
allNotNull
Tests if all values in the array are notnulls
.If any value is
null
or the array isnull
thenfalse
is returned. If all elements in array are notnull
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 benull
or empty- Returns:
false
if there is at least onenull
value in the array or the array isnull
,true
if all values in the array are notnull
s or array contains no elements.- Since:
- 3.5
-
allNull
Tests if all values in the given array arenull
.If all the values are
null
or the array isnull
or empty, thentrue
is returned, otherwisefalse
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 benull
or empty- Returns:
true
if all values in the array arenull
s,false
if there is at least one non-null value in the array.- Since:
- 3.11
-
anyNotNull
Tests if any value in the given array is notnull
.If all the values are
null
or the array isnull
or empty thenfalse
is returned. Otherwisetrue
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 benull
or empty- Returns:
true
if there is at least one non-null value in the array,false
if all values in the array arenull
s. If the array isnull
or emptyfalse
is also returned.- Since:
- 3.5
-
anyNull
Tests if any value in the given array isnull
.If any of the values are
null
or the array isnull
, thentrue
is returned, otherwisefalse
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 benull
or empty- Returns:
true
if there is at least onenull
value in the array,false
if all the values are non-null. If the array isnull
or empty,true
is also returned.- Since:
- 3.11
-
clone
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
otherwisenull
- Throws:
CloneFailedException
- if the object is cloneable and the clone operation fails- Since:
- 3.0
-
cloneIfPossible
Clones an object if possible.This method is similar to
clone(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.- 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:
CloneFailedException
- if the object is cloneable and the clone operation fails- Since:
- 3.0
-
compare
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 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
-
compare
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 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
- See Also:
-
CONST
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
-
CONST
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
-
CONST
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
-
CONST
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
-
CONST
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
-
CONST
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
-
CONST
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
-
CONST
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
-
CONST
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
-
CONST_BYTE
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- Returns:
- the byte v, unchanged
- Throws:
IllegalArgumentException
- if the value passed to v is larger than a byte, that is, smaller than -128 or larger than 127.- Since:
- 3.2
-
CONST_SHORT
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- Returns:
- the byte v, unchanged
- Throws:
IllegalArgumentException
- if the value passed to v is larger than a short, that is, smaller than -32768 or larger than 32767.- Since:
- 3.2
-
defaultIfNull
Returns a default value if the object passed isnull
.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
- theObject
to test, may benull
defaultValue
- the default value to return, may benull
- Returns:
object
if it is notnull
, defaultValue otherwise TODO Rename to getIfNull in 4.0
-
equals
Deprecated.this method has been replaced byjava.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 benull
.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
-
firstNonNull
Returns the first value in the array which is notnull
. If all the values arenull
or the array isnull
or empty thennull
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 benull
or empty- Returns:
- the first value from
values
which is notnull
, ornull
if there are no non-null values - Since:
- 3.0
-
getClass
Delegates toObject.getClass()
using generics.- Type Parameters:
T
- The argument type or null.- Parameters:
object
- The argument.- Returns:
- The argument's Class or null.
- Since:
- 3.13.0
-
getFirstNonNull
Executes the given suppliers in order and returns the first return value where a value other thannull
is returned. Once a non-null
value is obtained, all following suppliers are not executed anymore. If all the return values arenull
or no suppliers are provided thennull
is returned.ObjectUtils.firstNonNullLazy(null, () -> null) = null ObjectUtils.firstNonNullLazy(() -> null, () -> "") = "" ObjectUtils.firstNonNullLazy(() -> "", () -> throw new IllegalStateException()) = "" ObjectUtils.firstNonNullLazy(() -> null, () -> "zz) = "zz" ObjectUtils.firstNonNullLazy() = null
- Type Parameters:
T
- the type of the return values- Parameters:
suppliers
- the suppliers returning the values to test.null
values are ignored. Suppliers may returnnull
or a value of typeT
- Returns:
- the first return value from
suppliers
which is notnull
, ornull
if there are no non-null values - Since:
- 3.10
-
getIfNull
Returns the givenobject
is it is non-null, otherwise returns the Supplier'sSupplier.get()
value.The caller responsible for thread-safety and exception handling of 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
- Type Parameters:
T
- the type of the object- Parameters:
object
- theObject
to test, may benull
defaultSupplier
- the default value to return, may benull
- Returns:
object
if it is notnull
,defaultValueSupplier.get()
otherwise- Since:
- 3.10
-
hashCode
Deprecated.this method has been replaced byjava.util.Objects.hashCode(Object)
in Java 7 and will be removed in future releasesGets the hash code of an object returning zero when the object isnull
.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
-
hashCodeHex
Returns the hexadecimal hash code for the given object perObjects.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
-
hashCodeMulti
Deprecated.this method has been replaced byjava.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 anArrayList
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 benull
- Returns:
- the hash code of the objects, or zero if null
- Since:
- 3.0
-
identityHashCodeHex
Returns the hexadecimal hash code for the given object perSystem.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
-
identityToString
Appends the toString that would be produced byObject
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 toobject
- the object to create a toString for- Throws:
IOException
- if an I/O error occurs.- Since:
- 3.2
-
identityToString
Gets the toString that would be produced byObject
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
Deprecated.as of 3.6, because StrBuilder was moved to commons-text, use one of the otheridentityToString
methods insteadAppends the toString that would be produced byObject
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 toobject
- the object to create a toString for- Since:
- 3.2
-
identityToString
Appends the toString that would be produced byObject
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
-
identityToString
Appends the toString that would be produced byObject
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 toobject
- the object to create a toString for- Since:
- 3.2
-
isArray
Tests whether the given object is an Object array or a primitive array in a null-safe manner.A
null
object
Object will returnfalse
.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 benull
- Returns:
true
if the object is anarray
,false
otherwise- Since:
- 3.13.0
-
isEmpty
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 ifOptional.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
- theObject
to test, may benull
- Returns:
true
if the object has a supported type and is empty or null,false
otherwise- Since:
- 3.9
-
isNotEmpty
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 ifOptional.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
- theObject
to test, may benull
- Returns:
true
if the object has an unsupported type or is not empty and not null,false
otherwise- Since:
- 3.9
-
max
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.
-
median
Find 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 comparisonsitems
- to compare- Returns:
- T at middle position
- Throws:
NullPointerException
- if items or comparator isnull
IllegalArgumentException
- if items is empty or containsnull
values- Since:
- 3.0.1
-
median
Find 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:
NullPointerException
- if items isnull
IllegalArgumentException
- if items is empty or containsnull
values- Since:
- 3.0.1
-
min
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.
-
mode
Find 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
-
notEqual
Compares two objects for inequality, where either one or both objects may benull
.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
-
requireNonEmpty
Checks that the specified object reference is notnull
or empty perisEmpty(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 notnull
.- Throws:
NullPointerException
- ifobj
isnull
.IllegalArgumentException
- ifobj
is empty perisEmpty(Object)
.- Since:
- 3.12.0
- See Also:
-
requireNonEmpty
Checks that the specified object reference is notnull
or empty perisEmpty(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 notnull
.- Throws:
NullPointerException
- ifobj
isnull
.IllegalArgumentException
- ifobj
is empty perisEmpty(Object)
.- Since:
- 3.12.0
- See Also:
-
toString
Deprecated.this method has been replaced byjava.util.Objects.toString(Object)
in Java 7 and will be removed in future releases. Note however that said method will return "null" for null references, while this method returns an empty String. To preserve behavior usejava.util.Objects.toString(myObject, "")
Gets thetoString
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
""
ifnull
input - Since:
- 2.0
- See Also:
-
toString
Deprecated.this method has been replaced byjava.util.Objects.toString(Object, String)
in Java 7 and will be removed in future releases.Gets thetoString
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
ifnull
input - Since:
- 2.0
- See Also:
-
toString
Gets thetoString
of anSupplier
'sSupplier.get()
returning a specified text ifnull
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"
- Parameters:
obj
- the Object totoString
, may be nullsupplier
- the Supplier of String used onnull
input, may be null- Returns:
- the passed in Object's toString, or
nullStr
ifnull
input - Since:
- 3.14.0
-
toString
Gets thetoString
of anObject
returning a specified text ifnull
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 totoString
, may be nullsupplier
- the Supplier of String used onnull
input, may be null- Returns:
- the passed in Object's toString, or
nullStr
ifnull
input - Since:
- 3.11
-
wait
CallsObject.wait(long, int)
for the given Duration.- Parameters:
obj
- The receiver of the wait call.duration
- How long to wait.- Throws:
IllegalArgumentException
- if the timeout duration is negative.IllegalMonitorStateException
- if the current thread is not the owner of theobj
's monitor.InterruptedException
- 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.- Since:
- 3.12.0
- See Also:
-