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 emptyOptioninstance.booleanequals(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 anOptiondescribing 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.Tget()returns the valueinthashCode()Returns the hash code of the value, if present, otherwise0(zero) if no value is present.voidifAbsent(Runnable action)If a value is absent, performs the given action with the value, otherwise does nothing.voidifPresent(Consumer<? super T> action)If a value is present, performs the given action with the value, otherwise does nothing.voidifPresentOrElse(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.booleanisEmpty()If a value is not present, returnstrue, otherwisefalse.booleanisPresent()If a value is present, returnstrue, otherwisefalse.<U> Option<U>map(Function<? super T,? extends U> mapper)If a value is present, returns anOptiondescribing the result of applying the given mapping function to the value, otherwise returns an emptyOption.static <T> Option<T>of(T value)Returns anOptiondescribing the given value.Option<T>or(Supplier<? extends Option<? extends T>> supplier)If a value is present, returns anOptiondescribing the value, otherwise returns anOptionproduced by the supplying function.TorElse(T other)If a value is present, returns the value, otherwise returnsother.TorElseGet(Supplier<? extends T> supplier)If a value is present, returns the value, otherwise returns the result produced by the supplying function.TorElseRun(Runnable runnable)If a value is present, returns the value, otherwise runs runnableTorElseThrow()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 sequentialStreamcontaining only that value, otherwise returns an emptyStream.StringtoString()Returns a non-empty string representation of thisOptionsuitable for debugging.
-
-
-
Method Detail
-
empty
public static <T> Option<T> empty()
Returns an emptyOptioninstance. 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 anOptiondescribing the given value.- Type Parameters:
T- the type of the value- Parameters:
value- the value to describe- Returns:
- an
Optionwith 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:
trueif a value is present, otherwisefalse
-
isEmpty
public boolean isEmpty()
If a value is not present, returnstrue, otherwisefalse.- Returns:
trueif 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 anOptiondescribing the value, otherwise returns an emptyOption.- Parameters:
predicate- the predicate to apply to a value, if present- Returns:
- an
Optiondescribing 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 anOptiondescribing the result of applying the given mapping function to the value, otherwise returns an emptyOption.If the mapping function returns a
nullresult 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
Optiondescribing 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,flatMapdoes not wrap it within an additionalOption.- Type Parameters:
U- The type of value of theOptionreturned 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 isnullor returns anullresult
-
or
public Option<T> or(Supplier<? extends Option<? extends T>> supplier)
If a value is present, returns anOptiondescribing the value, otherwise returns anOptionproduced by the supplying function.- Parameters:
supplier- the supplying function that produces anOptionto be returned- Returns:
- returns an
Optiondescribing the value of thisOption, if a value is present, otherwise anOptionproduced by the supplying function. - Throws:
NullPointerException- if the supplying function isnullor produces anullresult
-
stream
public Stream<T> stream()
If a value is present, returns a sequentialStreamcontaining 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-
nullvalue 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 isnullX 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
Optionand; - 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.
-
-