public class OneElementNary<T> extends NarySupport<T> implements Unary<T>
Stream.Builder<T>| Constructor and Description |
|---|
OneElementNary() |
| Modifier and Type | Method and Description |
|---|---|
boolean |
allMatch(Predicate<? super T> predicate) |
boolean |
anyMatch(Predicate<? super T> predicate) |
Optional<T> |
asOptional()
Returns a native Optional instance to be used with Nary unaware code
|
protected Stream<T> |
asStream()
Gets a stream with the elements of this instance.
|
void |
close() |
<R,A> R |
collect(Collector<? super T,A,R> collector) |
<R> R |
collect(Supplier<R> supplier,
BiConsumer<R,? super T> accumulator,
BiConsumer<R,R> combiner) |
List<T> |
collectToList()
Returns the content of this nary in a list.
This method is a shorthand of calling collect(Collectors.toList()). This nary, as stream, will be consumed in the operation. |
Set<T> |
collectToSet()
Returns the content of this nary in a set.
This method is a shorthand of calling collect(Collectors.toSet()) This nary, as stream, will be consumed in the operation. |
Nary<T> |
concat(Optional<? extends T> other)
Creates a concatenated Nary with the elements of this instance and the one (if any) on the given
Optional |
long |
count() |
static <T> OneElementNary<T> |
create(T element) |
Unary<T> |
distinct() |
boolean |
equals(Object obj)
This method is redefined so two instances of
Nary are equal only if they contain
equal elements, comparing them in iteration order |
Unary<T> |
filter(Predicate<? super T> predicate) |
Optional<T> |
findAny() |
Unary<T> |
findAnyNary()
|
Optional<T> |
findFirst() |
Unary<T> |
findFirstNary()
|
Unary<T> |
findLast()
Returns the last element in this nary, if present.
Depending on the amount of elements on this Nary, the result may be empty or contain the last element |
void |
forEach(Consumer<? super T> action)
Solves conflict between
Iterable and Stream because Iterable defines a default
implementation.We use Iterable.forEach(Consumer) definition |
void |
forEachOrdered(Consumer<? super T> action) |
T |
get()
If only one value is present in this
Nary, returns the value,
otherwise throws NoSuchElementException, or MoreThanOneElementException. |
int |
hashCode()
This method is redefined so the hashcode of a
Nary is based on its contained elements. |
Unary<T> |
ifAbsent(Runnable runnable)
If the only value is absent invoke the given lambda, or else do nothing
This Nary as Stream is consumed.
|
Unary<T> |
ifPresent(Consumer<? super T> consumer)
If the only value is present, invoke the specified consumer with the value,
otherwise do nothing.
|
boolean |
isAbsent()
Negation of isPresent().
|
boolean |
isParallel() |
boolean |
isPresent()
Return
true if there is only a value present, otherwise false. |
Iterator<T> |
iterator() |
Unary<T> |
limit(long maxSize) |
<R> Unary<R> |
map(Function<? super T,? extends R> mapper) |
<U> Unary<U> |
mapFilteringNullResult(Function<? super T,? extends U> mapper)
Map each element on this instance and filter null results out.
If the result of mapping an element produces null, then it's skipped, reducing the
amount of elements contained in the returned Nary.This is semantically equivalent to Optional.map(Function) and
different from Stream.map(Function) that takes null
as valid results |
Optional<T> |
max(Comparator<? super T> comparator) |
Unary<T> |
maxNary(Comparator<? super T> comparator)
|
Optional<T> |
min(Comparator<? super T> comparator) |
Unary<T> |
minNary(Comparator<? super T> comparator)
|
boolean |
noneMatch(Predicate<? super T> predicate) |
T |
orElse(T other)
Return the only value if present, otherwise return
other. |
T |
orElseGet(Supplier<? extends T> other)
Return the only value if present, otherwise invoke
other and return
the result of that invocation. |
<X extends Throwable> |
orElseThrow(Supplier<? extends X> exceptionSupplier)
Return the only contained value, if present, otherwise throw an exception created by the provided supplier.
|
Unary<T> |
orElseUse(Supplier<? extends T> mapper)
Returns a Nary element that is populated with the given supplier only if this Nary is empty.
This method allows defining a mapping function for nary that can be empty and keep chaining other mapping definitions after that. This stream is consumed and it's assumed to have at most 1 element. |
Stream<T> |
parallel() |
Unary<T> |
peek(Consumer<? super T> action) |
Optional<T> |
reduce(BinaryOperator<T> accumulator) |
T |
reduce(T identity,
BinaryOperator<T> accumulator) |
<U> U |
reduce(U identity,
BiFunction<U,? super T,U> accumulator,
BinaryOperator<U> combiner) |
Unary<T> |
reduceNary(BinaryOperator<T> accumulator)
|
Stream<T> |
sequential() |
Unary<T> |
skip(long n) |
Unary<T> |
sorted() |
Unary<T> |
sorted(Comparator<? super T> comparator) |
Spliterator<T> |
spliterator()
Solves conflict between
Iterable and Stream because Iterable defines a default
implementation.We use Iterable.spliterator() definition |
Object[] |
toArray() |
<A> A[] |
toArray(IntFunction<A[]> generator) |
String |
toString()
This method is redefined so whenever possible, the contained elements are printed
|
Unary<T> |
unique()
Treats this instance as having a single element.
|
Stream<T> |
unordered() |
add, collect, concat, flatMap, flatMapOptional, flatMapToDouble, flatMapToInt, flatMapToLong, mapToDouble, mapToInt, mapToLong, onClose, returningNaryDo, returningNaryDoclone, finalize, getClass, notify, notifyAll, wait, wait, waitempty, from, from, from, from, from, from, from, from, from, from, of, ofNonNullable, ofNonNullableadd, collect, concat, flatMapOptionalflatMapbuilder, concat, flatMapToDouble, flatMapToInt, flatMapToLong, generate, iterate, mapToDouble, mapToInt, mapToLongonClosepublic Optional<T> asOptional() throws MoreThanOneElementException
MonoElementasOptional in interface MonoElement<T>MoreThanOneElementException - if there are more than one elements in this instancepublic Unary<T> unique() throws NaryException
MultiElementUnary
because expectations between runtime and compile time won't matchunique in interface MultiElement<T>MoreThanOneElementException - If this instance has more than one elementNaryExceptionprotected Stream<T> asStream()
NarySupportasStream in class NarySupport<T>public void close()
close in interface AutoCloseableclose in interface BaseStream<T,Stream<T>>close in class NarySupport<T>public <R> R collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner)
public List<T> collectToList()
MultiElementcollectToList in interface MultiElement<T>public Set<T> collectToSet()
MultiElementcollectToSet in interface MultiElement<T>public Nary<T> concat(Optional<? extends T> other)
MultiElementOptionalconcat in interface MultiElement<T>concat in class NarySupport<T>other - The optional to join elements withpublic long count()
public boolean equals(Object obj)
NaryNary are equal only if they contain
equal elements, comparing them in iteration orderequals in interface Nary<T>equals in class NarySupport<T>Object.equals(Object)public Unary<T> findAnyNary()
MultiElementfindAnyNary in interface MultiElement<T>findAnyNary in class NarySupport<T>Nary describing some element of this stream, or an
empty Nary if the stream is emptyStream.findFirst()public Unary<T> findFirstNary()
MultiElementfindFirstNary in interface MultiElement<T>findFirstNary in class NarySupport<T>Nary describing the first element of this stream,
or an empty Nary if the stream is emptypublic Unary<T> findLast()
MultiElementfindLast in interface MultiElement<T>findLast in class NarySupport<T>public void forEach(Consumer<? super T> action)
MultiElementIterable and Stream because Iterable defines a default
implementation.Iterable.forEach(Consumer) definitionpublic void forEachOrdered(Consumer<? super T> action)
forEachOrdered in interface Stream<T>forEachOrdered in class NarySupport<T>public T get() throws NoSuchElementException
MonoElementNary, returns the value,
otherwise throws NoSuchElementException, or MoreThanOneElementException.
This Nary as Stream is consumed to return the value so any attempt to use it as stream will fail after this method
callget in interface MonoElement<T>get in interface Supplier<T>NaryNoSuchElementException - if there is no value presentOptional.get()public int hashCode()
NaryNary is based on its contained elements.hashCode in interface Nary<T>hashCode in class NarySupport<T>Object.hashCode()public Unary<T> ifAbsent(Runnable runnable)
MonoElementifAbsent in interface MonoElement<T>runnable - The code to execute if this optional is emptypublic Unary<T> ifPresent(Consumer<? super T> consumer) throws MoreThanOneElementException
MonoElementifPresent in interface MonoElement<T>consumer - block to be executed if a value is presentMoreThanOneElementException - if there are more than oneOptional.ifPresent(Consumer)public boolean isParallel()
isParallel in interface BaseStream<T,Stream<T>>isParallel in class NarySupport<T>public boolean isPresent()
MonoElementtrue if there is only a value present, otherwise false.
This Nary as Stream is consumed.isPresent in interface MonoElement<T>true if there is a value present, otherwise falseOptional.isPresent()public boolean isAbsent()
throws MoreThanOneElementException
MonoElementisAbsent in interface MonoElement<T>MoreThanOneElementException - If there's more than one value to get as Optionalpublic <U> Unary<U> mapFilteringNullResult(Function<? super T,? extends U> mapper)
MultiElementnull, then it's skipped, reducing the
amount of elements contained in the returned Nary.Optional.map(Function) and
different from Stream.map(Function) that takes null
as valid resultsmapFilteringNullResult in interface MultiElement<T>mapFilteringNullResult in interface Unary<T>mapFilteringNullResult in class NarySupport<T>U - The type of the result of the mapping functionmapper - a mapping function to apply to the value, if presentpublic Optional<T> max(Comparator<? super T> comparator)
public Unary<T> maxNary(Comparator<? super T> comparator)
MultiElementmaxNary in interface MultiElement<T>maxNary in class NarySupport<T>comparator - a non-interfering,
stateless
Comparator to compare elements of this streamNary describing the maximum element of this stream,
or an empty if there are no elementspublic Optional<T> min(Comparator<? super T> comparator)
public Unary<T> minNary(Comparator<? super T> comparator)
MultiElementminNary in interface MultiElement<T>minNary in class NarySupport<T>comparator - a non-interfering,
stateless
Comparator to compare elements of this streamNary describing the minimum element of this stream,
or an empty if there are no elementspublic T orElse(T other) throws MoreThanOneElementException
MonoElementother.
This Nary as Stream is consumed.orElse in interface MonoElement<T>other - the value to be returned if there is no value present, may
be nullotherMoreThanOneElementException - if there are more than onepublic T orElseGet(Supplier<? extends T> other) throws MoreThanOneElementException
MonoElementother and return
the result of that invocation.
This Nary as Stream is consumed.orElseGet in interface MonoElement<T>other - a Supplier whose result is returned if no value
is presentother.asUni().get()MoreThanOneElementException - if there are more than onepublic <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier)
MonoElementorElseThrow in interface MonoElement<T>X - Type of the exception to be thrownexceptionSupplier - The supplier which will return the exception to
be thrownpublic Unary<T> orElseUse(Supplier<? extends T> mapper) throws MoreThanOneElementException
MonoElementorElseUse in interface MonoElement<T>mapper - The function that supplies a value if missingMoreThanOneElementException - If this instance has more than 1 elementpublic Stream<T> parallel()
parallel in interface BaseStream<T,Stream<T>>parallel in class NarySupport<T>public T reduce(T identity, BinaryOperator<T> accumulator)
public Optional<T> reduce(BinaryOperator<T> accumulator)
public <U> U reduce(U identity,
BiFunction<U,? super T,U> accumulator,
BinaryOperator<U> combiner)
public Unary<T> reduceNary(BinaryOperator<T> accumulator)
MultiElementreduceNary in interface MultiElement<T>reduceNary in class NarySupport<T>accumulator - The associative function to reduce the elements of this nary to a result of the same typefor a complete referencepublic Stream<T> sequential()
sequential in interface BaseStream<T,Stream<T>>sequential in class NarySupport<T>public Unary<T> sorted(Comparator<? super T> comparator)
public Spliterator<T> spliterator()
MultiElementIterable and Stream because Iterable defines a default
implementation.Iterable.spliterator() definitionspliterator in interface MultiElement<T>spliterator in interface Iterable<T>spliterator in interface BaseStream<T,Stream<T>>spliterator in class NarySupport<T>public <A> A[] toArray(IntFunction<A[]> generator)
public Object[] toArray()
public String toString()
Narypublic Stream<T> unordered()
unordered in interface BaseStream<T,Stream<T>>unordered in class NarySupport<T>public static <T> OneElementNary<T> create(T element)
Copyright © 2020. All rights reserved.