com.speedment.runtime.core.internal.stream.builder.LongStreamBuilder Maven / Gradle / Ivy
/**
*
* 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.longs.*;
import com.speedment.runtime.core.internal.stream.builder.pipeline.LongPipeline;
import com.speedment.runtime.core.internal.stream.builder.pipeline.PipelineImpl;
import com.speedment.runtime.core.internal.stream.builder.streamterminator.StreamTerminator;
import com.speedment.runtime.core.internal.util.java9.Java9LongStreamAdditions;
import java.util.*;
import static java.util.Objects.requireNonNull;
import java.util.function.*;
import java.util.stream.*;
/**
*
* @author pemi
*/
public final class LongStreamBuilder extends AbstractStreamBuilder
implements LongStream, Java9LongStreamAdditions {
LongStreamBuilder(final 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 LongStreamBuilder(final PipelineImpl pipeline, final StreamTerminator streamTerminator) {
this(pipeline, streamTerminator, newStreamSet());
}
@Override
public LongStream filter(LongPredicate predicate) {
requireNonNull(predicate);
return append(new LongFilterAction(predicate));
}
@Override
public LongStream map(LongUnaryOperator mapper) {
requireNonNull(mapper);
return append(new LongMapAction(mapper));
}
@Override
public Stream mapToObj(LongFunction mapper) {
requireNonNull(mapper);
assertNotLinkedOrConsumedAndSet();
return new ReferenceStreamBuilder(pipeline, streamTerminator, streamSet).append(new LongMapToObjAction<>(mapper));
}
@Override
public IntStream mapToInt(LongToIntFunction mapper) {
requireNonNull(mapper);
assertNotLinkedOrConsumedAndSet();
return new IntStreamBuilder(pipeline, streamTerminator, streamSet).append(new LongMapToIntAction(mapper));
}
@Override
public DoubleStream mapToDouble(LongToDoubleFunction mapper) {
requireNonNull(mapper);
assertNotLinkedOrConsumedAndSet();
return new DoubleStreamBuilder(pipeline, streamTerminator, streamSet).append(new LongMapToDoubleAction(mapper));
}
@Override
public LongStream flatMap(LongFunction mapper) {
requireNonNull(mapper);
return append(new LongFlatMapAction(mapper));
}
@Override
public LongStream distinct() {
return append(new LongDistinctAction());
}
@Override
public LongStream sorted() {
return append(new LongSortedAction());
}
@Override
public LongStream peek(LongConsumer action) {
requireNonNull(action);
return append(new LongPeekAction(action));
}
@Override
public LongStream limit(long maxSize) {
return append(new LongLimitAction(maxSize));
}
@Override
public LongStream skip(long n) {
return append(new LongSkipAction(n));
}
@Override
public DoubleStream asDoubleStream() {
assertNotLinkedOrConsumedAndSet();
return new DoubleStreamBuilder(pipeline, streamTerminator, streamSet).append(new LongAsDoubleAction());
}
@Override
public Stream boxed() {
assertNotLinkedOrConsumedAndSet();
return new ReferenceStreamBuilder(pipeline, streamTerminator, streamSet).append(new LongBoxedAction());
}
@Override
public LongStream takeWhile(LongPredicate predicate) {
return append(new LongTakeWhileAction(predicate));
}
@Override
public LongStream dropWhile(LongPredicate predicate) {
return append(new LongDropWhileAction(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 void forEach(LongConsumer action) {
requireNonNull(action);
assertNotLinkedOrConsumedAndSet();
try {
streamTerminator.forEach(pipeline(), action);
} finally {
close();
}
}
/**
* {@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(LongConsumer action) {
requireNonNull(action);
assertNotLinkedOrConsumedAndSet();
try {
streamTerminator.forEachOrdered(pipeline(), action);
} finally {
close();
}
}
/**
* {@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[] toArray() {
assertNotLinkedOrConsumedAndSet();
try {
return streamTerminator.toArray(pipeline());
} finally {
close();
}
}
/**
* {@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 reduce(long identity, LongBinaryOperator op) {
requireNonNull(op);
assertNotLinkedOrConsumedAndSet();
return finallyCloseLong(() -> streamTerminator.reduce(pipeline(), identity, op));
}
/**
* {@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 OptionalLong reduce(LongBinaryOperator op) {
requireNonNull(op);
assertNotLinkedOrConsumedAndSet();
return finallyCloseReference(() -> streamTerminator.reduce(pipeline(), op));
}
/**
* {@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, ObjLongConsumer accumulator, BiConsumer combiner) {
requireNonNull(supplier);
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 long sum() {
assertNotLinkedOrConsumedAndSet();
return finallyCloseLong(() -> streamTerminator.sum(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 OptionalLong min() {
assertNotLinkedOrConsumedAndSet();
return finallyCloseReference(() -> streamTerminator.min(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 OptionalLong max() {
assertNotLinkedOrConsumedAndSet();
return finallyCloseReference(() -> streamTerminator.max(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 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 OptionalDouble average() {
assertNotLinkedOrConsumedAndSet();
return finallyCloseReference(() -> streamTerminator.average(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 LongSummaryStatistics summaryStatistics() {
assertNotLinkedOrConsumedAndSet();
return finallyCloseReference(() -> streamTerminator.summaryStatistics(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(LongPredicate 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(LongPredicate 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(LongPredicate 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 OptionalLong 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 OptionalLong 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 PrimitiveIterator.OfLong iterator() {
assertNotLinkedOrConsumedAndSet();
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.OfLong spliterator() {
assertNotLinkedOrConsumedAndSet();
return streamTerminator.spliterator(pipeline());
}
}