Class ObjectTools
Unified utility methods for emptiness-checking objects, arrays, collections, maps, and character sequences.
Emptiness is defined only for types where the concept is meaningful:
CharSequence, Collection, Map, and arrays.
All other non-null objects are considered not empty.
Multi-value helpers are provided as both predicates and validators.
- Since:
- 1.0
- Author:
- Erik C. Thauvin
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanReturnstrueif all provided values are empty.static booleanallNotEmpty(Object... values) Returnstrueif all provided values are not empty.static booleananyNotEmpty(Object... values) Returnstrueif at least one of the provided values is not empty.static booleanDetermines whether the given value isnullor empty.static booleanisNotEmpty(Object value) Determines whether the given value is notnulland not empty.static voidrequireAllEmpty(Object[] values, String message) Requires all provided values to be empty.static voidrequireAllEmpty(Object[] values, String message, Object... args) Requires all provided values to be empty.static voidrequireAllEmpty(Collection<?> values, String message) Requires all provided collection elements to be empty.static voidrequireAllEmpty(Collection<?> values, String message, Object... args) Requires all provided collection elements to be empty.static voidrequireAllEmpty(Map<?, ?> map, String message) Requires all values in the provided map to be empty.static voidrequireAllEmpty(Map<?, ?> map, String message, Object... args) Requires all values in the provided map to be empty.static voidrequireAllNotEmpty(Object[] values, String message) Requires all provided values to be not empty.static voidrequireAllNotEmpty(Object[] values, String message, Object... args) Requires all provided values to be not empty.static voidrequireAllNotEmpty(Collection<?> values, String message) Requires all provided collection elements to be not empty.static voidrequireAllNotEmpty(Collection<?> values, String message, Object... args) Requires all provided collection elements to be not empty.static voidrequireAllNotEmpty(Map<?, ?> map, String message) Requires the provided map to be not empty, and all of its values to be not empty.static voidrequireAllNotEmpty(Map<?, ?> map, String message, Object... args) Requires the provided map to be not empty, and all of its values to be not empty.static <T> TrequireEmpty(T value, String message) Requires the given value to be empty.static <T> TrequireEmpty(T value, String message, Object... args) Requires the given value to be empty.static <T> TrequireNotEmpty(T value, String message) Requires the given value to be not empty.static <T> TrequireNotEmpty(T value, String message, Object... args) Requires the given value to be not empty.
-
Method Details
-
allEmpty
Returnstrueif all provided values are empty.- Parameters:
values- the values to inspect; may benull- Returns:
trueif all values are empty- Since:
- 1.3
-
allNotEmpty
Returnstrueif all provided values are not empty.- Parameters:
values- the values to inspect; may benull- Returns:
trueif all values are not empty- Since:
- 1.3
-
anyNotEmpty
Returnstrueif at least one of the provided values is not empty.- Parameters:
values- the values to inspect; may benull- Returns:
trueif any value is not empty- Since:
- 1.3
-
isEmpty
Determines whether the given value isnullor empty.Emptiness is defined for
CharSequence,Collection,Map, and arrays. All other non-null objects are considered not empty.- Parameters:
value- the value to inspect; may benull- Returns:
trueif the value isnullor empty
-
isNotEmpty
Determines whether the given value is notnulland not empty.- Parameters:
value- the value to inspect; may benull- Returns:
trueif the value is notnulland not empty
-
requireAllEmpty
Requires all provided values to be empty.The array itself must not be null, but may be empty (all elements are empty). All elements must be null or empty.
- Parameters:
values- the values to inspect; must not be nullmessage- the exception message; must not be null or empty- Throws:
IllegalArgumentException- if any value is not emptyIllegalArgumentException- if values array is nullIllegalArgumentException- if message is null or empty- Since:
- 1.3
-
requireAllEmpty
public static void requireAllEmpty(@Nullable Object[] values, String message, @Nullable Object... args) Requires all provided values to be empty.The array itself must not be null, but may be empty (all elements are empty). All elements must be null or empty. The message may contain
String.format(String, Object...)placeholders, which are resolved using the suppliedargs. If formatting fails, the raw message is used.- Parameters:
values- the values to inspect; must not be nullmessage- the exception message or format string; must not be null or emptyargs- optional arguments used to format the message- Throws:
IllegalArgumentException- if any value is not emptyIllegalArgumentException- if values array is nullIllegalArgumentException- if message is null or empty- Since:
- 1.3
-
requireAllEmpty
Requires all values in the provided map to be empty.The map itself may be
nullor empty. If it is non-empty, all values must benullor empty as defined byisEmpty(Object). If any value is not empty, anIllegalArgumentExceptionis thrown.- Since:
- 1.3
-
requireAllEmpty
public static void requireAllEmpty(@Nullable Map<?, ?> map, String message, @Nullable Object... args) Requires all values in the provided map to be empty.The map itself may be
nullor empty. If it is non-empty, all values must benullor empty as defined byisEmpty(Object). The message may containString.format(String, Object...)placeholders, which are resolved using the suppliedargs. If formatting fails, the raw message is used.- Since:
- 1.3
-
requireAllEmpty
Requires all provided collection elements to be empty.The collection itself must not be
null, but may be empty. All elements must benullor empty as defined byisEmpty(Object). If any element is not empty, anIllegalArgumentExceptionis thrown.- Since:
- 1.2
-
requireAllEmpty
public static void requireAllEmpty(@Nullable Collection<?> values, String message, @Nullable Object... args) Requires all provided collection elements to be empty.The collection itself must not be
null, but may be empty. All elements must benullor empty as defined byisEmpty(Object). The message may containString.format(String, Object...)placeholders, which are resolved using the suppliedargs. If formatting fails, the raw message is used.- Since:
- 1.2
-
requireAllNotEmpty
Requires all provided values to be not empty.The array itself must not be null or empty, and none of its elements may be null or empty.
- Parameters:
values- the values to inspect; must not be null or emptymessage- the exception message; must not be null or empty- Throws:
IllegalArgumentException- if values array is null or emptyIllegalArgumentException- if any value is null or emptyIllegalArgumentException- if message is null or empty- Since:
- 1.3
-
requireAllNotEmpty
public static void requireAllNotEmpty(@Nullable Object[] values, String message, @Nullable Object... args) Requires all provided values to be not empty.The array itself must not be null or empty, and none of its elements may be null or empty. The message may contain
String.format(String, Object...)placeholders, which are resolved using the suppliedargs. If formatting fails, the raw message is used.- Parameters:
values- the values to inspect; must not be null or emptymessage- the exception message or format string; must not be null or emptyargs- optional arguments used to format the message- Throws:
IllegalArgumentException- if values array is null or emptyIllegalArgumentException- if any value is null or emptyIllegalArgumentException- if message is null or empty- Since:
- 1.3
-
requireAllNotEmpty
Requires the provided map to be not empty, and all of its values to be not empty.The map itself must not be
nullor empty, and none of its values may benullor empty as defined byisEmpty(Object). If the map isnull, empty, or contains any empty value, anIllegalArgumentExceptionis thrown.- Since:
- 1.3
-
requireAllNotEmpty
public static void requireAllNotEmpty(@Nullable Map<?, ?> map, String message, @Nullable Object... args) Requires the provided map to be not empty, and all of its values to be not empty.The map itself must not be
nullor empty, and none of its values may benullor empty as defined byisEmpty(Object). The message may containString.format(String, Object...)placeholders, which are resolved using the suppliedargs. If formatting fails, the raw message is used.- Since:
- 1.3
-
requireAllNotEmpty
Requires all provided collection elements to be not empty.The collection itself must not be
nullor empty, and none of its elements may benullor empty as defined byisEmpty(Object). If the collection isnull, empty, or contains any empty element, anIllegalArgumentExceptionis thrown.- Since:
- 1.2
-
requireAllNotEmpty
public static void requireAllNotEmpty(@Nullable Collection<?> values, String message, @Nullable Object... args) Requires all provided collection elements to be not empty.The collection itself must not be
nullor empty, and none of its elements may benullor empty as defined byisEmpty(Object). The message may containString.format(String, Object...)placeholders, which are resolved using the suppliedargs. If formatting fails, the raw message is used.- Since:
- 1.2
-
requireEmpty
Requires the given value to be empty.Emptiness is defined for
CharSequence,Collection,Map, and arrays. All other non-null objects are considered not empty. If the value is not empty, anIllegalArgumentExceptionis thrown. Otherwise, the original value is returned unchanged.- Type Parameters:
T- the value type- Parameters:
value- the value to inspect; may benullmessage- the exception message; must not be null or empty- Returns:
- the original value if it is empty
- Throws:
IllegalArgumentException- if the value is not emptyIllegalArgumentException- if message is null or empty- Since:
- 1.3
-
requireEmpty
Requires the given value to be empty.Emptiness is defined for
CharSequence,Collection,Map, and arrays. All other non-null objects are considered not empty. If the value is not empty, anIllegalArgumentExceptionis thrown. The message may containString.format(String, Object...)placeholders, which are resolved using the suppliedargs. If formatting fails, the raw message is used. Otherwise, the original value is returned unchanged.- Type Parameters:
T- the value type- Parameters:
value- the value to inspect; may benullmessage- the exception message or format string; must not be null or emptyargs- optional arguments used to format the message- Returns:
- the original value if it is empty
- Throws:
IllegalArgumentException- if the value is not emptyIllegalArgumentException- if message is null or empty- Since:
- 1.3
-
requireNotEmpty
Requires the given value to be not empty.Emptiness is defined for
CharSequence,Collection,Map, and arrays. All other non-null objects are considered not empty. If the value is empty, anIllegalArgumentExceptionis thrown. Otherwise, the original value is returned unchanged.- Type Parameters:
T- the value type- Parameters:
value- the value to inspect; may benullmessage- the exception message; must not be null or empty- Returns:
- the original value if it is not empty
- Throws:
IllegalArgumentException- if the value is null or emptyIllegalArgumentException- if message is null or empty- Since:
- 1.3
-
requireNotEmpty
Requires the given value to be not empty.Emptiness is defined for
CharSequence,Collection,Map, and arrays. All other non-null objects are considered not empty. If the value is empty, anIllegalArgumentExceptionis thrown. The message may containString.format(String, Object...)placeholders, which are resolved using the suppliedargs. If formatting fails, the raw message is used. Otherwise, the original value is returned unchanged.- Type Parameters:
T- the value type- Parameters:
value- the value to inspect; may benullmessage- the exception message or format string; must not be null or emptyargs- optional arguments used to format the message- Returns:
- the original value if it is not empty
- Throws:
IllegalArgumentException- if the value is null or emptyIllegalArgumentException- if message is null or empty- Since:
- 1.3
-