Output the current state if it’s an output state.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 31 Jul 2014 11:13:58 +0000 (13:13 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 31 Jul 2014 11:14:41 +0000 (13:14 +0200)
src/main/java/net/pterodactylus/rhynodge/states/OutputState.java [new file with mode: 0644]
src/main/java/net/pterodactylus/rhynodge/triggers/AlwaysTrigger.java

diff --git a/src/main/java/net/pterodactylus/rhynodge/states/OutputState.java b/src/main/java/net/pterodactylus/rhynodge/states/OutputState.java
new file mode 100644 (file)
index 0000000..68a951d
--- /dev/null
@@ -0,0 +1,37 @@
+package net.pterodactylus.rhynodge.states;
+
+import java.util.Optional;
+
+import net.pterodactylus.rhynodge.State;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+/**
+ * {@link State} implementation that can expose itself as plain text and/or
+ * HTML.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class OutputState extends AbstractState {
+
+       private final Optional<String> plainTextOutput;
+       private final Optional<String> htmlOutput;
+
+       public OutputState() {
+               this(Optional.<String>empty(), Optional.<String>empty());
+       }
+
+       public OutputState(Optional<String> plainTextOutput, Optional<String> htmlOutput) {
+               this.plainTextOutput = plainTextOutput;
+               this.htmlOutput = htmlOutput;
+       }
+
+       public Optional<String> plainTextOutput() {
+               return plainTextOutput;
+       }
+
+       public Optional<String> htmlOutput() {
+               return htmlOutput;
+       }
+
+}
index b9ec39a..b916237 100644 (file)
@@ -22,6 +22,7 @@ import net.pterodactylus.rhynodge.State;
 import net.pterodactylus.rhynodge.Trigger;
 import net.pterodactylus.rhynodge.output.DefaultOutput;
 import net.pterodactylus.rhynodge.output.Output;
+import net.pterodactylus.rhynodge.states.OutputState;
 
 /**
  * {@link Trigger} implementation that always triggers.
@@ -58,7 +59,18 @@ public class AlwaysTrigger implements Trigger {
         */
        @Override
        public Output output(Reaction reaction) {
-               return new DefaultOutput("true").addText("text/plain", "true").addText("text/html", "<div>true</div>");
+               DefaultOutput output = new DefaultOutput(reaction.name());
+               if (currentState instanceof OutputState) {
+                       OutputState outputState = (OutputState) currentState;
+                       if (outputState.plainTextOutput().isPresent()) {
+                               output = output.addText("text/plain", outputState.plainTextOutput().get());
+                       }
+                       if (outputState.htmlOutput().isPresent()) {
+                               output = output.addText("text/html", outputState.htmlOutput().get());
+                       }
+                       return output;
+               }
+               return output.addText("text/plain", "true").addText("text/html", "<div>true</div>");
        }
 
 }