T
- the type of the possible valuepublic interface Possible<T>
Optional
, but as an interface instead of a class. Other implementations
are provided in this package, including ones that allow present null
values and ones that
are mutable.Modifier and Type | Interface and Description |
---|---|
static interface |
Possible.Visitor<T,R>
A visitor of possible values.
|
Modifier and Type | Method and Description |
---|---|
Set<T> |
asSet()
Returns a view of this possible value as a set.
|
Possible<T> |
filter(Predicate<? super T> predicate)
Filters the current value per the specified predicate.
|
<U> Possible<U> |
flatMap(Function<? super T,? extends Possible<U>> function) |
T |
get()
Gets the contained value if present.
|
void |
ifPresent(Consumer<? super T> consumer) |
boolean |
isPresent()
Returns true if a value is present.
|
<U> Possible<U> |
map(Function<? super T,? extends U> function)
Returns the current possible value, transformed by the specified function, if present.
|
static <T> Possible<T> |
notNull(Possible<T> possible)
Converts a possible
null value to an optional one. |
static <T> List<T> |
nullIfNotPresent(Collection<? extends Possible<? extends T>> possibles)
Returns a list of the values,
null if no value is present, from the specified
collection of possible values. |
Possible<T> |
or(Possible<T> alternate)
Returns the current possible value if present or the specified value if not.
|
T |
orElse(T alternate)
Gets the contained value if present or the specified value if not.
|
T |
orElseGet(Supplier<? extends T> supplier) |
<X extends Throwable> |
orElseThrow(Supplier<? extends X> throwableSupplier)
Gets the contained value or gets an exception from the specified source and throws it
|
static <T> List<T> |
presentOnly(Collection<? extends Possible<? extends T>> possibles)
Returns a list of the present values from the specified collection of possible values.
|
static <T> Possible<T> |
snapshot(Possible<? extends T> possible)
Captures the current possible value into a new immutable possible value.
|
<R> R |
visit(Possible.Visitor<? super T,R> visitor)
Invokes the appropriate method on the specified visitor.
|
boolean isPresent()
Possible<T> or(Possible<T> alternate)
alternate
- an alternate value<U> Possible<U> map(Function<? super T,? extends U> function)
function
- the function used to transform the valuePossible<T> filter(Predicate<? super T> predicate)
predicate
- the predicated used to test the valueT get()
NoSuchElementException
- if a value is not presentT orElse(T alternate)
alternate
- the alternate value<X extends Throwable> T orElseThrow(Supplier<? extends X> throwableSupplier) throws X extends Throwable
throwableSupplier
- a supplier of the exception to throw if a value is not presentX
- if a value is not presentX extends Throwable
Set<T> asSet()
<R> R visit(Possible.Visitor<? super T,R> visitor)
visitor
- a visitorstatic <T> Possible<T> notNull(Possible<T> possible)
null
value to an optional one. If the given possible value is
present but null
, an absent value is returned.possible
- a possible valuenull
values treated as absentstatic <T> List<T> presentOnly(Collection<? extends Possible<? extends T>> possibles)
possibles
- a collection of possible valuesstatic <T> List<T> nullIfNotPresent(Collection<? extends Possible<? extends T>> possibles)
null
if no value is present, from the specified
collection of possible values. The resulting list will always have exactly the same number of
elements as the specified collection, even if not all values are present. The order of items
in the list matches the iteration order of the specified collection. The returned list is a
snapshot of the of the values present at the time this method is executed. If any of
the input values are mutable and later become present or absent, that will never be reflected
in the returned list.possibles
- a collection of possible valuesnull
for objects where a value
was not presentstatic <T> Possible<T> snapshot(Possible<? extends T> possible)
possible
- a possible value