com.speedment.runtime.core.internal.stream.autoclose.AutoClosingReferenceStream Maven / Gradle / Ivy
Show all versions of tool-deploy Show documentation
/*
*
* 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