afb7dc2e51b92d9edd8079a5920ba2cdc5825116
[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 net.pterodactylus.rhynodge.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         private final boolean empty;
41
42         /** The optional exception that occured while retrieving the state. */
43         private final Throwable exception;
44
45         /** The number of consecutive failures. */
46         @JsonProperty
47         private int failCount;
48
49         /**
50          * Creates a new successful state.
51          */
52         protected AbstractState() {
53                 this(true);
54         }
55
56         /**
57          * Creates a new state.
58          *
59          * @param success
60          *            {@code true} if the state is successful, {@code false}
61          *            otherwise
62          */
63         protected AbstractState(boolean success) {
64                 this(success, true, null);
65         }
66
67         protected AbstractState(boolean success, boolean empty) {
68                 this(success, empty, null);
69         }
70
71         /**
72          * Creates a new non-successful state with the given exception.
73          *
74          * @param exception
75          *            The exception that occured while retrieving the state
76          */
77         protected AbstractState(Throwable exception) {
78                 this(false, true, exception);
79         }
80
81         /**
82          * Creates a new state.
83          *
84          * @param success
85          *            {@code true} if the state is successful, {@code false}
86          *            otherwise
87          * @param exception
88          *            The exception that occured while retrieving the state
89          */
90         protected AbstractState(boolean success, boolean empty, Throwable exception) {
91                 this.time = System.currentTimeMillis();
92                 this.success = success;
93                 this.empty = empty;
94                 this.exception = exception;
95         }
96
97         //
98         // STATE METHODS
99         //
100
101         /**
102          * {@inheritDoc}
103          */
104         @Override
105         public long time() {
106                 return time;
107         }
108
109         /**
110          * {@inheritDoc}
111          */
112         @Override
113         public boolean success() {
114                 return success;
115         }
116
117         @Override
118         public boolean isEmpty() {
119                 return empty;
120         }
121
122         /**
123          * {@inheritDoc}
124          */
125         @Override
126         public int failCount() {
127                 return failCount;
128         }
129
130         /**
131          * {@inheritDoc}
132          */
133         @Override
134         public void setFailCount(int failCount) {
135                 this.failCount = failCount;
136         }
137
138         /**
139          * {@inheritDoc}
140          */
141         @Override
142         public Throwable exception() {
143                 return exception;
144         }
145
146 }