Class LegacyOptionalExtensions

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

public final class LegacyOptionalExtensions extends Object
Extensions to provide new Optional methods on older Java versions.
  • Method Details

    • isEmpty

      public static <T> boolean isEmpty(@NotNull @NotNull Optional<T> optional)
      Checks if the given optional is empty.
      Type Parameters:
      T - type of the value
      Parameters:
      optional - optional on which the operation happens
      Returns:
      true if the optional is Optional.empty() and false otherwise
      API note
      this method is available on Optional itself since Java 11
    • ifPresentOrElse

      public static <T> void ifPresentOrElse(@NotNull @NotNull Optional<T> optional, @NotNull @NotNull Consumer<? super T> action, @NotNull @NotNull Runnable emptyAction)
      Runs the corresponding action depending on whether the optional is present.
      Type Parameters:
      T - type of the value
      Parameters:
      optional - optional on which the operation happens
      action - action to be run on non-empty optional's value
      emptyAction - action to be run if the optional is empty
      Throws:
      NullPointerException - if the optional is present but action is null or if the optional is empty but emptyAction is null
      API note
      this method is available on Optional itself since Java 9
    • or

      @Contract("_, null -> fail") @NotNull public static <T> @NotNull Optional<T> or(@NotNull @NotNull Optional<T> optional, @NonNull @NonNull Supplier<@NonNull ? extends Optional<? extends T>> supplier)
      Returns the given optional if it is present or an optional provided by the given supplier otherwise.
      Type Parameters:
      T - type of the value
      Parameters:
      optional - optional on which the operation happens
      supplier - supplier of the value in case of optional being Optional.empty()
      Returns:
      given optional if it is present or an optional provided by the given supplier otherwise
      Throws:
      NullPointerException - if supplier is null or the value provided by it is null
      API note
      this method is available on Optional itself since Java 9
    • stream

      public static <T> Stream<T> stream(@NotNull @NotNull Optional<T> optional)
      Returns the stream created from the optional's value if it is present or an empty stream otherwise.
      Type Parameters:
      T - type of the value
      Parameters:
      optional - optional on which the operation happens
      Returns:
      given optional if it is present or an optional provided by the given supplier otherwise
    • orElseThrow

      public static <T> T orElseThrow(@NotNull @NotNull Optional<T> optional)
      Returns the value of the optional if it is present or throws NoSuchElementException otherwise.
      Type Parameters:
      T - type of the value
      Parameters:
      optional - optional on which the operation happens
      Returns:
      the value of the optional if it is present