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

com.alibaba.cola.statemachine.builder.TransitionBuilderImpl Maven / Gradle / Ivy

package com.alibaba.cola.statemachine.builder;

import com.alibaba.cola.statemachine.Action;
import com.alibaba.cola.statemachine.Condition;
import com.alibaba.cola.statemachine.State;
import com.alibaba.cola.statemachine.Transition;
import com.alibaba.cola.statemachine.impl.StateHelper;
import com.alibaba.cola.statemachine.impl.TransitionType;

import java.util.Map;

/**
 * TransitionBuilderImpl
 *
 * @author Frank Zhang
 * @date 2020-02-07 10:20 PM
 */
class TransitionBuilderImpl implements ExternalTransitionBuilder, InternalTransitionBuilder, From, On, To {

    final Map> stateMap;

    private State source;

    protected State target;

    private Transition transition;

    final TransitionType transitionType;

    public TransitionBuilderImpl(Map> stateMap, TransitionType transitionType) {
        this.stateMap = stateMap;
        this.transitionType = transitionType;
    }

    @Override
    public From from(S stateId) {
        source = StateHelper.getState(stateMap, stateId);
        return this;
    }

    @Override
    public To to(S stateId) {
        target = StateHelper.getState(stateMap, stateId);
        return this;
    }

    @Override
    public To within(S stateId) {
        source = target = StateHelper.getState(stateMap, stateId);
        return this;
    }

    @Override
    public When when(Condition condition) {
        transition.setCondition(condition);
        return this;
    }

    @Override
    public On on(E event) {
        transition = source.addTransition(event, target, transitionType);
        return this;
    }

    @Override
    public void perform(Action action) {
        transition.setAction(action);
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy