com.speedment.jpastreamer.builder.standard.internal.StreamBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of builder-standard Show documentation
Show all versions of builder-standard Show documentation
Standard provider for creating stream builders
/*
* JPAstreamer - Express JPA queries with Java Streams
* Copyright (c) 2020-2022, Speedment, Inc. All Rights Reserved.
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* See: https://github.com/speedment/jpa-streamer/blob/master/LICENSE
*/
package com.speedment.jpastreamer.builder.standard.internal;
import com.speedment.jpastreamer.pipeline.intermediate.IntermediateOperationFactory;
import com.speedment.jpastreamer.pipeline.terminal.TerminalOperationFactory;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Optional;
import java.util.Spliterator;
import java.util.function.*;
import java.util.stream.*;
/**
* A StreamBuilder that will accumulate intermediate and terminal operations
* and, upon a terminal operation, will invoke a renderer to actually create a
* Stream.
*
* @param Entity type which is the same as T for the initial stream but
* might be different after map operations.
* @param Type of the Stream to create
*
*/
final class StreamBuilder
extends BaseStreamBuilder>
implements Stream {
StreamBuilder(final BaseBuilderState baseState) {
super(baseState);
}
@Override
public Stream filter(Predicate super T> predicate) {
add(iof().createFilter(predicate));
return this;
}
@SuppressWarnings("unchecked")
@Override
public Stream map(Function super T, ? extends R> mapper) {
add(iof().createMap(mapper));
return (Stream) this;
}
@Override
public IntStream mapToInt(ToIntFunction super T> mapper) {
add(iof().createMapToInt(mapper));
linked();
return new IntStreamBuilder<>(baseState());
}
@Override
public LongStream mapToLong(ToLongFunction super T> mapper) {
add(iof().createMapToLong(mapper));
linked();
return new LongStreamBuilder<>(baseState());
}
@Override
public DoubleStream mapToDouble(ToDoubleFunction super T> mapper) {
add(iof().createMapToDouble(mapper));
linked();
return new DoubleStreamBuilder<>(baseState());
}
@SuppressWarnings("unchecked")
@Override
public Stream flatMap(Function super T, ? extends Stream extends R>> mapper) {
add(iof().createFlatMap(mapper));
return (Stream) this;
}
@Override
public IntStream flatMapToInt(Function super T, ? extends IntStream> mapper) {
add(iof().createFlatMapToInt(mapper));
linked();
return new IntStreamBuilder<>(baseState());
}
@Override
public LongStream flatMapToLong(Function super T, ? extends LongStream> mapper) {
add(iof().createFlatMapToLong(mapper));
linked();
return new LongStreamBuilder<>(baseState());
}
@Override
public DoubleStream flatMapToDouble(Function super T, ? extends DoubleStream> mapper) {
add(iof().createFlatMapToDouble(mapper));
linked();
return new DoubleStreamBuilder<>(baseState());
}
@Override
public Stream distinct() {
add(iof().acquireDistinct());
return this;
}
@Override
public Stream sorted() {
add(iof().acquireSorted());
return this;
}
@Override
public Stream sorted(Comparator super T> comparator) {
add(iof().createSorted(comparator));
return this;
}
@Override
public Stream peek(Consumer super T> action) {
add(iof().createPeek(action));
return this;
}
@Override
public Stream limit(long maxSize) {
add(iof().createLimit(maxSize));
return this;
}
@Override
public Stream skip(long n) {
add(iof().createSkip(n));
return this;
}
@Override
public void forEach(Consumer super T> action) {
set(tof().createForEach(action));
renderAndThenAccept();
}
@Override
public void forEachOrdered(Consumer super T> action) {
set(tof().createForEachOrdered(action));
renderAndThenAccept();
}
@Override
public Object[] toArray() {
set(tof().acquireToArray());
return renderAndThenApply();
}
@Override
public A[] toArray(IntFunction generator) {
set(tof().createToArray(generator));
return renderAndThenApply();
}
@Override
public T reduce(T identity, BinaryOperator accumulator) {
set(tof().createReduce(identity, accumulator));
return renderAndThenApply();
}
@Override
public Optional reduce(BinaryOperator accumulator) {
set(tof().createReduce(accumulator));
return renderAndThenApply();
}
@Override
public U reduce(U identity, BiFunction accumulator, BinaryOperator combiner) {
set(tof().createReduce(identity, accumulator, combiner));
return renderAndThenApply();
}
@Override
public R collect(Supplier supplier, BiConsumer accumulator, BiConsumer combiner) {
set(tof().createCollect(supplier, accumulator, combiner));
return renderAndThenApply();
}
@Override
public R collect(Collector super T, A, R> collector) {
set(tof().createCollect(collector));
return renderAndThenApply();
}
@Override
public Optional min(Comparator super T> comparator) {
set(tof().createMin(comparator));
return renderAndThenApply();
}
@Override
public Optional max(Comparator super T> comparator) {
set(tof().createMax(comparator));
return renderAndThenApply();
}
@Override
public long count() {
set(tof().acquireCount());
return renderCount();
}
@Override
public boolean anyMatch(Predicate super T> predicate) {
set(tof().createAnyMatch(predicate));
return renderAndThenTest();
}
@Override
public boolean allMatch(Predicate super T> predicate) {
set(tof().createAllMatch(predicate));
return renderAndThenTest();
}
@Override
public boolean noneMatch(Predicate super T> predicate) {
set(tof().createNoneMatch(predicate));
return renderAndThenTest();
}
@Override
public Optional findFirst() {
set(tof().acquireFindFirst());
return renderAndThenApply();
}
@Override
public Optional findAny() {
set(tof().acquireFindAny());
return renderAndThenApply();
}
@Override
public Iterator iterator() {
set(tof().acquireIterator());
return renderAndThenApply();
}
@Override
public Spliterator spliterator() {
set(tof().acquireSpliterator());
return renderAndThenApply();
}
private IntermediateOperationFactory iof() {
return baseState().factories().intermediate();
}
private TerminalOperationFactory tof() {
return baseState().factories().terminal();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy