com.alibaba.cola.statemachine.builder.TransitionsBuilderImpl 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.ArrayList;
import java.util.List;
import java.util.Map;
/**
* TransitionsBuilderImpl
*
* @author Frank Zhang
* @date 2020-02-08 7:43 PM
*/
public class TransitionsBuilderImpl extends TransitionBuilderImpl implements ExternalTransitionsBuilder {
/**
* This is for fromAmong where multiple sources can be configured to point to one target
*/
private List> sources = new ArrayList<>();
private List> transitions = new ArrayList<>();
public TransitionsBuilderImpl(Map> stateMap, TransitionType transitionType) {
super(stateMap, transitionType);
}
@Override
public From fromAmong(S... stateIds) {
for(S stateId : stateIds) {
sources.add(StateHelper.getState(super.stateMap, stateId));
}
return this;
}
@Override
public On on(E event) {
for(State source : sources) {
Transition transition = source.addTransition(event, super.target, super.transitionType);
transitions.add(transition);
}
return this;
}
@Override
public When when(Condition condition) {
for(Transition transition : transitions){
transition.setCondition(condition);
}
return this;
}
@Override
public void perform(Action action) {
for(Transition transition : transitions){
transition.setAction(action);
}
}
}