import java.util.*;

/**
 * JVtxHistory implements a simple history list.
 * @author Martin Denn <mdenn@unix-ag.uni-kl.de>
 * @version 0.02
 */

public class JVtxHistory {
    
    final int CAPACITY = 100;
    int history[];
    int head, tail, pointer;
    boolean empty;

    /**
     * Constructor. Builds a new history list
     */
    public JVtxHistory() {
        history = new int[CAPACITY];
        this.init();
    }

    /**
     * reinitializes the history list
     */
    public void init() {
        head=0;
        tail=0;
        pointer=0;
        empty=true;
    }

    /**
     * @return the size of the history list
     */
    public int size() {
        if (empty) {
            return 0;
        }
        else {
            return (head + CAPACITY - tail) % CAPACITY;
        }
    }

    /**
     * Put a new entry into the history list.
     * If the current position is not at the list's head, make the new
     * entry head of the list.
     * @param i the new entry
     */
    public void push(int i) {
        if (empty) {
            history[pointer]=i;
            empty=false;
        }
        else if (i != history[pointer]) {
            pointer = (pointer + 1) % CAPACITY;
            history[pointer] = i;
            if (pointer == tail) {
                tail = (tail + 1) % CAPACITY;
            }
            head = pointer;
        }
    }

    /**
     * Go back in the history list.
     * @return that entry
     * @exception java.util.NoSuchElementException if there are no more 
     *   entries in list
     */
    public int back() throws NoSuchElementException {
        int i;
        if ((!empty) && (pointer != tail)) {
            pointer = (pointer + CAPACITY - 1) % CAPACITY;            
            i = history[pointer];
            return i;
        }
        else throw new NoSuchElementException();
    }
    
    /**
     * Go forward in the history list.
     * @return that entry
     * @exception java.util.NoSuchElementException if there are no more 
     *   entries in list
     */
    public int forward() throws NoSuchElementException {
        int i;
        if ((!empty) && (pointer != head)) {
            pointer = (pointer + 1) % CAPACITY;
            i = history[pointer];
            return i;
        }
        else throw new NoSuchElementException();
    }
}
    
