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

org.vertexium.util.DelegatingStream Maven / Gradle / Ivy

There is a newer version: 4.10.0
Show newest version
package org.vertexium.util;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Optional;
import java.util.Spliterator;
import java.util.function.*;
import java.util.stream.*;

public class DelegatingStream implements Stream {
    private final Stream stream;
    private boolean streamCloseCalled;

    public DelegatingStream(Stream stream) {
        this.stream = stream.onClose(this::close);
    }

    @Override
    public Stream filter(Predicate predicate) {
        return stream.filter(predicate);
    }

    @Override
    public  Stream map(Function mapper) {
        return stream.map(mapper);
    }

    @Override
    public IntStream mapToInt(ToIntFunction mapper) {
        return stream.mapToInt(mapper);
    }

    @Override
    public LongStream mapToLong(ToLongFunction mapper) {
        return stream.mapToLong(mapper);
    }

    @Override
    public DoubleStream mapToDouble(ToDoubleFunction mapper) {
        return stream.mapToDouble(mapper);
    }

    @Override
    public  Stream flatMap(Function> mapper) {
        return stream.flatMap(mapper);
    }

    @Override
    public IntStream flatMapToInt(Function mapper) {
        return stream.flatMapToInt(mapper);
    }

    @Override
    public LongStream flatMapToLong(Function mapper) {
        return stream.flatMapToLong(mapper);
    }

    @Override
    public DoubleStream flatMapToDouble(Function mapper) {
        return stream.flatMapToDouble(mapper);
    }

    @Override
    public Stream distinct() {
        return stream.distinct();
    }

    @Override
    public Stream sorted() {
        return stream.sorted();
    }

    @Override
    public Stream sorted(Comparator comparator) {
        return stream.sorted(comparator);
    }

    @Override
    public Stream peek(Consumer action) {
        return stream.peek(action);
    }

    @Override
    public Stream limit(long maxSize) {
        return stream.limit(maxSize);
    }

    @Override
    public Stream skip(long n) {
        return stream.skip(n);
    }

    @Override
    public void forEach(Consumer action) {
        stream.forEach(action);
        close();
    }

    @Override
    public void forEachOrdered(Consumer action) {
        stream.forEachOrdered(action);
        close();
    }

    @Override
    public Object[] toArray() {
        Object[] arr = stream.toArray();
        close();
        return arr;
    }

    @Override
    public  A[] toArray(IntFunction generator) {
        A[] arr = stream.toArray(generator);
        close();
        return arr;
    }

    @Override
    public T reduce(T identity, BinaryOperator accumulator) {
        T reduce = stream.reduce(identity, accumulator);
        close();
        return reduce;
    }

    @Override
    public Optional reduce(BinaryOperator accumulator) {
        Optional reduce = stream.reduce(accumulator);
        close();
        return reduce;
    }

    @Override
    public  U reduce(U identity, BiFunction accumulator, BinaryOperator combiner) {
        U reduce = stream.reduce(identity, accumulator, combiner);
        close();
        return reduce;
    }

    @Override
    public  R collect(Supplier supplier, BiConsumer accumulator, BiConsumer combiner) {
        R collect = stream.collect(supplier, accumulator, combiner);
        close();
        return collect;
    }

    @Override
    public  R collect(Collector collector) {
        R collect = stream.collect(collector);
        close();
        return collect;
    }

    @Override
    public Optional min(Comparator comparator) {
        Optional min = stream.min(comparator);
        close();
        return min;
    }

    @Override
    public Optional max(Comparator comparator) {
        Optional max = stream.max(comparator);
        close();
        return max;
    }

    @Override
    public long count() {
        long count = stream.count();
        close();
        return count;
    }

    @Override
    public boolean anyMatch(Predicate predicate) {
        boolean b = stream.anyMatch(predicate);
        close();
        return b;
    }

    @Override
    public boolean allMatch(Predicate predicate) {
        boolean b = stream.allMatch(predicate);
        close();
        return b;
    }

    @Override
    public boolean noneMatch(Predicate predicate) {
        boolean b = stream.noneMatch(predicate);
        close();
        return b;
    }

    @Override
    public Optional findFirst() {
        Optional first = stream.findFirst();
        close();
        return first;
    }

    @Override
    public Optional findAny() {
        Optional any = stream.findAny();
        close();
        return any;
    }

    @Override
    public Iterator iterator() {
        Iterator it = stream.iterator();
        return new Iterator() {
            @Override
            public boolean hasNext() {
                boolean hasNext = it.hasNext();
                if (!hasNext) {
                    close();
                }
                return hasNext;
            }

            @Override
            public T next() {
                return it.next();
            }
        };
    }

    @Override
    public Spliterator spliterator() {
        Spliterator split = stream.spliterator();
        return new Spliterator() {
            @Override
            public boolean tryAdvance(Consumer action) {
                boolean b = split.tryAdvance(action);
                if (!b) {
                    close();
                }
                return b;
            }

            @Override
            public Spliterator trySplit() {
                return split.trySplit();
            }

            @Override
            public long estimateSize() {
                return split.estimateSize();
            }

            @Override
            public int characteristics() {
                return split.characteristics();
            }
        };
    }

    @Override
    public boolean isParallel() {
        return stream.isParallel();
    }

    @Override
    public Stream sequential() {
        return stream.sequential();
    }

    @Override
    public Stream parallel() {
        return stream.parallel();
    }

    @Override
    public Stream unordered() {
        return stream.unordered();
    }

    @Override
    public Stream onClose(Runnable closeHandler) {
        return stream.onClose(closeHandler);
    }

    @Override
    public void close() {
        if (!streamCloseCalled) {
            stream.close();
            streamCloseCalled = true;
        }
    }
}