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

java8.util.DelegatingSpliterator Maven / Gradle / Ivy

Go to download

streamsupport is a backport of the Java 8 java.util.function (functional interfaces) and java.util.stream (streams) API for Java 6 / 7 and Android developers

The newest version!
/*
 * Written by Stefan Zobel and released to the
 * public domain, as explained at
 * http://creativecommons.org/publicdomain/zero/1.0/
 */
package java8.util;

import java.util.Comparator;

import build.IgnoreJava8API;

import java8.util.function.Consumer;
import java8.util.function.Consumers;

/**
 * A j8.u.Spliterator implementation that delegates to a j.u.Spliterator.
 *
 * @param 
 *            the type of the input to the operation
 */
@IgnoreJava8API
final class DelegatingSpliterator implements Spliterator {

    private final java.util.Spliterator spliter;

    DelegatingSpliterator(java.util.Spliterator spliterator) {
        Objects.requireNonNull(spliterator);
        this.spliter = spliterator;
    }

    @Override
    public boolean tryAdvance(Consumer action) {
        return spliter.tryAdvance(new ConsumerDelegate<>(action));
    }

    @Override
    public Spliterator trySplit() {
        java.util.Spliterator spliterator = spliter.trySplit();
        if (spliterator == null) {
            return null;
        }
        return new DelegatingSpliterator<>(spliterator);
    }

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

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

    @Override
    public void forEachRemaining(Consumer action) {
        spliter.forEachRemaining(new ConsumerDelegate<>(action));
    }

    @Override
    public long getExactSizeIfKnown() {
        return spliter.getExactSizeIfKnown();
    }

    @Override
    public boolean hasCharacteristics(int characteristics) {
        return spliter.hasCharacteristics(characteristics);
    }

    @Override
    public Comparator getComparator() {
        return spliter.getComparator();
    }

    /**
     * A j.u.f.Consumer implementation that delegates to a j8.u.f.Consumer.
     *
     * @param 
     *            the type of the input to the operation
     */
    private static final class ConsumerDelegate implements
            java.util.function.Consumer {

        private final Consumer delegate;

        ConsumerDelegate(Consumer delegate) {
            Objects.requireNonNull(delegate);
            this.delegate = delegate;
        }

        @Override
        public void accept(T t) {
            delegate.accept(t);
        }

        @Override
        @IgnoreJava8API
        public java.util.function.Consumer andThen(
                java.util.function.Consumer after) {

            Objects.requireNonNull(after);

            return new ConsumerDelegate(Consumers.andThen(delegate,
                    new java8.util.function.Consumer() {
                        @Override
                        public void accept(T t) {
                            after.accept(t);
                        }
                    }));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy