UnknownStateMachine.java

/***************************************************************************
   Copyright 2013 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.util;


/** Use an instance of StateMachine as a component.
	This class is meant to be used as the component itself, and when provided with a state machine instance
	it will, when requested to provide an instance of a service use the CURRENT state of the machine as
	an implementation of Uknown.

	@param <S> The type of the states this state machine operates on.
*/
public final class UnknownStateMachine<S extends State> implements Unknown {
	private final StateMachine<S> sm;
	/** Create component from a state machine.
		@param sm The StateMachine instance backing this component.
	*/
	public UnknownStateMachine(final StateMachine<S> sm) { this.sm = sm; }

	@Override public <I> I lookupInterface(final Class<I> theInterface) {
		if(theInterface == Object.class) { return (I) this; }
		if(theInterface == Unknown.class) { return (I) this; }
		return sm.state().lookupInterface(theInterface);
	}
}