public final class Opt extends 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) the Checker Framework, 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,
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,
Consumer<? super T> consumer)
If primary is non-null, invoke the specified consumer with the value, otherwise do nothing.
|
static boolean |
isPresent(@Nullable Object primary)
Returns true if primary is non-null, false if primary is null.
|
static <T,U> U |
map(T primary,
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,
Supplier<? extends T> other)
Return primary if it is non-null.
|
static <T,X extends Throwable> |
orElseThrow(T primary,
Supplier<? extends X> exceptionSupplier)
Return primary if it is non-null.
|
public static <T> T get(T primary)
Optional.get()
@EnsuresNonNullIf(expression="#1", result=true) public static boolean isPresent(@Nullable Object primary)
Optional.isPresent()
public static <T> void ifPresent(T primary, Consumer<? super T> consumer)
Optional.ifPresent(Consumer)
public static <T> T filter(T primary, Predicate<? super T> predicate)
Optional.filter(Predicate)
public static <T,U> U map(T primary, 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, Supplier<? extends T> other)
other
and return the
result of that invocation.Optional.orElseGet(Supplier)
public static <T,X extends Throwable> T orElseThrow(T primary, Supplier<? extends X> exceptionSupplier) throws X extends Throwable
X extends Throwable
Optional.orElseThrow(Supplier)