From da24ae72789e842a3dc66444a69f3ff547e83a00 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 2 Jan 2013 21:12:21 +0100 Subject: [PATCH] Make the object that trigger an action available. --- src/main/java/net/pterodactylus/reactor/Action.java | 8 +++----- src/main/java/net/pterodactylus/reactor/Trigger.java | 9 +++++++++ .../net/pterodactylus/reactor/actions/StandardOutAction.java | 4 ++-- src/main/java/net/pterodactylus/reactor/engine/Engine.java | 5 +++-- .../java/net/pterodactylus/reactor/triggers/AlwaysTrigger.java | 8 ++++++++ .../net/pterodactylus/reactor/triggers/FileExistenceTrigger.java | 8 ++++++++ .../pterodactylus/reactor/triggers/FileStateModifiedTrigger.java | 8 ++++++++ 7 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/pterodactylus/reactor/Action.java b/src/main/java/net/pterodactylus/reactor/Action.java index 5c6a13f..05b2c9e 100644 --- a/src/main/java/net/pterodactylus/reactor/Action.java +++ b/src/main/java/net/pterodactylus/reactor/Action.java @@ -28,11 +28,9 @@ public interface Action { /** * Performs the action. * - * @param currentState - * The current state of a system - * @param previousState - * The previous state of the system + * @param trigger + * The object that triggered the action */ - void execute(State currentState, State previousState); + void execute(Object trigger); } diff --git a/src/main/java/net/pterodactylus/reactor/Trigger.java b/src/main/java/net/pterodactylus/reactor/Trigger.java index c4e1aab..b2eab0d 100644 --- a/src/main/java/net/pterodactylus/reactor/Trigger.java +++ b/src/main/java/net/pterodactylus/reactor/Trigger.java @@ -41,4 +41,13 @@ public interface Trigger { */ boolean triggers(State currentState, State previousState); + /** + * Returns the object (or objects) that triggered a change. This method will + * only return a meaningful value when {@link #triggers(State, State)} + * returns {@code true}. + * + * @return The object that triggered a change + */ + Object trigger(); + } diff --git a/src/main/java/net/pterodactylus/reactor/actions/StandardOutAction.java b/src/main/java/net/pterodactylus/reactor/actions/StandardOutAction.java index 992be1f..91fe7da 100644 --- a/src/main/java/net/pterodactylus/reactor/actions/StandardOutAction.java +++ b/src/main/java/net/pterodactylus/reactor/actions/StandardOutAction.java @@ -31,8 +31,8 @@ public class StandardOutAction implements Action { * {@inheritDoc} */ @Override - public void execute(State currentState, State previousState) { - System.out.println(String.format("currentState: %s, previousState: %s", currentState, previousState)); + public void execute(Object trigger) { + System.out.println(String.format("Triggered by %s.", trigger)); } } diff --git a/src/main/java/net/pterodactylus/reactor/engine/Engine.java b/src/main/java/net/pterodactylus/reactor/engine/Engine.java index 41aedb7..a8c9f4e 100644 --- a/src/main/java/net/pterodactylus/reactor/engine/Engine.java +++ b/src/main/java/net/pterodactylus/reactor/engine/Engine.java @@ -140,9 +140,9 @@ public class Engine extends AbstractExecutionThreadService { reactionExecution.addState(state); /* only run trigger if we have collected two states. */ + Trigger trigger = nextReaction.trigger(); boolean triggerHit = false; if (reactionExecution.previousState() != null) { - Trigger trigger = nextReaction.trigger(); logger.debug("Checking Trigger for changes..."); triggerHit = trigger.triggers(reactionExecution.currentState(), reactionExecution.previousState()); } @@ -151,8 +151,9 @@ public class Engine extends AbstractExecutionThreadService { logger.debug(String.format("Trigger was hit: %s.", triggerHit)); if (triggerHit) { logger.info("Executing Action..."); - nextReaction.action().execute(reactionExecution.currentState(), reactionExecution.previousState()); + nextReaction.action().execute(trigger.trigger()); } + } } diff --git a/src/main/java/net/pterodactylus/reactor/triggers/AlwaysTrigger.java b/src/main/java/net/pterodactylus/reactor/triggers/AlwaysTrigger.java index 7f5885a..0ca7704 100644 --- a/src/main/java/net/pterodactylus/reactor/triggers/AlwaysTrigger.java +++ b/src/main/java/net/pterodactylus/reactor/triggers/AlwaysTrigger.java @@ -35,4 +35,12 @@ public class AlwaysTrigger implements Trigger { return true; } + /** + * {@inheritDoc} + */ + @Override + public Object trigger() { + return true; + } + } diff --git a/src/main/java/net/pterodactylus/reactor/triggers/FileExistenceTrigger.java b/src/main/java/net/pterodactylus/reactor/triggers/FileExistenceTrigger.java index 33f1993..5864d79 100644 --- a/src/main/java/net/pterodactylus/reactor/triggers/FileExistenceTrigger.java +++ b/src/main/java/net/pterodactylus/reactor/triggers/FileExistenceTrigger.java @@ -44,4 +44,12 @@ public class FileExistenceTrigger implements Trigger { return ((FileState) previousState).exists() != ((FileState) currentState).exists(); } + /** + * {@inheritDoc} + */ + @Override + public Object trigger() { + return null; + } + } diff --git a/src/main/java/net/pterodactylus/reactor/triggers/FileStateModifiedTrigger.java b/src/main/java/net/pterodactylus/reactor/triggers/FileStateModifiedTrigger.java index 9263a5f..0226734 100644 --- a/src/main/java/net/pterodactylus/reactor/triggers/FileStateModifiedTrigger.java +++ b/src/main/java/net/pterodactylus/reactor/triggers/FileStateModifiedTrigger.java @@ -42,4 +42,12 @@ public class FileStateModifiedTrigger implements Trigger { return (currentFileState.exists() != previousFileState.exists()) || (currentFileState.size() != previousFileState.size()) || (currentFileState.modificationTime() != previousFileState.modificationTime()); } + /** + * {@inheritDoc} + */ + @Override + public Object trigger() { + return null; + } + } -- 2.7.4