Package org.storynode.pigeon.option
Class Some<T>
java.lang.Object
org.storynode.pigeon.option.Option<T>
org.storynode.pigeon.option.Some<T>
- All Implemented Interfaces:
SafelyWrapped<T>,Wrapped<T>
An
Option with some value in it.- Author:
- Andrea Coronese
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanIf 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 givenOptional-bearing mapping function to the value, otherwise returns an emptyOptional.inthashCode()voidExecutesfuncif this isSome, consuming the contained value.voidifPresentOrElse(@NotNull Consumer<T> whenPresent, Runnable otherwise) booleanisSome()Whether this option contains a value or not<U> Option<U> If a value is present, returns anOptiondescribing (as if byOption.some(T)) the result of applying the given mapping function to the value, otherwise returns an emptyOption.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 that value, otherwise throwsNoSuchElementException.orElseThrow(@NotNull Supplier<E> throwable) If a value is present returns that value, otherwise throws the suppliedThrowable.stream()Streams the contained value, if any.toString()unwrap()Gets the wrapped value.value()Returns the contained valueMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.storynode.pigeon.protocol.SafelyWrapped
tryUnwrap
-
Constructor Details
-
Some
Creates anOptionthat has a non-null value in it- Parameters:
value- The inner value
-
-
Method Details
-
isSome
public boolean isSome()Whether this option contains a value or not -
ifPresent
Executesfuncif this isSome, consuming the contained value. -
ifPresentOrElse
ExecuteswhenPresentif this isSome, consuming the contained value, or runsotherwiseif this isNone- Specified by:
ifPresentOrElsein classOption<T>- Parameters:
whenPresent- The function to execute if this isSomeotherwise- The function to execute if this isNone
-
filter
If a value is present, and the value matches the given predicate, returns anOptiondescribing the value, otherwise returns an emptyOption. -
map
If a value is present, returns anOptiondescribing (as if byOption.some(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.- Specified by:
mapin classOption<T>- 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
-
flatMap
public <U> Option<U> flatMap(@NotNull @NotNull Function<? super T, ? extends Option<? extends U>> mapper) 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
Option.map(Function), but the mapping function is one whose result is already anOptional, and if invoked,flatMapdoes not wrap it within an additionalOptional.- Specified by:
flatMapin classOption<T>- 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
Description copied from class:Option -
orElseGet
If a value is present, returns the value, otherwise returns the result produced by the supplying function. -
orElse
If a value is present, returns the value, otherwise returnsother. -
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 ofOption.orElse(Object)andOption.orElseGet(Supplier)- Specified by:
orElseThrowin classOption<T>- Returns:
- The contained value, if present
-
orElseThrow
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 ofOption.orElse(Object)andOption.orElseGet(Supplier)- Specified by:
orElseThrowin classOption<T>- Type Parameters:
E- The concrete type of theThrowable- Parameters:
throwable- The function that supplies the exception to throw- Returns:
- a T object
-
stream
Streams the contained value, if any. -
value
Returns the contained value- Returns:
- The contained value
- See Also:
-
equals
-
hashCode
public int hashCode() -
toString
-
unwrap
Gets the wrapped value. This method is allowed to throw aUnwrapExceptionif the specific implementors requires so.
If you need a non-throwing version ofunwrapusetryUnwrap.- Returns:
- The wrapped value
- Throws:
UnwrapException- if thisOptionisNone.- See Also:
-