Class Opt


  • @AnnotatedFor("nullness")
    public final class Opt
    extends java.lang.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:
    Optional
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> @Nullable T filter​(T primary, java.util.function.Predicate<? super @NonNull T> predicate)
      If primary is non-null, and its value matches the given predicate, return the value.
      static <T extends @NonNull java.lang.Object>
      T
      get​(T primary)
      If primary is non-null, returns it, otherwise throws NoSuchElementException.
      static <T> void ifPresent​(T primary, java.util.function.Consumer<? super @NonNull T> consumer)
      If primary is non-null, invoke the specified consumer with the value, otherwise do nothing.
      static boolean isPresent​(@Nullable java.lang.Object primary)
      Returns true if primary is non-null, false if primary is null.
      static <T,​U>
      @Nullable U
      map​(T primary, java.util.function.Function<? super @NonNull T,​? extends U> mapper)
      If primary is non-null, apply the provided mapping function to it and return the result.
      static <T> @NonNull T orElse​(T primary, @NonNull T other)
      Return primary if it is non-null.
      static <T> @NonNull T orElseGet​(T primary, java.util.function.Supplier<? extends @NonNull T> other)
      Return primary if it is non-null.
      static <T extends @NonNull java.lang.Object,​X extends @NonNull java.lang.Throwable>
      T
      orElseThrow​(T primary, java.util.function.Supplier<? extends X> exceptionSupplier)
      Return primary if it is non-null.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • get

        public static <T extends @NonNull java.lang.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:
        java.util.NoSuchElementException - if primary is null
        See Also:
        Optional.get()
      • isPresent

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

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

        public static <T> @Nullable T filter​(T primary,
                                             java.util.function.Predicate<? 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:
        Optional.filter(Predicate)
      • map

        public static <T,​U> @Nullable U map​(T primary,
                                                  java.util.function.Function<? 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:
        Optional.map(Function)
      • orElse

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

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

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