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

com.cmt.statemachine.builder.StateMachineBuilderImpl Maven / Gradle / Ivy

The newest version!
package com.cmt.statemachine.builder;

import com.cmt.statemachine.State;
import com.cmt.statemachine.StateMachine;
import com.cmt.statemachine.impl.*;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * StateMachineBuilderImpl
 *
 * @author Frank Zhang
 * @date 2020-02-07 9:40 PM
 */
public class StateMachineBuilderImpl implements StateMachineBuilder {

    /**
     * StateMap is the same with stateMachine, as the core of state machine is holding reference to states.
     */
    private final Map> stateMap = new ConcurrentHashMap<>();
    private final StateMachineImpl stateMachine = new StateMachineImpl<>(stateMap);
    private S initialState;

    @Override
    public ExternalTransitionBuilder externalTransition() {
        return new TransitionBuilderImpl<>(stateMap, TransitionType.EXTERNAL);
    }

    @Override
    public ExternalTransitionsBuilder externalTransitions() {
        return new TransitionsBuilderImpl<>(stateMap, TransitionType.EXTERNAL);
    }

    @Override
    public InternalTransitionBuilder internalTransition() {
        return new TransitionBuilderImpl<>(stateMap, TransitionType.INTERNAL);
    }

    @Override
    public StateMachine build(String machineId) {
        stateMachine.setMachineId(machineId);
        stateMachine.setInitialState(initialState);
        stateMachine.verify();
        stateMachine.setReady(true);
        return stateMachine;
    }

    @Override
    public StateMachineBuilder initialState(S initial) {
        initialState = StateHelper.getState(stateMap, initial).getId();
        return this;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy