All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.squirrelframework.foundation.fsm.StateMachineContext Maven / Gradle / Ivy

Go to download

foundation module of squirrel framework which provided event driven infrastructure and a finite state machine implementation.

There is a newer version: 0.3.10
Show newest version
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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy