Add JSON properties.
[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         /**
45          * Creates a new successful state.
46          */
47         protected AbstractState() {
48                 this(true);
49         }
50
51         /**
52          * Creates a new state.
53          *
54          * @param success
55          *            {@code true} if the state is successful, {@code false}
56          *            otherwise
57          */
58         protected AbstractState(boolean success) {
59                 this(success, null);
60         }
61
62         /**
63          * Creates a new non-successful state with the given exception.
64          *
65          * @param exception
66          *            The exception that occured while retrieving the state
67          */
68         protected AbstractState(Throwable exception) {
69                 this(false, exception);
70         }
71
72         /**
73          * Creates a new state.
74          *
75          * @param success
76          *            {@code true} if the state is successful, {@code false}
77          *            otherwise
78          * @param exception
79          *            The exception that occured while retrieving the state
80          */
81         protected AbstractState(boolean success, Throwable exception) {
82                 this.time = System.currentTimeMillis();
83                 this.success = success;
84                 this.exception = exception;
85         }
86
87         //
88         // STATE METHODS
89         //
90
91         /**
92          * {@inheritDoc}
93          */
94         @Override
95         public long time() {
96                 return time;
97         }
98
99         /**
100          * {@inheritDoc}
101          */
102         @Override
103         public boolean success() {
104                 return success;
105         }
106
107         /**
108          * {@inheritDoc}
109          */
110         @Override
111         public Throwable exception() {
112                 return exception;
113         }
114
115 }