Revision: 969c9c545155
Author: Thomas Quintana <[email protected]>
Date: Fri Jun 15 09:12:45 2012
Log: lots of bug fixes and cleaner interfaces.
http://code.google.com/p/commscale/source/detail?r=969c9c545155&repo=commons
Added:
/src/main/java/org/mobicents/commons/fsm/FiniteStateMachineException.java
Deleted:
/src/test/java/org/mobicents/commons/fsm/IncandescentLightBulb.java
/src/test/java/org/mobicents/commons/fsm/LightBulbReadyCondition.java
/src/test/java/org/mobicents/commons/fsm/LightBulbSocket.java
/src/test/java/org/mobicents/commons/fsm/OffEvent.java
/src/test/java/org/mobicents/commons/fsm/OnEvent.java
/src/test/java/org/mobicents/commons/fsm/RemoteSwitch.java
/src/test/java/org/mobicents/commons/fsm/TurnOffAction.java
/src/test/java/org/mobicents/commons/fsm/TurnOnAction.java
Modified:
/src/main/java/org/mobicents/commons/event/Event.java
/src/main/java/org/mobicents/commons/event/EventBus.java
/src/main/java/org/mobicents/commons/fsm/Action.java
/src/main/java/org/mobicents/commons/fsm/Condition.java
/src/main/java/org/mobicents/commons/fsm/FiniteStateMachine.java
/src/main/java/org/mobicents/commons/fsm/State.java
/src/main/java/org/mobicents/commons/fsm/Transition.java
/src/test/java/org/mobicents/commons/fsm/FiniteStateMachineTest.java
/src/test/java/org/mobicents/commons/fsm/LightBulb.java
=======================================
--- /dev/null
+++
/src/main/java/org/mobicents/commons/fsm/FiniteStateMachineException.java
Fri Jun 15 09:12:45 2012
@@ -0,0 +1,34 @@
+/*
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.mobicents.commons.fsm;
+
+import org.mobicents.commons.annotations.NotThreadSafe;
+
+/**
+ * @author [email protected] (Thomas Quintana)
+ */
+@NotThreadSafe public class FiniteStateMachineException extends Exception {
+ private static final long serialVersionUID = 1L;
+
+ public FiniteStateMachineException(final String message) {
+ super(message);
+ }
+
+ public FiniteStateMachineException(final String message, final Throwable
cause) {
+ super(message, cause);
+ }
+}
=======================================
--- /src/test/java/org/mobicents/commons/fsm/IncandescentLightBulb.java Tue
May 29 23:43:15 2012
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.mobicents.commons.fsm;
-
-import org.mobicents.commons.annotations.NotThreadSafe;
-
-/**
- * @author [email protected] (Thomas Quintana)
- */
-@NotThreadSafe public final class IncandescentLightBulb implements
LightBulb {
- private boolean on;
- private int timesTurnedOff;
- private int timesTurnedOn;
-
- public IncandescentLightBulb() {
- super();
- on = false;
- timesTurnedOff = 0;
- timesTurnedOn = 0;
- }
-
- @Override public int getTimesTurnedOff() {
- return timesTurnedOff;
- }
-
- @Override public int getTimesTurnedOn() {
- return timesTurnedOn;
- }
-
- @Override public boolean isOff() {
- return !on;
- }
-
- @Override public boolean isOn() {
- return on;
- }
-
- @Override public void turnOff() {
- on = false;
- ++timesTurnedOff;
- }
-
- @Override public void turnOn() {
- on = true;
- ++timesTurnedOn;
- }
-}
=======================================
--- /src/test/java/org/mobicents/commons/fsm/LightBulbReadyCondition.java
Tue May 29 23:43:15 2012
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.mobicents.commons.fsm;
-
-import org.mobicents.commons.annotations.NotThreadSafe;
-import org.mobicents.commons.event.Event;
-
-/**
- * @author [email protected] (Thomas Quintana)
- */
-@NotThreadSafe public final class LightBulbReadyCondition implements
Condition {
- private final LightBulb lightbulb;
-
- public LightBulbReadyCondition(final LightBulb lightbulb) {
- super();
- this.lightbulb = lightbulb;
- }
-
- @Override public boolean accept(final Event<?> event, final Transition
transition) {
- final Class<?> klass = event.getClass();
- if(klass.equals(OffEvent.class)) {
- return !lightbulb.isOff() && lightbulb.isOn();
- } else if(klass.equals(OnEvent.class)) {
- return lightbulb.isOff() && !lightbulb.isOn();
- }
- return false;
- }
-}
=======================================
--- /src/test/java/org/mobicents/commons/fsm/LightBulbSocket.java Tue May
29 23:43:15 2012
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.mobicents.commons.fsm;
-
-import java.util.HashSet;
-import java.util.Set;
-
-import org.mobicents.commons.event.Event;
-
-/**
- * @author [email protected] (Thomas Quintana)
- */
-public final class LightBulbSocket extends FiniteStateMachine {
- private final State on;
- private final State off;
-
- private final Set<Transition> transitions;
-
- public LightBulbSocket(final LightBulb lightbulb) {
- super();
- // Initialize states.
- this.on = new State("On", new TurnOnAction(lightbulb), null);
- this.off = new State("Off", new TurnOffAction(lightbulb), null);
- // Initialize transitions.
- final LightBulbReadyCondition condition = new
LightBulbReadyCondition(lightbulb);
- this.transitions = new HashSet<Transition>();
- this.transitions.add(new Transition(off, on, condition));
- this.transitions.add(new Transition(on, off, condition));
- // Initialize the finite state machine.
- initialize(off, transitions);
- }
-
- @Override public boolean accept(final Event<?> event) {
- final Class<?> klass = event.getClass();
- return klass.equals(OffEvent.class) || klass.equals(OnEvent.class);
- }
-
- @Override public void handle(final Event<?> event) {
- try {
- final Class<?> klass = event.getClass();
- if(klass.equals(OffEvent.class)) {
- transition(event, off);
- } else if(klass.equals(OnEvent.class)) {
- transition(event, on);
- }
- } catch(TransitionFailedException ignored) { }
- catch(TransitionNotFoundException ignored) { }
- }
-}
=======================================
--- /src/test/java/org/mobicents/commons/fsm/OffEvent.java Tue May 29
23:43:15 2012
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.mobicents.commons.fsm;
-
-import org.mobicents.commons.annotations.Immutable;
-import org.mobicents.commons.event.Event;
-
-/**
- * @author [email protected] (Thomas Quintana)
- */
-@Immutable public final class OffEvent extends Event<RemoteSwitch> {
- public OffEvent(final Object source, final Long timestamp, final
RemoteSwitch type) {
- super(source, timestamp, type);
- }
-}
=======================================
--- /src/test/java/org/mobicents/commons/fsm/OnEvent.java Tue May 29
23:43:15 2012
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.mobicents.commons.fsm;
-
-import org.mobicents.commons.annotations.Immutable;
-import org.mobicents.commons.event.Event;
-
-/**
- * @author [email protected] (Thomas Quintana)
- */
-@Immutable public final class OnEvent extends Event<RemoteSwitch> {
- public OnEvent(final Object source, final Long timestamp, final
RemoteSwitch type) {
- super(source, timestamp, type);
- }
-}
=======================================
--- /src/test/java/org/mobicents/commons/fsm/RemoteSwitch.java Tue May 29
23:43:15 2012
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.mobicents.commons.fsm;
-
-import org.mobicents.commons.annotations.ThreadSafe;
-
-/**
- * @author [email protected] (Thomas Quintana)
- */
-@ThreadSafe public final class RemoteSwitch {
- private final LightBulbSocket socket;
-
- public RemoteSwitch(final LightBulbSocket socket) {
- super();
- this.socket = socket;
- }
-
- public void turnOff() {
- socket.handle(new OffEvent(this, System.currentTimeMillis(), this));
- }
-
- public void turnOn() {
- socket.handle(new OnEvent(this, System.currentTimeMillis(), this));
- }
-}
=======================================
--- /src/test/java/org/mobicents/commons/fsm/TurnOffAction.java Tue May 29
23:43:15 2012
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.mobicents.commons.fsm;
-
-import org.mobicents.commons.annotations.NotThreadSafe;
-import org.mobicents.commons.event.Event;
-
-/**
- * @author [email protected] (Thomas Quintana)
- */
-@NotThreadSafe public final class TurnOffAction implements Action {
- private final LightBulb lightbulb;
-
- public TurnOffAction(final LightBulb lightbulb) {
- super();
- this.lightbulb = lightbulb;
- }
-
- @Override public void execute(final Event<?> event, final State state) {
- lightbulb.turnOff();
- final StringBuilder buffer = new StringBuilder();
- buffer.append(lightbulb.getClass().getName()).append(" Off");
- System.out.println(buffer.toString());
- }
-}
=======================================
--- /src/test/java/org/mobicents/commons/fsm/TurnOnAction.java Tue May 29
23:43:15 2012
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.mobicents.commons.fsm;
-
-import org.mobicents.commons.annotations.NotThreadSafe;
-import org.mobicents.commons.event.Event;
-
-/**
- * @author [email protected] (Thomas Quintana)
- */
-@NotThreadSafe public class TurnOnAction implements Action {
- private final LightBulb lightbulb;
-
- public TurnOnAction(final LightBulb lightbulb) {
- super();
- this.lightbulb = lightbulb;
- }
-
- @Override public void execute(final Event<?> event, final State state) {
- lightbulb.turnOn();
- final StringBuilder buffer = new StringBuilder();
- buffer.append(lightbulb.getClass().getName()).append(" On");
- System.out.println(buffer.toString());
- }
-}
=======================================
--- /src/main/java/org/mobicents/commons/event/Event.java Tue May 29
23:43:15 2012
+++ /src/main/java/org/mobicents/commons/event/Event.java Fri Jun 15
09:12:45 2012
@@ -16,10 +16,12 @@
*/
package org.mobicents.commons.event;
+import org.mobicents.commons.annotations.NotThreadSafe;
+
/**
* @author [email protected] (Thomas Quintana)
*/
-public abstract class Event<T> {
+@NotThreadSafe public class Event<T> {
private final Object source;
private final Long timestamp;
private final T type;
@@ -31,6 +33,17 @@
this.timestamp = timestamp;
this.type = type;
}
+
+ private void checkNotNull(final Object source, final Long timestamp,
final T type)
+ throws NullPointerException {
+ if(source == null) {
+ throw new NullPointerException("An event may not have a null
source.");
+ } else if(timestamp == null) {
+ throw new NullPointerException("An event may not have a null
timestamp.");
+ } else if(type == null) {
+ throw new NullPointerException("An event may not have a null type.");
+ }
+ }
@Override public boolean equals(final Object object) {
if(object == null) {
@@ -67,15 +80,4 @@
result = prime * result + type.hashCode();
return result;
}
-
- private void checkNotNull(final Object source, final Long timestamp,
final T type)
- throws NullPointerException {
- if(source == null) {
- throw new NullPointerException("An event may not have a null
source.");
- } else if(timestamp == null) {
- throw new NullPointerException("An event may not have a null
timestamp.");
- } else if(type == null) {
- throw new NullPointerException("An event may not have a null type.");
- }
- }
-}
+}
=======================================
--- /src/main/java/org/mobicents/commons/event/EventBus.java Tue May 29
23:43:15 2012
+++ /src/main/java/org/mobicents/commons/event/EventBus.java Fri Jun 15
09:12:45 2012
@@ -89,7 +89,7 @@
}
public EventBus build() {
- validate();
+ checkNotNull(executor);
final AbstractSet<EventHandler> handlers = new
CopyOnWriteArraySet<EventHandler>();
final BlockingQueue<Event<?>> queue = new
ArrayBlockingQueue<Event<?>>(queueSize);
final List<Dispatcher> dispatchers = new ArrayList<Dispatcher>();
@@ -98,6 +98,16 @@
}
return new EventBus(Collections.unmodifiableList(dispatchers),
executor, handlers, queue);
}
+
+ private void checkNotNull(final Executor executor) throws
NullPointerException {
+ if(executor == null) {
+ final StringBuilder buffer = new StringBuilder();
+ buffer.append("An event bus can not be built with a null value for
the executor.\n");
+ buffer.append("Please set an executor before calling the build()
method on ");
+ buffer.append(getClass().getName());
+ throw new NullPointerException(buffer.toString());
+ }
+ }
public Builder setExecutor(final Executor executor) {
this.executor = executor;
@@ -113,16 +123,6 @@
this.queueSize = queueSize;
return this;
}
-
- private void validate() throws RuntimeException {
- if(executor == null) {
- final StringBuilder buffer = new StringBuilder();
- buffer.append("An event bus can not be built with a null value for
the executor.\n");
- buffer.append("Please set an executor before calling the build()
method on ");
- buffer.append(getClass().getName());
- throw new NullPointerException(buffer.toString());
- }
- }
}
private static final class Dispatcher implements Runnable {
@@ -148,8 +148,8 @@
for(final EventHandler handler : handlers) {
if(logger.isTraceEnabled()) {
final StringBuilder buffer = new StringBuilder();
- buffer.append("Processing a(n) ");
- buffer.append(event.getType().toString()).append("
event.\n");
+ buffer.append("Processing a(n) event of type ");
+ buffer.append(event.getType().toString()).append(".\n");
buffer.append(event.toString());
logger.trace(buffer.toString());
}
=======================================
--- /src/main/java/org/mobicents/commons/fsm/Action.java Tue May 29
23:43:15 2012
+++ /src/main/java/org/mobicents/commons/fsm/Action.java Fri Jun 15
09:12:45 2012
@@ -22,5 +22,5 @@
* @author [email protected] (Thomas Quintana)
*/
public interface Action {
- public void execute(Event<?> event, State state);
-}
+ public <T> void execute(Event<T> event, State state);
+}
=======================================
--- /src/main/java/org/mobicents/commons/fsm/Condition.java Tue May 29
23:43:15 2012
+++ /src/main/java/org/mobicents/commons/fsm/Condition.java Fri Jun 15
09:12:45 2012
@@ -22,5 +22,5 @@
* @author [email protected] (Thomas Quintana)
*/
public interface Condition {
- public boolean accept(Event<?> event, Transition transition);
-}
+ public <T> boolean accept(Event<T> event, Transition transition);
+}
=======================================
--- /src/main/java/org/mobicents/commons/fsm/FiniteStateMachine.java Wed
May 30 18:12:19 2012
+++ /src/main/java/org/mobicents/commons/fsm/FiniteStateMachine.java Fri
Jun 15 09:12:45 2012
@@ -23,18 +23,18 @@
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
+import org.mobicents.commons.annotations.NotThreadSafe;
import org.mobicents.commons.event.Event;
-import org.mobicents.commons.event.EventHandler;
/**
* @author [email protected] (Thomas Quintana)
*/
-public abstract class FiniteStateMachine implements EventHandler {
+@NotThreadSafe public abstract class FiniteStateMachine {
private final Lock lock;
private State state;
private Map<State, Map<State, Transition>> transitions;
- public FiniteStateMachine() {
+ protected FiniteStateMachine() {
super();
this.lock = new ReentrantLock();
}
@@ -43,33 +43,91 @@
return transitions.get(state).containsKey(newState);
}
- protected State getState() {
- while(!lock.tryLock()) { /* Spin! */ }
- try { return state; }
- finally { lock.unlock(); }
+ private void checkIsInitialized() throws FiniteStateMachineException {
+ if(state == null || transitions == null) {
+ final StringBuilder buffer = new StringBuilder();
+ buffer.append("The finite state machine has not been initialized. ");
+ buffer.append("Please initialize the state machine before using
it.");
+ throw new FiniteStateMachineException(buffer.toString());
+ }
+ }
+
+ private void checkNotNull(final State state) throws NullPointerException
{
+ if(state == null) {
+ final StringBuilder buffer = new StringBuilder();
+ buffer.append("The state parameter can not be null. ");
+ buffer.append("Please provide a usable state and try again.");
+ throw new NullPointerException(buffer.toString());
+ }
+ }
+
+ private void checkNotNull(final State state, final Set<Transition>
transitions)
+ throws NullPointerException {
+ checkNotNull(state);
+ if(transitions == null) {
+ final StringBuilder buffer = new StringBuilder();
+ buffer.append("The transitions parameter can not be null. ");
+ buffer.append("Please provide a set of transitions and try again.");
+ throw new NullPointerException(buffer.toString());
+ }
+ }
+
+ private String conditionFailed(final Event<?> event, final Transition
transition) {
+ final StringBuilder buffer = new StringBuilder();
+ buffer.append("The condition guarding a transition from a(n) ");
+ buffer.append(transition.getStateOnEnter().getId());
+ buffer.append(" state to
a(n) ").append(transition.getStateOnExit().getId());
+ buffer.append(" state has failed. The event type that caused the
failure is of type ");
+ buffer.append(event.getType().getClass().getName());
+ return buffer.toString();
+ }
+
+ private String finiteStateMachineFailed() {
+ final StringBuilder buffer = new StringBuilder();
+ buffer.append("This finite state machine failed. It may be in an
illegal or unusable state.");
+ buffer.append("Please re-initialize before continuing use of this
finite state machine.");
+ return buffer.toString();
}
private Transition getTransitionTo(final State newState) {
return transitions.get(state).get(newState);
}
- protected void initialize(final State stateOnStart, final
Set<Transition> transitions) {
- this.state = stateOnStart;
- this.transitions = toImmutableMap(transitions);
+ protected void initialize(final State state, final Set<Transition>
transitions) {
+ checkNotNull(state, transitions);
+ lock();
+ try {
+ this.state = state;
+ this.transitions = toImmutableMap(transitions);
+ } finally {
+ unlock();
+ }
}
- protected void transition(final Event<?> event, final State newState)
- throws TransitionFailedException, TransitionNotFoundException {
- while(!lock.tryLock()) { /* Spin! */ }
+ protected void lock() {
+ lock.lock();
+ }
+
+ private String noTransitionFound(final State from, final State to) {
+ final StringBuilder buffer = new StringBuilder();
+ buffer.append("No transition could be found from a(n) ");
+ buffer.append(from.getId()).append(" state to a(n) ");
+ buffer.append(to.getId()).append(" state.");
+ return buffer.toString();
+ }
+
+ protected <T> void transition(final Event<T> event, final State state)
+ throws FiniteStateMachineException, NullPointerException,
+ TransitionFailedException, TransitionNotFoundException {
+ checkNotNull(state);
+ checkIsInitialized();
+ lock();
try {
- if(!canTransitionTo(newState)) {
- final StringBuilder buffer = new StringBuilder();
- buffer.append("No transition could be found from ");
- buffer.append(state.getId()).append(" ");
- buffer.append("to ").append(newState.getId());
- throw new TransitionNotFoundException(buffer.toString(), event,
newState);
- }
- final Transition transition = getTransitionTo(newState);
+ if(!canTransitionTo(state)) {
+ final String message = noTransitionFound(state, state);
+ throw new TransitionNotFoundException(message, event, state);
+ }
+ final Transition transition = getTransitionTo(state);
final Condition condition = transition.getCondition();
if(condition != null) {
if(condition.accept(event, transition)) {
@@ -77,18 +135,21 @@
if(actionOnExit != null) {
actionOnExit.execute(event, state);
}
- state = newState;
- final Action actionOnEnter = newState.getActionOnEnter();
+ this.state = state;
+ final Action actionOnEnter = state.getActionOnEnter();
if(actionOnEnter != null) {
actionOnEnter.execute(event, state);
}
} else {
- throw new TransitionFailedException("The condition guarding the
transition did not pass.",
- event, transition);
+ final String message = conditionFailed(event, transition);
+ throw new TransitionFailedException(message, event, transition);
}
}
+ } catch(final RuntimeException exception) {
+ final String message = finiteStateMachineFailed();
+ throw new FiniteStateMachineException(message, exception);
} finally {
- lock.unlock();
+ unlock();
}
}
@@ -104,4 +165,12 @@
}
return Collections.unmodifiableMap(map);
}
-}
+
+ protected boolean tryLock() {
+ return lock.tryLock();
+ }
+
+ protected void unlock() {
+ lock.unlock();
+ }
+}
=======================================
--- /src/main/java/org/mobicents/commons/fsm/State.java Tue May 29 23:43:15
2012
+++ /src/main/java/org/mobicents/commons/fsm/State.java Fri Jun 15 09:12:45
2012
@@ -16,12 +16,12 @@
*/
package org.mobicents.commons.fsm;
-import org.mobicents.commons.annotations.Immutable;
+import org.mobicents.commons.annotations.NotThreadSafe;
/**
* @author [email protected] (Thomas Quintana)
*/
-@Immutable public final class State {
+@NotThreadSafe public class State {
private final Action actionOnEnter;
private final Action actionOnExit;
private final String id;
@@ -34,10 +34,10 @@
this.id = id;
}
- private void checkNotNull(final String type) throws NullPointerException
{
- if(type == null) {
+ private void checkNotNull(final String id) throws NullPointerException {
+ if(id == null) {
final StringBuilder buffer = new StringBuilder();
- buffer.append("A state can not be built with a null value for the
type.");
+ buffer.append("A state can not be built with a null value for the id
parameter.");
throw new NullPointerException(buffer.toString());
}
}
=======================================
--- /src/main/java/org/mobicents/commons/fsm/Transition.java Tue May 29
23:43:15 2012
+++ /src/main/java/org/mobicents/commons/fsm/Transition.java Fri Jun 15
09:12:45 2012
@@ -48,6 +48,20 @@
throw new NullPointerException(buffer.toString());
}
}
+
+ @Override public boolean equals(final Object object) {
+ if(this == object) {
+ return true;
+ } else if(object == null) {
+ return false;
+ } else if(getClass() != object.getClass()) {
+ return false;
+ }
+ final Transition transition = (Transition)object;
+ if(!stateOnEnter.equals(transition.getStateOnEnter())) { return false; }
+ if(!stateOnExit.equals(transition.getStateOnExit())) { return false; }
+ return true;
+ }
public Condition getCondition() {
return condition;
@@ -60,4 +74,12 @@
public State getStateOnExit() {
return stateOnExit;
}
-}
+
+ @Override public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + stateOnEnter.hashCode();
+ result = prime * result + stateOnExit.hashCode();
+ return result;
+ }
+}
=======================================
--- /src/test/java/org/mobicents/commons/fsm/FiniteStateMachineTest.java
Tue May 29 23:43:15 2012
+++ /src/test/java/org/mobicents/commons/fsm/FiniteStateMachineTest.java
Fri Jun 15 09:12:45 2012
@@ -30,30 +30,6 @@
}
@Test public void test() {
- // Initialize
- final LightBulb lightbulb = new IncandescentLightBulb();
- final LightBulbSocket socket = new LightBulbSocket(lightbulb);
- final RemoteSwitch remote = new RemoteSwitch(socket);
- // Can't turn off a socket that is already off.
- remote.turnOff();
- assertTrue(lightbulb.getTimesTurnedOff() == 0);
- // Turn on the socket and verify.
- remote.turnOn();
- assertTrue(lightbulb.getTimesTurnedOn() == 1);
- // Turn off the socket and verify.
- remote.turnOff();
- assertTrue(lightbulb.getTimesTurnedOff() == 1);
- // Try to turn on the socket twice.
- remote.turnOn();
- remote.turnOn();
- // Turn off the socket.
- remote.turnOff();
- // Turn on the socket.
- remote.turnOn();
- // Turn off the socket.
- remote.turnOff();
- // Verify that transitions between states were executed successfully.
- assertTrue(lightbulb.getTimesTurnedOff() == 3);
- assertTrue(lightbulb.getTimesTurnedOn() == 3);
+
}
}
=======================================
--- /src/test/java/org/mobicents/commons/fsm/LightBulb.java Tue May 29
23:43:15 2012
+++ /src/test/java/org/mobicents/commons/fsm/LightBulb.java Fri Jun 15
09:12:45 2012
@@ -1,29 +1,11 @@
-/*
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
package org.mobicents.commons.fsm;
-/**
- * @author [email protected] (Thomas Quintana)
- */
-public interface LightBulb {
- public int getTimesTurnedOff();
- public int getTimesTurnedOn();
- public boolean isOff();
- public boolean isOn();
- public void turnOff();
- public void turnOn();
-}
+public final class LightBulb extends FiniteStateMachine {
+ public LightBulb() {
+ super();
+ }
+
+ public void power() {
+
+ }
+}