com.alibaba.cola.statemachine.impl.StateImpl Maven / Gradle / Ivy
package com.alibaba.cola.statemachine.impl;
import com.alibaba.cola.statemachine.State;
import com.alibaba.cola.statemachine.Transition;
import com.alibaba.cola.statemachine.Visitor;
import java.util.Collection;
import java.util.List;
/**
* StateImpl
*
* @author Frank Zhang
* @date 2020-02-07 11:19 PM
*/
public class StateImpl implements State {
protected final S stateId;
private EventTransitions eventTransitions = new EventTransitions();
StateImpl(S stateId){
this.stateId = stateId;
}
@Override
public Transition addTransition(E event, State target, TransitionType transitionType) {
Transition newTransition = new TransitionImpl<>();
newTransition.setSource(this);
newTransition.setTarget(target);
newTransition.setEvent(event);
newTransition.setType(transitionType);
Debugger.debug("Begin to add new transition: "+ newTransition);
eventTransitions.put(event, newTransition);
return newTransition;
}
@Override
public List> getEventTransitions(E event) {
return eventTransitions.get(event);
}
@Override
public Collection> getAllTransitions() {
return eventTransitions.allTransitions();
}
@Override
public S getId() {
return stateId;
}
@Override
public String accept(Visitor visitor) {
String entry = visitor.visitOnEntry(this);
String exit = visitor.visitOnExit(this);
return entry + exit;
}
@Override
public boolean equals(Object anObject){
if(anObject instanceof State){
State other = (State)anObject;
if(this.stateId.equals(other.getId()))
return true;
}
return false;
}
@Override
public String toString(){
return stateId.toString();
}
}