public final class Opt
extends java.lang.Object
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.Opt.orElse;or
import static org.checkerframework.checker.nullness.Opt.*;
Runtime Dependency
Please note that using this class introduces a runtime dependency. This means that you need to
distribute (or link to) checker-qual.jar
, along with your binaries.
To eliminate this dependency, you can simply copy this class into your own project.
Optional
Modifier and Type | Method and Description |
---|---|
static <T> T |
filter(T primary,
java.util.function.Predicate<? super T> predicate)
If primary is non-null, and its value matches the given predicate, return the value.
|
static <T> 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 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> U |
map(T primary,
java.util.function.Function<? super T,? extends U> mapper)
If primary is non-null, apply the provided mapping function to it and return the result.
|
static <T> T |
orElse(T primary,
T other)
Return primary if it is non-null.
|
static <T> T |
orElseGet(T primary,
java.util.function.Supplier<? extends T> other)
Return primary if it is non-null.
|
static <T,X extends java.lang.Throwable> |
orElseThrow(T primary,
java.util.function.Supplier<? extends X> exceptionSupplier)
Return primary if it is non-null.
|
public static <T> T get(T primary)
primary
- a non-null value to returnprimary
if it is non-nulljava.util.NoSuchElementException
- if primary is nullOptional.get()
@EnsuresNonNullIf(expression="#1", result=true) public static boolean isPresent(@Nullable java.lang.Object primary)
Optional.isPresent()
public static <T> void ifPresent(T primary, java.util.function.Consumer<? super T> consumer)
Optional.ifPresent(Consumer)
public static <T> T filter(T primary, java.util.function.Predicate<? super T> predicate)
Optional.filter(Predicate)
public static <T,U> U map(T primary, java.util.function.Function<? super T,? extends U> mapper)
Optional.map(Function)
public static <T> T orElse(T primary, T other)
Optional.orElse(Object)
public static <T> T orElseGet(T primary, java.util.function.Supplier<? extends T> other)
other
and return the
result of that invocation.Optional.orElseGet(Supplier)
public static <T,X extends java.lang.Throwable> T orElseThrow(T primary, java.util.function.Supplier<? extends X> exceptionSupplier) throws X extends java.lang.Throwable
X extends java.lang.Throwable
Optional.orElseThrow(Supplier)