java.lang.Object
org.checkerframework.checker.nullness.util.Opt

@AnnotatedFor("nullness") public final class Opt extends Object
Utility class providing every method in Optional, but written for possibly-null references rather than for the Optional type.

To avoid the need to write the Opt class name at invocation sites, do:

import static org.checkerframework.checker.nullness.util.Opt.orElse;
or
import static org.checkerframework.checker.nullness.util.Opt.*;

Runtime Dependency: If you use this class, you must distribute (or link to) checker-qual.jar, along with your binaries. Or, you can copy this class into your own project.

See Also:
  • Method Details

    • get

      public static <T extends @NonNull Object> T get(T primary)
      If primary is non-null, returns it, otherwise throws NoSuchElementException.
      Type Parameters:
      T - the type of the argument
      Parameters:
      primary - a non-null value to return
      Returns:
      primary if it is non-null
      Throws:
      NoSuchElementException - if primary is null
      See Also:
    • isPresent

      @EnsuresNonNullIf(expression="#1", result=true) public static boolean isPresent(@Nullable Object primary)
      Returns true if primary is non-null, false if primary is null.
      See Also:
    • ifPresent

      public static <T> void ifPresent(T primary, Consumer<@NonNull ? super @NonNull T> consumer)
      If primary is non-null, invoke the specified consumer with the value, otherwise do nothing.
      See Also:
    • ifPresentOrElse

      public static <T> void ifPresentOrElse(T primary, Consumer<@NonNull ? super @NonNull T> action, Runnable emptyAction)
      If primary is non-null, invoke the specified consumer with the value, otherwise invoke the empty action.
      Type Parameters:
      T - the type of the argument
      Parameters:
      primary - the nullable value
      action - the consumer to be executed if primary is non-null
      emptyAction - the action to be executed if primary is null
      See Also:
    • filter

      public static <T> @Nullable T filter(T primary, Predicate<@NonNull ? super @NonNull T> predicate)
      If primary is non-null, and its value matches the given predicate, return the value. If primary is null or its non-null value does not match the predicate, return null.
      See Also:
    • map

      public static <T, U> @Nullable U map(T primary, Function<@NonNull ? super @NonNull T,? extends U> mapper)
      If primary is non-null, apply the provided mapping function to it and return the result. If primary is null, return null.
      See Also:
    • orElse

      public static <T> @NonNull T orElse(T primary, @NonNull T other)
      Returns primary if it is non-null. If primary is null, return other.
      See Also:
    • orElseGet

      public static <T> @NonNull T orElseGet(T primary, Supplier<? extends @NonNull T> other)
      Returns primary if it is non-null. If primary is null, invoke other and return the result of that invocation.
      See Also:
    • orElseThrow

      public static <T extends @NonNull Object, X extends @NonNull Throwable> T orElseThrow(T primary, Supplier<? extends X> exceptionSupplier) throws X
      Returns primary if it is non-null. If primary is null, throw an exception to be created by the provided supplier.
      Throws:
      X
      See Also: