@AnnotatedFor(value="nullness") 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.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 can 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)
T
- the type of the argumentprimary
- a non-null value to returnprimary
if it is non-nullNoSuchElementException
- if primary is nullOptional.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)