2 * Rhynodge - AbstractState.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.rhynodge.states;
20 import javax.annotation.Nonnull;
21 import javax.annotation.Nullable;
23 import net.pterodactylus.rhynodge.Reaction;
24 import net.pterodactylus.rhynodge.State;
25 import net.pterodactylus.rhynodge.output.DefaultOutput;
26 import net.pterodactylus.rhynodge.output.Output;
28 import com.fasterxml.jackson.annotation.JsonProperty;
29 import com.fasterxml.jackson.annotation.JsonTypeInfo;
30 import com.google.common.escape.Escaper;
31 import com.google.common.html.HtmlEscapers;
34 * Abstract implementation of a {@link State} that knows about the basic
35 * attributes of a {@link State}.
37 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39 @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
40 public abstract class AbstractState implements State {
42 /** The time of this state. */
44 private final long time;
46 /** Whether the state was successfully retrieved. */
47 private final boolean success;
48 private final boolean empty;
50 /** The optional exception that occured while retrieving the state. */
51 private final Throwable exception;
53 /** The number of consecutive failures. */
55 private int failCount;
58 * Creates a new successful state.
60 protected AbstractState() {
65 * Creates a new state.
68 * {@code true} if the state is successful, {@code false}
71 protected AbstractState(boolean success) {
72 this(success, true, null);
75 protected AbstractState(boolean success, boolean empty) {
76 this(success, empty, null);
80 * Creates a new non-successful state with the given exception.
83 * The exception that occured while retrieving the state
85 protected AbstractState(Throwable exception) {
86 this(false, true, exception);
90 * Creates a new state.
93 * {@code true} if the state is successful, {@code false}
96 * The exception that occured while retrieving the state
98 protected AbstractState(boolean success, boolean empty, Throwable exception) {
99 this.time = System.currentTimeMillis();
100 this.success = success;
102 this.exception = exception;
121 public boolean success() {
126 public boolean isEmpty() {
134 public int failCount() {
142 public void setFailCount(int failCount) {
143 this.failCount = failCount;
150 public Throwable exception() {
156 public Output output(Reaction reaction) {
157 return new DefaultOutput(summary(reaction))
158 .addText("text/plain", plainText())
159 .addText("text/html", htmlText());
163 protected String summary(Reaction reaction) {
164 return reaction.name();
168 protected abstract String plainText();
171 protected String htmlText() {
172 //noinspection UnstableApiUsage
173 return "<div>" + htmlEscaper.escape(plainText()) + "</div>";
176 @SuppressWarnings("UnstableApiUsage")
177 private static final Escaper htmlEscaper = HtmlEscapers.htmlEscaper();