Skip to content
This repository has been archived by the owner on Nov 18, 2020. It is now read-only.

Commit

Permalink
Change EventHandler to be generic
Browse files Browse the repository at this point in the history
  • Loading branch information
fmbenhassine committed Mar 4, 2020
1 parent 6d99172 commit 6b72eff
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/jeasy/states/api/EventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
*
* @author Mahmoud Ben Hassine ([email protected])
*/
public interface EventHandler {
public interface EventHandler<E extends Event> {

/**
* 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;

}
4 changes: 2 additions & 2 deletions src/main/java/org/jeasy/states/api/FiniteStateMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jeasy/states/api/Transition.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends Event> getEventType();

/**
* Return event handler to execute when an event is fired.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class FiniteStateMachineImpl implements FiniteStateMachine {
private Set<State> finalStates;
private Set<State> states;
private Set<Transition> transitions;
private AbstractEvent lastEvent;
private Event lastEvent;
private Transition lastTransition;

FiniteStateMachineImpl(final Set<State> states, final State initialState) {
Expand All @@ -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.");
Expand Down Expand Up @@ -143,7 +144,7 @@ public Set<Transition> getTransitions() {
}

@Override
public AbstractEvent getLastEvent() {
public Event getLastEvent() {
return lastEvent;
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/jeasy/states/core/TransitionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<? extends Event> eventType) {
transition.setEventType(eventType);
return this;
}
Expand All @@ -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 <E extends Event> TransitionBuilder eventHandler(final EventHandler<E> eventHandler) {
transition.setEventHandler(eventHandler);
return this;
}
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/org/jeasy/states/core/TransitionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<E extends Event> implements Transition {

private String name;
private State sourceState;
private State targetState;
private Class<?> eventType;
private EventHandler eventHandler;
private Class<E> eventType;
private EventHandler<E> eventHandler;

public TransitionImpl() {
name = Utils.DEFAULT_TRANSITION_NAME;
Expand Down Expand Up @@ -64,19 +65,19 @@ public void setName(String name) {
this.name = name;
}

public Class<?> getEventType() {
public Class<E> getEventType() {
return eventType;
}

public void setEventType(Class<?> eventType) {
public void setEventType(Class<E> eventType) {
this.eventType = eventType;
}

public EventHandler getEventHandler() {
public EventHandler<? extends Event> getEventHandler() {
return eventHandler;
}

public void setEventHandler(EventHandler eventHandler) {
public void setEventHandler(EventHandler<E> eventHandler) {
this.eventHandler = eventHandler;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class FiniteStateMachineImplTest {

private State s1, s2;
@Mock
private EventHandler eventHandler;
private EventHandler<MoveEvent> eventHandler;
private FiniteStateMachineImpl stateMachine;

@Before
Expand Down
10 changes: 4 additions & 6 deletions src/test/java/org/jeasy/states/samples/turnstile/Lock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<PushEvent> {

public void handleEvent(PushEvent event) {
System.out.println("Notified about event '" + event.getName() + "' triggered at " + new Date(event.getTimestamp()));
System.out.println("Locking turnstile..");

}

}
9 changes: 4 additions & 5 deletions src/test/java/org/jeasy/states/samples/turnstile/Unlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<CoinEvent> {

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..");
}
Expand Down

0 comments on commit 6b72eff

Please sign in to comment.