Class Opt
java.lang.Object
org.checkerframework.checker.nullness.util.Opt
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 Summary
Modifier and TypeMethodDescriptionstatic <T> @Nullable TIf primary is non-null, and its value matches the given predicate, return the value.get(T primary) If primary is non-null, returns it, otherwise throws NoSuchElementException.static <T> voidIf primary is non-null, invoke the specified consumer with the value, otherwise do nothing.static booleanReturns true if primary is non-null, false if primary is null.static <T,U> @Nullable U If primary is non-null, apply the provided mapping function to it and return the result.static <T> @NonNull TReturn primary if it is non-null.static <T> @NonNull TReturnprimaryif it is non-null.orElseThrow(T primary, Supplier<? extends X> exceptionSupplier) Return primary if it is non-null.
-
Method Details
-
get
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:
primaryif 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
-
filter
-
map
-
orElse
-
orElseGet
-
orElseThrow
-