All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.speedment.jpastreamer.builder.standard.internal.StreamBuilder Maven / Gradle / Ivy

There is a newer version: 3.0.4
Show newest version
/*
 * 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 predicate) {
        add(iof().createFilter(predicate));
        return this;
    }

    @SuppressWarnings("unchecked")
    @Override
    public  Stream map(Function mapper) {
        add(iof().createMap(mapper));
        return (Stream) this;
    }

    @Override
    public IntStream mapToInt(ToIntFunction mapper) {
        add(iof().createMapToInt(mapper));
        linked();
        return new IntStreamBuilder<>(baseState());
    }

    @Override
    public LongStream mapToLong(ToLongFunction mapper) {
        add(iof().createMapToLong(mapper));
        linked();
        return new LongStreamBuilder<>(baseState());
    }

    @Override
    public DoubleStream mapToDouble(ToDoubleFunction mapper) {
        add(iof().createMapToDouble(mapper));
        linked();
        return new DoubleStreamBuilder<>(baseState());
    }

    @SuppressWarnings("unchecked")
    @Override
    public  Stream flatMap(Function> mapper) {
        add(iof().createFlatMap(mapper));
        return (Stream) this;
    }

    @Override
    public IntStream flatMapToInt(Function mapper) {
        add(iof().createFlatMapToInt(mapper));
        linked();
        return new IntStreamBuilder<>(baseState());
    }

    @Override
    public LongStream flatMapToLong(Function mapper) {
        add(iof().createFlatMapToLong(mapper));
        linked();
        return new LongStreamBuilder<>(baseState());
    }

    @Override
    public DoubleStream flatMapToDouble(Function 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 comparator) {
        add(iof().createSorted(comparator));
        return this;
    }

    @Override
    public Stream peek(Consumer 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 action) {
        set(tof().createForEach(action));
        renderAndThenAccept();
    }

    @Override
    public void forEachOrdered(Consumer 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 collector) {
        set(tof().createCollect(collector));
        return renderAndThenApply();
    }

    @Override
    public Optional min(Comparator comparator) {
        set(tof().createMin(comparator));
        return renderAndThenApply();
    }

    @Override
    public Optional max(Comparator comparator) {
        set(tof().createMax(comparator));
        return renderAndThenApply();
    }

    @Override
    public long count() {
        set(tof().acquireCount());
        return renderCount();
    }

    @Override
    public boolean anyMatch(Predicate predicate) {
        set(tof().createAnyMatch(predicate));
        return renderAndThenTest();
    }

    @Override
    public boolean allMatch(Predicate predicate) {
        set(tof().createAllMatch(predicate));
        return renderAndThenTest();
    }

    @Override
    public boolean noneMatch(Predicate 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();
    }

}