class StateMachine {

    public T trySet(T newState)
    {
        checkState(!Thread.holdsLock(lock), "Cannot set state while holding the lock");
        requireNonNull(newState, "newState is null");

        T oldState;
        FutureStateChange<T> futureStateChange;
        ImmutableList<StateChangeListener<T>> stateChangeListeners;
        synchronized (lock) {
            if (state.equals(newState) || isTerminalState(state)) {
                return state;
            }

            oldState = state;
            state = newState;

            futureStateChange = this.futureStateChange.getAndSet(new FutureStateChange<>());
            stateChangeListeners = ImmutableList.copyOf(this.stateChangeListeners);

            // if we are now in a terminal state, free the listeners since this will be the last notification
            if (isTerminalState(state)) {
                this.stateChangeListeners.clear();
            }
        }

        fireStateChanged(newState, futureStateChange, stateChangeListeners);
        return oldState;
    }

    public  StateMachine(String name, Executor executor, T initialState);
    public  StateMachine(String name, Executor executor, T initialState, Iterable<T> terminalStates);

}