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 java.time.Clock;
21 import java.util.Objects;
22 import javax.annotation.Nonnull;
23 import javax.annotation.Nullable;
25 import net.pterodactylus.rhynodge.Reaction;
26 import net.pterodactylus.rhynodge.State;
27 import net.pterodactylus.rhynodge.output.DefaultOutput;
28 import net.pterodactylus.rhynodge.output.Output;
30 import com.fasterxml.jackson.annotation.JsonProperty;
31 import com.fasterxml.jackson.annotation.JsonTypeInfo;
32 import com.google.common.escape.Escaper;
33 import com.google.common.html.HtmlEscapers;
36 * Abstract implementation of a {@link State} that knows about the basic
37 * attributes of a {@link State}.
39 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41 @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
42 public abstract class AbstractState implements State {
44 /** The time of this state. */
46 private final long time;
48 /** Whether the state was successfully retrieved. */
49 private final boolean success;
50 private final boolean empty;
52 /** The optional exception that occured while retrieving the state. */
53 private final Throwable exception;
55 /** The number of consecutive failures. */
57 private int failCount;
60 * Creates a new successful state.
62 protected AbstractState() {
67 * Creates a new state.
70 * {@code true} if the state is successful, {@code false}
73 protected AbstractState(boolean success) {
74 this(Clock.systemUTC(), success);
78 * Creates a new state.
80 * @param clock The clock for generating {@link #time}
81 * @param success {@code true} if the state is successful, {@code false}
84 protected AbstractState(Clock clock, boolean success) {
85 this(clock, success, true, null);
88 protected AbstractState(boolean success, boolean empty) {
89 this(Clock.systemUTC(), success, empty);
92 protected AbstractState(Clock clock, boolean success, boolean empty) {
93 this(clock, success, empty, null);
97 * Creates a new non-successful state with the given exception.
100 * The exception that occured while retrieving the state
102 protected AbstractState(Throwable exception) {
103 this(Clock.systemUTC(), exception);
107 * Creates a new non-successful state with the given exception.
109 * @param clock The clock for generating {@link #time}
110 * @param exception The exception that occured while retrieving the state
112 protected AbstractState(Clock clock, Throwable exception) {
113 this(clock, false, true, exception);
117 * Creates a new state.
120 * {@code true} if the state is successful, {@code false}
123 * The exception that occured while retrieving the state
125 protected AbstractState(boolean success, boolean empty, Throwable exception) {
126 this(Clock.systemUTC(), success, empty, exception);
130 * Creates a new state.
132 * @param clock The clock for generating {@link #time}
133 * @param success {@code true} if the state is successful, {@code false}
135 * @param exception The exception that occured while retrieving the state
137 protected AbstractState(Clock clock, boolean success, boolean empty, Throwable exception) {
138 this.time = clock.millis();
139 this.success = success;
141 this.exception = exception;
160 public boolean success() {
165 public boolean isEmpty() {
173 public int failCount() {
181 public void setFailCount(int failCount) {
182 this.failCount = failCount;
189 public Throwable exception() {
195 public Output output(Reaction reaction) {
196 return new DefaultOutput(summary(reaction))
197 .addText("text/plain", plainText())
198 .addText("text/html", htmlText());
202 protected String summary(Reaction reaction) {
203 return reaction.name();
207 protected abstract String plainText();
210 protected String htmlText() {
211 //noinspection UnstableApiUsage
212 return "<div>" + htmlEscaper.escape(plainText()) + "</div>";
215 @SuppressWarnings("UnstableApiUsage")
216 private static final Escaper htmlEscaper = HtmlEscapers.htmlEscaper();
219 public int hashCode() {
220 return Objects.hash(success, empty, time, failCount, exception);
224 public boolean equals(Object object) {
225 if (!(object instanceof AbstractState)) {
228 AbstractState abstractState = (AbstractState) object;
229 return (success == abstractState.success) && (empty == abstractState.empty) && (time == abstractState.time) && (failCount == abstractState.failCount) && Objects.equals(exception, abstractState.exception);