From 9cd054fb2fec1c2a1f0f24b3b88f477720563094 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 3 Jan 2013 16:38:33 +0100 Subject: [PATCH] Return an output instead of a trigger object. --- src/main/java/net/pterodactylus/reactor/Action.java | 8 +++++--- src/main/java/net/pterodactylus/reactor/Trigger.java | 10 +++++----- .../java/net/pterodactylus/reactor/actions/EmailAction.java | 3 ++- .../net/pterodactylus/reactor/actions/StandardOutAction.java | 5 +++-- src/main/java/net/pterodactylus/reactor/engine/Engine.java | 2 +- .../java/net/pterodactylus/reactor/triggers/AlwaysTrigger.java | 6 ++++-- .../pterodactylus/reactor/triggers/FileExistenceTrigger.java | 6 ++++-- .../reactor/triggers/FileStateModifiedTrigger.java | 6 ++++-- .../net/pterodactylus/reactor/triggers/NewTorrentTrigger.java | 5 +++-- 9 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/main/java/net/pterodactylus/reactor/Action.java b/src/main/java/net/pterodactylus/reactor/Action.java index 05b2c9e..650c3ad 100644 --- a/src/main/java/net/pterodactylus/reactor/Action.java +++ b/src/main/java/net/pterodactylus/reactor/Action.java @@ -17,6 +17,8 @@ package net.pterodactylus.reactor; +import net.pterodactylus.reactor.output.Output; + /** * An action is performed when a {@link Trigger} determines that two given * {@link State}s of a {@link Query} signify a change. @@ -28,9 +30,9 @@ public interface Action { /** * Performs the action. * - * @param trigger - * The object that triggered the action + * @param output + * The output for the action */ - void execute(Object trigger); + void execute(Output output); } diff --git a/src/main/java/net/pterodactylus/reactor/Trigger.java b/src/main/java/net/pterodactylus/reactor/Trigger.java index b2eab0d..fbd9d07 100644 --- a/src/main/java/net/pterodactylus/reactor/Trigger.java +++ b/src/main/java/net/pterodactylus/reactor/Trigger.java @@ -17,6 +17,7 @@ package net.pterodactylus.reactor; +import net.pterodactylus.reactor.output.Output; import net.pterodactylus.reactor.states.FileState; /** @@ -42,12 +43,11 @@ 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}. + * Returns the output of this trigger. This will only return a meaningful + * value if {@link #triggers(State, State)} returns {@code true}. * - * @return The object that triggered a change + * @return The output of this trigger */ - Object trigger(); + Output output(); } diff --git a/src/main/java/net/pterodactylus/reactor/actions/EmailAction.java b/src/main/java/net/pterodactylus/reactor/actions/EmailAction.java index 64673e4..ef9d602 100644 --- a/src/main/java/net/pterodactylus/reactor/actions/EmailAction.java +++ b/src/main/java/net/pterodactylus/reactor/actions/EmailAction.java @@ -29,6 +29,7 @@ import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import net.pterodactylus.reactor.Action; +import net.pterodactylus.reactor.output.Output; /** * {@link Action} implementation that sends an email containing the triggering @@ -71,7 +72,7 @@ public class EmailAction implements Action { * {@inheritDoc} */ @Override - public void execute(Object trigger) { + public void execute(Output output) { Properties properties = System.getProperties(); properties.put("mail.smtp.host", hostname); Session session = Session.getInstance(properties); diff --git a/src/main/java/net/pterodactylus/reactor/actions/StandardOutAction.java b/src/main/java/net/pterodactylus/reactor/actions/StandardOutAction.java index 91fe7da..8cad092 100644 --- a/src/main/java/net/pterodactylus/reactor/actions/StandardOutAction.java +++ b/src/main/java/net/pterodactylus/reactor/actions/StandardOutAction.java @@ -19,6 +19,7 @@ package net.pterodactylus.reactor.actions; import net.pterodactylus.reactor.Action; import net.pterodactylus.reactor.State; +import net.pterodactylus.reactor.output.Output; /** * {@link Action} that simply dumps all {@link State}s to standard output. @@ -31,8 +32,8 @@ public class StandardOutAction implements Action { * {@inheritDoc} */ @Override - public void execute(Object trigger) { - System.out.println(String.format("Triggered by %s.", trigger)); + public void execute(Output output) { + System.out.println(String.format("Triggered by %s.", output.text("text/plain", -1))); } } diff --git a/src/main/java/net/pterodactylus/reactor/engine/Engine.java b/src/main/java/net/pterodactylus/reactor/engine/Engine.java index 4ba7887..9edbdb4 100644 --- a/src/main/java/net/pterodactylus/reactor/engine/Engine.java +++ b/src/main/java/net/pterodactylus/reactor/engine/Engine.java @@ -159,7 +159,7 @@ public class Engine extends AbstractExecutionThreadService { logger.debug(String.format("Trigger was hit: %s.", triggerHit)); if (triggerHit) { logger.info("Executing Action..."); - nextReaction.action().execute(trigger.trigger()); + nextReaction.action().execute(trigger.output()); } } diff --git a/src/main/java/net/pterodactylus/reactor/triggers/AlwaysTrigger.java b/src/main/java/net/pterodactylus/reactor/triggers/AlwaysTrigger.java index 0ca7704..928d213 100644 --- a/src/main/java/net/pterodactylus/reactor/triggers/AlwaysTrigger.java +++ b/src/main/java/net/pterodactylus/reactor/triggers/AlwaysTrigger.java @@ -19,6 +19,8 @@ package net.pterodactylus.reactor.triggers; import net.pterodactylus.reactor.State; import net.pterodactylus.reactor.Trigger; +import net.pterodactylus.reactor.output.DefaultOutput; +import net.pterodactylus.reactor.output.Output; /** * {@link Trigger} implementation that always triggers. @@ -39,8 +41,8 @@ public class AlwaysTrigger implements Trigger { * {@inheritDoc} */ @Override - public Object trigger() { - return true; + public Output output() { + return new DefaultOutput("true").addText("text/plain", "true").addText("text/html", "
true
"); } } diff --git a/src/main/java/net/pterodactylus/reactor/triggers/FileExistenceTrigger.java b/src/main/java/net/pterodactylus/reactor/triggers/FileExistenceTrigger.java index 5864d79..f3464e9 100644 --- a/src/main/java/net/pterodactylus/reactor/triggers/FileExistenceTrigger.java +++ b/src/main/java/net/pterodactylus/reactor/triggers/FileExistenceTrigger.java @@ -19,6 +19,8 @@ package net.pterodactylus.reactor.triggers; import net.pterodactylus.reactor.State; import net.pterodactylus.reactor.Trigger; +import net.pterodactylus.reactor.output.DefaultOutput; +import net.pterodactylus.reactor.output.Output; import net.pterodactylus.reactor.states.FileState; import com.google.common.base.Preconditions; @@ -48,8 +50,8 @@ public class FileExistenceTrigger implements Trigger { * {@inheritDoc} */ @Override - public Object trigger() { - return null; + public Output output() { + return new DefaultOutput("File appeared/disappeared").addText("text/plain", "File appeared/disappeared").addText("text/html", "
File appeared/disappeared
"); } } diff --git a/src/main/java/net/pterodactylus/reactor/triggers/FileStateModifiedTrigger.java b/src/main/java/net/pterodactylus/reactor/triggers/FileStateModifiedTrigger.java index 0226734..a598a53 100644 --- a/src/main/java/net/pterodactylus/reactor/triggers/FileStateModifiedTrigger.java +++ b/src/main/java/net/pterodactylus/reactor/triggers/FileStateModifiedTrigger.java @@ -20,6 +20,8 @@ package net.pterodactylus.reactor.triggers; import static com.google.common.base.Preconditions.checkState; import net.pterodactylus.reactor.State; import net.pterodactylus.reactor.Trigger; +import net.pterodactylus.reactor.output.DefaultOutput; +import net.pterodactylus.reactor.output.Output; import net.pterodactylus.reactor.states.FileState; /** @@ -46,8 +48,8 @@ public class FileStateModifiedTrigger implements Trigger { * {@inheritDoc} */ @Override - public Object trigger() { - return null; + public Output output() { + return new DefaultOutput("File modified").addText("text/plain", "File modified").addText("text/html", "
File modified
"); } } diff --git a/src/main/java/net/pterodactylus/reactor/triggers/NewTorrentTrigger.java b/src/main/java/net/pterodactylus/reactor/triggers/NewTorrentTrigger.java index 1eb2247..f655ff0 100644 --- a/src/main/java/net/pterodactylus/reactor/triggers/NewTorrentTrigger.java +++ b/src/main/java/net/pterodactylus/reactor/triggers/NewTorrentTrigger.java @@ -62,8 +62,9 @@ public class NewTorrentTrigger implements Trigger { * {@inheritDoc} */ @Override - public Object trigger() { - return torrentFiles; + public Output output() { + DefaultOutput output = new DefaultOutput(String.format("Found %d new Torrent(s)!", torrentFiles.size())); + return output; } } -- 2.7.4