package java.util;
import java.util.function.Consumer;
import java.util.function.DoubleConsumer;
import java.util.function.IntConsumer;
import java.util.function.LongConsumer;

//继承Iterator,通过泛型和消费型函数接口
public interface PrimitiveIterator<T, T_CONS> extends Iterator<T> {

    //按元素在迭代时发生的顺序为每个剩余元素执行给定的操作,直到所有元素都被处理或操作引发异常为止
    @SuppressWarnings("overloads")
    void forEachRemaining(T_CONS action);

    //内部静态接口,针对int
    public static interface OfInt extends PrimitiveIterator<Integer, IntConsumer> {

        int nextInt();//下一个整型

        default void forEachRemaining(IntConsumer action){
            Objects.requireNonNull(action);
            while (hasNext())
                action.accept(nextInt());
        }

        @Override
        default Integer next(){
            if(Tripwire.ENABLED)
                Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfInt.nextInt()");
            return nextInt();
        }

        @Override
        default void forEachRemaining(Consumer<? super Integer> action){
            if(action instanceof IntConsumer){
                forEachRemaining((IntConsumer) action);
            }else{
                // The method reference action::accept is never null
                Objects.requireNonNull(action);
                if(Tripwire.ENABLED)
                    Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfInt.forEachRemainingInt(action::accept)");
                forEachRemaining((IntConsumer) action::accept);
            }
        }

    }

    //内部静态接口,针对Long
    public static interface OfLong extends PrimitiveIterator<Long, LongConsumer> {

        long nextLong();

        default void forEachRemaining(LongConsumer action){
            Objects.requireNonNull(action);
            while (hasNext())
                action.accept(nextLong());
        }

        @Override
        default Long next(){
            if(Tripwire.ENABLED)
                Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfLong.nextLong()");
            return nextLong();
        }

        @Override
        default void forEachRemaining(Consumer<? super Long> action){
            if(action instanceof LongConsumer){
                forEachRemaining((LongConsumer) action);
            }else{
                // The method reference action::accept is never null
                Objects.requireNonNull(action);
                if(Tripwire.ENABLED)
                    Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfLong.forEachRemainingLong(action::accept)");
                forEachRemaining((LongConsumer) action::accept);
            }
        }
    }

    //内部静态接口,针对Double
    public static interface OfDouble extends PrimitiveIterator<Double, DoubleConsumer> {
        double nextDouble();

        default void forEachRemaining(DoubleConsumer action){
            Objects.requireNonNull(action);
            while (hasNext())
                action.accept(nextDouble());
        }

        @Override
        default Double next(){
            if(Tripwire.ENABLED)
                Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfDouble.nextLong()");
            return nextDouble();
        }

        @Override
        default void forEachRemaining(Consumer<? super Double> action){
            if(action instanceof DoubleConsumer){
                forEachRemaining((DoubleConsumer) action);
            }else{
                // The method reference action::accept is never null
                Objects.requireNonNull(action);
                if(Tripwire.ENABLED)
                    Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfDouble.forEachRemainingDouble(action::accept)");
                forEachRemaining((DoubleConsumer) action::accept);
            }
        }
    }
}