package fauxExchange; import java.util.*; class OrderBookStateController extends Thread implements Observer { //-- Member Data -------------------------------------------------------// //the state of the Segment (e.g. We aren't trading if the segment is in a closed state) private State itsState, nextState; //the orderBook with which this OrderStateController is associated private OrderBook ob; private int stopDelayTime = 20000; public final static State STOPPED_STATE = new StoppedState(); public final static State BREAK_STATE = new BreakState(); public final static State TRADING_STATE = new TradingState(); //----------------------------------------------------------------------// //-- Accessor/Modifiers -------------------------------------------------------// public OrderBook getOrderBook(){ return this.ob; } public State getOrderBookState(){ return this.itsState; } public void setOrderBook(OrderBook ob){ this.ob = ob; } public void handleTradeClosedException (TradeClosedException tce){ // If you are in BreakState, set nextState to stopped if (itsState instanceof BreakState){ System.out.println("*********TRADE WILL OPEN LATE ON SECURITY " + ob.getSecurity().getSecurityId() + "**********"); setNextState(STOPPED_STATE); // If you are in TradingState, switch immediately to stopped state }else if (itsState instanceof TradingState){ System.out.println("*********TRADE HAS BEEN STOPPED ON SECURITY " + ob.getSecurity().getSecurityId() + "*********"); setOrderBookState(STOPPED_STATE); } } public void setOrderBookState(State s){ this.itsState = s; if (s instanceof StoppedState){ this.setDaemon(true); this.start(); } try { s.bootstrapState(this.getOrderBook()); this.setNextState(null); }catch(TradeClosedException tce){ handleTradeClosedException(tce); } } public void setNextState(State s){ this.nextState = s; } public State nextState(){ return this.nextState();} //-----------------------------------------------------------------------------// /** This thread is run when the State Controller is in StoppedState */ public void run (){ long start = System.currentTimeMillis(); while (start > System.currentTimeMillis() - stopDelayTime && itsState instanceof StoppedState){ try { sleep(1000); }catch (Exception e){;} } // If we are still in the stopped state, go to trade state, otherwise, exit the thread if (itsState instanceof StoppedState){ this.setOrderBookState(TRADING_STATE); } } //-- CONSTRUCTORS //-----------------------------------------------------------// public OrderBookStateController(State startState){ System.out.println("Constructing the OrderBookStateController..."); this.itsState = startState; } //-----------------------------------------------------------------------------// //Herein lies the Observer magic...If the segment says we are in a //particular state we need to know if that affects the OrderBookState public void update(Observable o, Object requestedState){ //If the segement is in break the OrderBook needs to be placed in the breakState as well if (requestedState instanceof BreakState){ setOrderBookState((BreakState)requestedState); } else if (requestedState instanceof TradingState) { if(itsState instanceof BreakState && !(nextState() instanceof StoppedState) ){ setOrderBookState((TradingState)requestedState); } else if (itsState instanceof StoppedState){ setOrderBookState(new StoppedState()); } else{ System.out.println("You messed up bro!"); } } } //end update(); //-----------------------------------------------------------------------------// /** * This method is called when the Rules for the current state need * to be applied. */ public void applyRules (OrderBook ob, Order o) { try { this.itsState.applyRules(ob, o); }catch(TradeClosedException tce){ handleTradeClosedException(tce); } } } //end class