import java.util.*;

/**
 * Implementation of C-like enum object cribbed from
 * http://www.javaworld.com/javaworld/jw-07-1997/jw-07-enumerated-p4.html
 * (Java 1.5 has an Enum class, but I don't feel like upgrading.)
 **/
public final class MoveType {
    private String id;
    public final int ord;
    private MoveType prev;
    private MoveType next;
    
    private static int upperBound = 0;
    private static MoveType first = null;
    private static MoveType last = null;
    
    private MoveType(String anID) {
	this.id = anID;
	this.ord = upperBound++;
	if (first == null) first = this;
	if (last != null) {
	    this.prev = last;
	    last.next = this;
	}
	last = this;
    }
    public static Enumeration elements() {
	return new Enumeration() {
		private MoveType curr = first;
		public boolean hasMoreElements() {
		    return curr != null;
		}
		public Object nextElement() {
		    MoveType c = curr;
		    curr = curr.next();
		    return c;
		}
	    };
    }

    // not efficient.
    public static MoveType get(int which) {
	MoveType move = MoveType.first;
	for (int i=0; i < which; i++) {
	    move = move.next;
	}
	return(move);
    }

    public static MoveType random(Random rand) {
	return(MoveType.get(rand.nextInt(MoveType.size())));
    }	

    public String toString() {return this.id; }
    public static int size() { return upperBound; }
    public static MoveType first() { return first; }
    public static MoveType last()  { return last;  }
    public MoveType prev()  { return this.prev; }
    public MoveType next()  { return this.next; }
    
    public static final MoveType ADD     = new MoveType("Add");
    public static final MoveType DELETE  = new MoveType("Delete");
    public static final MoveType REVERSE = new MoveType("Reverse");
}
