package net.ranides.assira.observable;

import java.util.ListIterator;

public abstract class ObservableListIterator<T> implements ListIterator<T> {

    private final ListIterator<T> itr;

    private T last;

    public ObservableListIterator(ListIterator<T> itr) {
        this.itr = itr;
    }

    @Override
    public final boolean hasNext() {
        return itr.hasNext();
    }

    @Override
    public final T next() {
        return last = itr.next();
    }

    @Override
    public final boolean hasPrevious() {
        return itr.hasPrevious();
    }

    @Override
    public final T previous() {
        return last = itr.previous();
    }

    @Override
    public final int nextIndex() {
        return itr.nextIndex();
    }

    @Override
    public final int previousIndex() {
        return itr.previousIndex();
    }

    @Override
    public final void set(T t) {
        itr.set(t);
        signalSet(last, t);
    }

    @Override
    public final void add(T t) {
        itr.add(t);
        signalAdd(t);
    }

    @Override
    public final void remove() {
        itr.remove();
        signalRemove(last);
    }

    protected abstract void signalSet(T previous, T value);

    protected abstract void signalAdd(T value);

    protected abstract void signalRemove(T value);

}