T
- The type of the wrapped value.public interface Value<T> extends Iterable<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, no elements, etc.
Basic operations:
get()
getOption()
getOrElse(Object)
getOrElse(Supplier)
getOrElseThrow(Supplier)
isEmpty()
isSingleValued()
map(Function)
stringPrefix()
toArray()
toCharSeq()
toJavaArray()
toJavaArray(Class)
toJavaCollection(Supplier)
toJavaList()
toJavaList(Supplier)
toJavaMap(Function)
toJavaMap(Supplier, Function)
toJavaOptional()
toJavaSet()
toJavaSet(Supplier)
toJavaStream()
toLeft(Object)
toLeft(Supplier)
toList()
toMap(Function)
toOption()
toQueue()
toRight(Object)
toRight(Supplier)
toSet()
toStack()
toStream()
toString()
toTree()
toTry()
toTry(Supplier)
toVector()
Modifier and Type | Method and Description |
---|---|
default boolean |
contains(T element)
Shortcut for
exists(e -> Objects.equals(e, element)) , tests if the given element is contained. |
default <U> boolean |
corresponds(Iterable<U> that,
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(Predicate<? super T> predicate)
Checks, if an element exists such that the predicate holds.
|
default boolean |
forAll(Predicate<? super T> predicate)
Checks, if the given predicate holds for all elements.
|
default void |
forEach(Consumer<? super T> action)
Performs an action on each element.
|
T |
get()
Gets the underlying value or throws if no value is present.
|
default Option<T> |
getOption()
Gets the underlying value as Option.
|
default T |
getOrElse(Supplier<? extends T> supplier)
Returns the underlying value if present, otherwise
other . |
default T |
getOrElse(T other)
Returns the underlying value if present, otherwise
other . |
default <X extends Throwable> |
getOrElseThrow(Supplier<X> supplier)
Returns the underlying value if present, otherwise throws
supplier.get() . |
default T |
getOrElseTry(Try.CheckedSupplier<? extends T> supplier)
Returns the underlying value if present, otherwise returns the result of
Try.of(supplier).get() . |
int |
hashCode()
Clarifies that values have a proper hashCode() method implemented.
|
boolean |
isEmpty()
Checks, this
Value is empty, i.e. |
boolean |
isSingleValued()
States whether this is a single-valued type.
|
Iterator<T> |
iterator()
Returns a rich
javaslang.collection.Iterator . |
<U> Value<U> |
map(Function<? super T,? extends U> mapper)
Maps the underlying value to a different component type.
|
static <T> Value<T> |
narrow(Value<? extends T> value)
Narrows a widened
Value<? extends T> to Value<T>
by performing a type safe-cast. |
default void |
out(PrintStream out)
Sends the string representations of this to the
PrintStream . |
default void |
out(PrintWriter writer)
Sends the string representations of this to the
PrintWriter . |
Value<T> |
peek(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 to the standard error stream System.err.
|
default void |
stdout()
Sends the string representations of this to the standard output stream System.out.
|
String |
stringPrefix()
Returns the name of this Value type, which is used by toString().
|
default Array<T> |
toArray()
Converts this to a
Array . |
default CharSeq |
toCharSeq()
Converts this to a
CharSeq . |
default Object[] |
toJavaArray()
Converts this to an untyped Java array.
|
default T[] |
toJavaArray(Class<T> componentType)
Converts this to a typed Java array.
|
default <C extends Collection<T>> |
toJavaCollection(Supplier<C> factory)
Converts this to a specific
Collection . |
default List<T> |
toJavaList()
Converts this to an
List . |
default <LIST extends List<T>> |
toJavaList(Supplier<LIST> factory)
Converts this to a specific
List . |
default <K,V> Map<K,V> |
toJavaMap(Function<? super T,? extends Tuple2<? extends K,? extends V>> f)
Converts this to a
Map . |
default <K,V,MAP extends Map<K,V>> |
toJavaMap(Supplier<MAP> factory,
Function<? super T,? extends Tuple2<? extends K,? extends V>> f)
Converts this to a specific
Map . |
default Optional<T> |
toJavaOptional()
Converts this to an
Optional . |
default Set<T> |
toJavaSet()
Converts this to a
Set . |
default <SET extends Set<T>> |
toJavaSet(Supplier<SET> factory)
Converts this to a specific
Set . |
default Stream<T> |
toJavaStream()
Converts this to a
Stream . |
default <R> Either<T,R> |
toLeft(R right)
Converts this to a
Either . |
default <R> Either<T,R> |
toLeft(Supplier<? extends R> right)
Converts this to a
Either . |
default List<T> |
toList()
Converts this to a
List . |
default <K,V> Map<K,V> |
toMap(Function<? super T,? extends Tuple2<? extends K,? extends V>> f)
Converts this to a
Map . |
default Option<T> |
toOption()
Converts this to an
Option . |
default Queue<T> |
toQueue()
Converts this to a
Queue . |
default <L> Either<L,T> |
toRight(L left)
Converts this to a
Either . |
default <L> Either<L,T> |
toRight(Supplier<? extends L> left)
Converts this to a
Either . |
default Set<T> |
toSet()
Converts this to a
Set . |
default Stack<T> |
toStack()
Converts this to a
Stack . |
default Stream<T> |
toStream()
Converts this to a
Stream . |
String |
toString()
Clarifies that values have a proper toString() method implemented.
|
default Tree<T> |
toTree()
Converts this to a
Tree . |
default Try<T> |
toTry()
Converts this to a
Try . |
default Try<T> |
toTry(Supplier<? extends Throwable> ifEmpty)
Converts this to a
Try . |
default Vector<T> |
toVector()
Converts this to a
Vector . |
spliterator
static <T> Value<T> narrow(Value<? extends T> value)
Value<? extends T>
to Value<T>
by performing a type safe-cast. This is eligible because immutable/read-only
collections are covariant.T
- Component type of the Value
.value
- A Value
.value
instance as narrowed type Value<T>
.default boolean contains(T element)
exists(e -> Objects.equals(e, element))
, tests if the given element
is contained.element
- An Object of type A, may be null.default <U> boolean corresponds(Iterable<U> that, 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 Value<?> i1 = List.of(List.of(1, 2), Arrays.asList(List.of(3)));
final Value<?> i2 = Queue.of(Stream.of(1, 2), List.of(Lazy.of(() -> 3)));
assertThat(i1.eq(i2)).isTrue();
Semantics:
o == this : true
o instanceof Value : iterable elements are eq, non-iterable elements equals, for all (o1, o2) in (this, o)
o instanceof Iterable : this eq Iterator.of((Iterable<?>) o);
otherwise : false
o
- An objectdefault boolean exists(Predicate<? super T> predicate)
predicate
- A PredicateNullPointerException
- if predicate
is nulldefault boolean forAll(Predicate<? super T> predicate)
predicate
- A PredicateNullPointerException
- if predicate
is nulldefault void forEach(Consumer<? super T> action)
forEach
in interface Iterable<T>
action
- A Consumer
NullPointerException
- if action
is nullT get()
NoSuchElementException
- if no value is defineddefault Option<T> getOption()
default T getOrElse(T other)
other
.other
- An alternative value.T
default T getOrElse(Supplier<? extends T> supplier)
other
.supplier
- An alternative value supplier.T
NullPointerException
- if supplier is nulldefault <X extends Throwable> T getOrElseThrow(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 getOrElseTry(Try.CheckedSupplier<? extends T> supplier)
Try.of(supplier).get()
.supplier
- An alternative value supplier.T
.NullPointerException
- if supplier is nullTry.NonFatalException
- containing the original exception if this Value was empty and the Try failed.boolean isEmpty()
Value
is empty, i.e. if the underlying value is absent.boolean isSingleValued()
true
if this is single-valued, false
otherwise.<U> Value<U> map(Function<? super T,? extends U> mapper)
U
- The new component typemapper
- A mapperValue<T> peek(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).String stringPrefix()
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 toIllegalStateException
- if PrintStream.checkError()
is true after writing to stream.default 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 toIllegalStateException
- if PrintWriter.checkError()
is true after writing to writer.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.default <C extends Collection<T>> C toJavaCollection(Supplier<C> factory)
Collection
.C
- a sub-type of java.util.Collection
factory
- A java.util.Collection
factoryjava.util.Collection
of type C
default Object[] toJavaArray()
default T[] toJavaArray(Class<T> componentType)
componentType
- Component type of the arrayNullPointerException
- if componentType is nulldefault <LIST extends List<T>> LIST toJavaList(Supplier<LIST> factory)
List
.LIST
- a sub-type of java.util.List
factory
- A java.util.List
factoryjava.util.List
of type LIST
default <K,V> Map<K,V> toJavaMap(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 <K,V,MAP extends Map<K,V>> MAP toJavaMap(Supplier<MAP> factory, Function<? super T,? extends Tuple2<? extends K,? extends V>> f)
Map
.K
- The key typeV
- The value typeMAP
- a sub-type of java.util.Map
factory
- A java.util.Map
factoryf
- A function that maps an element to a key/value pair represented by Tuple2java.util.Map
of type MAP
default Optional<T> toJavaOptional()
Optional
.Optional
.default <SET extends Set<T>> SET toJavaSet(Supplier<SET> factory)
Set
.SET
- a sub-type of java.util.Set
factory
- A java.util.Set
factoryjava.util.Set
of type SET
default <R> Either<T,R> toLeft(Supplier<? extends R> right)
Either
.R
- right typeright
- A supplier of a right valueEither.Right
containing the result of right
if this is empty, otherwise
a new Either.Left
containing this value.NullPointerException
- if right
is nulldefault <R> Either<T,R> toLeft(R right)
Either
.R
- right typeright
- An instance of a right valueEither.Right
containing the value of right
if this is empty, otherwise
a new Either.Left
containing this value.NullPointerException
- if right
is nulldefault <K,V> Map<K,V> toMap(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 <L> Either<L,T> toRight(Supplier<? extends L> left)
Either
.L
- left typeleft
- A supplier of a left valueEither.Left
containing the result of left
if this is empty, otherwise
a new Either.Right
containing this value.NullPointerException
- if left
is nulldefault <L> Either<L,T> toRight(L left)
Either
.L
- left typeleft
- An instance of a left valueEither.Left
containing the value of left
if this is empty, otherwise
a new Either.Right
containing this value.NullPointerException
- if left
is nulldefault 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(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
.boolean equals(Object o)
int hashCode()
See Object.hashCode().
String toString()
See Object.toString().
Copyright © 2017. All Rights Reserved.