🔀 Merge branch 'website/epic-games' into next
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / states / AbstractState.java
1 /*
2  * Rhynodge - AbstractState.java - Copyright Â© 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.rhynodge.states;
19
20 import java.time.Clock;
21 import java.util.Objects;
22 import javax.annotation.Nonnull;
23 import javax.annotation.Nullable;
24
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;
29
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;
34
35 /**
36  * Abstract implementation of a {@link State} that knows about the basic
37  * attributes of a {@link State}.
38  *
39  * @author <a href="mailto:bombe@pterodactylus.net">David â€˜Bombe’ Roden</a>
40  */
41 @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
42 public abstract class AbstractState implements State {
43
44         /** The time of this state. */
45         @JsonProperty
46         private final long time;
47
48         /** Whether the state was successfully retrieved. */
49         @JsonProperty
50         private final boolean success;
51         private final boolean empty;
52         private boolean triggered = false;
53
54         /** The optional exception that occured while retrieving the state. */
55         private final Throwable exception;
56
57         /** The number of consecutive failures. */
58         @JsonProperty
59         private int failCount;
60
61         /**
62          * Creates a new successful state.
63          */
64         protected AbstractState() {
65                 this(true);
66         }
67
68         /**
69          * Creates a new state.
70          *
71          * @param success
72          *            {@code true} if the state is successful, {@code false}
73          *            otherwise
74          */
75         protected AbstractState(boolean success) {
76                 this(Clock.systemUTC(), success);
77         }
78
79         /**
80          * Creates a new state.
81          *
82          * @param clock The clock for generating {@link #time}
83          * @param success {@code true} if the state is successful, {@code false}
84          *              otherwise
85          */
86         protected AbstractState(Clock clock, boolean success) {
87                 this(clock, success, true, null);
88         }
89
90         protected AbstractState(boolean success, boolean empty) {
91                 this(Clock.systemUTC(), success, empty);
92         }
93
94         protected AbstractState(Clock clock, boolean success, boolean empty) {
95                 this(clock, success, empty, null);
96         }
97
98         /**
99          * Creates a new non-successful state with the given exception.
100          *
101          * @param exception
102          *            The exception that occured while retrieving the state
103          */
104         protected AbstractState(Throwable exception) {
105                 this(Clock.systemUTC(), exception);
106         }
107
108         /**
109          * Creates a new non-successful state with the given exception.
110          *
111          * @param clock The clock for generating {@link #time}
112          * @param exception The exception that occured while retrieving the state
113          */
114         protected AbstractState(Clock clock, Throwable exception) {
115                 this(clock, false, true, exception);
116         }
117
118         /**
119          * Creates a new state.
120          *
121          * @param success
122          *            {@code true} if the state is successful, {@code false}
123          *            otherwise
124          * @param exception
125          *            The exception that occured while retrieving the state
126          */
127         protected AbstractState(boolean success, boolean empty, Throwable exception) {
128                 this(Clock.systemUTC(), success, empty, exception);
129         }
130
131         /**
132          * Creates a new state.
133          *
134          * @param clock The clock for generating {@link #time}
135          * @param success {@code true} if the state is successful, {@code false}
136          *              otherwise
137          * @param exception The exception that occured while retrieving the state
138          */
139         protected AbstractState(Clock clock, boolean success, boolean empty, Throwable exception) {
140                 this.time = clock.millis();
141                 this.success = success;
142                 this.empty = empty;
143                 this.exception = exception;
144         }
145
146         //
147         // STATE METHODS
148         //
149
150         /**
151          * {@inheritDoc}
152          */
153         @Override
154         public long time() {
155                 return time;
156         }
157
158         /**
159          * {@inheritDoc}
160          */
161         @Override
162         public boolean success() {
163                 return success;
164         }
165
166         @Override
167         public boolean isEmpty() {
168                 return empty;
169         }
170
171         @Override
172         public boolean triggered() {
173                 return triggered;
174         }
175
176         @Override
177         public void trigger() {
178                 triggered = true;
179         }
180
181         /**
182          * {@inheritDoc}
183          */
184         @Override
185         public int failCount() {
186                 return failCount;
187         }
188
189         /**
190          * {@inheritDoc}
191          */
192         @Override
193         public void setFailCount(int failCount) {
194                 this.failCount = failCount;
195         }
196
197         /**
198          * {@inheritDoc}
199          */
200         @Override
201         public Throwable exception() {
202                 return exception;
203         }
204
205         @Nonnull
206         @Override
207         public Output output(Reaction reaction) {
208                 return new DefaultOutput(summary(reaction))
209                                 .addText("text/plain", plainText())
210                                 .addText("text/html", htmlText());
211         }
212
213         @Nonnull
214         protected String summary(Reaction reaction) {
215                 return reaction.name();
216         }
217
218         @Nonnull
219         protected abstract String plainText();
220
221         @Nullable
222         protected String htmlText() {
223                 //noinspection UnstableApiUsage
224                 return "<div>" + htmlEscaper.escape(plainText()) + "</div>";
225         }
226
227         @SuppressWarnings("UnstableApiUsage")
228         private static final Escaper htmlEscaper = HtmlEscapers.htmlEscaper();
229
230         @Override
231         public int hashCode() {
232                 return Objects.hash(success, empty, time, failCount, exception);
233         }
234
235         @Override
236         public boolean equals(Object object) {
237                 if (!(object instanceof AbstractState)) {
238                         return false;
239                 }
240                 AbstractState abstractState = (AbstractState) object;
241                 return (success == abstractState.success) && (empty == abstractState.empty) && (time == abstractState.time) && (failCount == abstractState.failCount) && Objects.equals(exception, abstractState.exception);
242         }
243
244 }