Class ObjectTools
Unified utility methods for validating objects: emptiness-checking (arrays,
collections, maps, and character sequences) and null/sign/range checks for
Comparable values, including all require* guard methods.
Emptiness. Emptiness is defined for CharSequence, Collection,
Map, and arrays. All other non-null objects are considered not empty.
allEmpty(Object), allNotEmpty(Object), and the container-checking
require* methods check containers recursively, up to 128
levels deep per branch (see MAX_NESTING_DEPTH). anyEmpty(Object) and
anyNotEmpty(Object) check only the container's direct elements/entries — they
do not descend into nested containers.
Map key checking: both keys and values are evaluated by emptiness predicates.
Keys of non-container, non-CharSequence types (e.g. Integer, enum
constants) are never considered empty, so a map with such keys never reports all keys
as empty — this affects allEmpty(Object) and anyEmpty(Object).
Sign and range checks. requirePositive(Object, String),
requireNegative(Object, String), and requireNonNegative(Object, String)
work with any Comparable type that has a natural zero: Integer,
Long, Double, Float, Short, Byte,
BigInteger, and BigDecimal. For Double and
Float, NaN is rejected by all three, and -0.0 is treated as equal
to zero rather than negative.
Validation order: all require* methods throw NullPointerException
first if the value (or, for container-checking overloads, any nested element) is
null, then IllegalArgumentException if the value fails its specific check
(empty, wrong sign, etc.).
For text-specific operations like blank-checking, see TextTools.
- Since:
- 1.0
- Author:
- Erik C. Thauvin
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanReturnstrueif the value and all nested elements/entries are empty.static booleanallNotEmpty(@Nullable Object value) Returnstrueif the value and all nested elements/entries are not empty.static booleanReturnstrueif the value, or any of its direct elements/entries, is empty.static booleananyNotEmpty(@Nullable Object value) Returnstrueif the value, or any of its direct elements/entries, is not empty.static booleanDetermines whether the givenvalueisnullor empty.static booleanisNotEmpty(@Nullable Object value) Determines whether the givenvalueis notnulland not empty.requireEmpty(@Nullable T value, @NonNull String message) Requires the value to be empty (and, for containers, every element/entry to be empty).requireEmpty(@Nullable T value, @NonNull Supplier<String> messageSupplier) Requires the value to be empty.static <T extends @Nullable Object & Comparable<T>>
@NonNull TrequireNegative(@Nullable T value, @NonNull String context) Checks that the specified value is strictly negative.static <T extends @Nullable Object & Comparable<T>>
@NonNull TrequireNegative(@Nullable T value, @NonNull Supplier<String> messageSupplier) Checks that the specified value is strictly negative.static <T extends @Nullable Object & Comparable<T>>
@NonNull TrequireNonNegative(@Nullable T value, @NonNull String context) Checks that the specified value is non-negative.static <T extends @Nullable Object & Comparable<T>>
@NonNull TrequireNonNegative(@Nullable T value, @NonNull Supplier<String> messageSupplier) Checks that the specified value is non-negative.requireNonNull(@Nullable T value, @NonNull String context) Requires the value to be notnull, and all elements/entries to be notnull.requireNonNull(@Nullable T value, @NonNull Supplier<String> messageSupplier) Requires the value to be notnull, and all elements/entries to be notnull.requireNotEmpty(@Nullable T value, @NonNull String context) Requires the value to be notnulland not empty (and, for containers, every element/entry to be notnulland not empty).Requires the value to be notnulland not empty.static <T extends @Nullable Object & Comparable<T>>
@NonNull TrequirePositive(@Nullable T value, @NonNull String context) Checks that the specified value is strictly positive.static <T extends @Nullable Object & Comparable<T>>
@NonNull TrequirePositive(@Nullable T value, @NonNull Supplier<String> messageSupplier) Checks that the specified value is strictly positive.
-
Method Details
-
allEmpty
Returnstrueif the value and all nested elements/entries are empty.For non-containers, equivalent to
isEmpty(Object). For containers, returnstrueonly if the container itself is empty, or every nested element/entry (checked recursively, see class docs) isnullor empty.allEmpty(List.of()); // true - vacuously allEmpty(List.of(List.of("", ""), List.of())); // true allEmpty(List.of("foo", "bar")); // false- Parameters:
value- the value to inspect; may benull- Returns:
trueif the value and all nested elements arenullor empty- Since:
- 1.3
- API Note:
- An empty container vacuously satisfies this and returns
true— the logical complement of requiring any element non-empty, not the inverse ofallNotEmpty(Object)for empty containers. Anullelement is treated as empty.
-
allNotEmpty
Returnstrueif the value and all nested elements/entries are not empty.For non-containers, equivalent to
isNotEmpty(Object). For containers, the container itself and every nested element/entry (checked recursively, see class docs) must be notnulland not empty.allNotEmpty(List.of("foo", "bar")); // true allNotEmpty(List.of(List.of(""))); // false - nested element is empty allNotEmpty(List.of()); // false - empty container- Parameters:
value- the value to inspect; may benull- Returns:
trueif the value and all nested elements are notnulland not empty- Since:
- 1.3
- API Note:
- An empty container returns
false— there are no non-empty elements to satisfy the condition. This is intentionally asymmetric withallEmpty(Object)for empty containers. Anullelement is treated as empty and causes this method to returnfalse.
-
anyEmpty
Returnstrueif the value, or any of its direct elements/entries, is empty.For non-containers, equivalent to
isEmpty(Object). For containers, only the direct elements/entries are checked — nested containers are not descended into (unlikeallEmpty(Object)). See the class-level note on map key checking.- Parameters:
value- the value to inspect; may benull- Returns:
trueif the value or any direct element is empty- Since:
- 1.3
-
anyNotEmpty
Returnstrueif the value, or any of its direct elements/entries, is not empty.For non-containers, equivalent to
isNotEmpty(Object). For containers, only the direct elements/entries are checked — nested containers are not descended into (unlikeallNotEmpty(Object)).- Parameters:
value- the value to inspect; may benull- Returns:
trueif the value or any direct element is not empty- Since:
- 1.3
-
isEmpty
Determines whether the givenvalueisnullor empty.For arrays,
Collection, orMap, returnstrueonly if the container itself is empty. To check elements as well, useallEmpty(Object).- Parameters:
value- the value to inspect; may benull- Returns:
trueif thevalueisnullor empty
-
isNotEmpty
Determines whether the givenvalueis notnulland not empty.- Parameters:
value- the value to inspect; may benull- Returns:
trueif thevalueis notnulland not empty
-
requireEmpty
@Contract("null, _ -> fail; !null, _ -> !null") @NullUnmarked public static <T extends @Nullable Object> @NonNull T requireEmpty(@Nullable T value, @NonNull String message) Requires the value to be empty (and, for containers, every element/entry to be empty).- Type Parameters:
T- the value type- Parameters:
value- the value to validate and return; must not benullmessage- the exception message; must not benull, empty, or blank- Returns:
- the validated value
- Throws:
NullPointerException- if the value or any element isnullIllegalArgumentException- if the value is not emptyIllegalArgumentException- ifmessageisnull, empty, or blank- Since:
- 1.3
-
requireEmpty
@Contract("null, _ -> fail; !null, _ -> !null") @NullUnmarked public static <T extends @Nullable Object> @NonNull T requireEmpty(@Nullable T value, @NonNull Supplier<String> messageSupplier) Requires the value to be empty. SeerequireEmpty(Object, String).messageSupplieris invoked unconditionally, once, so its produced message is validated (non-null, non-blank) regardless of whethervaluepasses.- Type Parameters:
T- the value type- Parameters:
value- the value to validate and return; must not benullmessageSupplier- the supplier of the exception message; must not benull- Returns:
- the validated value
- Throws:
NullPointerException- if the value or any element isnullIllegalArgumentException- if the value is not emptyIllegalArgumentException- ifmessageSupplierproduces anull, empty, or blank message- Since:
- 1.3
-
requireNegative
@Contract("null, _ -> fail; !null, _ -> !null") @NullUnmarked public static <T extends @Nullable Object & Comparable<T>> @NonNull T requireNegative(@Nullable T value, @NonNull String context) Checks that the specified value is strictly negative. Works with anyComparabletype that has a natural zero value:Integer,Long,Double,Float,Byte,Short,BigInteger, andBigDecimal.For
DoubleandFloat,NaNis rejected by all threerequire*sign-check methods, and-0.0is treated as equal to zero rather than negative.- Type Parameters:
T- the type of the value, must implementComparable- Parameters:
value- the value to check for negativity; must not benullcontext- the context string used in exception messages; must not benull, empty, or blank- Returns:
- the validated value if it is less than zero
- Throws:
NullPointerException- ifvalueorcontextisnullIllegalArgumentException- ifcontextis empty, or blankIllegalArgumentException- ifvalueis zero or positiveIllegalArgumentException- ifvalueis of an unsupported type- Since:
- 1.3
-
requireNegative
@Contract("null, _ -> fail; !null, _ -> !null") @NullUnmarked public static <T extends @Nullable Object & Comparable<T>> @NonNull T requireNegative(@Nullable T value, @NonNull Supplier<String> messageSupplier) Checks that the specified value is strictly negative. SeerequireNegative(Object, String).- Type Parameters:
T- the type of the value, must implementComparable- Parameters:
value- the value to check for negativity; must not benullmessageSupplier- the supplier of the exception message; must not benull- Returns:
- the validated value if it is less than zero
- Throws:
NullPointerException- ifvalueormessageSupplierisnullIllegalArgumentException- ifvalueis zero or positiveIllegalArgumentException- ifvalueis of an unsupported type- Since:
- 1.3
-
requireNonNegative
@Contract("null, _ -> fail; !null, _ -> !null") @NullUnmarked public static <T extends @Nullable Object & Comparable<T>> @NonNull T requireNonNegative(@Nullable T value, @NonNull String context) Checks that the specified value is non-negative. SeerequireNegative(Object, String)for supported types.- Type Parameters:
T- the type of the value, must implementComparable- Parameters:
value- the value to check for non-negativity; must not benullcontext- the context string used in exception messages; must not benull, empty, or blank- Returns:
- the validated value if it is greater than or equal to zero
- Throws:
NullPointerException- ifvalueorcontextisnullIllegalArgumentException- ifcontextis empty, or blankIllegalArgumentException- ifvalueis negativeIllegalArgumentException- ifvalueis of an unsupported type- Since:
- 1.3
-
requireNonNegative
@Contract("null, _ -> fail; !null, _ -> !null") @NullUnmarked public static <T extends @Nullable Object & Comparable<T>> @NonNull T requireNonNegative(@Nullable T value, @NonNull Supplier<String> messageSupplier) Checks that the specified value is non-negative. SeerequireNonNegative(Object, String).- Type Parameters:
T- the type of the value, must implementComparable- Parameters:
value- the value to check for non-negativity; must not benullmessageSupplier- the supplier of the exception message; must not benull- Returns:
- the validated value if it is greater than or equal to zero
- Throws:
NullPointerException- ifvalueormessageSupplierisnullIllegalArgumentException- ifvalueis negativeIllegalArgumentException- ifvalueis of an unsupported type- Since:
- 1.3
-
requireNonNull
@Contract("null, _ -> fail; !null, _ -> !null") @NullUnmarked public static <T extends @Nullable Object> @NonNull T requireNonNull(@Nullable T value, @NonNull String context) Requires the value to be notnull, and all elements/entries to be notnull. UnlikerequireNotEmpty(Object, String), empty containers are allowed as long as they contain nonullelements. Nested containers are checked recursively (see class docs).- Type Parameters:
T- the value type- Parameters:
value- the value to validate and return; must not benullcontext- the context string used in exception message; must not benull, empty, or blank- Returns:
- the validated value, never
null - Throws:
NullPointerException- if thevalueisnullor containsnullelementsNullPointerException- ifcontextisnullIllegalArgumentException- ifcontextis empty, or blank- Since:
- 1.3
-
requireNonNull
@Contract("null, _ -> fail; !null, _ -> !null") @NullUnmarked public static <T extends @Nullable Object> @NonNull T requireNonNull(@Nullable T value, @NonNull Supplier<String> messageSupplier) Requires the value to be notnull, and all elements/entries to be notnull. SeerequireNonNull(Object, String).Unlike the
Stringoverload,messageSupplieris invoked lazily — only when the value actually fails validation — so it is never evaluated on the success path.- Type Parameters:
T- the value type- Parameters:
value- the value to validate and return; must not benullmessageSupplier- the supplier of the exception message; must not benull- Returns:
- the validated value, never
null - Throws:
NullPointerException- if thevalueisnullor containsnullelementsNullPointerException- ifmessageSupplierisnull- Since:
- 1.3
-
requireNotEmpty
@Contract("null, _ -> fail; !null, _ -> !null") @NullUnmarked public static <T extends @Nullable Object> @NonNull T requireNotEmpty(@Nullable T value, @NonNull String context) Requires the value to be notnulland not empty (and, for containers, every element/entry to be notnulland not empty).Exception messages are constructed from
contextas"{context} must not be null"and"{context} must not be empty".- Type Parameters:
T- the value type- Parameters:
value- the value to validate and return; must not benullor emptycontext- the context string used in exception messages; must not benull, empty, or blank- Returns:
- the validated value, never
nullor empty - Throws:
NullPointerException- if thevalueor any element isnullIllegalArgumentException- if thevalueis emptyNullPointerException- ifcontextisnullIllegalArgumentException- ifcontextis empty, or blank- Since:
- 1.3
-
requireNotEmpty
@Contract("null, _, _ -> fail; !null, _, _ -> !null") @NullUnmarked public static <T extends @Nullable Object> @NonNull T requireNotEmpty(@Nullable T value, @NonNull String nullMessage, @NonNull String emptyMessage) Requires the value to be notnulland not empty. SeerequireNotEmpty(Object, String).- Type Parameters:
T- the value type- Parameters:
value- the value to validate and return; must not benullor emptynullMessage- the message forNullPointerException; must not benull, empty, or blankemptyMessage- the message forIllegalArgumentException; must not benull, empty, or blank- Returns:
- the validated value, never
nullor empty - Throws:
NullPointerException- if thevalueor any element isnullIllegalArgumentException- if thevalueis emptyIllegalArgumentException- if either message isnull, empty, or blank- Since:
- 1.3
-
requirePositive
@Contract("null, _ -> fail; !null, _ -> !null") @NullUnmarked public static <T extends @Nullable Object & Comparable<T>> @NonNull T requirePositive(@Nullable T value, @NonNull String context) Checks that the specified value is strictly positive. SeerequireNegative(Object, String)for supported types.- Type Parameters:
T- the type of the value, must implementComparable- Parameters:
value- the value to check for positivity; must not benullcontext- the context string used in exception messages; must not benull, empty, or blank- Returns:
- the validated value if it is greater than zero
- Throws:
NullPointerException- ifvalueorcontextisnullIllegalArgumentException- ifcontextis empty, or blankIllegalArgumentException- ifvalueis zero or negativeIllegalArgumentException- ifvalueis of an unsupported type- Since:
- 1.3
-
requirePositive
@Contract("null, _ -> fail; !null, _ -> !null") @NullUnmarked public static <T extends @Nullable Object & Comparable<T>> @NonNull T requirePositive(@Nullable T value, @NonNull Supplier<String> messageSupplier) Checks that the specified value is strictly positive. SeerequirePositive(Object, String).- Type Parameters:
T- the type of the value, must implementComparable- Parameters:
value- the value to check for positivity; must not benullmessageSupplier- the supplier of the exception message; must not benull- Returns:
- the validated value if it is greater than zero
- Throws:
NullPointerException- ifvalueormessageSupplierisnullIllegalArgumentException- ifvalueis zero or negativeIllegalArgumentException- ifvalueis of an unsupported type- Since:
- 1.3
-