Package me.nemo_64.betterinputs.api.util
Class Option<T>
- java.lang.Object
-
- me.nemo_64.betterinputs.api.util.Option<T>
-
public final class Option<T> extends Object
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Optional<T>
asOptional()
returns the value as an optionalRef<T>
asReference()
returns the value as an referencestatic <T> Option<T>
empty()
Returns an emptyOption
instance.boolean
equals(Object obj)
Indicates whether some other object is "equal to" thisOption
.Option<T>
filter(Predicate<? super T> predicate)
If a value is present, and the value matches the given predicate, returns anOption
describing the value, otherwise returns an emptyOption
.<U> Option<U>
flatMap(Function<? super T,? extends Option<? extends U>> mapper)
If a value is present, returns the result of applying the givenOption
-bearing mapping function to the value, otherwise returns an emptyOption
.T
get()
returns the valueint
hashCode()
Returns the hash code of the value, if present, otherwise0
(zero) if no value is present.void
ifAbsent(Runnable action)
If a value is absent, performs the given action with the value, otherwise does nothing.void
ifPresent(Consumer<? super T> action)
If a value is present, performs the given action with the value, otherwise does nothing.void
ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction)
If a value is present, performs the given action with the value, otherwise performs the given empty-based action.boolean
isEmpty()
If a value is not present, returnstrue
, otherwisefalse
.boolean
isPresent()
If a value is present, returnstrue
, otherwisefalse
.<U> Option<U>
map(Function<? super T,? extends U> mapper)
If a value is present, returns anOption
describing the result of applying the given mapping function to the value, otherwise returns an emptyOption
.static <T> Option<T>
of(T value)
Returns anOption
describing the given value.Option<T>
or(Supplier<? extends Option<? extends T>> supplier)
If a value is present, returns anOption
describing the value, otherwise returns anOption
produced by the supplying function.T
orElse(T other)
If a value is present, returns the value, otherwise returnsother
.T
orElseGet(Supplier<? extends T> supplier)
If a value is present, returns the value, otherwise returns the result produced by the supplying function.T
orElseRun(Runnable runnable)
If a value is present, returns the value, otherwise runs runnableT
orElseThrow()
If a value is present, returns the value, otherwise throwsNoSuchElementException
.<X extends Throwable>
TorElseThrow(Supplier<? extends X> exceptionSupplier)
If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.Stream<T>
stream()
If a value is present, returns a sequentialStream
containing only that value, otherwise returns an emptyStream
.String
toString()
Returns a non-empty string representation of thisOption
suitable for debugging.
-
-
-
Method Detail
-
empty
public static <T> Option<T> empty()
Returns an emptyOption
instance. No value is present for thisOption
.- Type Parameters:
T
- The type of the non-existent value- Returns:
- an empty
Option
-
of
public static <T> Option<T> of(T value)
Returns anOption
describing the given value.- Type Parameters:
T
- the type of the value- Parameters:
value
- the value to describe- Returns:
- an
Option
with the value
-
get
public T get()
returns the value- Returns:
- the value described by this
Option
-
isPresent
public boolean isPresent()
If a value is present, returnstrue
, otherwisefalse
.- Returns:
true
if a value is present, otherwisefalse
-
isEmpty
public boolean isEmpty()
If a value is not present, returnstrue
, otherwisefalse
.- Returns:
true
if a value is not present, otherwisefalse
-
ifPresent
public void ifPresent(Consumer<? super T> action)
If a value is present, performs the given action with the value, otherwise does nothing.- Parameters:
action
- the action to be performed, if a value is present- Throws:
NullPointerException
- if value is present and the given action isnull
-
ifPresentOrElse
public void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction)
If a value is present, performs the given action with the value, otherwise performs the given empty-based action.- Parameters:
action
- the action to be performed, if a value is presentemptyAction
- the empty-based action to be performed, if no value is present- Throws:
NullPointerException
- if a value is present and the given action isnull
, or no value is present and the given empty-based action isnull
.
-
ifAbsent
public void ifAbsent(Runnable action)
If a value is absent, performs the given action with the value, otherwise does nothing.- Parameters:
action
- the action to be performed, if a value is absent- Throws:
NullPointerException
- if value is absent and the given action isnull
-
filter
public Option<T> filter(Predicate<? super T> predicate)
If a value is present, and the value matches the given predicate, returns anOption
describing the value, otherwise returns an emptyOption
.- Parameters:
predicate
- the predicate to apply to a value, if present- Returns:
- an
Option
describing the value of thisOption
, if a value is present and the value matches the given predicate, otherwise an emptyOption
- Throws:
NullPointerException
- if the predicate isnull
-
map
public <U> Option<U> map(Function<? super T,? extends U> mapper)
If a value is present, returns anOption
describing the result of applying the given mapping function to the value, otherwise returns an emptyOption
.If the mapping function returns a
null
result then this method returns an emptyOption
.- Type Parameters:
U
- The type of the value returned from the mapping function- Parameters:
mapper
- the mapping function to apply to a value, if present- Returns:
- an
Option
describing the result of applying a mapping function to the value of thisOption
, if a value is present, otherwise an emptyOption
- Throws:
NullPointerException
- if the mapping function isnull
-
flatMap
public <U> Option<U> flatMap(Function<? super T,? extends Option<? extends U>> mapper)
If a value is present, returns the result of applying the givenOption
-bearing mapping function to the value, otherwise returns an emptyOption
.This method is similar to
map(Function)
, but the mapping function is one whose result is already anOption
, and if invoked,flatMap
does not wrap it within an additionalOption
.- Type Parameters:
U
- The type of value of theOption
returned by the mapping function- Parameters:
mapper
- the mapping function to apply to a value, if present- Returns:
- the result of applying an
Option
-bearing mapping function to the value of thisOption
, if a value is present, otherwise an emptyOption
- Throws:
NullPointerException
- if the mapping function isnull
or returns anull
result
-
or
public Option<T> or(Supplier<? extends Option<? extends T>> supplier)
If a value is present, returns anOption
describing the value, otherwise returns anOption
produced by the supplying function.- Parameters:
supplier
- the supplying function that produces anOption
to be returned- Returns:
- returns an
Option
describing the value of thisOption
, if a value is present, otherwise anOption
produced by the supplying function. - Throws:
NullPointerException
- if the supplying function isnull
or produces anull
result
-
stream
public Stream<T> stream()
If a value is present, returns a sequentialStream
containing only that value, otherwise returns an emptyStream
.- Returns:
- the optional value as a
Stream
-
orElse
public T orElse(T other)
If a value is present, returns the value, otherwise returnsother
.- Parameters:
other
- the value to be returned, if no value is present. May benull
.- Returns:
- the value, if present, otherwise
other
-
orElseGet
public T orElseGet(Supplier<? extends T> supplier)
If a value is present, returns the value, otherwise returns the result produced by the supplying function.- Parameters:
supplier
- the supplying function that produces a value to be returned- Returns:
- the value, if present, otherwise the result produced by the supplying function
- Throws:
NullPointerException
- if no value is present and the supplying function isnull
-
orElseRun
public T orElseRun(Runnable runnable)
If a value is present, returns the value, otherwise runs runnable- Parameters:
runnable
- the runnable to be execute if no value is present- Returns:
- the value described by this
Option
- Throws:
NullPointerException
- if no value is present and the runnable isnull
-
orElseThrow
public T orElseThrow()
If a value is present, returns the value, otherwise throwsNoSuchElementException
.- Returns:
- the non-
null
value described by thisOption
- Throws:
NoSuchElementException
- if no value is present
-
orElseThrow
public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X extends Throwable
If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.- Type Parameters:
X
- Type of the exception to be thrown- Parameters:
exceptionSupplier
- the supplying function that produces an exception to be thrown- Returns:
- the value, if present
- Throws:
X
- if no value is presentNullPointerException
- if no value is present and the exception supplying function isnull
X extends Throwable
-
equals
public boolean equals(Object obj)
Indicates whether some other object is "equal to" thisOption
. The other object is considered equal if:- it is also an
Option
and; - both instances have no value present or;
- the present values are "equal to" each other via
equals()
.
- it is also an
-
hashCode
public int hashCode()
Returns the hash code of the value, if present, otherwise0
(zero) if no value is present.
-
-