Class NullSafetyExtensions

java.lang.Object
ru.progrm_jarvis.javacommons.object.extension.NullSafetyExtensions

public final class NullSafetyExtensions extends Object
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 Type
    Method
    Description
    static <T> T
    _filter(T value, @NonNull Predicate<T> predicate)
    Returns the passed value if it is not null and matches the predicate and null otherwise.
    static <T> T
    _filterNullable(T value, @NonNull Predicate<@Nullable T> predicate)
    Returns the passed value if it matches the predicate and null otherwise.
    static <T> void
    _ifPresentOrElse(T value, @NonNull Consumer<@NotNull ? super T> action, @NonNull Runnable nullAction)
    Runs the corresponding action depending on whether the value is null.
    static <T, R> R
    _map(T value, @NonNull Function<@NotNull T,R> mappingFunction)
    Maps the given value if it is null otherwise returning null.
    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 not null otherwise 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> T
    _orGet(T value, @NonNull Supplier<@Nullable ? extends T> defaultValueSupplier)
    Returns the passed value if it not null otherwise returning the default one.
    static <T> @NotNull Stream<@NotNull T>
    _stream(T value)
    Converts the given value into a stream containing the value if is not null or an empty stream otherwise.
    static <T> @NotNull Stream<@Nullable T>
    Converts the given value into a stream.
    static <T> @NotNull Optional<T>
    _toOptional(T value)
    Converts the value into an optional returning Optional.empty() if the value is null.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 not null otherwise returning the default one.
      Type Parameters:
      T - type of the value
      Parameters:
      value - value whose nullability is tested
      defaultValue - value returned if value is null
      Returns:
      value if it is not null and defaultValue otherwise
    • _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 not null otherwise returning the default one.
      Type Parameters:
      T - type of the value
      Parameters:
      value - value whose nullability is tested
      defaultValueSupplier - supplier of the value returned if value is null
      Returns:
      value if it is not null and defaultValue otherwise
      Throws:
      NullPointerException - if defaultValueSupplier is null
    • _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 is null otherwise returning null.
      Type Parameters:
      T - type of the source value
      R - type of the mapped value
      Parameters:
      value - value to be mapped if it is not null
      mappingFunction - function to be used for mapping of the the non-null value
      Returns:
      mapped value if the latter was not null or null otherwise
      Throws:
      NullPointerException - if mappingFunction is null
    • _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 value
      R - type of the mapped value
      Parameters:
      value - value to be mapped
      mappingFunction - function to be used for mapping of the value
      Returns:
      mapped value
      Throws:
      NullPointerException - if mappingFunction is null
    • _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 not null and matches the predicate and null otherwise.
      Type Parameters:
      T - type of the value
      Parameters:
      value - tested value
      predicate - condition to test the value agains
      Returns:
      value if it is not null and matches the predicate and null otherwise.
      Throws:
      NullPointerException - if predicate is null
    • _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 and null otherwise.
      Type Parameters:
      T - type of the value
      Parameters:
      value - tested value
      predicate - condition to test the value agains
      Returns:
      value if it matches the predicate and null otherwise.
      Throws:
      NullPointerException - if predicate is null
    • _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 value
      X - thrown type throwable
      Parameters:
      value - checked value
      throwableSupplier - supplier of a throwable used when the value is null
      Returns:
      value if it is not null
      Throws:
      X - if value is null
      NullPointerException - if throwableSupplier or it supplies null
    • _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 is null.
      Type Parameters:
      T - type of the value
      Parameters:
      value - value to be checked
      action - action to be run on non-null value
      nullAction - action to be run if the value is null
      Throws:
      NullPointerException - if action or nullAction is null
    • _stream

      @NotNull public static <T> @NotNull Stream<@NotNull T> _stream(@Nullable T value)
      Converts the given value into a stream containing the value if is not null or an empty stream otherwise.
      Type Parameters:
      T - type of the value
      Parameters:
      value - value to be converted into a stream if it is not null
      Returns:
      stream containing the value if it is not null or an empty stream otherwise.
    • _streamOfNullable

      @NotNull public static <T> @NotNull Stream<@Nullable T> _streamOfNullable(@Nullable T value)
      Converts the given value into a stream.
      Type Parameters:
      T - type of the value
      Parameters:
      value - value to be converted into a stream if it is not null
      Returns:
      stream containing the value.
    • _toOptional

      @Contract("null -> _; _ -> new") @NotNull public static <T> @NotNull Optional<T> _toOptional(@Nullable T value)
      Converts the value into an optional returning Optional.empty() if the value is null.
      Type Parameters:
      T - type of the value
      Parameters:
      value - value to be converted into an optional
      Returns:
      value wrapped into an Optional