Includes the time in the state.
[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 /**
23  * Abstract implementation of a {@link State} that knows about the basic
24  * attributes of a {@link State}.
25  *
26  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
27  */
28 public abstract class AbstractState implements State {
29
30         /** The time of this state. */
31         private final long time;
32
33         /** Whether the state was successfully retrieved. */
34         private final boolean success;
35
36         /** The optional exception that occured while retrieving the state. */
37         private final Throwable exception;
38
39         /**
40          * Creates a new successful state.
41          */
42         protected AbstractState() {
43                 this(true);
44         }
45
46         /**
47          * Creates a new state.
48          *
49          * @param success
50          *            {@code true} if the state is successful, {@code false}
51          *            otherwise
52          */
53         protected AbstractState(boolean success) {
54                 this(success, null);
55         }
56
57         /**
58          * Creates a new non-successful state with the given exception.
59          *
60          * @param exception
61          *            The exception that occured while retrieving the state
62          */
63         protected AbstractState(Throwable exception) {
64                 this(false, exception);
65         }
66
67         /**
68          * Creates a new state.
69          *
70          * @param success
71          *            {@code true} if the state is successful, {@code false}
72          *            otherwise
73          * @param exception
74          *            The exception that occured while retrieving the state
75          */
76         protected AbstractState(boolean success, Throwable exception) {
77                 this.time = System.currentTimeMillis();
78                 this.success = success;
79                 this.exception = exception;
80         }
81
82         //
83         // STATE METHODS
84         //
85
86         /**
87          * {@inheritDoc}
88          */
89         @Override
90         public long time() {
91                 return time;
92         }
93
94         /**
95          * {@inheritDoc}
96          */
97         @Override
98         public boolean success() {
99                 return success;
100         }
101
102         /**
103          * {@inheritDoc}
104          */
105         @Override
106         public Throwable exception() {
107                 return exception;
108         }
109
110 }