Top Description Methods
io.vavr

public Interface Value<T>

Additional top-level class in compilation unit: ValueModule.

extends Iterable<T>
Known Direct Subinterfaces
io.vavr.collection.Traversable, io.vavr.concurrent.Future, io.vavr.control.Either, io.vavr.control.Option, io.vavr.control.Try, io.vavr.control.Validation
Known Direct Implementers
io.vavr.control.Either.LeftProjection, io.vavr.control.Either.RightProjection, io.vavr.Lazy
Type Parameters
<T>
The type of the wrapped value.
Imports
io.vavr.collection.Array, .CharSeq, .HashMap, .HashSet, .Iterator, .LinkedHashMap, .LinkedHashSet, .List, .Map, io.vavr.collection.*, .PriorityQueue, .Queue, .Set, .SortedMap, .SortedSet, .Stream, .TreeMap, .TreeSet, .Vector, io.vavr.concurrent.Future, io.vavr.control.Either, .Option, .Try, .Validation, java.io.PrintStream, .PrintWriter, java.util.*, java.util.concurrent.CompletableFuture, java.util.function.*, .Function, java.util.stream.Collector, .StreamSupport

Functional programming is all about values and transformation of values using functions. The Value 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:

Equality checks: Iterable extensions: Side-effects: Tests: Type conversion:

Note

flatMap signatures are manifold and have to be declared by subclasses of Value.

Author
Daniel Dietrich

Method Summary

Modifier and TypeMethod and Description
public default <
the result type of the reduction operation
R
,
the mutable accumulation type of the reduction operation
A
>
R

Returns:

R reduction result
collect
(Collector<? super T, A, R>
Collector performing reduction
collector
)

Collects the underlying value(s) (if present) using the provided collector.

public default <
type of the result
R
>
R

Returns:

R reduction result
collect
(Supplier<R>
provide unit value for reduction
supplier
,
BiConsumer<R, ? super T>
perform reduction with unit value
accumulator
,
BiConsumer<R, R>
function for combining two values, which must be compatible with the accumulator.
combiner
)

Collects the underlying value(s) (if present) using the given supplier, accumulator and combiner.

public default boolean

Returns:

true, if element is contained, false otherwise.
contains
(T
An Object of type A, may be null.
element
)

Shortcut for exists(e -> Objects.equals(e, element)), tests if the given element is contained.

public default <
Component type of that iterable
U
>
boolean

Returns:

true if both iterables have the same length and predicate(x, y) is true for all corresponding elements x of this iterable and y of that, otherwise false.
corresponds
(Iterable<U>
the other iterable
that
,
BiPredicate<? super T, ? super U>
the test predicate, which relates elements from both iterables
predicate
)

Tests whether every element of this iterable relates to the corresponding element of another iterable by satisfying a test predicate.

public default boolean

Returns:

true, if this equals o according to the rules defined above, otherwise false.
eq
(Object
An object
o
)

A smoothing replacement for equals.

public boolean

Returns:

true, if this equals o, false otherwise
equals
(Object
An object
o
)

Clarifies that values have a proper equals() method implemented.

public default boolean

Returns:

true, if predicate holds for one or more elements, false otherwise
exists
(Predicate<? super T>
A Predicate
predicate
)

Checks, if an element exists such that the predicate holds.

public default boolean

Returns:

true, if the predicate holds for all elements, false otherwise
forAll
(Predicate<? super T>
A Predicate
predicate
)

Checks, if the given predicate holds for all elements.

public default void
forEach(Consumer<? super T>
A Consumer
action
)

Overrides default java.lang.Iterable.forEach.

Performs an action on each element.

public T

Returns:

the underlying value if this is not empty, otherwise get() throws a Throwable
get
()

Gets the underlying value or throws if no value is present.

public default T

Returns:

A value of type T
getOrElse
(T
An alternative value.
other
)

Returns the underlying value if present, otherwise other.

public default T

Returns:

A value of type T
getOrElse
(Supplier<? extends T>
An alternative value supplier.
supplier
)

Returns the underlying value if present, otherwise other.

public default <
a Throwable type
X extends Throwable
>
T

Returns:

A value of type T.
getOrElseThrow
(Supplier<X>
An exception supplier.
supplier
)

Returns the underlying value if present, otherwise throws supplier.get().

public default T

Returns:

A value of type T.
getOrElseTry
(CheckedFunction0<? extends T>
An alternative value supplier.
supplier
)

Returns the underlying value if present, otherwise returns the result of Try.of(supplier).get().

public default T

Returns:

A value of type T or null.
getOrNull
()

Returns the underlying value if present, otherwise null.

public int

Returns:

The hashcode of this object
hashCode
()

Clarifies that values have a proper hashCode() method implemented.

public boolean

Returns:

true if this Value is async (like io.vavr.concurrent.Future), false otherwise.
isAsync
()

Checks if this Value is asynchronously (short: async) computed.

public boolean

Returns:

false, if no underlying value is present, true otherwise.
isEmpty
()

Checks, this Value is empty, i.e. if the underlying value is absent.

public boolean

Returns:

true if this Value is lazy (like Lazy and Stream), false otherwise.
isLazy
()

Checks if this Value is lazily evaluated.

public boolean

Returns:

true if this is single-valued, false otherwise.
isSingleValued
()

States whether this is a single-valued type.

public Iterator<T>

Returns:

A new Iterator
iterator
()

Redeclares java.lang.Iterable.iterator.

Returns a rich io.vavr.collection.Iterator.

public <
The new component type
U
>
Value<U>

Returns:

A new value
map
(Function<? super T, ? extends U>
A mapper
mapper
)

Maps the underlying value to a different component type.

public static <
Component type of the Value.
T
>
Value<T>

Returns:

the given value instance as narrowed type Value<T>.
narrow
(Value<? extends T>
A Value.
value
)

Narrows a widened Value<?

public default void
out(PrintStream
The PrintStream to write to
out
)

Sends the string representations of this to the PrintStream.

public default void
out(PrintWriter
The PrintWriter to write to
writer
)

Sends the string representations of this to the PrintWriter.

public Value<T>

Returns:

this instance
peek
(Consumer<? super T>
The action that will be performed on the element(s).
action
)

Performs the given action on the first element if this is an eager implementation.

public default Spliterator<T>
spliterator()

Overrides default java.lang.Iterable.spliterator.

Creates a Spliterator over the elements described by this Iterable.

public default void
stderr()

Sends the string representations of this to the standard error stream System#err.

public default void
stdout()

Sends the string representations of this to the standard output stream System#out.

public String

Returns:

This type name.
stringPrefix
()

Returns the name of this Value type, which is used by toString().

public default Array<T>

Returns:

A new Array.
toArray
()

Converts this to a Array.

public default CharSeq

Returns:

A new CharSeq.
toCharSeq
()

Converts this to a CharSeq.

public default CompletableFuture<T>

Returns:

A new CompletableFuture containing the value
toCompletableFuture
()

Converts this to a CompletableFuture

public default <
Either left component type
L
>
Either<L, T>

Returns:

A new Either.
toEither
(L
A left value for the Either
left
)

Converts this to an Either.

public default <
Validation error component type
L
>
Either<L, T>

Returns:

A new Either.
toEither
(Supplier<? extends L>
A Supplier for the left value for the Either
leftSupplier
)

Converts this to an Either.

public default <
value type of a Valid
U
>
Validation<T, U>

Returns:

A new Validation.Valid containing the given value if this is empty, otherwise a new Validation.Invalid containing this value.
toInvalid
(U
An instance of a Valid value
value
)
Deprecated Use toValidation(Object) instead.

Converts this to a Validation.

public default <
value type of a Valid
U
>
Validation<T, U>

Returns:

A new Validation.Valid containing the result of valueSupplier if this is empty, otherwise a new Validation.Invalid containing this value.
toInvalid
(Supplier<? extends U>
A supplier of a Valid value
valueSupplier
)
Deprecated Use toValidation(Supplier) instead.

Converts this to a Validation.

public default Object[]

Returns:

A new Java array.
toJavaArray
()

Converts this to a Java array with component type Object

// = [] of type Object[]
Future.
public default T[]

Returns:

A new Java array.
toJavaArray
(Class<T>
Component type of the array
componentType
)
Deprecated Use toJavaArray(IntFunction) instead

Converts this to a Java array having an accurate component type.

public default T[]

Returns:

The array provided by the factory filled with the values from this Value.
toJavaArray
(IntFunction<T[]>
an int argument function that creates an array of the correct component type with the specified size
arrayFactory
)

Converts this to a Java array having an accurate component type.

public default <
a sub-type of java.util.Collection
C extends Collection<T>
>
C

Returns:

a new java.util.Collection of type C
toJavaCollection
(Function<Integer, C>
A factory that returns an empty mutable java.util.Collection with the specified initial capacity
factory
)

Converts this to a specific mutable java.util.Collection of type C.

public default List<T>

Returns:

A new java.util.ArrayList.
toJavaList
()

Converts this to a mutable java.util.List.

public default <
A sub-type of java.util.List
LIST extends List<T>
>
LIST

Returns:

a new java.util.List of type LIST
toJavaList
(Function<Integer, LIST>
A factory that returns an empty mutable java.util.List with the specified initial capacity
factory
)

Converts this to a specific mutable java.util.List.

public default <
The key type
K
,
The value type
V
>
Map<K, V>

Returns:

A new java.util.HashMap.
toJavaMap
(Function<? super T, ? extends Tuple2<? extends K, ? extends V>>
A function that maps an element to a key/value pair represented by Tuple2
f
)

Converts this to a mutable java.util.Map.

public default <
The key type
K
,
The value type
V
,
a sub-type of java.util.Map
MAP extends Map<K, V>
>
MAP

Returns:

a new java.util.Map of type MAP
toJavaMap
(Supplier<MAP>
A factory that creates an empty mutable java.util.Map
factory
,
Function<? super T, ? extends K>
A function that maps an element to a key
keyMapper
,
Function<? super T, ? extends V>
A function that maps an element to a value
valueMapper
)

Converts this to a specific mutable java.util.Map.

public default <
The key type
K
,
The value type
V
,
a sub-type of java.util.Map
MAP extends Map<K, V>
>
MAP

Returns:

a new java.util.Map of type MAP
toJavaMap
(Supplier<MAP>
A factory that creates an empty mutable java.util.Map
factory
,
Function<? super T, ? extends Tuple2<? extends K, ? extends V>>
A function that maps an element to a key/value pair represented by Tuple2
f
)

Converts this to a specific mutable java.util.Map.

public default Optional<T>

Returns:

A new java.util.Optional.
toJavaOptional
()

Converts this to an java.util.Optional.

public default Stream<T>

Returns:

A new parallel java.util.stream.Stream.
toJavaParallelStream
()

Converts this to a parallel java.util.stream.Stream by calling StreamSupport.stream(this.spliterator(), true).

public default Set<T>

Returns:

A new java.util.HashSet.
toJavaSet
()

Converts this to a mutable java.util.Set.

public default <
a sub-type of java.util.Set
SET extends Set<T>
>
SET

Returns:

a new java.util.Set of type SET
toJavaSet
(Function<Integer, SET>
A factory that returns an empty mutable java.util.Set with the specified initial capacity
factory
)

Converts this to a specific java.util.Set.

public default Stream<T>

Returns:

A new sequential java.util.stream.Stream.
toJavaStream
()

Converts this to a sequential java.util.stream.Stream by calling StreamSupport.stream(this.spliterator(), false).

public default <
right type
R
>
Either<T, R>

Returns:

A new Either.Right containing the value of right if this is empty, otherwise a new Either.Left containing this value.
toLeft
(R
An instance of a right value
right
)
Deprecated Use toEither(Object) instead.

Converts this to a Either.

public default <
right type
R
>
Either<T, R>

Returns:

A new Either.Right containing the result of right if this is empty, otherwise a new Either.Left containing this value.
toLeft
(Supplier<? extends R>
A supplier of a right value
right
)
Deprecated Use toEither(Supplier) instead.

Converts this to a Either.

public default <
The key type
K
,
The value type
V
>
Map<K, V>

Returns:

A new LinkedHashMap.
toLinkedMap
(Function<? super T, ? extends K>
A function that maps an element to a key
keyMapper
,
Function<? super T, ? extends V>
A function that maps an element to a value
valueMapper
)

Converts this to a Map.

public default <
The key type
K
,
The value type
V
>
Map<K, V>

Returns:

A new LinkedHashMap.
toLinkedMap
(Function<? super T, ? extends Tuple2<? extends K, ? extends V>>
A function that maps an element to a key/value pair represented by Tuple2
f
)

Converts this to a Map.

public default Set<T>

Returns:

A new LinkedHashSet.
toLinkedSet
()

Converts this to a Set.

public default List<T>

Returns:

A new List.
toList
()

Converts this to a List.

public default <
The key type
K
,
The value type
V
>
Map<K, V>

Returns:

A new HashMap.
toMap
(Function<? super T, ? extends K>
A function that maps an element to a key
keyMapper
,
Function<? super T, ? extends V>
A function that maps an element to a value
valueMapper
)

Converts this to a Map.

public default <
The key type
K
,
The value type
V
>
Map<K, V>

Returns:

A new HashMap.
toMap
(Function<? super T, ? extends Tuple2<? extends K, ? extends V>>
A function that maps an element to a key/value pair represented by Tuple2
f
)

Converts this to a Map.

public default Option<T>

Returns:

A new Option.
toOption
()

Converts this to an Option.

public default PriorityQueue<T>

Returns:

A new PriorityQueue.
toPriorityQueue
()

Converts this to a PriorityQueue.

public default PriorityQueue<T>

Returns:

A new PriorityQueue.
toPriorityQueue
(Comparator<? super T>
A comparator that induces an order of the PriorityQueue elements.
comparator
)

Converts this to a PriorityQueue.

public default Queue<T>

Returns:

A new Queue.
toQueue
()

Converts this to a Queue.

public default <
left type
L
>
Either<L, T>

Returns:

A new Either.Left containing the value of left if this is empty, otherwise a new Either.Right containing this value.
toRight
(L
An instance of a left value
left
)
Deprecated Use toEither(Object) instead.

Converts this to a Either.

public default <
left type
L
>
Either<L, T>

Returns:

A new Either.Left containing the result of left if this is empty, otherwise a new Either.Right containing this value.
toRight
(Supplier<? extends L>
A supplier of a left value
left
)
Deprecated Use toEither(Supplier) instead.

Converts this to a Either.

public default Set<T>

Returns:

A new HashSet.
toSet
()

Converts this to a Set.

public default <
The key type
K extends Comparable<? super K>
,
The value type
V
>
SortedMap<K, V>

Returns:

A new TreeMap.
toSortedMap
(Function<? super T, ? extends K>
A function that maps an element to a key
keyMapper
,
Function<? super T, ? extends V>
A function that maps an element to a value
valueMapper
)

Converts this to a Map.

public default <
The key type
K extends Comparable<? super K>
,
The value type
V
>
SortedMap<K, V>

Returns:

A new TreeMap.
toSortedMap
(Function<? super T, ? extends Tuple2<? extends K, ? extends V>>
A function that maps an element to a key/value pair represented by Tuple2
f
)

Converts this to a Map.

public default <
The key type
K
,
The value type
V
>
SortedMap<K, V>

Returns:

A new TreeMap.
toSortedMap
(Comparator<? super K>
A comparator that induces an order of the Map keys.
comparator
,
Function<? super T, ? extends K>
A function that maps an element to a key
keyMapper
,
Function<? super T, ? extends V>
A function that maps an element to a value
valueMapper
)

Converts this to a Map.

public default <
The key type
K
,
The value type
V
>
SortedMap<K, V>

Returns:

A new TreeMap.
toSortedMap
(Comparator<? super K>
A comparator that induces an order of the Map keys.
comparator
,
Function<? super T, ? extends Tuple2<? extends K, ? extends V>>
A function that maps an element to a key/value pair represented by Tuple2
f
)

Converts this to a Map.

public default SortedSet<T>

Returns:

A new TreeSet.
toSortedSet
()

Converts this to a SortedSet.

public default SortedSet<T>

Returns:

A new TreeSet.
toSortedSet
(Comparator<? super T>
A comparator that induces an order of the SortedSet elements.
comparator
)

Converts this to a SortedSet.

public default Stream<T>

Returns:

A new Stream.
toStream
()

Converts this to a Stream.

public String

Returns:

A String representation of this object
toString
()

Clarifies that values have a proper toString() method implemented.

public default Tree<T>

Returns:

A new Tree.
toTree
()

Converts this to a Tree.

public default <
Id type
ID
>
List<Tree.Node<T>>

Returns:

A new Tree.
toTree
(Function<? super T, ? extends ID>
A mapper from source item to unique identifier of that item
idMapper
,
Function<? super T, ? extends ID>
A mapper from source item to unique identifier of parent item. Need return null for root items
parentMapper
)

Converts this to a Tree using a idMapper and parentMapper.

public default Try<T>

Returns:

A new Try.
toTry
()

Converts this to a Try.

public default Try<T>

Returns:

A new Try.
toTry
(Supplier<? extends Throwable>
an exception supplier
ifEmpty
)

Converts this to a Try.

public default <
error type of an Invalid
E
>
Validation<E, T>

Returns:

A new Validation.Invalid containing the given error if this is empty, otherwise a new Validation.Valid containing this value.
toValid
(E
An error
error
)
Deprecated Use toValidation(Object) instead.

Converts this to a Validation.

public default <
error type of an Invalid
E
>
Validation<E, T>

Returns:

A new Validation.Invalid containing the result of errorSupplier if this is empty, otherwise a new Validation.Valid containing this value.
toValid
(Supplier<? extends E>
A supplier of an error
errorSupplier
)
Deprecated Use toValidation(Supplier) instead.

Converts this to a Validation.

public default <
Validation error component type
E
>
Validation<E, T>

Returns:

A new Validation.
toValidation
(E
An invalid value for the Validation
invalid
)

Converts this to an Validation.

public default <
Validation error component type
E
>
Validation<E, T>

Returns:

A new Validation.
toValidation
(Supplier<? extends E>
A Supplier for the invalid value for the Validation
invalidSupplier
)

Converts this to an Validation.

public default Vector<T>

Returns:

A new Vector.
toVector
()

Converts this to a Vector.