Exit Full View

Feather2 / feather2-runtime / src / main / java / uk / co / nickthecoder / feather / runtime / LongRange.java

package uk.co.nickthecoder.feather.runtime;

final public class LongRange extends LongProgression {

    public LongRange(long start, long endInclusive) {
        super(start, endInclusive, 1L);
    }

    public static final LongRange EMPTY = new LongRange(1L, 0L);


    public static LongRange exclusiveRange(long start, long endInclusive) {
        if (endInclusive == Long.MIN_VALUE) return EMPTY;
        return new LongRange(start, endInclusive - 1L);
    }

    public boolean contains(long value) {
        return start <= value && value <= endInclusive;
    }


    public LongProgression backwards() {
        return new LongProgression(endInclusive, start, -1L);
    }


    public LongProgression step(long step) {
        if (step > 0L) {
            return new LongProgression(start, endInclusive, step);
        }
        if (step < 0L) {
            return new LongProgression(endInclusive, start, step);
        }
        throw new IllegalArgumentException("Step cannot be 0");
    }
}