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. */
50 private final boolean success;
51 private final boolean empty;
53 /** The optional exception that occured while retrieving the state. */
54 private final Throwable exception;
56 /** The number of consecutive failures. */
58 private int failCount;
61 * Creates a new successful state.
63 protected AbstractState() {
68 * Creates a new state.
71 * {@code true} if the state is successful, {@code false}
74 protected AbstractState(boolean success) {
75 this(Clock.systemUTC(), success);
79 * Creates a new state.
81 * @param clock The clock for generating {@link #time}
82 * @param success {@code true} if the state is successful, {@code false}
85 protected AbstractState(Clock clock, boolean success) {
86 this(clock, success, true, null);
89 protected AbstractState(boolean success, boolean empty) {
90 this(Clock.systemUTC(), success, empty);
93 protected AbstractState(Clock clock, boolean success, boolean empty) {
94 this(clock, success, empty, null);
98 * Creates a new non-successful state with the given exception.
101 * The exception that occured while retrieving the state
103 protected AbstractState(Throwable exception) {
104 this(Clock.systemUTC(), exception);
108 * Creates a new non-successful state with the given exception.
110 * @param clock The clock for generating {@link #time}
111 * @param exception The exception that occured while retrieving the state
113 protected AbstractState(Clock clock, Throwable exception) {
114 this(clock, false, true, exception);
118 * Creates a new state.
121 * {@code true} if the state is successful, {@code false}
124 * The exception that occured while retrieving the state
126 protected AbstractState(boolean success, boolean empty, Throwable exception) {
127 this(Clock.systemUTC(), success, empty, exception);
131 * Creates a new state.
133 * @param clock The clock for generating {@link #time}
134 * @param success {@code true} if the state is successful, {@code false}
136 * @param exception The exception that occured while retrieving the state
138 protected AbstractState(Clock clock, boolean success, boolean empty, Throwable exception) {
139 this.time = clock.millis();
140 this.success = success;
142 this.exception = exception;
161 public boolean success() {
166 public boolean isEmpty() {
174 public int failCount() {
182 public void setFailCount(int failCount) {
183 this.failCount = failCount;
190 public Throwable exception() {
196 public Output output(Reaction reaction) {
197 return new DefaultOutput(summary(reaction))
198 .addText("text/plain", plainText())
199 .addText("text/html", htmlText());
203 protected String summary(Reaction reaction) {
204 return reaction.name();
208 protected abstract String plainText();
211 protected String htmlText() {
212 //noinspection UnstableApiUsage
213 return "<div>" + htmlEscaper.escape(plainText()) + "</div>";
216 @SuppressWarnings("UnstableApiUsage")
217 private static final Escaper htmlEscaper = HtmlEscapers.htmlEscaper();
220 public int hashCode() {
221 return Objects.hash(success, empty, time, failCount, exception);
225 public boolean equals(Object object) {
226 if (!(object instanceof AbstractState)) {
229 AbstractState abstractState = (AbstractState) object;
230 return (success == abstractState.success) && (empty == abstractState.empty) && (time == abstractState.time) && (failCount == abstractState.failCount) && Objects.equals(exception, abstractState.exception);