cn.org.atool.fluentmachine.Machines Maven / Gradle / Ivy
package cn.org.atool.fluentmachine;
import cn.org.atool.fluentmachine.exception.StateMachineException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* 状态机全局注册
*
* @author darui.wu
*/
public final class Machines {
/**
* 所有构建好的状态机
* key: 状态机标识
*/
private static final Map stateMachines = new ConcurrentHashMap<>();
public static void register(StateMachine stateMachine) {
String machineId = stateMachine.getMachineId();
if (stateMachines.get(machineId) != null) {
throw new StateMachineException("The state machine with id [%s] is already built, no need to build again", machineId);
}
stateMachines.put(machineId, stateMachine);
}
public static M get(String machineId) {
StateMachine stateMachine = stateMachines.get(machineId);
if (stateMachine == null) {
throw new StateMachineException("There is no stateMachine instance for %s, please build it first", machineId);
}
return (M) stateMachine;
}
}