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;
52 private boolean triggered = false;
54 /** The optional exception that occured while retrieving the state. */
55 private final Throwable exception;
57 /** The number of consecutive failures. */
59 private int failCount;
62 * Creates a new successful state.
64 protected AbstractState() {
69 * Creates a new state.
72 * {@code true} if the state is successful, {@code false}
75 protected AbstractState(boolean success) {
76 this(Clock.systemUTC(), success);
80 * Creates a new state.
82 * @param clock The clock for generating {@link #time}
83 * @param success {@code true} if the state is successful, {@code false}
86 protected AbstractState(Clock clock, boolean success) {
87 this(clock, success, true, null);
90 protected AbstractState(boolean success, boolean empty) {
91 this(Clock.systemUTC(), success, empty);
94 protected AbstractState(Clock clock, boolean success, boolean empty) {
95 this(clock, success, empty, null);
99 * Creates a new non-successful state with the given exception.
102 * The exception that occured while retrieving the state
104 protected AbstractState(Throwable exception) {
105 this(Clock.systemUTC(), exception);
109 * Creates a new non-successful state with the given exception.
111 * @param clock The clock for generating {@link #time}
112 * @param exception The exception that occured while retrieving the state
114 protected AbstractState(Clock clock, Throwable exception) {
115 this(clock, false, true, exception);
119 * Creates a new state.
122 * {@code true} if the state is successful, {@code false}
125 * The exception that occured while retrieving the state
127 protected AbstractState(boolean success, boolean empty, Throwable exception) {
128 this(Clock.systemUTC(), success, empty, exception);
132 * Creates a new state.
134 * @param clock The clock for generating {@link #time}
135 * @param success {@code true} if the state is successful, {@code false}
137 * @param exception The exception that occured while retrieving the state
139 protected AbstractState(Clock clock, boolean success, boolean empty, Throwable exception) {
140 this.time = clock.millis();
141 this.success = success;
143 this.exception = exception;
162 public boolean success() {
167 public boolean isEmpty() {
172 public boolean triggered() {
177 public void trigger() {
185 public int failCount() {
193 public void setFailCount(int failCount) {
194 this.failCount = failCount;
201 public Throwable exception() {
207 public Output output(Reaction reaction) {
208 return new DefaultOutput(summary(reaction))
209 .addText("text/plain", plainText())
210 .addText("text/html", htmlText());
214 protected String summary(Reaction reaction) {
215 return reaction.name();
219 protected abstract String plainText();
222 protected String htmlText() {
223 //noinspection UnstableApiUsage
224 return "<div>" + htmlEscaper.escape(plainText()) + "</div>";
227 @SuppressWarnings("UnstableApiUsage")
228 private static final Escaper htmlEscaper = HtmlEscapers.htmlEscaper();
231 public int hashCode() {
232 return Objects.hash(success, empty, time, failCount, exception);
236 public boolean equals(Object object) {
237 if (!(object instanceof AbstractState)) {
240 AbstractState abstractState = (AbstractState) object;
241 return (success == abstractState.success) && (empty == abstractState.empty) && (time == abstractState.time) && (failCount == abstractState.failCount) && Objects.equals(exception, abstractState.exception);