Class ObjectTools
Utility methods for null-checking and emptiness-checking objects, arrays, collections, and maps.
Varargs semantics note: single-argument isEmpty overloads
return true if that one argument is null or empty. Multi-argument (varargs)
isEmpty overloads return true only if all arguments are null
or empty. Conversely, multi-argument isNotEmpty overloads return true
if any argument is not null and not empty.
The same applies to isNull, isAnyNull and isNotNull.
Callers should take care to use the correct overload for their intent.
- Since:
- 1.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanisAnyNull(Collection<?> collection) Checks if any element in the collection isnull.static <T> booleanisAnyNull(T... objects) Checks if any of the provided objects arenull.static booleanisEmpty(Collection<?> collection) Checks if the provided collection is empty ornull.static booleanChecks if the providedMapis empty ornull.static <T> booleanisEmpty(T[] array) Checks if the provided array is empty ornull.static <T> booleanisEmpty(T[]... arrays) Checks if all provided arrays are empty ornull.static booleanisNotEmpty(Collection<?> collection) Checks if the provided collection is notnulland not empty.static booleanisNotEmpty(Map<?, ?> map) Checks if the providedMapis notnulland not empty.static <T> booleanisNotEmpty(T[] array) Checks if the provided array is notnulland not empty.static <T> booleanisNotEmpty(T[]... arrays) Checks if any of the provided arrays are notnulland not empty.static booleanisNotNull(Collection<?> collection) Checks if all elements in the collection are non-null.static <T> booleanisNotNull(T... objects) Checks if all provided objects are non-null.static booleanisNull(Collection<?> collection) Checks if all elements in the collection arenull.static <T> booleanisNull(T... objects) Checks if all provided objects arenull.
-
Method Details
-
isAnyNull
Checks if any of the provided objects arenull.Returns
trueif the varargs array itself isnull.- Parameters:
objects- the objects to check- Returns:
trueif any object isnull;falseotherwise- Since:
- 1.0
-
isAnyNull
Checks if any element in the collection isnull.Returns
trueif the collection itself isnull.- Parameters:
collection- the collection to check- Returns:
trueif any element isnull;falseotherwise- Since:
- 1.0
-
isEmpty
public static <T> boolean isEmpty(T[] array) Checks if the provided array is empty ornull.Note: this overload takes a plain array parameter. PMD's UseVarargs warning is suppressed intentionally as converting to varargs would create an ambiguous overload conflict with
isEmpty(Object[]...).- Parameters:
array- the array to check; can benull- Returns:
trueif the array isnullor empty;falseotherwise- Since:
- 1.0
-
isEmpty
Checks if the providedMapis empty ornull.- Parameters:
map- the map to check; can benull- Returns:
trueif the map isnullor empty;falseotherwise- Since:
- 1.0
-
isEmpty
Checks if all provided arrays are empty ornull.Returns
trueif the varargs array itself isnullor if every individual array isnullor empty. Returnsfalseas soon as any array contains at least one element. See the class-level note on varargs semantics.- Parameters:
arrays- the arrays to check; can benullor containnullelements- Returns:
trueif all arrays arenullor empty;falseif any array is not empty- Since:
- 1.0
-
isEmpty
Checks if the provided collection is empty ornull.- Parameters:
collection- the collection to check; can benull- Returns:
trueif the collection isnullor empty;falseotherwise- Since:
- 1.0
-
isEmpty
Checks if all provided collections are empty ornull.Returns
trueif the varargs array itself isnullor if every individual collection isnullor empty. Returnsfalseas soon as any collection contains at least one element. See the class-level note on varargs semantics.- Parameters:
collections- the collections to check; can benullor containnullelements- Returns:
trueif all collections arenullor empty;falseif any collection is not empty- Since:
- 1.0
-
isNotEmpty
public static <T> boolean isNotEmpty(T[] array) Checks if the provided array is notnulland not empty.Note: this overload takes a plain array parameter. PMD's UseVarargs warning is suppressed intentionally as converting to varargs would create an ambiguous overload conflict with
isNotEmpty(Object[]...).- Parameters:
array- the array to check; can benull- Returns:
trueif the array is notnulland not empty;falseotherwise- Since:
- 1.0
-
isNotEmpty
Checks if the providedMapis notnulland not empty.- Parameters:
map- the map to check; can benull- Returns:
trueif the map is notnulland not empty;falseotherwise- Since:
- 1.0
-
isNotEmpty
Checks if any of the provided arrays are notnulland not empty.Logical negation of
isEmpty(Object[]...). Returnstrueas soon as any array contains at least one element. See the class-level note on varargs semantics.- Parameters:
arrays- the arrays to check; can benullor containnullelements- Returns:
trueif any array is notnulland not empty;falseif all arenullor empty- Since:
- 1.0
-
isNotEmpty
Checks if the provided collection is notnulland not empty.- Parameters:
collection- the collection to check; can benull- Returns:
trueif the collection is notnulland not empty;falseotherwise- Since:
- 1.0
-
isNotEmpty
Checks if any of the provided collections are notnulland not empty.Logical negation of
isEmpty(Collection[]). Returnstrueas soon as any collection contains at least one element. See the class-level note on varargs semantics.- Parameters:
collections- the collections to check; can benullor containnullelements- Returns:
trueif any collection is notnulland not empty;falseif all arenullor empty- Since:
- 1.0
-
isNotNull
Checks if all provided objects are non-null.Returns
falseif the varargs array itself isnull.Returns
truefor an empty argument list (vacuously true).- Parameters:
objects- the objects to check- Returns:
trueif all objects are non-null;falseotherwise- Since:
- 1.0
-
isNotNull
Checks if all elements in the collection are non-null.Returns
falseif the collection itself isnull.- Parameters:
collection- the collection to check- Returns:
trueif all elements are non-null;falseotherwise- Since:
- 1.0
-
isNull
Checks if all provided objects arenull.Returns
trueif the varargs array itself isnull.Returns
truefor an empty argument list (vacuously true).- Parameters:
objects- the objects to check- Returns:
trueif all objects arenull;falseotherwise- Since:
- 1.0
-
isNull
Checks if all elements in the collection arenull.Returns
trueif the collection itself isnull.- Parameters:
collection- the collection to check- Returns:
trueif all elements arenull;falseotherwise- Since:
- 1.0
-