Make the object that trigger an action available.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 2 Jan 2013 20:12:21 +0000 (21:12 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 2 Jan 2013 20:15:06 +0000 (21:15 +0100)
src/main/java/net/pterodactylus/reactor/Action.java
src/main/java/net/pterodactylus/reactor/Trigger.java
src/main/java/net/pterodactylus/reactor/actions/StandardOutAction.java
src/main/java/net/pterodactylus/reactor/engine/Engine.java
src/main/java/net/pterodactylus/reactor/triggers/AlwaysTrigger.java
src/main/java/net/pterodactylus/reactor/triggers/FileExistenceTrigger.java
src/main/java/net/pterodactylus/reactor/triggers/FileStateModifiedTrigger.java

index 5c6a13f..05b2c9e 100644 (file)
@@ -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);
 
 }
index c4e1aab..b2eab0d 100644 (file)
@@ -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();
+
 }
index 992be1f..91fe7da 100644 (file)
@@ -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));
        }
 
 }
index 41aedb7..a8c9f4e 100644 (file)
@@ -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());
                        }
+
                }
        }
 
index 7f5885a..0ca7704 100644 (file)
@@ -35,4 +35,12 @@ public class AlwaysTrigger implements Trigger {
                return true;
        }
 
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public Object trigger() {
+               return true;
+       }
+
 }
index 33f1993..5864d79 100644 (file)
@@ -44,4 +44,12 @@ public class FileExistenceTrigger implements Trigger {
                return ((FileState) previousState).exists() != ((FileState) currentState).exists();
        }
 
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public Object trigger() {
+               return null;
+       }
+
 }
index 9263a5f..0226734 100644 (file)
@@ -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;
+       }
+
 }