StateMachineSessionFactory.java

/***************************************************************************
   Copyright 2012 Emily Estes

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
***************************************************************************/
package net.metanotion.web.concrete;


import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

import net.metanotion.util.Pair;
import net.metanotion.util.State;
import net.metanotion.util.StateMachine;
import net.metanotion.util.StateMachineFactory;
import net.metanotion.util.Unknown;
import net.metanotion.web.SessionFactory;

/** A session factory implementation that returns a StateMachine instance constructed
from a new initial state provided by a StateMachineFactory and properly stores the computed
states provided by State instances.
	@param <D> The type of the inital values the session factory is provided with that this class makes the value of
		the pair it provides to the state machines as an initial condition. The key of the pair is the state machine
		instance itself.
	@param <S> The type of states the state machines produced by this factory operate on.
*/
public final class StateMachineSessionFactory<D,S extends State> implements SessionFactory<D> {
	/** The actual session instance for a state machine. */
	private static final class Session<E,T extends State> implements Unknown, StateMachine<State<T>> {
		/** By storing the current state in an AtomicReference, hopefully we will simplify many concurrency issues. */
		private final AtomicReference<State<T>> state = new AtomicReference<State<T>>();
		public Session(StateMachineFactory<T, Map.Entry<StateMachine, E>> factory, E ro) {
			this.state.set(factory.newMachine(new Pair<StateMachine, E>(this, ro)));
		}
		// StateMachine
		@Override public void nextState(Object event) { state.set(state.get().nextState(event)); }
		@Override public State<T> state() { return state.get(); }
		// Unknown
		@Override public <I> I lookupInterface(Class<I> theInterface) {
			if(theInterface == Object.class) { return (I) this; }
			if(theInterface == Unknown.class) { return (I) this; }
			if(theInterface == StateMachine.class) { return (I) this; }
			return state.get().lookupInterface(theInterface);
		}
	}

	private final StateMachineFactory<S,Map.Entry<StateMachine, D>> factory;
	/** Create a new StateMachineSessionFactory.
		@param factory A factory to create initial states from a RequestObject.
	*/
	public StateMachineSessionFactory(StateMachineFactory<S, Map.Entry<StateMachine, D>> factory) { this.factory = factory; }
	// Unknown
	@Override public Unknown newSession(D ro) { return new Session<D,S>(factory, ro); }
}