com.alibaba.cola.statemachine.impl.EventTransitions Maven / Gradle / Ivy
package com.alibaba.cola.statemachine.impl;
import com.alibaba.cola.statemachine.Transition;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* EventTransitions
*
* 同一个Event可以触发多个Transitions,https://github.com/alibaba/COLA/pull/158
*
* @author Frank Zhang
* @date 2021-05-28 5:17 PM
*/
public class EventTransitions {
private HashMap>> eventTransitions;
public EventTransitions(){
eventTransitions = new HashMap<>();
}
public void put(E event, Transition transition){
if(eventTransitions.get(event) == null){
List> transitions = new ArrayList<>();
transitions.add(transition);
eventTransitions.put(event, transitions);
}
else{
List existingTransitions = eventTransitions.get(event);
verify(existingTransitions, transition);
existingTransitions.add(transition);
}
}
/**
* Per one source and target state, there is only one transition is allowed
* @param existingTransitions
* @param newTransition
*/
private void verify(List> existingTransitions, Transition newTransition) {
for (Transition transition : existingTransitions) {
if (transition.equals(newTransition)) {
throw new StateMachineException(transition + " already Exist, you can not add another one");
}
}
}
public List> get(E event){
return eventTransitions.get(event);
}
public List> allTransitions(){
List> allTransitions = new ArrayList<>();
for(List> transitions : eventTransitions.values()){
allTransitions.addAll(transitions);
}
return allTransitions;
}
}