♻️ Move output generation to state
[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 javax.annotation.Nonnull;
21 import javax.annotation.Nullable;
22
23 import net.pterodactylus.rhynodge.Reaction;
24 import net.pterodactylus.rhynodge.State;
25 import net.pterodactylus.rhynodge.output.DefaultOutput;
26 import net.pterodactylus.rhynodge.output.Output;
27
28 import com.fasterxml.jackson.annotation.JsonProperty;
29 import com.fasterxml.jackson.annotation.JsonTypeInfo;
30 import com.google.common.escape.Escaper;
31 import com.google.common.html.HtmlEscapers;
32
33 /**
34  * Abstract implementation of a {@link State} that knows about the basic
35  * attributes of a {@link State}.
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38  */
39 @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
40 public abstract class AbstractState implements State {
41
42         /** The time of this state. */
43         @JsonProperty
44         private final long time;
45
46         /** Whether the state was successfully retrieved. */
47         private final boolean success;
48         private final boolean empty;
49
50         /** The optional exception that occured while retrieving the state. */
51         private final Throwable exception;
52
53         /** The number of consecutive failures. */
54         @JsonProperty
55         private int failCount;
56
57         /**
58          * Creates a new successful state.
59          */
60         protected AbstractState() {
61                 this(true);
62         }
63
64         /**
65          * Creates a new state.
66          *
67          * @param success
68          *            {@code true} if the state is successful, {@code false}
69          *            otherwise
70          */
71         protected AbstractState(boolean success) {
72                 this(success, true, null);
73         }
74
75         protected AbstractState(boolean success, boolean empty) {
76                 this(success, empty, null);
77         }
78
79         /**
80          * Creates a new non-successful state with the given exception.
81          *
82          * @param exception
83          *            The exception that occured while retrieving the state
84          */
85         protected AbstractState(Throwable exception) {
86                 this(false, true, exception);
87         }
88
89         /**
90          * Creates a new state.
91          *
92          * @param success
93          *            {@code true} if the state is successful, {@code false}
94          *            otherwise
95          * @param exception
96          *            The exception that occured while retrieving the state
97          */
98         protected AbstractState(boolean success, boolean empty, Throwable exception) {
99                 this.time = System.currentTimeMillis();
100                 this.success = success;
101                 this.empty = empty;
102                 this.exception = exception;
103         }
104
105         //
106         // STATE METHODS
107         //
108
109         /**
110          * {@inheritDoc}
111          */
112         @Override
113         public long time() {
114                 return time;
115         }
116
117         /**
118          * {@inheritDoc}
119          */
120         @Override
121         public boolean success() {
122                 return success;
123         }
124
125         @Override
126         public boolean isEmpty() {
127                 return empty;
128         }
129
130         /**
131          * {@inheritDoc}
132          */
133         @Override
134         public int failCount() {
135                 return failCount;
136         }
137
138         /**
139          * {@inheritDoc}
140          */
141         @Override
142         public void setFailCount(int failCount) {
143                 this.failCount = failCount;
144         }
145
146         /**
147          * {@inheritDoc}
148          */
149         @Override
150         public Throwable exception() {
151                 return exception;
152         }
153
154         @Nonnull
155         @Override
156         public Output output(Reaction reaction) {
157                 return new DefaultOutput(summary(reaction))
158                                 .addText("text/plain", plainText())
159                                 .addText("text/html", htmlText());
160         }
161
162         @Nonnull
163         protected String summary(Reaction reaction) {
164                 return reaction.name();
165         }
166
167         @Nonnull
168         protected abstract String plainText();
169
170         @Nullable
171         protected String htmlText() {
172                 //noinspection UnstableApiUsage
173                 return "<div>" + htmlEscaper.escape(plainText()) + "</div>";
174         }
175
176         @SuppressWarnings("UnstableApiUsage")
177         private static final Escaper htmlEscaper = HtmlEscapers.htmlEscaper();
178
179 }