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

com.xyzwps.lib.dollar.iterable.Range Maven / Gradle / Ivy

The newest version!
package com.xyzwps.lib.dollar.iterable;

import java.util.Iterator;
import java.util.NoSuchElementException;

public final class Range implements Iterable {

    /**
     * Include
     */
    private final int start;

    /**
     * Exclude
     */
    private final int end;

    public Range(int start, int end) {
        if (start >= end) {
            throw new IllegalArgumentException("The start value should be less than end value.");
        }

        this.start = start;
        this.end = end;
    }

    @Override
    public Iterator iterator() {
        return new Iterator() {

            private int current = start;

            @Override
            public boolean hasNext() {
                return current < end;
            }

            @Override
            public Integer next() {
                if (hasNext()) {
                    return current++;
                }
                throw new NoSuchElementException();
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy