Class TextTools
Provides null‑safe checks for blankness and emptiness across CharSequence
and general Object inputs, along with a whitespace‑insensitive equality
comparison.
- Since:
- 1.0
- Author:
- Erik C. Thauvin
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanequalsIgnoreWhitespace(@Nullable CharSequence @Nullable ... strings) Compares two or more character sequences by removing all whitespace.static booleanisBlank(@Nullable CharSequence str) Checks if a character sequence isnull, empty, or contains only whitespace characters.static booleanisBlank(@Nullable CharSequence @Nullable ... strings) Checks if all character sequences arenull, empty, or contain only whitespace characters.static booleanChecks if all objects arenull, empty, or contain only whitespace characters.static booleanisEmpty(@Nullable CharSequence str) Checks if a character sequence isnullor empty.static booleanisEmpty(@Nullable CharSequence @Nullable ... strings) Checks if all character sequences arenullor empty.static booleanChecks if all objects arenullor their string representations are empty.static booleanChecks if a character sequence is notnull, not empty, and not whitespace-only.static booleanisNotBlank(@Nullable CharSequence @Nullable ... strings) Checks if all character sequences are notnull, not empty, and not whitespace-only.static booleanisNotBlank(@Nullable Object @Nullable ... objects) Checks if all objects are notnull, not empty, and not whitespace-only.static booleanChecks if a character sequence is notnulland not empty.static booleanisNotEmpty(@Nullable CharSequence @Nullable ... strings) Checks if all character sequences are notnulland not empty.static booleanisNotEmpty(@Nullable Object @Nullable ... objects) Checks if all objects are notnulland their string representations are not empty.static <T extends CharSequence>
@NonNull T @NonNull []requireNotBlank(@NonNull String context, @Nullable T @Nullable ... elements) Checks that the specified varargs array of character sequences is notnull, not empty, and contains nonull, empty, or whitespace-only elements.static <T extends CharSequence>
@NonNull T @NonNull []requireNotBlank(@NonNull Supplier<String> nullMessage, @NonNull Supplier<String> blankMessage, @Nullable T @Nullable ... elements) Checks that the specified varargs array of character sequences is notnull, not empty, and contains nonull, empty, or whitespace-only elements.static <T extends CharSequence>
@NonNull Collection<@NonNull T>requireNotBlank(@Nullable Collection<@Nullable T> coll, @NonNull String context) Checks that the specified collection of character sequences is notnull, not empty, and contains nonull, empty, or whitespace-only elements.static <T extends CharSequence>
@NonNull Collection<@NonNull T>requireNotBlank(@Nullable Collection<@Nullable T> coll, @NonNull Supplier<String> nullMessage, @NonNull Supplier<String> blankMessage) Checks that the specified collection of character sequences is notnull, not empty, and contains nonull, empty, or whitespace-only elements.static <T extends @Nullable Object & CharSequence>
@NonNull TrequireNotBlank(@Nullable T str, @NonNull String context) Checks that the specified character sequence is notnull, not empty, and not whitespace-only.static <T extends @Nullable Object & CharSequence>
@NonNull TrequireNotBlank(@Nullable T str, @NonNull Supplier<String> nullMessage, @NonNull Supplier<String> blankMessage) Checks that the specified character sequence is notnull, not empty, and not whitespace-only.static <T extends CharSequence>
@NonNull T @NonNull []requireNotEmpty(@NonNull String context, @Nullable T @Nullable ... elements) Checks that the specified varargs array of character sequences is notnull, not empty, and contains nonullor empty elements.static <T extends CharSequence>
@NonNull T @NonNull []requireNotEmpty(@NonNull Supplier<String> nullMessage, @NonNull Supplier<String> emptyMessage, @Nullable T @Nullable ... elements) Checks that the specified varargs array of character sequences is notnull, not empty, and contains nonullor empty elements.static <T extends CharSequence>
@NonNull Collection<@NonNull T>requireNotEmpty(@Nullable Collection<@Nullable T> coll, @NonNull String context) Checks that the specified collection of character sequences is notnull, not empty, and contains nonullor empty elements.static <T extends CharSequence>
@NonNull Collection<@NonNull T>requireNotEmpty(@Nullable Collection<@Nullable T> coll, @NonNull Supplier<String> nullMessage, @NonNull Supplier<String> emptyMessage) Checks that the specified collection of character sequences is notnull, not empty, and contains nonullor empty elements.static <T extends @Nullable Object & CharSequence>
@NonNull TrequireNotEmpty(@Nullable T str, @NonNull String context) Checks that the specified character sequence is notnulland not empty.static <T extends @Nullable Object & CharSequence>
@NonNull TrequireNotEmpty(@Nullable T str, @NonNull Supplier<String> nullMessage, @NonNull Supplier<String> emptyMessage) Checks that the specified character sequence is notnulland not empty.
-
Method Details
-
equalsIgnoreWhitespace
Compares two or more character sequences by removing all whitespace.- Parameters:
strings- the character sequences to compare- Returns:
trueif all character sequences are equivalent when whitespace is ignored,falseotherwise. Returnsfalseifnullor fewer than 2 elements- Since:
- 1.0
- Implementation Note:
nullelements within the array are treated as empty strings, soequalsIgnoreWhitespace(null, "")returnstrue.
-
isBlank
Checks if a character sequence isnull, empty, or contains only whitespace characters.- Parameters:
str- the character sequence to check- Returns:
trueif the character sequence isnull, empty, or whitespace-only;falseotherwise- Since:
- 1.0
-
isBlank
Checks if all character sequences arenull, empty, or contain only whitespace characters.- Parameters:
strings- the character sequences to check- Returns:
trueif all character sequences arenull, empty, or whitespace-only;falseotherwise. Returnstruefornullor empty array (vacuous truth)- Since:
- 1.0
-
isBlank
Checks if all objects arenull, empty, or contain only whitespace characters.CharSequenceinstances are checked directly; all other objects are checked via theirObject.toString()representation. This varargs overload exists to support mixed-type arrays — prefer theCharSequence...overload for homogeneous inputs.- Parameters:
objects- the objects to check- Returns:
trueif all objects arenull, their string representations are empty, or their string representations are whitespace-only;falseotherwise. Returnstruefornullor empty array (vacuous truth)- Since:
- 1.0
-
isEmpty
Checks if a character sequence isnullor empty.- Parameters:
str- the character sequence to check- Returns:
trueif the character sequence isnullor empty;falseotherwise- Since:
- 1.0
-
isEmpty
Checks if all character sequences arenullor empty.- Parameters:
strings- the character sequences to check- Returns:
trueif all character sequences arenullor empty;falseotherwise. Returnstruefornullor empty array (vacuous truth)- Since:
- 1.0
-
isEmpty
Checks if all objects arenullor their string representations are empty.CharSequenceinstances are checked directly; all other objects are checked via theirObject.toString()representation. This varargs overload exists to support mixed-type arrays — prefer theCharSequence...overload for homogeneous inputs.- Parameters:
objects- the objects to check- Returns:
trueif all objects arenullor their string representations are empty;falseotherwise. Returnstruefornullor empty array (vacuous truth)- Since:
- 1.0
-
isNotBlank
Checks if a character sequence is notnull, not empty, and not whitespace-only.- Parameters:
str- the character sequence to check- Returns:
trueif the character sequence is notnull, not empty, and not whitespace-only;falseotherwise- Since:
- 1.0
-
isNotBlank
Checks if all character sequences are notnull, not empty, and not whitespace-only.- Parameters:
strings- the character sequences to check- Returns:
trueif all character sequences are notnull, not empty, and not whitespace-only;falseotherwise. Returnsfalsefornullor empty array- Since:
- 1.0
-
isNotBlank
Checks if all objects are notnull, not empty, and not whitespace-only.CharSequenceinstances are checked directly; all other objects are checked via theirObject.toString()representation. This varargs overload exists to support mixed-type arrays — prefer theCharSequence...overload for homogeneous inputs.- Parameters:
objects- the objects to check- Returns:
trueif all objects are notnull, their string representations are not empty, and their string representations are not whitespace-only;falseotherwise- Since:
- 1.0
-
isNotEmpty
Checks if a character sequence is notnulland not empty.- Parameters:
str- the character sequence to check- Returns:
trueif the character sequence is notnulland not empty;falseotherwise- Since:
- 1.0
-
isNotEmpty
Checks if all character sequences are notnulland not empty.- Parameters:
strings- the character sequences to check- Returns:
trueif all character sequences are notnulland not empty;falseotherwise. Returnsfalsefornullor empty array- Since:
- 1.0
-
isNotEmpty
Checks if all objects are notnulland their string representations are not empty.CharSequenceinstances are checked directly; all other objects are checked via theirObject.toString()representation. This varargs overload exists to support mixed-type arrays — prefer theCharSequence...overload for homogeneous inputs.- Parameters:
objects- the objects to check- Returns:
trueif all objects are notnulland their string representations are not empty;falseotherwise- Since:
- 1.0
-
requireNotBlank
@Contract("null, _, _ -> fail;!null, _, _ ->!null") @NullUnmarked public static <T extends @Nullable Object & CharSequence> @NonNull T requireNotBlank(@Nullable T str, @NonNull Supplier<String> nullMessage, @NonNull Supplier<String> blankMessage) Checks that the specified character sequence is notnull, not empty, and not whitespace-only.- Type Parameters:
T- the type of the character sequence- Parameters:
str- the character sequence to checknullMessage- the supplier of theNullPointerExceptionmessage; must not benullblankMessage- the supplier of theIllegalArgumentExceptionmessage; must not benull- Returns:
strif not blank- Throws:
NullPointerException- ifstrisnullIllegalArgumentException- ifstris empty or whitespace-only- Since:
- 1.0
-
requireNotBlank
@Contract("null, _ -> fail;!null, _ ->!null") @NullUnmarked public static <T extends @Nullable Object & CharSequence> @NonNull T requireNotBlank(@Nullable T str, @NonNull String context) Checks that the specified character sequence is notnull, not empty, and not whitespace-only.- Type Parameters:
T- the type of the character sequence- Parameters:
str- the character sequence to checkcontext- the context description used in the exception messages- Returns:
strif not blank- Throws:
NullPointerException- ifstrorcontextisnullIllegalArgumentException- ifstris empty or whitespace-only- Since:
- 1.0
-
requireNotBlank
@Contract("null, _, _ -> fail;!null, _, _ ->!null") @NullUnmarked public static <T extends CharSequence> @NonNull Collection<@NonNull T> requireNotBlank(@Nullable Collection<@Nullable T> coll, @NonNull Supplier<String> nullMessage, @NonNull Supplier<String> blankMessage) Checks that the specified collection of character sequences is notnull, not empty, and contains nonull, empty, or whitespace-only elements.- Type Parameters:
T- the type of the character sequence- Parameters:
coll- the collection to checknullMessage- the supplier of theNullPointerExceptionmessage; must not benullblankMessage- the supplier of theIllegalArgumentExceptionmessage; must not benull- Returns:
collif valid- Throws:
NullPointerException- ifcollisnullor containsnullelementsIllegalArgumentException- ifcollis empty or contains blank elements- Since:
- 1.0
-
requireNotBlank
@Contract("null, _ -> fail;!null, _ ->!null") @NullUnmarked public static <T extends CharSequence> @NonNull Collection<@NonNull T> requireNotBlank(@Nullable Collection<@Nullable T> coll, @NonNull String context) Checks that the specified collection of character sequences is notnull, not empty, and contains nonull, empty, or whitespace-only elements.- Type Parameters:
T- the type of the character sequence- Parameters:
coll- the collection to checkcontext- the context description used in the exception messages- Returns:
collif valid- Throws:
NullPointerException- ifcollisnullor containsnullelementsIllegalArgumentException- ifcollis empty or contains blank elements- Since:
- 1.0
-
requireNotBlank
@Contract("_, _, null -> fail; _, _,!null ->!null") @NullUnmarked @SafeVarargs public static <T extends CharSequence> @NonNull T @NonNull [] requireNotBlank(@NonNull Supplier<String> nullMessage, @NonNull Supplier<String> blankMessage, @Nullable T @Nullable ... elements) Checks that the specified varargs array of character sequences is notnull, not empty, and contains nonull, empty, or whitespace-only elements.- Type Parameters:
T- the type of the character sequence- Parameters:
nullMessage- the supplier of theNullPointerExceptionmessage; must not benullblankMessage- the supplier of theIllegalArgumentExceptionmessage; must not benullelements- the elements to check- Returns:
elementsif valid- Throws:
NullPointerException- ifelementsisnullor containsnullelementsIllegalArgumentException- ifelementsis empty or contains blank elements- Since:
- 1.0
-
requireNotBlank
@Contract("_, null -> fail; _,!null ->!null") @NullUnmarked @SafeVarargs public static <T extends CharSequence> @NonNull T @NonNull [] requireNotBlank(@NonNull String context, @Nullable T @Nullable ... elements) Checks that the specified varargs array of character sequences is notnull, not empty, and contains nonull, empty, or whitespace-only elements.- Type Parameters:
T- the type of the character sequence- Parameters:
context- the context description used in the exception messageselements- the elements to check- Returns:
elementsif valid- Throws:
NullPointerException- ifelementsisnullor containsnullelementsIllegalArgumentException- ifelementsis empty or contains blank elements- Since:
- 1.0
-
requireNotEmpty
@Contract("null, _, _ -> fail;!null, _, _ ->!null") @NullUnmarked public static <T extends @Nullable Object & CharSequence> @NonNull T requireNotEmpty(@Nullable T str, @NonNull Supplier<String> nullMessage, @NonNull Supplier<String> emptyMessage) Checks that the specified character sequence is notnulland not empty.- Type Parameters:
T- the type of the character sequence- Parameters:
str- the character sequence to checknullMessage- the supplier of theNullPointerExceptionmessage; must not benullemptyMessage- the supplier of theIllegalArgumentExceptionmessage; must not benull- Returns:
strif not empty- Throws:
NullPointerException- ifstrisnullIllegalArgumentException- ifstris empty- Since:
- 1.0
-
requireNotEmpty
@Contract("null, _ -> fail;!null, _ ->!null") @NullUnmarked public static <T extends @Nullable Object & CharSequence> @NonNull T requireNotEmpty(@Nullable T str, @NonNull String context) Checks that the specified character sequence is notnulland not empty.- Type Parameters:
T- the type of the character sequence- Parameters:
str- the character sequence to checkcontext- the context description used in the exception messages- Returns:
strif not empty- Throws:
NullPointerException- ifstrisnullIllegalArgumentException- ifstris empty- Since:
- 1.0
-
requireNotEmpty
@Contract("null, _, _ -> fail;!null, _, _ ->!null") @NullUnmarked public static <T extends CharSequence> @NonNull Collection<@NonNull T> requireNotEmpty(@Nullable Collection<@Nullable T> coll, @NonNull Supplier<String> nullMessage, @NonNull Supplier<String> emptyMessage) Checks that the specified collection of character sequences is notnull, not empty, and contains nonullor empty elements.- Type Parameters:
T- the type of the character sequence- Parameters:
coll- the collection to checknullMessage- the supplier of theNullPointerExceptionmessage; must not benullemptyMessage- the supplier of theIllegalArgumentExceptionmessage; must not benull- Returns:
collif valid- Throws:
NullPointerException- ifcollisnullor containsnullelementsIllegalArgumentException- ifcollis empty or contains empty elements- Since:
- 1.0
-
requireNotEmpty
@Contract("null, _ -> fail;!null, _ ->!null") @NullUnmarked public static <T extends CharSequence> @NonNull Collection<@NonNull T> requireNotEmpty(@Nullable Collection<@Nullable T> coll, @NonNull String context) Checks that the specified collection of character sequences is notnull, not empty, and contains nonullor empty elements.- Type Parameters:
T- the type of the character sequence- Parameters:
coll- the collection to checkcontext- the context description used in the exception messages- Returns:
collif valid- Throws:
NullPointerException- ifcollisnullor containsnullelementsIllegalArgumentException- ifcollis empty or contains empty elements- Since:
- 1.0
-
requireNotEmpty
@Contract("_, _, null -> fail; _, _,!null ->!null") @NullUnmarked @SafeVarargs public static <T extends CharSequence> @NonNull T @NonNull [] requireNotEmpty(@NonNull Supplier<String> nullMessage, @NonNull Supplier<String> emptyMessage, @Nullable T @Nullable ... elements) Checks that the specified varargs array of character sequences is notnull, not empty, and contains nonullor empty elements.- Type Parameters:
T- the type of the character sequence- Parameters:
nullMessage- the supplier of theNullPointerExceptionmessage; must not benullemptyMessage- the supplier of theIllegalArgumentExceptionmessage; must not benullelements- the elements to check- Returns:
elementsif valid- Throws:
NullPointerException- ifelementsisnullor containsnullelementsIllegalArgumentException- ifelementsis empty or contains empty elements- Since:
- 1.0
-
requireNotEmpty
@Contract("_, null -> fail; _,!null ->!null") @NullUnmarked @SafeVarargs public static <T extends CharSequence> @NonNull T @NonNull [] requireNotEmpty(@NonNull String context, @Nullable T @Nullable ... elements) Checks that the specified varargs array of character sequences is notnull, not empty, and contains nonullor empty elements.- Type Parameters:
T- the type of the character sequence- Parameters:
context- the context description used in the exception messageselements- the elements to check- Returns:
elementsif valid- Throws:
NullPointerException- ifelementsisnullor containsnullelementsIllegalArgumentException- ifelementsis empty or contains empty elements- Since:
- 1.0
-