🐛 Serialize the success value, remove equals override
[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
53         /** The optional exception that occured while retrieving the state. */
54         private final Throwable exception;
55
56         /** The number of consecutive failures. */
57         @JsonProperty
58         private int failCount;
59
60         /**
61          * Creates a new successful state.
62          */
63         protected AbstractState() {
64                 this(true);
65         }
66
67         /**
68          * Creates a new state.
69          *
70          * @param success
71          *            {@code true} if the state is successful, {@code false}
72          *            otherwise
73          */
74         protected AbstractState(boolean success) {
75                 this(Clock.systemUTC(), success);
76         }
77
78         /**
79          * Creates a new state.
80          *
81          * @param clock The clock for generating {@link #time}
82          * @param success {@code true} if the state is successful, {@code false}
83          *              otherwise
84          */
85         protected AbstractState(Clock clock, boolean success) {
86                 this(clock, success, true, null);
87         }
88
89         protected AbstractState(boolean success, boolean empty) {
90                 this(Clock.systemUTC(), success, empty);
91         }
92
93         protected AbstractState(Clock clock, boolean success, boolean empty) {
94                 this(clock, success, empty, null);
95         }
96
97         /**
98          * Creates a new non-successful state with the given exception.
99          *
100          * @param exception
101          *            The exception that occured while retrieving the state
102          */
103         protected AbstractState(Throwable exception) {
104                 this(Clock.systemUTC(), exception);
105         }
106
107         /**
108          * Creates a new non-successful state with the given exception.
109          *
110          * @param clock The clock for generating {@link #time}
111          * @param exception The exception that occured while retrieving the state
112          */
113         protected AbstractState(Clock clock, Throwable exception) {
114                 this(clock, false, true, exception);
115         }
116
117         /**
118          * Creates a new state.
119          *
120          * @param success
121          *            {@code true} if the state is successful, {@code false}
122          *            otherwise
123          * @param exception
124          *            The exception that occured while retrieving the state
125          */
126         protected AbstractState(boolean success, boolean empty, Throwable exception) {
127                 this(Clock.systemUTC(), success, empty, exception);
128         }
129
130         /**
131          * Creates a new state.
132          *
133          * @param clock The clock for generating {@link #time}
134          * @param success {@code true} if the state is successful, {@code false}
135          *              otherwise
136          * @param exception The exception that occured while retrieving the state
137          */
138         protected AbstractState(Clock clock, boolean success, boolean empty, Throwable exception) {
139                 this.time = clock.millis();
140                 this.success = success;
141                 this.empty = empty;
142                 this.exception = exception;
143         }
144
145         //
146         // STATE METHODS
147         //
148
149         /**
150          * {@inheritDoc}
151          */
152         @Override
153         public long time() {
154                 return time;
155         }
156
157         /**
158          * {@inheritDoc}
159          */
160         @Override
161         public boolean success() {
162                 return success;
163         }
164
165         @Override
166         public boolean isEmpty() {
167                 return empty;
168         }
169
170         /**
171          * {@inheritDoc}
172          */
173         @Override
174         public int failCount() {
175                 return failCount;
176         }
177
178         /**
179          * {@inheritDoc}
180          */
181         @Override
182         public void setFailCount(int failCount) {
183                 this.failCount = failCount;
184         }
185
186         /**
187          * {@inheritDoc}
188          */
189         @Override
190         public Throwable exception() {
191                 return exception;
192         }
193
194         @Nonnull
195         @Override
196         public Output output(Reaction reaction) {
197                 return new DefaultOutput(summary(reaction))
198                                 .addText("text/plain", plainText())
199                                 .addText("text/html", htmlText());
200         }
201
202         @Nonnull
203         protected String summary(Reaction reaction) {
204                 return reaction.name();
205         }
206
207         @Nonnull
208         protected abstract String plainText();
209
210         @Nullable
211         protected String htmlText() {
212                 //noinspection UnstableApiUsage
213                 return "<div>" + htmlEscaper.escape(plainText()) + "</div>";
214         }
215
216         @SuppressWarnings("UnstableApiUsage")
217         private static final Escaper htmlEscaper = HtmlEscapers.htmlEscaper();
218
219         @Override
220         public int hashCode() {
221                 return Objects.hash(success, empty, time, failCount, exception);
222         }
223
224         @Override
225         public boolean equals(Object object) {
226                 if (!(object instanceof AbstractState)) {
227                         return false;
228                 }
229                 AbstractState abstractState = (AbstractState) object;
230                 return (success == abstractState.success) && (empty == abstractState.empty) && (time == abstractState.time) && (failCount == abstractState.failCount) && Objects.equals(exception, abstractState.exception);
231         }
232
233 }