package jrequest.base;

import java.util.*;

public class State {

    private static final int OPEN     = 1;
    private static final int ACCEPTED = 2;
    private static final int ASSIGNED = 3;
    private static final int CLOSED   = 9;
    private static final int REJECTED = 10;
    
    private int state = OPEN;
    
//    static {
//        System.out.println("*** static demo code ***");
//    }
    
    public List history = new Vector();
    
    public State() {
        this(OPEN);
    }
    
    public State(int n) {
        this.state = n;
        history.add(new Integer(n));
    }

    public void setAccepted() {
        if (this.isNotOpen()) {
            throw new RuntimeException("called in wrong state");
        }
    	this.state = ACCEPTED;
    }
    
    public void setAssigned() {
        if (this.isAssigned()) {
            return;
        } else if (this.isNotAccepted()) {
    	    throw new RuntimeException("called in wrong state");
    	}
    	this.state = ASSIGNED;
    }
    
    public void setClosed() {
        if (this.isNotAssigned()) {
            throw new RuntimeException("called in wrong state");
        }
        this.state = CLOSED;
    }
    
    public void setRejected() {
        if (this.isClosed()) {
            throw new RuntimeException("called in wrong state");
        }
        this.state = REJECTED;
    }
    
    public boolean isOpen() {
        return this.state == OPEN;
    }
    
    public boolean isNotOpen() {
        return this.state != OPEN;
    }
    
    public boolean isAccepted() {
    	return this.state == ACCEPTED;
    }
    
    public boolean isNotAccepted() {
    	return this.state != ACCEPTED;
    }
    
    public boolean isAssigned() {
        return this.state == ASSIGNED;
    }
    
    public boolean isNotAssigned() {
        return this.state != ASSIGNED;
    }
    
    public boolean isClosed() {
        return this.state == CLOSED;
    }
    
    public boolean isNotClosed() {
        return this.state != CLOSED;
    }
    
    public boolean isRejected() {
        return this.state == REJECTED;
    }
    
    public boolean isNotRejected() {
        return this.state != REJECTED;
    }
    
    public String toString() {
        switch (this.state) {
            case OPEN:      return "OPEN";
            case ACCEPTED:  return "ACCEPTED";
            case ASSIGNED:  return "ASSIGNED";
            case CLOSED:    return "CLOSED";
            case REJECTED:  return "REJECTED";
            default:        return Integer.toString(this.state);
        }
    }

}
