/*
 * @author Ranides Atterwim {@literal <ranides@gmail.com>}
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.collection.iterators;

import java.util.NoSuchElementException;

/**
 * Abstract class which can be used to implement iterators for any collection which offers random access.
 * Class implements both forward and backward iteration.
 * Class does not support modifications.
 */
public abstract class RandomAccessIntIterator implements IntListIterator {
    
    private int index;
    
    private int last;

    /**
     * Must be implemented: returns size of collection (in constant time)
     * @return int
     */
    protected abstract int size();

    /**
     * Must be implemented: returns element at specified index (in constant time).
     * Iterator validates index before calling this method.
     *
     * @param index index
     * @return T
     */
    protected abstract int get(int index);

    /**
     * Optional: remove element at specified index.
     * Iterator validates index before calling this method.
     *
     * @param index index
     */
    protected void remove(int index) {
        throw new UnsupportedOperationException();
    }

    /**
     * Optional: add element at specified index.
     * Iterator validates index before calling this method.
     *
     * @param index index
     * @param value value
     */
    protected void add(int index, int value) {
        throw new UnsupportedOperationException();
    }

    /**
     * Optional: change element at specified index.
     * Iterator validates index before calling this method.
     *
     * @param index index
     * @param value value
     */
    protected void set(int index, int value) {
        throw new UnsupportedOperationException();
    }

    /**
     * Creates iterator starting from first element.
     */
    protected RandomAccessIntIterator() {
        this(0);
    }

    /**
     * Creates iterator starting from "index" element.
     *
     * @param index index
     */
    protected RandomAccessIntIterator(int index) {
        this.index = index;
        this.last = -1;
    }

    @Override
    public boolean hasNext() {
        return index < size();
    }

    @Override
    public boolean hasPrevious() {
        return index > 0;
    }

    @Override
    public int nextInt() {
        if (!hasNext()) {
            throw new NoSuchElementException();
        }
        return get(last = index++);
    }

    @Override
    public int previousInt() {
        if (!hasPrevious()) {
            throw new NoSuchElementException();
        }
        return get(last = --index);
    }

    @Override
    public int nextIndex() {
        return index;
    }

    @Override
    public int previousIndex() {
        return index - 1;
    }

    @Override
    public void add(int k) {
        add(index++, k);
    }

    @Override
    public void set(int k) {
        if (last == -1) {
            throw new IllegalStateException();
        }
        set(last, k);
    }

    @Override
    public void remove() {
        if (last == -1) {
            throw new IllegalStateException();
        }
        remove(last);
        // If the last operation was a next(), 
        // we are removing an element *before* us, 
        // and we must decrease pos correspondingly.
        if (last < index) {
            index--;
        }
        last = -1;
    }
    
    
}
