Class NullSafetyExtensions
java.lang.Object
ru.progrm_jarvis.javacommons.object.extension.NullSafetyExtensions
Extensions to provide null-safety on nullable types.
These methods behave specifically, for example call on
null may not throw NullPointerException
thus they have underscores (_) in their names.-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TReturns the passed value if it is notnulland matches the predicate andnullotherwise.static <T> T_filterNullable(T value, @NonNull Predicate<@Nullable T> predicate) Returns the passed value if it matches the predicate andnullotherwise.static <T> void_ifPresentOrElse(T value, @NonNull Consumer<@NotNull ? super T> action, @NonNull Runnable nullAction) Runs the corresponding action depending on whether the value isnull.static <T,R> R Maps the given value if it isnullotherwise returningnull.static <T,R> R _mapNullable(T value, @NonNull Function<@Nullable T, R> mappingFunction) Maps the given value.static <T> T_or(T value, T defaultValue) Returns the passed value if it notnullotherwise returning the default one.static <T,X extends Throwable>
T_orElseThrow(T value, @NonNull Supplier<@NonNull X> throwableSupplier) Throws the supplied exception if the given value is null.static <T> TReturns the passed value if it notnullotherwise returning the default one.static <T> @NotNull Stream<@NotNull T>_stream(T value) Converts the given value into astreamcontaining the value if is notnullor anempty streamotherwise.static <T> @NotNull Stream<@Nullable T>_streamOfNullable(T value) Converts the given value into astream.static <T> @NotNull Optional<T>_toOptional(T value)
-
Method Details
-
_or
@Contract("!null, _ -> param1; null, _ -> param2") public static <T> T _or(@Nullable T value, @Nullable T defaultValue) Returns the passed value if it notnullotherwise returning the default one.- Type Parameters:
T- type of the value- Parameters:
value- value whose nullability is testeddefaultValue- value returned ifvalueisnull- Returns:
valueif it is notnullanddefaultValueotherwise
-
_orGet
@Contract("_, null -> fail; !null, _ -> param1; null, _ -> _") public static <T> T _orGet(@Nullable T value, @NonNull @NonNull Supplier<@Nullable ? extends T> defaultValueSupplier) Returns the passed value if it notnullotherwise returning the default one.- Type Parameters:
T- type of the value- Parameters:
value- value whose nullability is testeddefaultValueSupplier- supplier of the value returned ifvalueisnull- Returns:
valueif it is notnullanddefaultValueotherwise- Throws:
NullPointerException- ifdefaultValueSupplierisnull
-
_map
@Contract("_, null -> fail; !null, _ -> _; null, _ -> null") @Nullable public static <T,R> R _map(@Nullable T value, @NonNull @NonNull Function<@NotNull T, R> mappingFunction) Maps the given value if it isnullotherwise returningnull.- Type Parameters:
T- type of the source valueR- type of the mapped value- Parameters:
value- value to be mapped if it is notnullmappingFunction- function to be used for mapping of the the non-nullvalue- Returns:
- mapped
valueif the latter was notnullornullotherwise - Throws:
NullPointerException- ifmappingFunctionisnull
-
_mapNullable
@Contract("_, null -> fail; _, _ -> _") @Nullable public static <T,R> R _mapNullable(@Nullable T value, @NonNull @NonNull Function<@Nullable T, R> mappingFunction) Maps the given value.- Type Parameters:
T- type of the source valueR- type of the mapped value- Parameters:
value- value to be mappedmappingFunction- function to be used for mapping of the value- Returns:
- mapped value
- Throws:
NullPointerException- ifmappingFunctionisnull
-
_filter
@Contract("_, null -> fail; !null, _ -> _; null, _ -> null") @Nullable public static <T> T _filter(@Nullable T value, @NonNull @NonNull Predicate<T> predicate) Returns the passed value if it is notnulland matches the predicate andnullotherwise.- Type Parameters:
T- type of the value- Parameters:
value- tested valuepredicate- condition to test the value agains- Returns:
valueif it is notnulland matches the predicate andnullotherwise.- Throws:
NullPointerException- ifpredicateisnull
-
_filterNullable
@Contract("_, null -> fail; _, _ -> _") @Nullable public static <T> T _filterNullable(@Nullable T value, @NonNull @NonNull Predicate<@Nullable T> predicate) Returns the passed value if it matches the predicate andnullotherwise.- Type Parameters:
T- type of the value- Parameters:
value- tested valuepredicate- condition to test the value agains- Returns:
valueif it matches the predicate andnullotherwise.- Throws:
NullPointerException- ifpredicateisnull
-
_orElseThrow
@Contract("null, _ -> fail; _, null -> fail; _, _ -> param1") @NotNull public static <T,X extends Throwable> T _orElseThrow(@Nullable T value, @NonNull @NonNull Supplier<@NonNull X> throwableSupplier) throws X Throws the supplied exception if the given value is null.- Type Parameters:
T- type of the valueX- thrown type throwable- Parameters:
value- checked valuethrowableSupplier- supplier of a throwable used when the value isnull- Returns:
valueif it is notnull- Throws:
X- ifvalueis nullNullPointerException- ifthrowableSupplieror it suppliesnull
-
_ifPresentOrElse
@Contract("_, null, _ -> fail; _, _, null -> fail") public static <T> void _ifPresentOrElse(@Nullable T value, @NonNull @NonNull Consumer<@NotNull ? super T> action, @NonNull @NonNull Runnable nullAction) Runs the corresponding action depending on whether the value isnull.- Type Parameters:
T- type of the value- Parameters:
value- value to be checkedaction- action to be run on non-nullvaluenullAction- action to be run if the value isnull- Throws:
NullPointerException- ifactionornullActionisnull
-
_stream
Converts the given value into astreamcontaining the value if is notnullor anempty streamotherwise.- Type Parameters:
T- type of the value- Parameters:
value- value to be converted into a stream if it is notnull- Returns:
streamcontaining the value if it is notnullor anempty streamotherwise.
-
_streamOfNullable
Converts the given value into astream.- Type Parameters:
T- type of the value- Parameters:
value- value to be converted into a stream if it is notnull- Returns:
streamcontaining the value.
-
_toOptional
@Contract("null -> _; _ -> new") @NotNull public static <T> @NotNull Optional<T> _toOptional(@Nullable T value) - Type Parameters:
T- type of the value- Parameters:
value- value to be converted into an optional- Returns:
- value wrapped into an
Optional
-