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