For maximum generality, all type parameters of methods in this class are not constraint to either non-null nor nullable. Users of these methods can freely choose these types.
Methods in this class come in three groups: Assertions, Requirements, as well as Queries, conversions and computations.
All methods in this group start with the prefix "assert", and work similar to the assert keyword.
null.null, an exception is thrown.All methods in this group start with the prefix "require". They encode domain knowledge of the developer and make this knowledge visible to static null analysis.
null.null, an exception is thrown.For Strings and Collections additional checks for empty may be included.
Typical usage includes the case of interfacing with legacy API, that is not specified using null annotations, but where corresponding guarantees are given informally:
interface LegacyThing {
/** @return the name of this thing, never <code>null</code>. */
String getName();
}
...
public void process(@NonNull LegacyThing t) {
@NonNull String name = Checks.requireNonNull(t.getName(), "LegacyThing is supposed to have a name");
...
}
A commonality among methods in this group is the null-safety that can be verified by static null analysis, hence in a fully analyzed program none of these methods should throw an exception.
isNull(Object), isAnyNull(Object...) , containsNull(Iterable) simply
answer whether a null value could be found in the argument(s).nonNullElse(Object, Object), nonNullElseGet(Object, Supplier), asNullable(Optional)
and the unboxElse(Boolean, boolean) family of methods provide null-safe conversions,
which can be checked by static analysis.ifNonNull(Object, Consumer), applyIfNonNull(Object, Function),
applyIfNonNullElse(Object, Function, Object) and applyIfNonNullElseGet(Object, Function, Supplier)
feed unsafe values into a given functional expression in a null-safe way.| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public static <T, U> @Nullable U | Returns: the result of applying 'function' with 'value', ornull if 'value' was nullthe value to be checked for value, @NonNull Function<@NonNull ? super T, ? extends U> null and possibly passed into the function.the function to apply after checking the value for function)null.Apply the given function if and only if the given value is not |
| public static <T, U> U | Returns: the result of applying 'function' with 'value', or the 'fallbackValue' if 'value' wasnullthe value to be checked for value, @NonNull Function<@NonNull ? super T, ? extends U> null and possibly passed into the function.the function to apply after checking the value for function, U null.value to be returned when the 'value' is fallbackValue)null.Apply the given function if and only if the given value is not |
| public static <T, U> U | Returns: the result of applying 'function' with 'value', or a value provided by invoking 'fallbackSupplier' if 'value' wasnullthe value to be checked for value, @NonNull Function<@NonNull ? super T, ? extends U> null and possibly passed into the function.the function to apply after checking the value for function, @NonNull Supplier<? extends U> fallbackSupplier)null.Apply the given function if and only if the given value is not |
| public static <T> @Nullable T | Returns: the value ornull.wrapper for an optional value optional)Answer the value of an |
| public static <T> void | assertNonNull(@NonNull T...
arbitrary values to be checked values)Checks whether any of the provided values is |
| public static <T> void | assertNonNullElements(@NonNull Iterable<T>
an Iterable of arbitrary values to be checked values)Checks whether any element in the provided values is |
| public static <T> void | assertNonNullElements(@NonNull Iterable<T>
an iterable of arbitrary values to be checked values, String explanatory message to be used when throwing message)NullPointerException.Checks whether any of the provided values is |
| public static <T> void | assertNonNullWithMessage(String
explanatory message to be used when throwing message, @NonNull T... NullPointerException.arbitrary values to be checked values)Checks whether any of the provided values is |
| public static boolean | Returns: true if the argument contains the valuenull, else false.an iterable of arbitrary values values)Answer whether an element in the provided values is |
| public static <T> void | |
| public static <T> boolean | |
| public static boolean | |
| public static <T> @NonNull T | Returns: the original passed value, guaranteed not to benull, or the fallback value.an arbitrary value, maybe value, @NonNull T null.value to be returned when the value is fallbackValue)null.Answer the given value as a non-null value. |
| public static <T> @NonNull T | Returns: the original passed value, guaranteed not to benull,
or a fallback value provided by the 'fallbackSupplier'.an arbitrary value, maybe value, @NonNull Supplier<? extends @NonNull T> null.will compute the value to be returned when the 'value' is fallbackSupplier)null.Answer the given value as a non-null value. |
| public static @NonNull String | Returns: the given value, guaranteed to be neithernull nor
and empty string.value to be checked value)Answer the given value, guaranteeing it to be neither |
| public static @NonNull String | Returns: the given value, guaranteed to be neithernull nor
an empty string.value to be checked value, String explanatory message to be used when throwing an exception. message)Answer the given value, guaranteeing it to be neither |
| public static <C extends Collection | Returns: the given value, guaranteed to be neithernull nor
an empty collection.value to be checked value)Answer the given value, guaranteeing it to be neither |
| public static <C extends Collection | Returns: the given value, guaranteed to be neithernull nor
an empty collection.value to be checked value, String explanatory message to be used when throwing an exception. message)Answer the given value, guaranteeing it to be neither |
| public static <T> @NonNull T | Returns: the original passed value, guaranteed not to benull.an arbitrary value, maybe value)null.Answer the given value as a non-null value. |
| public static <T> @NonNull T | Returns: the original passed value, guaranteed not to benull.an arbitrary value, maybe value, @NonNull String null.explanatory message to be used when throwing message)NullPointerException.Answer the given value as a non-null value. |
| public static boolean | |
| public static byte | |
| public static char | |
| public static int | |
| public static long | |
| public static short | |
| public static float | |
| public static double |
| Checks | back to summary |
|---|---|
| public Checks() | |
| applyIfNonNull | back to summary |
|---|---|
| public static <T, U> @Nullable U applyIfNonNull(@Nullable T value, @NonNull Function<@NonNull ? super T, ? extends U> function) Apply the given function if and only if the given value is not
| |
| applyIfNonNullElse | back to summary |
|---|---|
| public static <T, U> U applyIfNonNullElse(@Nullable T value, @NonNull Function<@NonNull ? super T, ? extends U> function, U fallbackValue) Apply the given function if and only if the given value is not
| |
| applyIfNonNullElseGet | back to summary |
|---|---|
| public static <T, U> U applyIfNonNullElseGet(@Nullable T value, @NonNull Function<@NonNull ? super T, ? extends U> function, @NonNull Supplier<? extends U> fallbackSupplier) Apply the given function if and only if the given value is not
| |
| asNullable | back to summary |
|---|---|
| public static <T> @Nullable T asNullable(@NonNull Optional<T> optional) Answer the value of an | |
| assertNonNull | back to summary |
|---|---|
| public static <T> void assertNonNull(@NonNull T... values) Checks whether any of the provided values is
| |
| assertNonNullElements | back to summary |
|---|---|
| public static <T> void assertNonNullElements(@NonNull Iterable<T> values) Checks whether any element in the provided values is
| |
| assertNonNullElements | back to summary |
|---|---|
| public static <T> void assertNonNullElements(@NonNull Iterable<T> values, String message) Checks whether any of the provided values is
| |
| assertNonNullWithMessage | back to summary |
|---|---|
| public static <T> void assertNonNullWithMessage(String message, @NonNull T... values) Checks whether any of the provided values is
| |
| containsNull | back to summary |
|---|---|
| public static boolean containsNull(@NonNull Iterable<?> values) Answer whether an element in the provided values is | |
| ifNonNull | back to summary |
|---|---|
| public static <T> void ifNonNull(@Nullable T value, @NonNull Consumer<@NonNull ? super T> consumer) Invoke the given consumer if and only if the given value is not | |
| isAnyNull | back to summary |
|---|---|
| public static <T> boolean isAnyNull(@NonNull T... values) Answer whether any of the given values is
| |
| isNull | back to summary |
|---|---|
| public static boolean isNull(@NonNull Object value) Answer whether the given value is
| |
| nonNullElse | back to summary |
|---|---|
| public static <T> @NonNull T nonNullElse(@Nullable T value, @NonNull T fallbackValue) Answer the given value as a non-null value.
If the value was actually | |
| nonNullElseGet | back to summary |
|---|---|
| public static <T> @NonNull T nonNullElseGet(@Nullable T value, @NonNull Supplier<? extends @NonNull T> fallbackSupplier) Answer the given value as a non-null value.
If the value was actually
| |
| requireNonEmpty | back to summary |
|---|---|
| public static @NonNull String requireNonEmpty(@Nullable String value) Answer the given value, guaranteeing it to be neither
| |
| requireNonEmpty | back to summary |
|---|---|
| public static @NonNull String requireNonEmpty(@Nullable String value, String message) Answer the given value, guaranteeing it to be neither
| |
| requireNonEmpty | back to summary |
|---|---|
| public static <C extends Collection Answer the given value, guaranteeing it to be neither
| |
| requireNonEmpty | back to summary |
|---|---|
| public static <C extends Collection Answer the given value, guaranteeing it to be neither
| |
| requireNonNull | back to summary |
|---|---|
| public static <T> @NonNull T requireNonNull(@Nullable T value) Answer the given value as a non-null value.
Throws
| |
| requireNonNull | back to summary |
|---|---|
| public static <T> @NonNull T requireNonNull(@Nullable T value, @NonNull String message) Answer the given value as a non-null value.
Throws
| |
| unboxElse | back to summary |
|---|---|
| public static boolean unboxElse(@Nullable Boolean boxedValue, boolean fallbackValue) Unbox the given 'boxedValue' if and only if it is not | |
| unboxElse | back to summary |
|---|---|
| public static byte unboxElse(@Nullable Byte boxedValue, byte fallbackValue) Unbox the given 'boxedValue' if and only if it is not | |
| unboxElse | back to summary |
|---|---|
| public static char unboxElse(@Nullable Character boxedValue, char fallbackValue) Unbox the given 'boxedValue' if and only if it is not | |
| unboxElse | back to summary |
|---|---|
| public static int unboxElse(@Nullable Integer boxedValue, int fallbackValue) Unbox the given 'boxedValue' if and only if it is not | |
| unboxElse | back to summary |
|---|---|
| public static long unboxElse(@Nullable Long boxedValue, long fallbackValue) Unbox the given 'boxedValue' if and only if it is not | |
| unboxElse | back to summary |
|---|---|
| public static short unboxElse(@Nullable Short boxedValue, short fallbackValue) Unbox the given 'boxedValue' if and only if it is not | |
| unboxElse | back to summary |
|---|---|
| public static float unboxElse(@Nullable Float boxedValue, float fallbackValue) Unbox the given 'boxedValue' if and only if it is not | |
| unboxElse | back to summary |
|---|---|
| public static double unboxElse(@Nullable Double boxedValue, double fallbackValue) Unbox the given 'boxedValue' if and only if it is not | |