Class Opt
- java.lang.Object
-
- org.checkerframework.checker.nullness.util.Opt
-
@AnnotatedFor("nullness") public final class Opt extends java.lang.Object
Utility class providing every method inOptional
, but written for possibly-null references rather than for theOptional
type.To avoid the need to write the
Opt
class name at invocation sites, do:import static org.checkerframework.checker.nullness.util.Opt.orElse;
orimport 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:
Optional
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> @Nullable T
filter(T primary, java.util.function.Predicate<? super @NonNull T> predicate)
If primary is non-null, and its value matches the given predicate, return the value.static <T extends @NonNull java.lang.Object>
Tget(T primary)
If primary is non-null, returns it, otherwise throws NoSuchElementException.static <T> void
ifPresent(T primary, java.util.function.Consumer<? super @NonNull 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>
@Nullable Umap(T primary, java.util.function.Function<? super @NonNull T,? extends U> mapper)
If primary is non-null, apply the provided mapping function to it and return the result.static <T> @NonNull T
orElse(T primary, @NonNull T other)
Return primary if it is non-null.static <T> @NonNull T
orElseGet(T primary, java.util.function.Supplier<? extends @NonNull T> other)
Returnprimary
if it is non-null.static <T extends @NonNull java.lang.Object,X extends @NonNull java.lang.Throwable>
TorElseThrow(T primary, java.util.function.Supplier<? extends X> exceptionSupplier)
Return primary if it is non-null.
-
-
-
Method Detail
-
get
public static <T extends @NonNull java.lang.Object> T get(T primary)
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:
java.util.NoSuchElementException
- if primary is null- See Also:
Optional.get()
-
isPresent
@EnsuresNonNullIf(expression="#1", result=true) public static boolean isPresent(@Nullable java.lang.Object primary)
Returns true if primary is non-null, false if primary is null.- See Also:
Optional.isPresent()
-
ifPresent
public static <T> void ifPresent(T primary, java.util.function.Consumer<? super @NonNull T> consumer)
If primary is non-null, invoke the specified consumer with the value, otherwise do nothing.- See Also:
Optional.ifPresent(Consumer)
-
filter
public static <T> @Nullable T filter(T primary, java.util.function.Predicate<? super @NonNull T> predicate)
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:
Optional.filter(Predicate)
-
map
public static <T,U> @Nullable U map(T primary, java.util.function.Function<? 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:
Optional.map(Function)
-
orElse
public static <T> @NonNull T orElse(T primary, @NonNull T other)
Return primary if it is non-null. If primary is null, return other.- See Also:
Optional.orElse(Object)
-
orElseGet
public static <T> @NonNull T orElseGet(T primary, java.util.function.Supplier<? extends @NonNull T> other)
Returnprimary
if it is non-null. Ifprimary
is null, invokeother
and return the result of that invocation.- See Also:
Optional.orElseGet(Supplier)
-
orElseThrow
public static <T extends @NonNull java.lang.Object,X extends @NonNull java.lang.Throwable> T orElseThrow(T primary, java.util.function.Supplier<? extends X> exceptionSupplier) throws X extends @NonNull java.lang.Throwable
Return primary if it is non-null. If primary is null, throw an exception to be created by the provided supplier.- Throws:
X extends @NonNull java.lang.Throwable
- See Also:
Optional.orElseThrow(Supplier)
-
-