Class Option<T>
- Type Parameters:
T-
- All Implemented Interfaces:
SafelyWrapped<T>,Wrapped<T>
Some value or None.
Why this and not Optional?
This implementation aims to be a - slightly - faster implementation of Optional, ditching all checks that comes at every method call that needs to assess if
the value is empty or not before doing anything, in favour of fixed implementations.
For example: when calling map(Function) it doesn't
need to check if this is empty or not because Option itself
is abstract, and its inheritors (Some and None) already know if they must execute the mapping function or not.
The same applies to a variety of other methods, so for an intensive of this type of construct
using Option instead of Optional can avoid
** a lot** of condition checks (potentially).
- Author:
- Andrea Coronese
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionIf a value is present, and the value matches the given predicate, returns anOptiondescribing the value, otherwise returns an emptyOption.abstract <U> Option<U> If a value is present, returns the result of applying the givenOptional-bearing mapping function to the value, otherwise returns an emptyOptional.abstract voidExecutesfuncif this isSome, consuming the contained value.abstract voidifPresentOrElse(Consumer<T> whenPresent, Runnable otherwise) booleanisNone()Whether this option contains a value or notabstract booleanisSome()Whether this option contains a value or notabstract <U> Option<U> If a value is present, returns anOptiondescribing (as if bysome(T)) the result of applying the given mapping function to the value, otherwise returns an emptyOption.static <T> @NotNull None<T> none()Creates an empty (None)Optionstatic <T> @NotNull Option<T> Creates a newOptionwith some value in it.static <T> @NotNull Option<T> of(T value) Creates a newOptionwith some value in it.abstract TIf a value is present, returns the value, otherwise returnsother.abstract TIf a value is present, returns the value, otherwise returns the result produced by the supplying function.abstract TIf a value is present returns that value, otherwise throwsNoSuchElementException.orElseThrow(@NotNull Supplier<E> throwable) If a value is present returns that value, otherwise throws the suppliedThrowable.static <T> @NotNull Option<T> some(T value) Creates a newOptionwith some value in it.stream()Streams the contained value, if any.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.storynode.pigeon.protocol.SafelyWrapped
tryUnwrap
-
Constructor Details
-
Option
public Option()
-
-
Method Details
-
some
@Contract(value="_ -> new", pure=true) @NotNull public static <T> @NotNull Option<T> some(@NotNull T value) Creates a newOptionwith some value in it. If the provided value is null then the option will be empty (None).- Type Parameters:
T- The type of the inner value- Parameters:
value- The value to wrap- Returns:
- The created
Option
-
of
Creates a newOptionwith some value in it. If the provided value is null then the option will be empty (None).- Type Parameters:
T- The type of the inner value- Parameters:
value- The value to wrap- Returns:
- The created
Option
-
of
Creates a newOptionwith some value in it. If the provided value is null then the option will be empty (None).- Type Parameters:
T- The type of the inner value- Parameters:
valueSupplier- The supplier for the value to wrap- Returns:
- The created
Option
-
none
Creates an empty (None)Option- Type Parameters:
T- a T class- Returns:
- a
Noneobject
-
isSome
public abstract boolean isSome()Whether this option contains a value or not- Returns:
trueif this contains a value,falseotherwise- See Also:
-
isNone
public boolean isNone()Whether this option contains a value or not- Returns:
falseif this contains a value,trueotherwise- See Also:
-
ifPresent
Executesfuncif this isSome, consuming the contained value.- Parameters:
func- The function to execute
-
ifPresentOrElse
-
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 (as if bysome(T)) 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 givenOptional-bearing mapping function to the value, otherwise returns an emptyOptional.This method is similar to
map(Function), but the mapping function is one whose result is already anOptional, and if invoked,flatMapdoes not wrap it within an additionalOptional.- Type Parameters:
U- The type of value of theOptionalreturned by the mapping function- Parameters:
mapper- the mapping function to apply to a value, if present- Returns:
- the result of applying an
Optional-bearing mapping function to the value of thisOptional, if a value is present, otherwise an emptyOptional
-
or
public abstract Option<? extends T> or(@NotNull @NotNull Supplier<? extends Option<? extends T>> supplier) -
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
-
orElse
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
-
orElseThrow
If a value is present returns that value, otherwise throwsNoSuchElementException.
Please note that this is mainly for API compatibility with
Optionalbut should be avoided in favour oforElse(Object)andorElseGet(Supplier)- Returns:
- The contained value, if present
- Throws:
NoSuchElementException- When the option isNone
-
orElseThrow
public abstract <E extends Throwable> T orElseThrow(@NotNull @NotNull Supplier<E> throwable) throws E If a value is present returns that value, otherwise throws the suppliedThrowable.
Please note that this is mainly for API compatibility with
Optionalbut should be avoided in favour oforElse(Object)andorElseGet(Supplier)- Type Parameters:
E- The concrete type of theThrowable- Parameters:
throwable- The function that supplies the exception to throw- Returns:
- a T object
- Throws:
E- if any.
-
stream
Streams the contained value, if any.- Returns:
- a
Streamobject
-