Store failed states and fail count, too.
[rhynodge.git] / src / main / java / net / pterodactylus / reactor / states / AbstractState.java
1 /*
2  * Reactor - 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.reactor.states;
19
20 import net.pterodactylus.reactor.State;
21
22 import com.fasterxml.jackson.annotation.JsonProperty;
23 import com.fasterxml.jackson.annotation.JsonTypeInfo;
24
25 /**
26  * Abstract implementation of a {@link State} that knows about the basic
27  * attributes of a {@link State}.
28  *
29  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30  */
31 @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
32 public abstract class AbstractState implements State {
33
34         /** The time of this state. */
35         @JsonProperty
36         private final long time;
37
38         /** Whether the state was successfully retrieved. */
39         private final boolean success;
40
41         /** The optional exception that occured while retrieving the state. */
42         private final Throwable exception;
43
44         /** The number of consecutive failures. */
45         private int failCount;
46
47         /**
48          * Creates a new successful state.
49          */
50         protected AbstractState() {
51                 this(true);
52         }
53
54         /**
55          * Creates a new state.
56          *
57          * @param success
58          *            {@code true} if the state is successful, {@code false}
59          *            otherwise
60          */
61         protected AbstractState(boolean success) {
62                 this(success, null);
63         }
64
65         /**
66          * Creates a new non-successful state with the given exception.
67          *
68          * @param exception
69          *            The exception that occured while retrieving the state
70          */
71         protected AbstractState(Throwable exception) {
72                 this(false, exception);
73         }
74
75         /**
76          * Creates a new state.
77          *
78          * @param success
79          *            {@code true} if the state is successful, {@code false}
80          *            otherwise
81          * @param exception
82          *            The exception that occured while retrieving the state
83          */
84         protected AbstractState(boolean success, Throwable exception) {
85                 this.time = System.currentTimeMillis();
86                 this.success = success;
87                 this.exception = exception;
88         }
89
90         //
91         // STATE METHODS
92         //
93
94         /**
95          * {@inheritDoc}
96          */
97         @Override
98         public long time() {
99                 return time;
100         }
101
102         /**
103          * {@inheritDoc}
104          */
105         @Override
106         public boolean success() {
107                 return success;
108         }
109
110         /**
111          * {@inheritDoc}
112          */
113         @Override
114         public int failCount() {
115                 return failCount;
116         }
117
118         /**
119          * {@inheritDoc}
120          */
121         @Override
122         public void setFailCount(int failCount) {
123                 this.failCount = failCount;
124         }
125
126         /**
127          * {@inheritDoc}
128          */
129         @Override
130         public Throwable exception() {
131                 return exception;
132         }
133
134 }