e35c933ec28fbae79ba69acfeeca08ef1adb9c94
[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         /** Whether the state was successfully retrieved. */
31         private final boolean success;
32
33         /** The optional exception that occured while retrieving the state. */
34         private final Throwable exception;
35
36         /**
37          * Creates a new successful state.
38          */
39         protected AbstractState() {
40                 this(true);
41         }
42
43         /**
44          * Creates a new state.
45          *
46          * @param success
47          *            {@code true} if the state is successful, {@code false}
48          *            otherwise
49          */
50         protected AbstractState(boolean success) {
51                 this(success, null);
52         }
53
54         /**
55          * Creates a new non-successful state with the given exception.
56          *
57          * @param exception
58          *            The exception that occured while retrieving the state
59          */
60         protected AbstractState(Throwable exception) {
61                 this(false, exception);
62         }
63
64         /**
65          * Creates a new state.
66          *
67          * @param success
68          *            {@code true} if the state is successful, {@code false}
69          *            otherwise
70          * @param exception
71          *            The exception that occured while retrieving the state
72          */
73         protected AbstractState(boolean success, Throwable exception) {
74                 this.success = success;
75                 this.exception = exception;
76         }
77
78         //
79         // STATE METHODS
80         //
81
82         /**
83          * {@inheritDoc}
84          */
85         @Override
86         public boolean success() {
87                 return success;
88         }
89
90         /**
91          * {@inheritDoc}
92          */
93         @Override
94         public Throwable exception() {
95                 return exception;
96         }
97
98 }