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

gg.jte.support.ForSupport Maven / Gradle / Ivy

There is a newer version: 3.1.12
Show newest version
package gg.jte.support;

import java.util.Iterator;

public class ForSupport implements Iterable> {
    private final Iterator iterator;

    private T item;
    private int index = -1;

    public static  ForSupport of(Iterable iterable) {
        return new ForSupport<>(iterable.iterator());
    }

    public static  ForSupport of(T[] array) {
        return new ForSupport<>(new Iterator<>() {
            private int i;
            @Override
            public boolean hasNext() {
                return i < array.length;
            }

            @Override
            public T next() {
                return array[i++];
            }
        });
    }

    public ForSupport(Iterator iterator) {
        this.iterator = iterator;
    }

    public boolean isLast() {
        return !iterator.hasNext();
    }

    public boolean isFirst() {
        return index == 0;
    }

    public int getIndex() {
        return index;
    }

    public T get() {
        return item;
    }

    @Override
    public Iterator> iterator() {
        return new Iterator<>() {
            @Override
            public boolean hasNext() {
                return iterator.hasNext();
            }

            @Override
            public ForSupport next() {
                item = iterator.next();
                ++index;
                return ForSupport.this;
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy