org.statefulj.fsm.model.impl.StateImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of statefulj-fsm Show documentation
Show all versions of statefulj-fsm Show documentation
Finite State Machine with Non-Detereminstic Transitions
/***
*
* Copyright 2014 Andrew Hall
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.statefulj.fsm.model.impl;
import java.util.HashMap;
import java.util.Map;
import org.statefulj.fsm.model.Action;
import org.statefulj.fsm.model.State;
import org.statefulj.fsm.model.Transition;
public class StateImpl implements State {
private String name;
private Map> transitions = new HashMap>();
boolean isEndState = false;
boolean isBlocking = false;
public StateImpl() {
}
public StateImpl(String name) {
if (name == null || name.trim().equals("")) {
throw new RuntimeException("Name must be a non-empty value");
}
this.name = name;
}
public StateImpl(String name, boolean isEndState) {
this(name);
this.isEndState = isEndState;
}
public StateImpl(String name, boolean isEndState, boolean isBlocking) {
this(name, isEndState);
this.isBlocking = isBlocking;
}
public StateImpl(String name, Map> transitions, boolean isEndState) {
this(name, isEndState);
this.transitions = transitions;
}
public StateImpl(String name, Map> transitions, boolean isEndState, boolean isBlocking) {
this(name, transitions, isEndState);
this.isBlocking = isBlocking;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Transition getTransition(String event) {
return transitions.get(event);
}
public Map> getTransitions() {
return transitions;
}
public void setTransitions(Map> transitions) {
this.transitions = transitions;
}
public boolean isEndState() {
return isEndState;
}
public void setEndState(boolean isEndState) {
this.isEndState = isEndState;
}
@Override
public void addTransition(String event, State next) {
this.transitions.put(event, new DeterministicTransitionImpl(next, null));
}
@Override
public void addTransition(String event, State next, Action action) {
this.transitions.put(event, new DeterministicTransitionImpl(next, action));
}
@Override
public void addTransition(String event, Transition transition) {
this.transitions.put(event, transition);
}
@Override
public void removeTransition(String event) {
this.transitions.remove(event);
}
@Override
public void setBlocking(boolean isBlocking) {
this.isBlocking = isBlocking;
}
@Override
public boolean isBlocking() {
return this.isBlocking;
}
@Override
public String toString() {
return "State[name=" + this.name + ", isEndState=" + this.isEndState + ", isBlocking=" + this.isBlocking +"]";
}
}