Class Option<T>
java.lang.Object
me.lauriichan.laylib.command.util.Option<T>
-
Method Summary
Modifier and TypeMethodDescriptionreturns the value as an optionalstatic <T> Option<T> empty()Returns an emptyOptioninstance.booleanIndicates whether some other object is "equal to" thisOption.If a value is present, and the value matches the given predicate, returns anOptiondescribing the value, otherwise returns an emptyOption.<U> Option<U> If a value is present, returns the result of applying the givenOption-bearing mapping function to the value, otherwise returns an emptyOption.get()returns the valueinthashCode()Returns the hash code of the value, if present, otherwise0(zero) if no value is present.voidIf a value is absent, performs the given action with the value, otherwise does nothing.voidIf 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.booleanIf a value is present, returnstrue, otherwisefalse.<U> Option<U> 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.If a value is present, returns anOptiondescribing the value, otherwise returns anOptionproduced by the supplying function.If a value is present, returns the value, otherwise returnsother.If a value is present, returns the value, otherwise returns the result produced by the supplying function.If a value is present, returns the value, otherwise runs runnableIf a value is present, returns the value, otherwise throwsNoSuchElementException.orElseThrow(Supplier<? extends X> exceptionSupplier) If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.stream()If a value is present, returns a sequentialStreamcontaining only that value, otherwise returns an emptyStream.toString()Returns a non-empty string representation of thisOptionsuitable for debugging.
-
Method Details
-
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
Returns anOptiondescribing the given value.- Type Parameters:
T- the type of the value- Parameters:
value- the value to describe- Returns:
- an
Optionwith the value
-
asOptional
-
get
-
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
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
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
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
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
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
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
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
-
orElse
-
orElseGet
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
If a value is present, returns the value, otherwise runs runnable- Returns:
- the value described by this
Option - Throws:
NullPointerException- if no value is present and the runnable isnull
-
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
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
-
equals
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
-
toString
-