org.squirrelframework.foundation.fsm.StateMachineContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of squirrel-foundation Show documentation
Show all versions of squirrel-foundation Show documentation
foundation module of squirrel framework which provided event driven infrastructure and a finite state machine implementation.
package org.squirrelframework.foundation.fsm;
import java.util.Stack;
public class StateMachineContext {
private final Object currentInstance;
private final boolean isTestEvent;
private static ThreadLocal> contextContainer = new ThreadLocal>() {
protected Stack initialValue() {
return new Stack();
}
};
public StateMachineContext(Object stateMachine, boolean isTestEvent) {
this.currentInstance = stateMachine;
this.isTestEvent = isTestEvent;
}
public static void set(Object instance) {
set(instance, false);
}
public static void set(Object instance, boolean isTestEvent) {
if (instance == null) {
contextContainer.get().pop();
} else {
contextContainer.get().push(new StateMachineContext(instance, isTestEvent));
}
}
@SuppressWarnings("unchecked")
public static T currentInstance() {
return contextContainer.get().size()>0 ? (T) contextContainer.get().peek().currentInstance : null;
}
public static boolean isTestEvent() {
return contextContainer.get().size()>0 ? contextContainer.get().peek().isTestEvent : false;
}
}