com.cmt.statemachine.builder.TransitionsBuilderImpl Maven / Gradle / Ivy
The newest version!
package com.cmt.statemachine.builder;
import com.cmt.statemachine.Action;
import com.cmt.statemachine.Condition;
import com.cmt.statemachine.State;
import com.cmt.statemachine.Transition;
import com.cmt.statemachine.impl.StateHelper;
import com.cmt.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 When when(Condition condition, String desc) {
for (Transition transition : transitions) {
transition.setCondition(condition, desc);
}
return this;
}
@Override
public void perform(Action action) {
for (Transition transition : transitions) {
transition.setAction(action);
}
}
}