From 6b72eff96cb90f966fab32245c09d56611735513 Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Wed, 4 Mar 2020 19:50:35 +0100 Subject: [PATCH] Change EventHandler to be generic --- .../java/org/jeasy/states/api/EventHandler.java | 4 ++-- .../org/jeasy/states/api/FiniteStateMachine.java | 4 ++-- .../states/api/FiniteStateMachineException.java | 6 +++--- .../java/org/jeasy/states/api/Transition.java | 2 +- .../jeasy/states/core/FiniteStateMachineImpl.java | 7 ++++--- .../org/jeasy/states/core/TransitionBuilder.java | 5 +++-- .../org/jeasy/states/core/TransitionImpl.java | 15 ++++++++------- .../states/core/FiniteStateMachineImplTest.java | 2 +- .../org/jeasy/states/samples/turnstile/Lock.java | 10 ++++------ .../jeasy/states/samples/turnstile/Unlock.java | 9 ++++----- 10 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/jeasy/states/api/EventHandler.java b/src/main/java/org/jeasy/states/api/EventHandler.java index 8ea5de4..11a1321 100644 --- a/src/main/java/org/jeasy/states/api/EventHandler.java +++ b/src/main/java/org/jeasy/states/api/EventHandler.java @@ -28,13 +28,13 @@ * * @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com) */ -public interface EventHandler { +public interface EventHandler { /** * Action method to execute when an event occurs. * @param event the triggered event * @throws Exception thrown if a problem occurs during action performing */ - void handleEvent(AbstractEvent event) throws Exception; + void handleEvent(E event) throws Exception; } diff --git a/src/main/java/org/jeasy/states/api/FiniteStateMachine.java b/src/main/java/org/jeasy/states/api/FiniteStateMachine.java index 7eb6c24..56ee0c5 100644 --- a/src/main/java/org/jeasy/states/api/FiniteStateMachine.java +++ b/src/main/java/org/jeasy/states/api/FiniteStateMachine.java @@ -66,7 +66,7 @@ public interface FiniteStateMachine { * Return the last triggered event. * @return the last triggered event */ - AbstractEvent getLastEvent(); + Event getLastEvent(); /** * Return the last transition made. @@ -80,6 +80,6 @@ public interface FiniteStateMachine { * @return The next FSM state defined by the transition to make * @throws FiniteStateMachineException thrown if an exception occurs during event handling */ - State fire(AbstractEvent event) throws FiniteStateMachineException; + State fire(Event event) throws FiniteStateMachineException; } diff --git a/src/main/java/org/jeasy/states/api/FiniteStateMachineException.java b/src/main/java/org/jeasy/states/api/FiniteStateMachineException.java index a53d9fc..8ff0235 100644 --- a/src/main/java/org/jeasy/states/api/FiniteStateMachineException.java +++ b/src/main/java/org/jeasy/states/api/FiniteStateMachineException.java @@ -39,7 +39,7 @@ public class FiniteStateMachineException extends Exception { /** * The event triggered when the exception occurred. */ - private AbstractEvent event; + private Event event; /** * The root cause of the exception. @@ -53,7 +53,7 @@ public class FiniteStateMachineException extends Exception { * @param event triggered when the exception occurred * @param cause root cause of the exception */ - public FiniteStateMachineException(final Transition transition, final AbstractEvent event, final Throwable cause) { + public FiniteStateMachineException(final Transition transition, final Event event, final Throwable cause) { this.transition = transition; this.event = event; this.cause = cause; @@ -71,7 +71,7 @@ public Transition getTransition() { * Get the event triggered when the exception occurred. * @return the event triggered when the exception occurred. */ - public AbstractEvent getEvent() { + public Event getEvent() { return event; } diff --git a/src/main/java/org/jeasy/states/api/Transition.java b/src/main/java/org/jeasy/states/api/Transition.java index a8edb4e..512e9e7 100644 --- a/src/main/java/org/jeasy/states/api/Transition.java +++ b/src/main/java/org/jeasy/states/api/Transition.java @@ -53,7 +53,7 @@ public interface Transition { * Return fired event type upon which the transition should be made. * @return Event type class */ - Class getEventType(); + Class getEventType(); /** * Return event handler to execute when an event is fired. diff --git a/src/main/java/org/jeasy/states/core/FiniteStateMachineImpl.java b/src/main/java/org/jeasy/states/core/FiniteStateMachineImpl.java index 5884b8b..6a3f4c2 100644 --- a/src/main/java/org/jeasy/states/core/FiniteStateMachineImpl.java +++ b/src/main/java/org/jeasy/states/core/FiniteStateMachineImpl.java @@ -39,7 +39,7 @@ final class FiniteStateMachineImpl implements FiniteStateMachine { private Set finalStates; private Set states; private Set transitions; - private AbstractEvent lastEvent; + private Event lastEvent; private Transition lastTransition; FiniteStateMachineImpl(final Set states, final State initialState) { @@ -54,7 +54,8 @@ final class FiniteStateMachineImpl implements FiniteStateMachine { * {@inheritDoc} */ @Override - public final synchronized State fire(final AbstractEvent event) throws FiniteStateMachineException { + @SuppressWarnings("unchecked") + public final synchronized State fire(final Event event) throws FiniteStateMachineException { if (!finalStates.isEmpty() && finalStates.contains(currentState)) { LOGGER.log(Level.WARNING, "FSM is in final state '" + currentState.getName() + "', event " + event + " is ignored."); @@ -143,7 +144,7 @@ public Set getTransitions() { } @Override - public AbstractEvent getLastEvent() { + public Event getLastEvent() { return lastEvent; } diff --git a/src/main/java/org/jeasy/states/core/TransitionBuilder.java b/src/main/java/org/jeasy/states/core/TransitionBuilder.java index 05dfab2..e39eb7c 100644 --- a/src/main/java/org/jeasy/states/core/TransitionBuilder.java +++ b/src/main/java/org/jeasy/states/core/TransitionBuilder.java @@ -23,6 +23,7 @@ */ package org.jeasy.states.core; +import org.jeasy.states.api.Event; import org.jeasy.states.api.EventHandler; import org.jeasy.states.api.State; import org.jeasy.states.api.Transition; @@ -85,7 +86,7 @@ public TransitionBuilder targetState(final State targetState) { * @param eventType of the transition * @return FSM transition builder */ - public TransitionBuilder eventType(final Class eventType) { + public TransitionBuilder eventType(final Class eventType) { transition.setEventType(eventType); return this; } @@ -95,7 +96,7 @@ public TransitionBuilder eventType(final Class eventType) { * @param eventHandler of the transition * @return FSM transition builder */ - public TransitionBuilder eventHandler(final EventHandler eventHandler) { + public TransitionBuilder eventHandler(final EventHandler eventHandler) { transition.setEventHandler(eventHandler); return this; } diff --git a/src/main/java/org/jeasy/states/core/TransitionImpl.java b/src/main/java/org/jeasy/states/core/TransitionImpl.java index 999de4f..d80ccb2 100644 --- a/src/main/java/org/jeasy/states/core/TransitionImpl.java +++ b/src/main/java/org/jeasy/states/core/TransitionImpl.java @@ -23,18 +23,19 @@ */ package org.jeasy.states.core; +import org.jeasy.states.api.Event; import org.jeasy.states.api.EventHandler; import org.jeasy.states.api.State; import org.jeasy.states.api.Transition; import org.jeasy.states.util.Utils; -final class TransitionImpl implements Transition { +final class TransitionImpl implements Transition { private String name; private State sourceState; private State targetState; - private Class eventType; - private EventHandler eventHandler; + private Class eventType; + private EventHandler eventHandler; public TransitionImpl() { name = Utils.DEFAULT_TRANSITION_NAME; @@ -64,19 +65,19 @@ public void setName(String name) { this.name = name; } - public Class getEventType() { + public Class getEventType() { return eventType; } - public void setEventType(Class eventType) { + public void setEventType(Class eventType) { this.eventType = eventType; } - public EventHandler getEventHandler() { + public EventHandler getEventHandler() { return eventHandler; } - public void setEventHandler(EventHandler eventHandler) { + public void setEventHandler(EventHandler eventHandler) { this.eventHandler = eventHandler; } diff --git a/src/test/java/org/jeasy/states/core/FiniteStateMachineImplTest.java b/src/test/java/org/jeasy/states/core/FiniteStateMachineImplTest.java index b6891c3..1870267 100644 --- a/src/test/java/org/jeasy/states/core/FiniteStateMachineImplTest.java +++ b/src/test/java/org/jeasy/states/core/FiniteStateMachineImplTest.java @@ -46,7 +46,7 @@ public class FiniteStateMachineImplTest { private State s1, s2; @Mock - private EventHandler eventHandler; + private EventHandler eventHandler; private FiniteStateMachineImpl stateMachine; @Before diff --git a/src/test/java/org/jeasy/states/samples/turnstile/Lock.java b/src/test/java/org/jeasy/states/samples/turnstile/Lock.java index 5fd7958..015e098 100644 --- a/src/test/java/org/jeasy/states/samples/turnstile/Lock.java +++ b/src/test/java/org/jeasy/states/samples/turnstile/Lock.java @@ -23,17 +23,15 @@ */ package org.jeasy.states.samples.turnstile; -import org.jeasy.states.api.AbstractEvent; -import org.jeasy.states.api.EventHandler; - import java.util.Date; -class Lock implements EventHandler { +import org.jeasy.states.api.EventHandler; - public void handleEvent(AbstractEvent event) throws Exception { +class Lock implements EventHandler { + + public void handleEvent(PushEvent event) { System.out.println("Notified about event '" + event.getName() + "' triggered at " + new Date(event.getTimestamp())); System.out.println("Locking turnstile.."); - } } diff --git a/src/test/java/org/jeasy/states/samples/turnstile/Unlock.java b/src/test/java/org/jeasy/states/samples/turnstile/Unlock.java index 5889ddf..4250f99 100644 --- a/src/test/java/org/jeasy/states/samples/turnstile/Unlock.java +++ b/src/test/java/org/jeasy/states/samples/turnstile/Unlock.java @@ -23,14 +23,13 @@ */ package org.jeasy.states.samples.turnstile; -import org.jeasy.states.api.AbstractEvent; -import org.jeasy.states.api.EventHandler; - import java.util.Date; -class Unlock implements EventHandler { +import org.jeasy.states.api.EventHandler; + +class Unlock implements EventHandler { - public void handleEvent(AbstractEvent event) throws Exception { + public void handleEvent(CoinEvent event) { System.out.println("Notified about event '" + event.getName() + "' triggered at " + new Date(event.getTimestamp())); System.out.println("Unlocking turnstile.."); }