Back to project page khandroid.
The source code is released under:
Apache License
If you think the Android project khandroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.github.khandroid.state; // ww w . java 2 s. c o m import org.slf4j.LoggerFactory; public class StateMachineImpl implements StateMachine { private volatile State mCurrentState; private volatile State mGlobalState; private volatile State mPreviousState; private final org.slf4j.Logger mLogger = LoggerFactory.getLogger(StateMachineImpl.class.getSimpleName()); @Override public synchronized void switchToState(State newState) { if (newState == null) { throw new IllegalArgumentException("state is null"); } if (mCurrentState != null) { mCurrentState.onExit(); mPreviousState = mCurrentState; } mCurrentState = newState; mCurrentState.onEnter(); } @Override public synchronized void revertToPreviousState() { if (mPreviousState != null) { switchToState(mPreviousState); } else { throw new IllegalStateException("No previous state to switch to"); } } @Override public void setGlobalState(State state) { mGlobalState = state; } @Override public State getCurrentState() { return mCurrentState; } @Override public State getGlobalState() { return mGlobalState; } @Override public boolean isInState(State state) { if (state == mCurrentState) { return true; } else { return false; } } @Override public synchronized boolean handleEvent(StateEvent event) { boolean ret = mCurrentState.handleEvent(event); if (!ret) { if (mGlobalState != null) { ret = mGlobalState.handleEvent(event); if (!ret) { mLogger.warn("Unhandled event {} in state {}, global state {}", event.getClass().getSimpleName(), mCurrentState.getClass().getSimpleName(), mGlobalState.getClass().getSimpleName()); } } else { mLogger.warn("Unhandled event {} in state {}, global state not set", event.getClass().getSimpleName(), mCurrentState.getClass().getSimpleName()); } } return ret; } }