♻️ Move output generation to state
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / states / AbstractState.java
index 17cd518..081fbcc 100644 (file)
 
 package net.pterodactylus.rhynodge.states;
 
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import net.pterodactylus.rhynodge.Reaction;
 import net.pterodactylus.rhynodge.State;
+import net.pterodactylus.rhynodge.output.DefaultOutput;
+import net.pterodactylus.rhynodge.output.Output;
 
 import com.fasterxml.jackson.annotation.JsonProperty;
 import com.fasterxml.jackson.annotation.JsonTypeInfo;
+import com.google.common.escape.Escaper;
+import com.google.common.html.HtmlEscapers;
 
 /**
  * Abstract implementation of a {@link State} that knows about the basic
@@ -37,6 +45,7 @@ public abstract class AbstractState implements State {
 
        /** Whether the state was successfully retrieved. */
        private final boolean success;
+       private final boolean empty;
 
        /** The optional exception that occured while retrieving the state. */
        private final Throwable exception;
@@ -60,7 +69,11 @@ public abstract class AbstractState implements State {
         *            otherwise
         */
        protected AbstractState(boolean success) {
-               this(success, null);
+               this(success, true, null);
+       }
+
+       protected AbstractState(boolean success, boolean empty) {
+               this(success, empty, null);
        }
 
        /**
@@ -70,7 +83,7 @@ public abstract class AbstractState implements State {
         *            The exception that occured while retrieving the state
         */
        protected AbstractState(Throwable exception) {
-               this(false, exception);
+               this(false, true, exception);
        }
 
        /**
@@ -82,9 +95,10 @@ public abstract class AbstractState implements State {
         * @param exception
         *            The exception that occured while retrieving the state
         */
-       protected AbstractState(boolean success, Throwable exception) {
+       protected AbstractState(boolean success, boolean empty, Throwable exception) {
                this.time = System.currentTimeMillis();
                this.success = success;
+               this.empty = empty;
                this.exception = exception;
        }
 
@@ -108,6 +122,11 @@ public abstract class AbstractState implements State {
                return success;
        }
 
+       @Override
+       public boolean isEmpty() {
+               return empty;
+       }
+
        /**
         * {@inheritDoc}
         */
@@ -132,4 +151,29 @@ public abstract class AbstractState implements State {
                return exception;
        }
 
+       @Nonnull
+       @Override
+       public Output output(Reaction reaction) {
+               return new DefaultOutput(summary(reaction))
+                               .addText("text/plain", plainText())
+                               .addText("text/html", htmlText());
+       }
+
+       @Nonnull
+       protected String summary(Reaction reaction) {
+               return reaction.name();
+       }
+
+       @Nonnull
+       protected abstract String plainText();
+
+       @Nullable
+       protected String htmlText() {
+               //noinspection UnstableApiUsage
+               return "<div>" + htmlEscaper.escape(plainText()) + "</div>";
+       }
+
+       @SuppressWarnings("UnstableApiUsage")
+       private static final Escaper htmlEscaper = HtmlEscapers.htmlEscaper();
+
 }