T
- The type of the wrapped value.public interface Value<T> extends Foldable<T>, Monad<T>
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, not yet defined, etc.
Static methods:
Basic operations: Equality checks: Side-effects: Tests:Modifier and Type | Method and Description |
---|---|
default <U> boolean |
corresponds(Iterable<U> that,
java.util.function.BiPredicate<? super T,? super U> predicate)
Tests whether every element of this iterable relates to the corresponding element of another iterable by
satisfying a test predicate.
|
default boolean |
eq(Object o)
A smoothing replacement for
equals . |
boolean |
equals(Object o)
Clarifies that values 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.
|
Value<T> |
filter(java.util.function.Predicate<? super T> predicate)
Filters this
Monad by testing a predicate. |
<U> Value<U> |
flatMap(java.util.function.Function<? super T,? extends Iterable<? extends U>> mapper)
FlatMaps this value to a new value with different component type.
|
default <U> U |
foldLeft(U zero,
java.util.function.BiFunction<? super U,? super T,? extends U> combine)
Folds this elements from the left, starting with
zero and successively calling combine . |
default <U> U |
foldRight(U zero,
java.util.function.BiFunction<? super T,? super U,? extends U> combine)
Folds this elements from the right, starting with
zero and successively calling combine . |
default boolean |
forAll(java.util.function.Predicate<? super T> predicate)
Checks, if the given predicate holds for all elements.
|
default void |
forEach(java.util.function.Consumer<? super T> action)
Performs an action on each element.
|
T |
get()
Gets the underlying value or throws if no value is present.
|
static <T> T |
get(Iterable<? extends T> iterable)
Gets the first value of the given Iterable if exists, otherwise throws.
|
default Option<T> |
getOption()
Gets the underlying as Option.
|
int |
hashCode()
Clarifies that values have a proper hashCode() method implemented.
|
default T |
ifDefined(java.util.function.Supplier<? extends T> trueSupplier,
java.util.function.Supplier<? extends T> falseSupplier)
A fluent if-expression for this value.
|
default T |
ifDefined(T trueVal,
T falseVal)
A fluent if-expression for this value.
|
default T |
ifEmpty(java.util.function.Supplier<? extends T> trueSupplier,
java.util.function.Supplier<? extends T> falseSupplier)
A fluent if-expression for this value.
|
default T |
ifEmpty(T trueVal,
T falseVal)
A fluent if-expression for this value.
|
default boolean |
isDefined()
Checks, this
Value is defined, i.e. |
boolean |
isEmpty()
Checks, this
Value is empty, i.e. |
boolean |
isSingletonType()
States, if this value may contain (at most) one element or more than one element, like collections.
|
Iterator<T> |
iterator()
Returns a rich
javaslang.collection.Iterator . |
<U> Value<U> |
map(java.util.function.Function<? super T,? extends U> mapper)
Maps this value to a new value with different component type.
|
default T |
orElse(T other)
Returns the underlying value if present, otherwise
other . |
default T |
orElseGet(java.util.function.Supplier<? extends T> supplier)
Returns the underlying value if present, otherwise
other . |
default <X extends Throwable> |
orElseThrow(java.util.function.Supplier<X> supplier)
Returns the underlying value if present, otherwise throws
supplier.get() . |
default T |
orElseTry(Try.CheckedSupplier<? extends T> supplier)
Returns the underlying value if present, otherwise returns the result of
Try.of(supplier).get() . |
default void |
out(PrintStream out)
Sends the string representations of this value to the
PrintStream . |
default void |
out(PrintWriter writer)
Sends the string representations of this value to the
PrintWriter . |
Value<T> |
peek(java.util.function.Consumer<? super T> action)
Performs the given
action on the first element if this is an eager implementation. |
default void |
stderr()
Sends the string representations of this value to the standard error stream System.err.
|
default void |
stdout()
Sends the string representations of this value to the standard output stream System.out.
|
default Array<T> |
toArray()
Converts this value to a
Array . |
default CharSeq |
toCharSeq()
Converts this value to a
CharSeq . |
default Object[] |
toJavaArray()
Converts this value to an untyped Java array.
|
default T[] |
toJavaArray(Class<T> componentType)
Converts this value to a typed Java array.
|
default List<T> |
toJavaList()
Converts this value to an
List . |
default <K,V> Map<K,V> |
toJavaMap(java.util.function.Function<? super T,? extends Tuple2<? extends K,? extends V>> f)
Converts this value to a
Map . |
default Optional<T> |
toJavaOptional()
Converts this value to an
Optional . |
default Set<T> |
toJavaSet()
Converts this value to a
Set . |
default java.util.stream.Stream<T> |
toJavaStream()
Converts this value to a
Stream . |
default Lazy<T> |
toLazy()
Converts this value to a
Lazy . |
default List<T> |
toList()
Converts this value to a
List . |
default <K,V> Map<K,V> |
toMap(java.util.function.Function<? super T,? extends Tuple2<? extends K,? extends V>> f)
Converts this value to a
Map . |
default Option<T> |
toOption()
Converts this value to an
Option . |
default Queue<T> |
toQueue()
Converts this value to a
Queue . |
default Set<T> |
toSet()
Converts this value to a
Set . |
default Stack<T> |
toStack()
Converts this value to a
Stack . |
default Stream<T> |
toStream()
Converts this value to a
Stream . |
String |
toString()
Clarifies that values have a proper toString() method implemented.
|
default Tree<T> |
toTree()
Converts this value to a
Tree . |
default Try<T> |
toTry()
Converts this value to a
Try . |
default Try<T> |
toTry(java.util.function.Supplier<? extends Throwable> ifEmpty)
Converts this value to a
Try . |
default Vector<T> |
toVector()
Converts this value to a
Vector . |
fold, fold, foldLeft, foldMap, foldRight
lift, lift, lift, lift, lift, lift, lift, lift
spliterator
static <T> T get(Iterable<? extends T> iterable)
T
- Component typeiterable
- An java.lang.IterableNoSuchElementException
- if the given iterable is emptyT get()
NoSuchElementException
- if no value is defineddefault Option<T> getOption()
default T ifDefined(T trueVal, T falseVal)
trueVal
- The result, if this is defined.falseVal
- The result, if this is not defined.default T ifDefined(java.util.function.Supplier<? extends T> trueSupplier, java.util.function.Supplier<? extends T> falseSupplier)
trueSupplier
- The result, if this is defined.falseSupplier
- The result, if this is not defined.default T ifEmpty(T trueVal, T falseVal)
trueVal
- The result, if this is empty.falseVal
- The result, if this is not empty.default T ifEmpty(java.util.function.Supplier<? extends T> trueSupplier, java.util.function.Supplier<? extends T> falseSupplier)
trueSupplier
- The result, if this is defined.falseSupplier
- The result, if this is not defined.boolean isEmpty()
Value
is empty, i.e. if the underlying value is absent.default boolean isDefined()
Value
is defined, i.e. if the underlying value is present.boolean isSingletonType()
We call a type singleton type, which may contain at most one element.
true
, if this is a singleton type, false
otherwise.default T orElse(T other)
other
.other
- An alternative value.T
default T orElseGet(java.util.function.Supplier<? extends T> supplier)
other
.supplier
- An alternative value supplier.T
NullPointerException
- if supplier is nulldefault <X extends Throwable> T orElseThrow(java.util.function.Supplier<X> supplier) throws X extends Throwable
supplier.get()
.X
- a Throwable typesupplier
- An exception supplier.T
.NullPointerException
- if supplier is nullX
- if no value is presentX extends Throwable
default T orElseTry(Try.CheckedSupplier<? extends T> supplier)
Try.of(supplier).get()
.supplier
- An alternative value supplier.T
.NullPointerException
- if supplier is nullFailure.NonFatal
- containing the original exception if this Value was empty and the Try failed.Value<T> peek(java.util.function.Consumer<? super T> action)
action
on the first element if this is an eager implementation.
Performs the given action
on all elements (the first immediately, successive deferred),
if this is a lazy implementation.action
- The action that will be performed on the element(s).boolean equals(Object o)
int hashCode()
See Object.hashCode().
String toString()
See Object.toString().
Value<T> filter(java.util.function.Predicate<? super T> predicate)
Monad
Monad
by testing a predicate.
The semantics may vary from class to class, e.g. for single-valued type (like Option) and multi-values types (like Traversable). The commonality is, that filtered.isEmpty() will return true, if no element satisfied the given predicate.
Also, an implementation may throw NoSuchElementException
, if no element makes it through the filter
and this state cannot be reflected. E.g. this is the case for Either.LeftProjection
and
Either.RightProjection
.
<U> Value<U> flatMap(java.util.function.Function<? super T,? extends Iterable<? extends U>> mapper)
Monad
FlatMap is the sequence operation for functions and behaves like the imperative ;
.
If the previous results are needed, flatMap cascades:
m1().flatMap(result1 ->
m2(result1).flatMap(result2 ->
m3(result1, result2).flatMap(result3 ->
...
)
)
);
If only the last result is needed, flatMap may be used sequentially:
m1().flatMap(this::m2)
.flatMap(this::m3)
.flatMap(...);
default <U> U foldLeft(U zero, java.util.function.BiFunction<? super U,? super T,? extends U> combine)
Foldable
zero
and successively calling combine
.default <U> U foldRight(U zero, java.util.function.BiFunction<? super T,? super U,? extends U> combine)
Foldable
zero
and successively calling combine
.<U> Value<U> map(java.util.function.Function<? super T,? extends U> mapper)
Monad
default T[] toJavaArray(Class<T> componentType)
componentType
- Component type of the arraydefault <K,V> Map<K,V> toJavaMap(java.util.function.Function<? super T,? extends Tuple2<? extends K,? extends V>> f)
Map
.K
- The key typeV
- The value typef
- A function that maps an element to a key/value pair represented by Tuple2HashMap
.default Optional<T> toJavaOptional()
Optional
.Optional
.default java.util.stream.Stream<T> toJavaStream()
Stream
.Stream
.default <K,V> Map<K,V> toMap(java.util.function.Function<? super T,? extends Tuple2<? extends K,? extends V>> f)
Map
.K
- The key typeV
- The value typef
- A function that maps an element to a key/value pair represented by Tuple2HashMap
.default Try<T> toTry()
Try
.
If this value is undefined, i.e. empty, then a new Failure(NoSuchElementException)
is returned,
otherwise a new Success(value)
is returned.
Try
.default Try<T> toTry(java.util.function.Supplier<? extends Throwable> ifEmpty)
Try
.
If this value is undefined, i.e. empty, then a new Failure(ifEmpty.get())
is returned,
otherwise a new Success(value)
is returned.
ifEmpty
- an exception supplierTry
.default void out(PrintStream out)
PrintStream
.
If this value consists of multiple elements, each element is displayed in a new line.out
- The PrintStream to write todefault void out(PrintWriter writer)
PrintWriter
.
If this value consists of multiple elements, each element is displayed in a new line.writer
- The PrintWriter to write toIterator<T> iterator()
javaslang.collection.Iterator
.default <U> boolean corresponds(Iterable<U> that, java.util.function.BiPredicate<? super T,? super U> predicate)
U
- Component type of that iterablethat
- the other iterablepredicate
- the test predicate, which relates elements from both iterablestrue
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
.default boolean eq(Object o)
equals
. It is similar to Scala's ==
but better in the way
that it is not limited to collection types, e.g. Some(1) eq List(1)
, None eq Failure(x)
etc.
In a nutshell: eq checks congruence of structures and equality of contained values.
Example:
// ((1, 2), ((3))) => structure: (()(())) values: 1, 2, 3
final Iterable<?> i1 = List.of(List.of(1, 2), Arrays.asList(List.of(3)));
final Iterable<?> i2 = Queue.of(Stream.of(1, 2), List.of(Lazy.of(() -> 3)));
assertThat(i1.eq(i2)).isTrue();
Semantics:
o == this : true
o instanceof javaslang.Iterable : iterable elements are eq, non-iterable elements equals, for all (o1, o2) in (this, o)
o instanceof java.lang.Iterable : this eq Iterator.of((java.lang.Iterable<?>) o);
otherwise : false
o
- An objectdefault boolean exists(java.util.function.Predicate<? super T> predicate)
predicate
- A PredicateNullPointerException
- if predicate
is nulldefault boolean forAll(java.util.function.Predicate<? super T> predicate)
predicate
- A PredicateNullPointerException
- if predicate
is nulldefault void forEach(java.util.function.Consumer<? super T> action)
forEach
in interface Iterable<T>
action
- A Consumer
NullPointerException
- if action
is nulldefault Object[] toJavaArray()
default void stderr()
IllegalStateException
- if PrintStream.checkError()
is true after writing to stderr.default void stdout()
IllegalStateException
- if PrintStream.checkError()
is true after writing to stdout.Copyright © 2015. All Rights Reserved.