Interface Value<T>
-
- Type Parameters:
T- The type of the wrapped value.
- All Superinterfaces:
java.lang.Iterable<T>
- All Known Subinterfaces:
BitSet<T>,Either<L,R>,Future<T>,IndexedSeq<T>,Iterator<T>,LinearSeq<T>,List<T>,Map<K,V>,Multimap<K,V>,Option<T>,Seq<T>,Set<T>,SortedMap<K,V>,SortedMultimap<K,V>,SortedSet<T>,Stream<T>,Traversable<T>,Tree<T>,Try<T>,Validation<E,T>
- All Known Implementing Classes:
AbstractIterator,AbstractMultimap,AbstractQueue,Array,BitSetModule.AbstractBitSet,BitSetModule.BitSet1,BitSetModule.BitSet2,BitSetModule.BitSetIterator,BitSetModule.BitSetN,CharSeq,Either.Left,Either.LeftProjection,Either.Right,Either.RightProjection,FutureImpl,HashArrayMappedTrieModule.LeafNodeIterator,HashMap,HashMultimap,HashSet,IteratorModule.CachedIterator,IteratorModule.ConcatIterator,IteratorModule.DistinctIterator,IteratorModule.EmptyIterator,IteratorModule.GroupedIterator,Lazy,LinkedHashMap,LinkedHashMultimap,LinkedHashSet,List.Cons,List.Nil,Option.None,Option.Some,PriorityQueue,Queue,Stream.Cons,Stream.Empty,StreamModule.AppendElements,StreamModule.ConsImpl,StreamModule.FlatMapIterator,StreamModule.StreamIterator,Tree.Empty,Tree.Node,TreeMap,TreeMultimap,TreeSet,Try.Failure,Try.Success,Validation.Invalid,Validation.Valid,Vector
public interface Value<T> extends java.lang.Iterable<T>Functional programming is all about values and transformation of values using functions. TheValuetype 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)stringPrefix()
collect(Collector)collect(Supplier, BiConsumer, BiConsumer)toArray()toCharSeq()toEither(Object)toEither(Supplier)toInvalid(Object)toInvalid(Supplier)toJavaArray()toJavaArray(Class)toJavaCollection(Function)toJavaList()toJavaList(Function)toJavaMap(Function)toJavaMap(Supplier, Function)toJavaMap(Supplier, Function, Function)toJavaOptional()toJavaParallelStream()toJavaSet()toJavaSet(Function)toJavaStream()toLeft(Object)toLeft(Supplier)toLinkedMap(Function)toLinkedMap(Function, Function)toLinkedSet()toList()toMap(Function)toMap(Function, Function)toOption()toPriorityQueue()toPriorityQueue(Comparator)toQueue()toRight(Object)toRight(Supplier)toSet()toSortedMap(Comparator, Function)toSortedMap(Comparator, Function, Function)toSortedMap(Function)toSortedMap(Function, Function)toSortedSet()toSortedSet(Comparator)toStream()toString()toTree()toTry()toTry(Supplier)toValid(Object)toValid(Supplier)toValidation(Object)toValidation(Supplier)toVector()
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Deprecated Methods Modifier and Type Method Description default <R> Rcollect(java.util.function.Supplier<R> supplier, java.util.function.BiConsumer<R,? super T> accumulator, java.util.function.BiConsumer<R,R> combiner)Collects the underlying value(s) (if present) using the givensupplier,accumulatorandcombiner.default <R,A>
Rcollect(java.util.stream.Collector<? super T,A,R> collector)Collects the underlying value(s) (if present) using the providedcollector.default booleancontains(T element)Shortcut forexists(e -> Objects.equals(e, element)), tests if the givenelementis contained.default <U> booleancorresponds(java.lang.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 booleaneq(java.lang.Object o)A smoothing replacement forequals.booleanequals(java.lang.Object o)Clarifies that values have a proper equals() method implemented.default booleanexists(java.util.function.Predicate<? super T> predicate)Checks, if an element exists such that the predicate holds.default booleanforAll(java.util.function.Predicate<? super T> predicate)Checks, if the given predicate holds for all elements.default voidforEach(java.util.function.Consumer<? super T> action)Performs an action on each element.Tget()Gets the underlying value or throws if no value is present.default TgetOrElse(java.util.function.Supplier<? extends T> supplier)Returns the underlying value if present, otherwiseother.default TgetOrElse(T other)Returns the underlying value if present, otherwiseother.default <X extends java.lang.Throwable>
TgetOrElseThrow(java.util.function.Supplier<X> supplier)Returns the underlying value if present, otherwise throwssupplier.get().default TgetOrElseTry(CheckedFunction0<? extends T> supplier)Returns the underlying value if present, otherwise returns the result ofTry.of(supplier).get().default TgetOrNull()Returns the underlying value if present, otherwisenull.inthashCode()Clarifies that values have a proper hashCode() method implemented.booleanisAsync()Checks if thisValueis asynchronously (short: async) computed.booleanisEmpty()Checks, thisValueis empty, i.e.booleanisLazy()Checks if thisValueis lazily evaluated.booleanisSingleValued()States whether this is a single-valued type.Iterator<T>iterator()Returns a richio.vavr.collection.Iterator.<U> Value<U>map(java.util.function.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 widenedValue<? extends T>toValue<T>by performing a type-safe cast.default voidout(java.io.PrintStream out)Sends the string representations of this to thePrintStream.default voidout(java.io.PrintWriter writer)Sends the string representations of this to thePrintWriter.Value<T>peek(java.util.function.Consumer<? super T> action)Performs the givenactionon the first element if this is an eager implementation.default java.util.Spliterator<T>spliterator()default voidstderr()Sends the string representations of this to the standard error stream System.err.default voidstdout()Sends the string representations of this to the standard output stream System.out.java.lang.StringstringPrefix()Returns the name of this Value type, which is used by toString().default Array<T>toArray()Converts this to aArray.default CharSeqtoCharSeq()Converts this to aCharSeq.default java.util.concurrent.CompletableFuture<T>toCompletableFuture()Converts this to aCompletableFuturedefault <L> Either<L,T>toEither(java.util.function.Supplier<? extends L> leftSupplier)Converts this to anEither.default <L> Either<L,T>toEither(L left)Converts this to anEither.default <U> Validation<T,U>toInvalid(java.util.function.Supplier<? extends U> valueSupplier)Deprecated.UsetoValidation(Supplier)instead.default <U> Validation<T,U>toInvalid(U value)Deprecated.UsetoValidation(Object)instead.default java.lang.Object[]toJavaArray()Converts this to a Java array with component typeObjectdefault T[]toJavaArray(java.lang.Class<T> componentType)Deprecated.UsetoJavaArray(IntFunction)insteaddefault T[]toJavaArray(java.util.function.IntFunction<T[]> arrayFactory)Converts this to a Java array having an accurate component type.default <C extends java.util.Collection<T>>
CtoJavaCollection(java.util.function.Function<java.lang.Integer,C> factory)Converts this to a specific mutableCollectionof typeC.default java.util.List<T>toJavaList()Converts this to a mutableList.default <LIST extends java.util.List<T>>
LISTtoJavaList(java.util.function.Function<java.lang.Integer,LIST> factory)Converts this to a specific mutableList.default <K,V>
java.util.Map<K,V>toJavaMap(java.util.function.Function<? super T,? extends Tuple2<? extends K,? extends V>> f)Converts this to a mutableMap.default <K,V,MAP extends java.util.Map<K,V>>
MAPtoJavaMap(java.util.function.Supplier<MAP> factory, java.util.function.Function<? super T,? extends Tuple2<? extends K,? extends V>> f)Converts this to a specific mutableMap.default <K,V,MAP extends java.util.Map<K,V>>
MAPtoJavaMap(java.util.function.Supplier<MAP> factory, java.util.function.Function<? super T,? extends K> keyMapper, java.util.function.Function<? super T,? extends V> valueMapper)Converts this to a specific mutableMap.default java.util.Optional<T>toJavaOptional()Converts this to anOptional.default java.util.stream.Stream<T>toJavaParallelStream()Converts this to a parallelStreamby callingStreamSupport.stream(this.spliterator(), true).default java.util.Set<T>toJavaSet()Converts this to a mutableSet.default <SET extends java.util.Set<T>>
SETtoJavaSet(java.util.function.Function<java.lang.Integer,SET> factory)Converts this to a specificSet.default java.util.stream.Stream<T>toJavaStream()Converts this to a sequentialStreamby callingStreamSupport.stream(this.spliterator(), false).default <R> Either<T,R>toLeft(java.util.function.Supplier<? extends R> right)Deprecated.UsetoEither(Supplier)instead.default <R> Either<T,R>toLeft(R right)Deprecated.UsetoEither(Object)instead.default <K,V>
Map<K,V>toLinkedMap(java.util.function.Function<? super T,? extends Tuple2<? extends K,? extends V>> f)Converts this to aMap.default <K,V>
Map<K,V>toLinkedMap(java.util.function.Function<? super T,? extends K> keyMapper, java.util.function.Function<? super T,? extends V> valueMapper)Converts this to aMap.default Set<T>toLinkedSet()Converts this to aSet.default List<T>toList()Converts this to aList.default <K,V>
Map<K,V>toMap(java.util.function.Function<? super T,? extends Tuple2<? extends K,? extends V>> f)Converts this to aMap.default <K,V>
Map<K,V>toMap(java.util.function.Function<? super T,? extends K> keyMapper, java.util.function.Function<? super T,? extends V> valueMapper)Converts this to aMap.default Option<T>toOption()Converts this to anOption.default PriorityQueue<T>toPriorityQueue()Converts this to aPriorityQueue.default PriorityQueue<T>toPriorityQueue(java.util.Comparator<? super T> comparator)Converts this to aPriorityQueue.default Queue<T>toQueue()Converts this to aQueue.default <L> Either<L,T>toRight(java.util.function.Supplier<? extends L> left)Deprecated.UsetoEither(Supplier)instead.default <L> Either<L,T>toRight(L left)Deprecated.UsetoEither(Object)instead.default Set<T>toSet()Converts this to aSet.default <K,V>
SortedMap<K,V>toSortedMap(java.util.Comparator<? super K> comparator, java.util.function.Function<? super T,? extends Tuple2<? extends K,? extends V>> f)Converts this to aMap.default <K,V>
SortedMap<K,V>toSortedMap(java.util.Comparator<? super K> comparator, java.util.function.Function<? super T,? extends K> keyMapper, java.util.function.Function<? super T,? extends V> valueMapper)Converts this to aMap.default <K extends java.lang.Comparable<? super K>,V>
SortedMap<K,V>toSortedMap(java.util.function.Function<? super T,? extends Tuple2<? extends K,? extends V>> f)Converts this to aMap.default <K extends java.lang.Comparable<? super K>,V>
SortedMap<K,V>toSortedMap(java.util.function.Function<? super T,? extends K> keyMapper, java.util.function.Function<? super T,? extends V> valueMapper)Converts this to aMap.default SortedSet<T>toSortedSet()Converts this to aSortedSet.default SortedSet<T>toSortedSet(java.util.Comparator<? super T> comparator)Converts this to aSortedSet.default Stream<T>toStream()Converts this to aStream.java.lang.StringtoString()Clarifies that values have a proper toString() method implemented.default Tree<T>toTree()Converts this to aTree.default <ID> List<Tree.Node<T>>toTree(java.util.function.Function<? super T,? extends ID> idMapper, java.util.function.Function<? super T,? extends ID> parentMapper)default Try<T>toTry()Converts this to aTry.default Try<T>toTry(java.util.function.Supplier<? extends java.lang.Throwable> ifEmpty)Converts this to aTry.default <E> Validation<E,T>toValid(E error)Deprecated.UsetoValidation(Object)instead.default <E> Validation<E,T>toValid(java.util.function.Supplier<? extends E> errorSupplier)Deprecated.UsetoValidation(Supplier)instead.default <E> Validation<E,T>toValidation(E invalid)Converts this to anValidation.default <E> Validation<E,T>toValidation(java.util.function.Supplier<? extends E> invalidSupplier)Converts this to anValidation.default Vector<T>toVector()Converts this to aVector.
-
-
-
Method Detail
-
narrow
static <T> Value<T> narrow(Value<? extends T> value)
Narrows a widenedValue<? extends T>toValue<T>by performing a type-safe cast. This is eligible because immutable/read-only collections are covariant.- Type Parameters:
T- Component type of theValue.- Parameters:
value- AValue.- Returns:
- the given
valueinstance as narrowed typeValue<T>.
-
collect
default <R,A> R collect(java.util.stream.Collector<? super T,A,R> collector)
Collects the underlying value(s) (if present) using the providedcollector.- Type Parameters:
A- the mutable accumulation type of the reduction operationR- the result type of the reduction operation- Parameters:
collector- Collector performing reduction- Returns:
- R reduction result
-
collect
default <R> R collect(java.util.function.Supplier<R> supplier, java.util.function.BiConsumer<R,? super T> accumulator, java.util.function.BiConsumer<R,R> combiner)Collects the underlying value(s) (if present) using the givensupplier,accumulatorandcombiner.- Type Parameters:
R- type of the result- Parameters:
supplier- provide unit value for reductionaccumulator- perform reduction with unit valuecombiner- function for combining two values, which must be compatible with the accumulator.- Returns:
- R reduction result
-
contains
default boolean contains(T element)
Shortcut forexists(e -> Objects.equals(e, element)), tests if the givenelementis contained.- Parameters:
element- An Object of type A, may be null.- Returns:
- true, if element is contained, false otherwise.
-
corresponds
default <U> boolean corresponds(java.lang.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.- Type Parameters:
U- Component type of that iterable- Parameters:
that- the other iterablepredicate- the test predicate, which relates elements from both iterables- Returns:
trueif both iterables have the same length andpredicate(x, y)istruefor all corresponding elementsxof this iterable andyofthat, otherwisefalse.
-
eq
default boolean eq(java.lang.Object o)
A smoothing replacement forequals. 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- Parameters:
o- An object- Returns:
- true, if this equals o according to the rules defined above, otherwise false.
-
exists
default boolean exists(java.util.function.Predicate<? super T> predicate)
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- ifpredicateis null
-
forAll
default boolean forAll(java.util.function.Predicate<? super T> predicate)
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- ifpredicateis null
-
forEach
default void forEach(java.util.function.Consumer<? super T> action)
Performs an action on each element.- Specified by:
forEachin interfacejava.lang.Iterable<T>- Parameters:
action- AConsumer- Throws:
java.lang.NullPointerException- ifactionis null
-
get
T get()
Gets the underlying value or throws if no value is present.IMPORTANT! This method will throw an undeclared
ThrowableifisEmpty() == trueis true.Because the 'empty' state indicates that there is no value present that can be returned,
get()has to throw in such a case. Generally, implementing classes should throw aNoSuchElementExceptionifisEmpty()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 value if this is not empty, otherwise
get()throws aThrowable
-
getOrElse
default T getOrElse(T other)
Returns the underlying value if present, otherwiseother.- Parameters:
other- An alternative value.- Returns:
- A value of type
T
-
getOrElse
default T getOrElse(java.util.function.Supplier<? extends T> supplier)
Returns the underlying value if present, otherwiseother.- Parameters:
supplier- An alternative value supplier.- Returns:
- A value 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.Throwable
Returns the underlying value if present, otherwise throwssupplier.get().- Type Parameters:
X- a Throwable type- Parameters:
supplier- An exception supplier.- Returns:
- A value of type
T. - Throws:
java.lang.NullPointerException- if supplier is nullX- if no value is presentX extends java.lang.Throwable
-
getOrElseTry
default T getOrElseTry(CheckedFunction0<? extends T> supplier)
Returns the underlying value if present, otherwise returns the result ofTry.of(supplier).get().- Parameters:
supplier- An alternative value supplier.- Returns:
- A value of type
T. - Throws:
java.lang.NullPointerException- if supplier is null
-
getOrNull
default T getOrNull()
Returns the underlying value if present, otherwisenull.- Returns:
- A value of type
Tornull.
-
isAsync
boolean isAsync()
Checks if thisValueis asynchronously (short: async) computed.Methods of a
Valueinstance 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
Valueis async (likeFuture), false otherwise.
-
isEmpty
boolean isEmpty()
Checks, thisValueis empty, i.e. if the underlying value is absent.- Returns:
- false, if no underlying value is present, true otherwise.
-
isLazy
boolean isLazy()
Checks if thisValueis lazily evaluated.
-
isSingleValued
boolean isSingleValued()
States whether this is a single-valued type.- Returns:
trueif this is single-valued,falseotherwise.
-
map
<U> Value<U> map(java.util.function.Function<? super T,? extends U> mapper)
Maps the underlying value to a different component type.- Type Parameters:
U- The new component type- Parameters:
mapper- A mapper- Returns:
- A new value
-
peek
Value<T> peek(java.util.function.Consumer<? super T> action)
Performs the givenactionon the first element if this is an eager implementation. Performs the givenactionon 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
-
stringPrefix
java.lang.String stringPrefix()
Returns the name of this Value type, which is used by toString().- Returns:
- This type name.
-
out
@GwtIncompatible("java.io.PrintStream is not implemented") default void out(java.io.PrintStream out)
Sends the string representations of this to thePrintStream. If this value consists of multiple elements, each element is displayed in a new line.- Parameters:
out- The PrintStream to write to- Throws:
java.lang.IllegalStateException- ifPrintStream.checkError()is true after writing to stream.
-
out
@GwtIncompatible("java.io.PrintWriter is not implemented") default void out(java.io.PrintWriter writer)
Sends the string representations of this to thePrintWriter. If this value consists of multiple elements, each element is displayed in a new line.- Parameters:
writer- The PrintWriter to write to- Throws:
java.lang.IllegalStateException- ifPrintWriter.checkError()is true after writing to writer.
-
stderr
@GwtIncompatible("java.io.PrintStream is not implemented") default void stderr()
Sends the string representations of this to the standard error stream System.err. If this value consists of multiple elements, each element is displayed in a new line.- Throws:
java.lang.IllegalStateException- ifPrintStream.checkError()is true after writing to stderr.
-
stdout
@GwtIncompatible("java.io.PrintStream is not implemented") default void stdout()
Sends the string representations of this to the standard output stream System.out. If this value consists of multiple elements, each element is displayed in a new line.- Throws:
java.lang.IllegalStateException- ifPrintStream.checkError()is true after writing to stdout.
-
iterator
Iterator<T> iterator()
Returns a richio.vavr.collection.Iterator.- Specified by:
iteratorin interfacejava.lang.Iterable<T>- Returns:
- A new Iterator
-
toCompletableFuture
@GwtIncompatible default java.util.concurrent.CompletableFuture<T> toCompletableFuture()
Converts this to aCompletableFuture- Returns:
- A new
CompletableFuturecontaining the value
-
toInvalid
@Deprecated default <U> Validation<T,U> toInvalid(U value)
Deprecated.UsetoValidation(Object)instead.Converts this to aValidation.- Type Parameters:
U- value type of aValid- Parameters:
value- An instance of aValidvalue- Returns:
- A new
Validation.Validcontaining the givenvalueif this is empty, otherwise a newValidation.Invalidcontaining this value.
-
toInvalid
@Deprecated default <U> Validation<T,U> toInvalid(java.util.function.Supplier<? extends U> valueSupplier)
Deprecated.UsetoValidation(Supplier)instead.Converts this to aValidation.- Type Parameters:
U- value type of aValid- Parameters:
valueSupplier- A supplier of aValidvalue- Returns:
- A new
Validation.Validcontaining the result ofvalueSupplierif this is empty, otherwise a newValidation.Invalidcontaining this value. - Throws:
java.lang.NullPointerException- ifvalueSupplieris null
-
toJavaArray
default java.lang.Object[] toJavaArray()
Converts this to a Java array with component typeObject// = [] of type Object[] Future.<String> of(() -> { throw new Error(); }) .toJavaArray() // = [ok] of type Object[] Try.of(() -> "ok") .toJavaArray() // = [1, 2, 3] of type Object[] List.of(1, 2, 3) .toJavaArray()- Returns:
- A new Java array.
-
toJavaArray
@Deprecated @GwtIncompatible("reflection is not supported") default T[] toJavaArray(java.lang.Class<T> componentType)
Deprecated.UsetoJavaArray(IntFunction)insteadConverts this to a Java array having an accurate component type.// = [] of type String[] Future.<String> of(() -> { throw new Error(); }) .toJavaArray(String.class) // = [ok] of type String[] Try.of(() -> "ok") .toJavaArray(String.class) // = [1, 2, 3] of type Integer[] List.of(1, 2, 3) .toJavaArray(Integer.class)- Parameters:
componentType- Component type of the array- Returns:
- A new Java array.
- Throws:
java.lang.NullPointerException- if componentType is null
-
toJavaArray
default T[] toJavaArray(java.util.function.IntFunction<T[]> arrayFactory)
Converts this to a Java array having an accurate component type.// = [] of type String[] Future.<String> of(() -> { throw new Error(); }) .toJavaArray(String[]::new) // = [ok] of type String[] Try.of(() -> "ok") .toJavaArray(String[]::new) // = [1, 2, 3] of type Integer[] List.of(1, 2, 3) .toJavaArray(Integer[]::new)- Parameters:
arrayFactory- anintargument function that creates an array of the correct component type with the specified size- Returns:
- The array provided by the factory filled with the values from this
Value. - Throws:
java.lang.NullPointerException- if componentType is null
-
toJavaCollection
default <C extends java.util.Collection<T>> C toJavaCollection(java.util.function.Function<java.lang.Integer,C> factory)
Converts this to a specific mutableCollectionof typeC. Elements are added by callingCollection.add(Object).// = [] Future.<String> of(() -> { throw new Error(); }) .toJavaCollection(java.util.HashSet::new) // = [ok] Try.of(() -> "ok") .toJavaCollection(java.util.HashSet::new) // = [1, 2, 3] List.of(1, 2, 3) .toJavaCollection(java.util.LinkedHashSet::new)- Type Parameters:
C- a sub-type ofjava.util.Collection- Parameters:
factory- A factory that returns an empty mutablejava.util.Collectionwith the specified initial capacity- Returns:
- a new
java.util.Collectionof typeC
-
toJavaList
default java.util.List<T> toJavaList()
Converts this to a mutableList. Elements are added by callingList.add(Object).// = [] Future.<String> of(() -> { throw new Error(); }) .toJavaList() // = [ok] Try.of(() -> "ok") .toJavaList() // = [1, 2, 3] List.of(1, 2, 3) .toJavaList()- Returns:
- A new
ArrayList.
-
toJavaList
default <LIST extends java.util.List<T>> LIST toJavaList(java.util.function.Function<java.lang.Integer,LIST> factory)
Converts this to a specific mutableList. Elements are added by callingList.add(Object).// = [] Future.<String> of(() -> { throw new Error(); }) .toJavaList(java.util.ArrayList::new) // = [ok] Try.of(() -> "ok") .toJavaList(java.util.ArrayList::new) // = [1, 2, 3] List.of(1, 2, 3) .toJavaList(java.util.ArrayList::new) // = [1, 2, 3] List.of(1, 2, 3) .toJavaList(capacity -> new java.util.LinkedList<>())- Type Parameters:
LIST- A sub-type ofjava.util.List- Parameters:
factory- A factory that returns an empty mutablejava.util.Listwith the specified initial capacity- Returns:
- a new
java.util.Listof typeLIST
-
toJavaMap
default <K,V> java.util.Map<K,V> toJavaMap(java.util.function.Function<? super T,? extends Tuple2<? extends K,? extends V>> f)
Converts this to a mutableMap. Elements are added by callingMap.put(Object, Object).// = {} Future.<String> of(() -> { throw new Error(); }) .toJavaMap(s -> Tuple.of(s, s.length())) // = {ok=2} Try.of(() -> "ok") .toJavaMap(s -> Tuple.of(s, s.length())) // = {1=A, 2=B, 3=C} List.of(1, 2, 3) .toJavaMap(i -> Tuple.of(i, (char) (i + 64)))- Type Parameters:
K- The key typeV- The value type- Parameters:
f- A function that maps an element to a key/value pair represented by Tuple2- Returns:
- A new
HashMap.
-
toJavaMap
default <K,V,MAP extends java.util.Map<K,V>> MAP toJavaMap(java.util.function.Supplier<MAP> factory, java.util.function.Function<? super T,? extends K> keyMapper, java.util.function.Function<? super T,? extends V> valueMapper)Converts this to a specific mutableMap. Elements are added by callingMap.put(Object, Object).// = {} Future.<String> of(() -> { throw new Error(); }) .toJavaMap(java.util.HashMap::new, s -> s, String::length) // = {ok=2} Try.of(() -> "ok") .toJavaMap(java.util.TreeMap::new, s -> s, String::length) // = {1=A, 2=B, 3=C} List.of(1, 2, 3) .toJavaMap(java.util.TreeMap::new, i -> i, i -> (char) (i + 64))- Type Parameters:
K- The key typeV- The value typeMAP- a sub-type ofjava.util.Map- Parameters:
factory- A factory that creates an empty mutablejava.util.MapkeyMapper- A function that maps an element to a keyvalueMapper- A function that maps an element to a value- Returns:
- a new
java.util.Mapof typeMAP
-
toJavaMap
default <K,V,MAP extends java.util.Map<K,V>> MAP toJavaMap(java.util.function.Supplier<MAP> factory, java.util.function.Function<? super T,? extends Tuple2<? extends K,? extends V>> f)Converts this to a specific mutableMap. Elements are added by callingMap.put(Object, Object).// = {} Future.<String> of(() -> { throw new Error(); }) .toJavaMap(java.util.HashMap::new, s -> Tuple.of(s, s.length())) // = {ok=2} Try.of(() -> "ok") .toJavaMap(java.util.TreeMap::new, s -> Tuple.of(s, s.length())) // = {1=A, 2=B, 3=C} List.of(1, 2, 3) .toJavaMap(java.util.TreeMap::new, i -> Tuple.of(i, (char) (i + 64)))- Type Parameters:
K- The key typeV- The value typeMAP- a sub-type ofjava.util.Map- Parameters:
factory- A factory that creates an empty mutablejava.util.Mapf- A function that maps an element to a key/value pair represented by Tuple2- Returns:
- a new
java.util.Mapof typeMAP
-
toJavaOptional
default java.util.Optional<T> 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.
-
toJavaSet
default java.util.Set<T> toJavaSet()
Converts this to a mutableSet. Elements are added by callingSet.add(Object).// = [] Future.of(() -> { throw new Error(); }) .toJavaSet() // = [ok] Try.of(() -> "ok") .toJavaSet() // = [1, 2, 3] List.of(1, 2, 3) .toJavaSet()- Returns:
- A new
HashSet.
-
toJavaSet
default <SET extends java.util.Set<T>> SET toJavaSet(java.util.function.Function<java.lang.Integer,SET> factory)
Converts this to a specificSet. Elements are added by callingSet.add(Object).// = [] Future.of(() -> { throw new Error(); }) .toJavaSet(java.util.HashSet::new) // = [ok] Try.of(() -> "ok") .toJavaSet(java.util.HashSet::new) // = [3, 2, 1] List.of(1, 2, 3) .toJavaSet(capacity -> new java.util.TreeSet<>(Comparator.reverseOrder()))- Type Parameters:
SET- a sub-type ofjava.util.Set- Parameters:
factory- A factory that returns an empty mutablejava.util.Setwith the specified initial capacity- Returns:
- a new
java.util.Setof typeSET
-
toJavaStream
default java.util.stream.Stream<T> toJavaStream()
Converts this to a sequentialStreamby callingStreamSupport.stream(this.spliterator(), false).// empty Stream Future.of(() -> { throw new Error(); }) .toJavaStream() // Stream containing "ok" Try.of(() -> "ok") .toJavaStream() // Stream containing 1, 2, 3 List.of(1, 2, 3) .toJavaStream()- Returns:
- A new sequential
Stream. - See Also:
spliterator()
-
toJavaParallelStream
default java.util.stream.Stream<T> toJavaParallelStream()
Converts this to a parallelStreamby callingStreamSupport.stream(this.spliterator(), true).// empty Stream Future.of(() -> { throw new Error(); }) .toJavaParallelStream() // Stream containing "ok" Try.of(() -> "ok") .toJavaParallelStream() // Stream containing 1, 2, 3 List.of(1, 2, 3) .toJavaParallelStream()- Returns:
- A new parallel
Stream. - See Also:
spliterator()
-
toLeft
@Deprecated default <R> Either<T,R> toLeft(R right)
Deprecated.UsetoEither(Object)instead.Converts this to aEither.- Type Parameters:
R- right type- Parameters:
right- An instance of a right value- Returns:
- A new
Either.Rightcontaining the value ofrightif this is empty, otherwise a newEither.Leftcontaining this value.
-
toLeft
@Deprecated default <R> Either<T,R> toLeft(java.util.function.Supplier<? extends R> right)
Deprecated.UsetoEither(Supplier)instead.Converts this to aEither.- Type Parameters:
R- right type- Parameters:
right- A supplier of a right value- Returns:
- A new
Either.Rightcontaining the result ofrightif this is empty, otherwise a newEither.Leftcontaining this value. - Throws:
java.lang.NullPointerException- ifrightis null
-
toMap
default <K,V> Map<K,V> toMap(java.util.function.Function<? super T,? extends K> keyMapper, java.util.function.Function<? super T,? extends V> valueMapper)
Converts this to aMap.- Type Parameters:
K- The key typeV- The value type- Parameters:
keyMapper- A function that maps an element to a keyvalueMapper- A function that maps an element to a value- Returns:
- A new
HashMap.
-
toMap
default <K,V> Map<K,V> toMap(java.util.function.Function<? super T,? extends Tuple2<? extends K,? extends V>> f)
Converts this to aMap.- Type Parameters:
K- The key typeV- The value type- Parameters:
f- A function that maps an element to a key/value pair represented by Tuple2- Returns:
- A new
HashMap.
-
toLinkedMap
default <K,V> Map<K,V> toLinkedMap(java.util.function.Function<? super T,? extends K> keyMapper, java.util.function.Function<? super T,? extends V> valueMapper)
Converts this to aMap.- Type Parameters:
K- The key typeV- The value type- Parameters:
keyMapper- A function that maps an element to a keyvalueMapper- A function that maps an element to a value- Returns:
- A new
LinkedHashMap.
-
toLinkedMap
default <K,V> Map<K,V> toLinkedMap(java.util.function.Function<? super T,? extends Tuple2<? extends K,? extends V>> f)
Converts this to aMap.- Type Parameters:
K- The key typeV- The value type- Parameters:
f- A function that maps an element to a key/value pair represented by Tuple2- Returns:
- A new
LinkedHashMap.
-
toSortedMap
default <K extends java.lang.Comparable<? super K>,V> SortedMap<K,V> toSortedMap(java.util.function.Function<? super T,? extends K> keyMapper, java.util.function.Function<? super T,? extends V> valueMapper)
Converts this to aMap.- Type Parameters:
K- The key typeV- The value type- Parameters:
keyMapper- A function that maps an element to a keyvalueMapper- A function that maps an element to a value- Returns:
- A new
TreeMap.
-
toSortedMap
default <K extends java.lang.Comparable<? super K>,V> SortedMap<K,V> toSortedMap(java.util.function.Function<? super T,? extends Tuple2<? extends K,? extends V>> f)
Converts this to aMap.- Type Parameters:
K- The key typeV- The value type- Parameters:
f- A function that maps an element to a key/value pair represented by Tuple2- Returns:
- A new
TreeMap.
-
toSortedMap
default <K,V> SortedMap<K,V> toSortedMap(java.util.Comparator<? super K> comparator, java.util.function.Function<? super T,? extends K> keyMapper, java.util.function.Function<? super T,? extends V> valueMapper)
Converts this to aMap.- Type Parameters:
K- The key typeV- The value type- Parameters:
comparator- A comparator that induces an order of the Map keys.keyMapper- A function that maps an element to a keyvalueMapper- A function that maps an element to a value- Returns:
- A new
TreeMap.
-
toSortedMap
default <K,V> SortedMap<K,V> toSortedMap(java.util.Comparator<? super K> comparator, java.util.function.Function<? super T,? extends Tuple2<? extends K,? extends V>> f)
Converts this to aMap.- Type Parameters:
K- The key typeV- The value type- Parameters:
comparator- A comparator that induces an order of the Map keys.f- A function that maps an element to a key/value pair represented by Tuple2- Returns:
- A new
TreeMap.
-
toEither
default <L> Either<L,T> toEither(java.util.function.Supplier<? extends L> leftSupplier)
Converts this to anEither.
-
toValidation
default <E> Validation<E,T> toValidation(E invalid)
Converts this to anValidation.- Type Parameters:
E- Validation error component type- Parameters:
invalid- An invalid value for theValidation- Returns:
- A new
Validation.
-
toValidation
default <E> Validation<E,T> toValidation(java.util.function.Supplier<? extends E> invalidSupplier)
Converts this to anValidation.- Type Parameters:
E- Validation error component type- Parameters:
invalidSupplier- ASupplierfor the invalid value for theValidation- Returns:
- A new
Validation.
-
toPriorityQueue
default PriorityQueue<T> toPriorityQueue()
Converts this to aPriorityQueue.- Returns:
- A new
PriorityQueue.
-
toPriorityQueue
default PriorityQueue<T> toPriorityQueue(java.util.Comparator<? super T> comparator)
Converts this to aPriorityQueue.- Parameters:
comparator- A comparator that induces an order of the PriorityQueue elements.- Returns:
- A new
PriorityQueue.
-
toRight
@Deprecated default <L> Either<L,T> toRight(L left)
Deprecated.UsetoEither(Object)instead.Converts this to aEither.- Type Parameters:
L- left type- Parameters:
left- An instance of a left value- Returns:
- A new
Either.Leftcontaining the value ofleftif this is empty, otherwise a newEither.Rightcontaining this value.
-
toRight
@Deprecated default <L> Either<L,T> toRight(java.util.function.Supplier<? extends L> left)
Deprecated.UsetoEither(Supplier)instead.Converts this to aEither.- Type Parameters:
L- left type- Parameters:
left- A supplier of a left value- Returns:
- A new
Either.Leftcontaining the result ofleftif this is empty, otherwise a newEither.Rightcontaining this value. - Throws:
java.lang.NullPointerException- ifleftis null
-
toLinkedSet
default Set<T> toLinkedSet()
Converts this to aSet.- Returns:
- A new
LinkedHashSet.
-
toSortedSet
default SortedSet<T> toSortedSet() throws java.lang.ClassCastException
Converts this to aSortedSet. Current items must be comparable- Returns:
- A new
TreeSet. - Throws:
java.lang.ClassCastException- if items are not comparable
-
toSortedSet
default SortedSet<T> toSortedSet(java.util.Comparator<? super T> comparator)
Converts this to aSortedSet.- Parameters:
comparator- A comparator that induces an order of the SortedSet elements.- Returns:
- A new
TreeSet.
-
toTry
default Try<T> toTry()
Converts this to aTry.If this value is undefined, i.e. empty, then a new
Failure(NoSuchElementException)is returned, otherwise a newSuccess(value)is returned.- Returns:
- A new
Try.
-
toTry
default Try<T> toTry(java.util.function.Supplier<? extends java.lang.Throwable> ifEmpty)
Converts this to aTry.If this value is undefined, i.e. empty, then a new
Failure(ifEmpty.get())is returned, otherwise a newSuccess(value)is returned.- Parameters:
ifEmpty- an exception supplier- Returns:
- A new
Try.
-
toTree
default <ID> List<Tree.Node<T>> toTree(java.util.function.Function<? super T,? extends ID> idMapper, java.util.function.Function<? super T,? extends ID> parentMapper)
- Type Parameters:
ID- Id type- Parameters:
idMapper- A mapper from source item to unique identifier of that itemparentMapper- A mapper from source item to unique identifier of parent item. Need return null for root items- Returns:
- A new
Tree. - See Also:
Tree.build(Iterable, Function, Function)
-
toValid
@Deprecated default <E> Validation<E,T> toValid(E error)
Deprecated.UsetoValidation(Object)instead.Converts this to aValidation.- Type Parameters:
E- error type of anInvalid- Parameters:
error- An error- Returns:
- A new
Validation.Invalidcontaining the givenerrorif this is empty, otherwise a newValidation.Validcontaining this value.
-
toValid
@Deprecated default <E> Validation<E,T> toValid(java.util.function.Supplier<? extends E> errorSupplier)
Deprecated.UsetoValidation(Supplier)instead.Converts this to aValidation.- Type Parameters:
E- error type of anInvalid- Parameters:
errorSupplier- A supplier of an error- Returns:
- A new
Validation.Invalidcontaining the result oferrorSupplierif this is empty, otherwise a newValidation.Validcontaining this value. - Throws:
java.lang.NullPointerException- ifvalueSupplieris null
-
spliterator
default java.util.Spliterator<T> spliterator()
- Specified by:
spliteratorin interfacejava.lang.Iterable<T>
-
equals
boolean equals(java.lang.Object o)
Clarifies that values have a proper equals() method implemented.- Overrides:
equalsin classjava.lang.Object- Parameters:
o- An object- Returns:
- true, if this equals o, false otherwise
-
hashCode
int hashCode()
Clarifies that values have a proper hashCode() method implemented.See Object.hashCode().
- Overrides:
hashCodein classjava.lang.Object- Returns:
- The hashcode of this object
-
toString
java.lang.String toString()
Clarifies that values have a proper toString() method implemented.See Object.toString().
- Overrides:
toStringin classjava.lang.Object- Returns:
- A String representation of this object
-
-