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

com.speedment.runtime.core.internal.stream.autoclose.AutoClosingReferenceStream 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.autoclose;

import com.speedment.runtime.core.internal.util.java9.Java9StreamAdditions;
import com.speedment.runtime.core.internal.util.java9.Java9StreamUtil;

import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.*;
import java.util.stream.*;

import static java.util.Objects.requireNonNull;

/**
 * A Stream that will call its {@link #close()} method automatically after
 * a terminating operation has been called.
 * 

* N.B. The {@link #iterator()} {@link #spliterator()} methods will throw * an {@link UnsupportedOperationException} because otherwise the AutoClose * property cannot be guaranteed. This can be unlocked by setting the * allowStreamIteratorAndSpliterator flag * * @param Stream type * @author Per Minborg */ public final class AutoClosingReferenceStream extends AbstractAutoClosingStream> implements Stream, Java9StreamAdditions { public AutoClosingReferenceStream(Stream stream) { this(stream, false); } public AutoClosingReferenceStream( final Stream stream, final boolean allowStreamIteratorAndSpliterator ) { super(stream, allowStreamIteratorAndSpliterator); } @Override public Stream filter(Predicate predicate) { return wrap(stream().filter(predicate)); } @Override public Stream map(Function mapper) { return wrap(stream().map(mapper)); } @Override public IntStream mapToInt(ToIntFunction mapper) { return wrap(stream().mapToInt(mapper)); } @Override public LongStream mapToLong(ToLongFunction mapper) { return wrap(stream().mapToLong(mapper)); } @Override public DoubleStream mapToDouble(ToDoubleFunction mapper) { return wrap(stream().mapToDouble(mapper)); } @Override public Stream flatMap(Function> mapper) { return wrap(stream().flatMap(mapper)); } @Override public IntStream flatMapToInt(Function mapper) { return wrap(stream().flatMapToInt(mapper)); } @Override public LongStream flatMapToLong(Function mapper) { return wrap(stream().flatMapToLong(mapper)); } @Override public DoubleStream flatMapToDouble(Function mapper) { return wrap(stream().flatMapToDouble(mapper)); } @Override public Stream distinct() { return wrap(stream().distinct()); } @Override public Stream sorted() { return wrap(stream().sorted()); } @Override public Stream sorted(Comparator comparator) { return wrap(stream().sorted(comparator)); } @Override public Stream peek(Consumer action) { return wrap(stream().peek(action)); } @Override public Stream limit(long maxSize) { return wrap(stream().limit(maxSize)); } @Override public Stream skip(long n) { return wrap(stream().skip(n)); } @Override public Stream takeWhile(Predicate predicate) { return wrap(Java9StreamUtil.takeWhile(stream(), predicate)); } @Override public Stream dropWhile(Predicate predicate) { return wrap(Java9StreamUtil.dropWhile(stream(), predicate)); } @Override public void forEach(Consumer action) { finallyClose(() -> stream().forEach(action)); } @Override public void forEachOrdered(Consumer action) { finallyClose(() -> stream().forEachOrdered(action)); } @Override public Object[] toArray() { return finallyClose((Supplier) stream()::toArray); } @Override public A[] toArray(IntFunction generator) { return finallyClose(() -> stream().toArray(generator)); } @Override public T reduce(T identity, BinaryOperator accumulator) { return finallyClose(() -> stream().reduce(identity, accumulator)); } @Override public Optional reduce(BinaryOperator accumulator) { return finallyClose(() -> stream().reduce(accumulator)); } @Override public U reduce(U identity, BiFunction accumulator, BinaryOperator combiner) { return finallyClose(() -> stream().reduce(identity, accumulator, combiner)); } @Override public R collect(Supplier supplier, BiConsumer accumulator, BiConsumer combiner) { return finallyClose(() -> stream().collect(supplier, accumulator, combiner)); } @Override public R collect(Collector collector) { return finallyClose(() -> stream().collect(collector)); } @Override public Optional min(Comparator comparator) { return finallyClose(() -> stream().min(comparator)); } @Override public Optional max(Comparator comparator) { return finallyClose(() -> stream().max(comparator)); } @Override public long count() { return finallyClose(stream()::count); } @Override public boolean anyMatch(Predicate predicate) { return finallyClose(() -> stream().anyMatch(predicate)); } @Override public boolean allMatch(Predicate predicate) { return finallyClose(() -> stream().allMatch(predicate)); } @Override public boolean noneMatch(Predicate predicate) { return finallyClose(() -> stream().noneMatch(predicate)); } @Override public Optional findFirst() { return finallyClose(stream()::findFirst); } @Override public Optional findAny() { return finallyClose(stream()::findAny); } @Override public Iterator iterator() { if (isAllowStreamIteratorAndSpliterator()) { return stream().iterator(); } throw newUnsupportedException("iterator"); } @Override public Spliterator spliterator() { if (isAllowStreamIteratorAndSpliterator()) { return stream().spliterator(); } throw newUnsupportedException("spliterator"); } @Override public boolean isParallel() { return stream().isParallel(); } @Override public Stream sequential() { return wrap(stream().sequential()); } @Override public Stream parallel() { return wrap(stream().parallel()); } @Override public Stream unordered() { return wrap(stream().unordered()); } @Override public Stream onClose(Runnable closeHandler) { return wrap(stream().onClose(closeHandler)); } }