Interface FunctionalValue<T>
- Type Parameters:
T
- The type of the wrapped value.
- All Superinterfaces:
java.lang.Iterable<T>
- All Known Implementing Classes:
CallResult
,CallResult.Failure
,CallResult.FailureProjection
,CallResult.Success
,CallResult.SuccessProjection
public interface FunctionalValue<T>
extends java.lang.Iterable<T>
Value
adapted to the JCoinbase
needs. For more information, please take a look at the vavr
site
Functional programming is all about values and transformation of values using functions. The
FunctionalValue
type reflects the values in a functional setting. It can be seen as the
result of a partial function application. Hence the result may be undefined. If a value is
undefined, we say it is empty.
How the empty state is interpreted depends on the context, i.e. it may be undefined, failed, no elements, etc.
Basic operations:
get()
getOrElse(Object)
getOrElse(Supplier)
getOrElseThrow(Supplier)
getOrElseTry(CheckedFunction0)
getOrNull()
map(Function)
contains(Object)
exists(Predicate)
forAll(Predicate)
Iterable.forEach(Consumer)
iterator()
-
Method Summary
Modifier and Type Method Description default boolean
contains(T element)
Shortcut forexists(e -> Objects.equals(e, element))
, tests if the givenelement
is contained.boolean
equals(java.lang.Object o)
Clarifies that functionalValues have a proper equals() method implemented.default boolean
exists(java.util.function.Predicate<? super T> predicate)
Checks, if an element exists such that the predicate holds.default boolean
forAll(java.util.function.Predicate<? super T> predicate)
Checks, if the given predicate holds for all elements.T
get()
Gets the underlying functionalValue or throws if no functionalValue is present.default T
getOrElse(java.util.function.Supplier<? extends T> supplier)
Returns the underlying functionalValue if present, otherwiseother
.default T
getOrElse(T other)
Returns the underlying functionalValue if present, otherwiseother
.default <X extends java.lang.Throwable>
TgetOrElseThrow(java.util.function.Supplier<X> supplier)
Returns the underlying functionalValue if present, otherwise throwssupplier.get()
.default T
getOrElseTry(io.vavr.CheckedFunction0<? extends T> supplier)
Returns the underlying functionalValue if present, otherwise returns the result ofTry.of(supplier).get()
.default T
getOrNull()
Returns the underlying functionalValue if present, otherwisenull
.int
hashCode()
Clarifies that functionalValues have a proper hashCode() method implemented.default boolean
isAsync()
Checks if thisValue
is asynchronously (short: async) computed.boolean
isEmpty()
Checks, thisValue
is empty, i.e. if the underlying functionalValue is absent.default boolean
isLazy()
Checks if thisValue
is lazily evaluated.default boolean
isSingleValued()
States whether this is a single-valued type.io.vavr.collection.Iterator<T>
iterator()
Returns a richio.vavr.collection.Iterator
.<U> FunctionalValue<U>
map(java.util.function.Function<? super T,? extends U> mapper)
Maps the underlying functionalValue to a different component type.FunctionalValue<T>
peek(java.util.function.Consumer<? super T> action)
Performs the givenaction
on the first element if this is an eager implementation.default java.util.Spliterator<T>
spliterator()
default <L> CallResult<L,T>
toCallResult(java.util.function.Supplier<? extends L> leftSupplier)
Converts this to aCallResult
.default <L> CallResult<L,T>
toCallResult(L left)
Converts this to aCallResult
.default java.util.Optional<T>
toJavaOptional()
Converts this to anOptional
.default io.vavr.control.Option<T>
toOption()
Converts this to anOption
.java.lang.String
toString()
Clarifies that functionalValues have a proper toString() method implemented.Methods inherited from interface java.lang.Iterable
forEach
-
Method Details
-
contains
Shortcut forexists(e -> Objects.equals(e, element))
, tests if the givenelement
is contained.- Parameters:
element
- An Object of type A, may be null.- Returns:
- true, if element is contained, false otherwise.
-
exists
Checks, if an element exists such that the predicate holds.- Parameters:
predicate
- A Predicate- Returns:
- true, if predicate holds for one or more elements, false otherwise
- Throws:
java.lang.NullPointerException
- ifpredicate
is null
-
isAsync
default boolean isAsync()Checks if thisValue
is asynchronously (short: async) computed.Methods of a
Value
instance that operate on the underlying value may block the current thread until the value is present and the computation can be performed.- Returns:
- true if this
Value
is async (likeFuture
), false otherwise.
-
isLazy
default boolean isLazy()Checks if thisValue
is lazily evaluated.- Returns:
- true if this
Value
is lazy (likeLazy
andStream
), false otherwise.
-
isSingleValued
default boolean isSingleValued()States whether this is a single-valued type.- Returns:
true
if this is single-valued,false
otherwise.
-
forAll
Checks, if the given predicate holds for all elements.- Parameters:
predicate
- A Predicate- Returns:
- true, if the predicate holds for all elements, false otherwise
- Throws:
java.lang.NullPointerException
- ifpredicate
is null
-
get
T get()Gets the underlying functionalValue or throws if no functionalValue is present.IMPORTANT! This method will throw an undeclared
Throwable
ifisEmpty() == true
is true.Because the 'empty' state indicates that there is no functionalValue present that can be returned,
get()
has to throw in such a case. Generally, implementing classes should throw aNoSuchElementException
ifisEmpty()
returns true.However, there exist use-cases, where implementations may throw other exceptions. See
Try.get()
.Additional note: Dynamic proxies will wrap an undeclared exception in a
UndeclaredThrowableException
.- Returns:
- the underlying functionalValue if this is not empty, otherwise
get()
throws aThrowable
-
getOrElse
Returns the underlying functionalValue if present, otherwiseother
.- Parameters:
other
- An alternative functionalValue.- Returns:
- A functionalValue of type
T
-
getOrElse
Returns the underlying functionalValue if present, otherwiseother
.- Parameters:
supplier
- An alternative functionalValue supplier.- Returns:
- A functionalValue of type
T
- Throws:
java.lang.NullPointerException
- if supplier is null
-
getOrElseThrow
default <X extends java.lang.Throwable> T getOrElseThrow(java.util.function.Supplier<X> supplier) throws X extends java.lang.ThrowableReturns the underlying functionalValue if present, otherwise throwssupplier.get()
.- Type Parameters:
X
- a Throwable type- Parameters:
supplier
- An exception supplier.- Returns:
- A functionalValue of type
T
. - Throws:
java.lang.NullPointerException
- if supplier is nullX
- if no functionalValue is present
-
getOrElseTry
Returns the underlying functionalValue if present, otherwise returns the result ofTry.of(supplier).get()
.- Parameters:
supplier
- An alternative functionalValue supplier.- Returns:
- A functionalValue of type
T
. - Throws:
java.lang.NullPointerException
- if supplier is null
-
getOrNull
Returns the underlying functionalValue if present, otherwisenull
.- Returns:
- A functionalValue of type
T
ornull
.
-
isEmpty
boolean isEmpty()Checks, thisValue
is empty, i.e. if the underlying functionalValue is absent.- Returns:
- false, if no underlying functionalValue is present, true otherwise.
-
map
Maps the underlying functionalValue to a different component type.- Type Parameters:
U
- The new component type- Parameters:
mapper
- A mapper- Returns:
- A new functionalValue
-
peek
Performs the givenaction
on the first element if this is an eager implementation. Performs the givenaction
on all elements (the first immediately, successive deferred), if this is a lazy implementation.- Parameters:
action
- The action that will be performed on the element(s).- Returns:
- this instance
-
iterator
io.vavr.collection.Iterator<T> iterator()Returns a richio.vavr.collection.Iterator
.- Specified by:
iterator
in interfacejava.lang.Iterable<T>
- Returns:
- A new Iterator
-
toJavaOptional
Converts this to anOptional
.// = Optional.empty Future.of(() -> { throw new Error(); }) .toJavaOptional() // = Optional[ok] Try.of(() -> "ok") .toJavaOptional() // = Optional[1] List.of(1, 2, 3) .toJavaOptional()
- Returns:
- A new
Optional
.
-
toCallResult
Converts this to aCallResult
.- Type Parameters:
L
- Validation error component type- Parameters:
leftSupplier
- ASupplier
for the failure value for theCallResult
- Returns:
- A new
CallResult
.
-
toCallResult
Converts this to aCallResult
.- Type Parameters:
L
- CallResult failure component type- Parameters:
left
- A failure value for theCallResult
- Returns:
- A new
CallResult
.
-
toOption
Converts this to anOption
.- Returns:
- A new
Option
.
-
spliterator
- Specified by:
spliterator
in interfacejava.lang.Iterable<T>
-
equals
boolean equals(java.lang.Object o)Clarifies that functionalValues have a proper equals() method implemented.- Overrides:
equals
in classjava.lang.Object
- Parameters:
o
- An object- Returns:
- true, if this equals o, false otherwise
-
hashCode
int hashCode()Clarifies that functionalValues have a proper hashCode() method implemented.See Object.hashCode().
- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- The hashcode of this object
-
toString
java.lang.String toString()Clarifies that functionalValues have a proper toString() method implemented.See Object.toString().
- Overrides:
toString
in classjava.lang.Object
- Returns:
- A String representation of this object
-