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

com.speedment.runtime.core.internal.stream.builder.ReferenceStreamBuilder Maven / Gradle / Ivy

Go to download

A Speedment bundle that shades all dependencies into one jar. This is useful when deploying an application on a server.

The newest version!
/*
 *
 * Copyright (c) 2006-2019, Speedment, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); You may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.speedment.runtime.core.internal.stream.builder;

import com.speedment.runtime.core.internal.stream.builder.action.reference.*;
import com.speedment.runtime.core.internal.stream.builder.pipeline.PipelineImpl;
import com.speedment.runtime.core.internal.stream.builder.pipeline.ReferencePipeline;
import com.speedment.runtime.core.internal.stream.builder.streamterminator.StreamTerminator;
import java.util.*;
import static java.util.Objects.requireNonNull;
import java.util.function.*;
import java.util.stream.*;
import com.speedment.runtime.core.internal.util.java9.Java9StreamAdditions;

/**
 *
 * @author pemi
 * @param  steam type
 */
public final class ReferenceStreamBuilder extends AbstractStreamBuilder, ReferencePipeline>
    implements Stream, Java9StreamAdditions {

    ReferenceStreamBuilder(PipelineImpl pipeline, final StreamTerminator streamTerminator, Set> streamSet) {
        super(pipeline, streamTerminator, streamSet);
        streamSet.add(this); // Add this new stream to the streamSet so it may be closed later
    }

    public ReferenceStreamBuilder(PipelineImpl pipeline, final StreamTerminator streamTerminator) {
        this(pipeline, streamTerminator, newStreamSet());
    }

    @Override
    public Stream filter(Predicate predicate) {
        requireNonNull(predicate);
        return append(new FilterAction<>(predicate));
    }

    @Override
    public  Stream map(Function mapper) {
        requireNonNull(mapper);
        assertNotLinkedOrConsumedAndSet();
        return new ReferenceStreamBuilder(pipeline, streamTerminator, streamSet).append(new MapAction<>(mapper));
    }

    @Override
    public IntStream mapToInt(ToIntFunction mapper) {
        requireNonNull(mapper);
        assertNotLinkedOrConsumedAndSet();
        return new IntStreamBuilder(pipeline, streamTerminator, streamSet).append(new MapToIntAction<>(mapper));
    }

    @Override
    public LongStream mapToLong(ToLongFunction mapper) {
        requireNonNull(mapper);
        assertNotLinkedOrConsumedAndSet();
        return new LongStreamBuilder(pipeline, streamTerminator, streamSet).append(new MapToLongAction<>(mapper));
    }

    @Override
    public DoubleStream mapToDouble(ToDoubleFunction mapper) {
        requireNonNull(mapper);
        assertNotLinkedOrConsumedAndSet();
        return new DoubleStreamBuilder(pipeline, streamTerminator, streamSet).append(new MapToDoubleAction<>(mapper));
    }

    @Override
    public  Stream flatMap(Function> mapper) {
        requireNonNull(mapper);
        assertNotLinkedOrConsumedAndSet();
        return new ReferenceStreamBuilder(pipeline, streamTerminator, streamSet).append(new FlatMapAction<>(mapper));
    }

    @Override
    public IntStream flatMapToInt(Function mapper) {
        requireNonNull(mapper);
        assertNotLinkedOrConsumedAndSet();
        return new IntStreamBuilder(pipeline, streamTerminator, streamSet).append(new FlatMapToIntAction<>(mapper));
    }

    @Override
    public LongStream flatMapToLong(Function mapper) {
        requireNonNull(mapper);
        assertNotLinkedOrConsumedAndSet();
        return new LongStreamBuilder(pipeline, streamTerminator, streamSet).append(new FlatMapToLongAction<>(mapper));
    }

    @Override
    public DoubleStream flatMapToDouble(Function mapper) {
        requireNonNull(mapper);
        assertNotLinkedOrConsumedAndSet();
        return new DoubleStreamBuilder(pipeline, streamTerminator, streamSet).append(new FlatMapToDoubleAction<>(mapper));
    }

    @Override
    public Stream distinct() {
        return append(new DistinctAction<>());
    }

    @Override
    public Stream sorted() {
        return append(new SortedAction<>());
    }

    @Override
    public Stream sorted(Comparator comparator) {
        requireNonNull(comparator);
        return append(new SortedComparatorAction<>(comparator));
    }

    @Override
    public Stream peek(Consumer action) {
        requireNonNull(action);
        return append(new PeekAction<>(action));
    }

    @Override
    public Stream limit(long maxSize) {
        return append(new LimitAction<>(maxSize));
    }

    @Override
    public Stream skip(long n) {
        return append(new SkipAction<>(n));
    }

    @Override
    public Stream takeWhile(Predicate predicate) {
        requireNonNull(predicate);
        return append(new TakeWhileAction<>(predicate));
    }

    @Override
    public Stream dropWhile(Predicate predicate) {
        requireNonNull(predicate);
        return append(new DropWhileAction<>(predicate));
    }

    // Terminal operations
    /**
     * {@inheritDoc}
     *
     * 

* N.B. This method may short-circuit operations in the Stream pipeline and * closes the stream automatically when a terminal operation is performed. * */ @Override public void forEach(Consumer action) { requireNonNull(action); assertNotLinkedOrConsumedAndSet(); finallyClose(() -> streamTerminator.forEach(pipeline(), action)); } /** * {@inheritDoc} * *

* N.B. This method may short-circuit operations in the Stream pipeline and * closes the stream automatically when a terminal operation is performed. * */ @Override public void forEachOrdered(Consumer action) { requireNonNull(action); assertNotLinkedOrConsumedAndSet(); finallyClose(() -> streamTerminator.forEachOrdered(pipeline(), action)); } /** * {@inheritDoc} * *

* N.B. This method may short-circuit operations in the Stream pipeline and * closes the stream automatically when a terminal operation is performed. * */ @Override public Object[] toArray() { assertNotLinkedOrConsumedAndSet(); return finallyCloseReference(() -> streamTerminator.toArray(pipeline())); } /** * {@inheritDoc} * *

* N.B. This method may short-circuit operations in the Stream pipeline and * closes the stream automatically when a terminal operation is performed. * */ @Override public A[] toArray(IntFunction generator) { requireNonNull(generator); assertNotLinkedOrConsumedAndSet(); return finallyCloseReference(() -> streamTerminator.toArray(pipeline(), generator)); } /** * {@inheritDoc} * *

* N.B. This method may short-circuit operations in the Stream pipeline and * closes the stream automatically when a terminal operation is performed. * */ @Override public T reduce(T identity, BinaryOperator accumulator) { requireNonNull(identity); requireNonNull(accumulator); assertNotLinkedOrConsumedAndSet(); return finallyCloseReference(() -> streamTerminator.reduce(pipeline(), identity, accumulator)); } /** * {@inheritDoc} * *

* N.B. This method may short-circuit operations in the Stream pipeline and * closes the stream automatically when a terminal operation is performed. * */ @Override public Optional reduce(BinaryOperator accumulator) { requireNonNull(accumulator); assertNotLinkedOrConsumedAndSet(); return finallyCloseReference(() -> streamTerminator.reduce(pipeline(), accumulator)); } /** * {@inheritDoc} * *

* N.B. This method may short-circuit operations in the Stream pipeline and * closes the stream automatically when a terminal operation is performed. * */ @Override public U reduce(U identity, BiFunction accumulator, BinaryOperator combiner) { requireNonNull(identity); requireNonNull(accumulator); requireNonNull(combiner); assertNotLinkedOrConsumedAndSet(); return finallyCloseReference(() -> streamTerminator.reduce(pipeline(), identity, accumulator, combiner)); } /** * {@inheritDoc} * *

* N.B. This method may short-circuit operations in the Stream pipeline and * closes the stream automatically when a terminal operation is performed. * */ @Override public R collect(Supplier supplier, BiConsumer accumulator, BiConsumer combiner) { requireNonNull(supplier); requireNonNull(accumulator); requireNonNull(combiner); assertNotLinkedOrConsumedAndSet(); return finallyCloseReference(() -> streamTerminator.collect(pipeline(), supplier, accumulator, combiner)); } /** * {@inheritDoc} * *

* N.B. This method may short-circuit operations in the Stream pipeline and * closes the stream automatically when a terminal operation is performed. * */ @Override public R collect(Collector collector) { requireNonNull(collector); assertNotLinkedOrConsumedAndSet(); return finallyCloseReference(() -> streamTerminator.collect(pipeline(), collector)); } /** * {@inheritDoc} * *

* N.B. This method may short-circuit operations in the Stream pipeline and * closes the stream automatically when a terminal operation is performed. * */ @Override public Optional min(Comparator comparator) { requireNonNull(comparator); assertNotLinkedOrConsumedAndSet(); return finallyCloseReference(() -> streamTerminator.min(pipeline(), comparator)); } /** * {@inheritDoc} * *

* N.B. This method may short-circuit operations in the Stream pipeline and * closes the stream automatically when a terminal operation is performed. * */ @Override public Optional max(Comparator comparator) { requireNonNull(comparator); assertNotLinkedOrConsumedAndSet(); return finallyCloseReference(() -> streamTerminator.max(pipeline(), comparator)); } /** * {@inheritDoc} * *

* N.B. This method may short-circuit operations in the Stream pipeline and * closes the stream automatically when a terminal operation is performed. * */ @Override public long count() { assertNotLinkedOrConsumedAndSet(); return finallyCloseLong(() -> streamTerminator.count(pipeline())); } /** * {@inheritDoc} * *

* N.B. This method may short-circuit operations in the Stream pipeline and * closes the stream automatically when a terminal operation is performed. * */ @Override public boolean anyMatch(Predicate predicate) { requireNonNull(predicate); assertNotLinkedOrConsumedAndSet(); return finallyCloseBoolean(() -> streamTerminator.anyMatch(pipeline(), predicate)); } /** * {@inheritDoc} * *

* N.B. This method may short-circuit operations in the Stream pipeline and * closes the stream automatically when a terminal operation is performed. * */ @Override public boolean allMatch(Predicate predicate) { requireNonNull(predicate); assertNotLinkedOrConsumedAndSet(); return finallyCloseBoolean(() -> streamTerminator.allMatch(pipeline(), predicate)); } /** * {@inheritDoc} * *

* N.B. This method may short-circuit operations in the Stream pipeline and * closes the stream automatically when a terminal operation is performed. * */ @Override public boolean noneMatch(Predicate predicate) { requireNonNull(predicate); assertNotLinkedOrConsumedAndSet(); return finallyCloseBoolean(() -> streamTerminator.noneMatch(pipeline(), predicate)); } /** * {@inheritDoc} * *

* N.B. This method may short-circuit operations in the Stream pipeline and * closes the stream automatically when a terminal operation is performed. * */ @Override public Optional findFirst() { assertNotLinkedOrConsumedAndSet(); return finallyCloseReference(() -> streamTerminator.findFirst(pipeline())); } /** * {@inheritDoc} * *

* N.B. This method may short-circuit operations in the Stream pipeline and * closes the stream automatically when a terminal operation is performed. * */ @Override public Optional findAny() { assertNotLinkedOrConsumedAndSet(); return finallyCloseReference(() -> streamTerminator.findAny(pipeline())); } /** * {@inheritDoc} * *

* N.B. This method may short-circuit operations in the Stream pipeline. *

* If you call this method, you must ensure to call the stream's * {@link #close() } method or else resources may not be released properly. * */ @Override public Iterator iterator() { assertNotLinkedOrConsumedAndSet(); //throw new UnsupportedOperationException(UNSUPPORTED_BECAUSE_OF_CLOSE_MAY_NOT_BE_CALLED); return streamTerminator.iterator(pipeline()); } /** * {@inheritDoc} * *

* N.B. This method may short-circuit operations in the Stream pipeline. *

* If you call this method, you must ensure to call the stream's * {@link #close() } method or else resources may not be released properly. * */ @Override public Spliterator spliterator() { assertNotLinkedOrConsumedAndSet(); //throw new UnsupportedOperationException(UNSUPPORTED_BECAUSE_OF_CLOSE_MAY_NOT_BE_CALLED); return streamTerminator.spliterator(pipeline()); } }