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 T
If 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> void
If primary is non-null, invoke the specified consumer with the value, otherwise do nothing.static boolean
Returns 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 T
Return primary if it is non-null.static <T> @NonNull T
Return primary if 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:
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
If primary is non-null, invoke the specified consumer with the value, otherwise do nothing.- See Also:
-
filter
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
Return primary if it is non-null. If primary is null, return other.- See Also:
-
orElseGet
Return primary if it is non-null. If primary is null, invokeother
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 Return primary if it is non-null. If primary is null, throw an exception to be created by the provided supplier.
-