package xyz.cques.rangefun;

import java.util.NoSuchElementException;
import java.util.PrimitiveIterator;

/**
 * Created by Jacques on 2017/06/21.
 */
class RangeIterator implements PrimitiveIterator.OfInt {
    private boolean backwardsIteration;
    private final int step;
    private final int end;
    private int current;

    RangeIterator(int start, int end, int step) {
        this.current = start;
        this.end = end;

        if (isBackwardsIteration(start, end)) {
            backwardsIteration = true;
            this.step = negative(step);
        } else {
            this.step = step;
        }
    }

    private boolean isBackwardsIteration(int start, int end) {
        return start > end;
    }

    private int negative(int number) {
        int newNumber = number;

        if (newNumber > 0) {
            newNumber = -newNumber;
        }

        return newNumber;
    }

    /**
     * Returns the next {@code int} element in the range.
     *
     * @return the next {@code int} element in the range
     * @throws NoSuchElementException if the end of the range has been reached
     */
    @Override
    public int nextInt() {
        if (hasNext()) {
            int oldCurrent = current;
            current = current + step;
            return oldCurrent;
        } else {
            throw new NoSuchElementException(
                    "the end of the range has been reached"
            );
        }
    }

    /**
     * Returns {@code true} if the iteration has more elements.
     * (In other words, returns {@code true} if {@link #next} would
     * return an element rather than throwing an exception.)
     *
     * @return {@code true} if the iteration has more elements
     */
    @Override
    public boolean hasNext() {
        if (backwardsIteration) {
            return current >= end;
        }
        return current <= end;
    }
}

@@ -63,16 +63,42 @@
 eption;%0A
+import java.util.Objects;%0A
 import j
@@ -1908,11 +1908,655 @@
 d;%0A    %7D
+%0A%0A    @Override%0A    public boolean equals(Object o) %7B%0A        if (this == o) %7B%0A            return true;%0A        %7D%0A        if (o == null %7C%7C getClass() != o.getClass()) %7B%0A            return false;%0A        %7D%0A%0A        RangeIterator other = (RangeIterator) o;%0A%0A        return backwardsIteration == other.backwardsIteration &&%0A                step == other.step &&%0A                end == other.end &&%0A                current == other.current;%0A    %7D%0A%0A    @Override%0A    public int hashCode() %7B%0A        return Objects.hash(backwardsIteration,%0A                            step,%0A                            end,%0A                            current);%0A    %7D
 %0A%7D%0A

Implement RangeIterator equals() and hashCode()