Ignore chains and states default directories.
[rhynodge.git] / src / main / java / net / pterodactylus / reactor / states / FailedState.java
1 /*
2  * Reactor - FailedState.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  * {@link State} implementation that signals failure.
24  *
25  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
26  */
27 public class FailedState extends AbstractState {
28
29         /** A failed state instance without an exception. */
30         public static final State INSTANCE = new FailedState();
31
32         /**
33          * Creates a new failed state.
34          */
35         public FailedState() {
36                 super(false);
37         }
38
39         /**
40          * Creates a new failed state with the given exception
41          *
42          * @param exception
43          *            The exception of the state
44          */
45         public FailedState(Throwable exception) {
46                 super(exception);
47         }
48
49         //
50         // STATIC METHODS
51         //
52
53         /**
54          * Returns a failed state for the given state. The failed state will be
55          * unsuccessful ({@link #success()} returns false) and it will contain the
56          * same {@link #exception()} as the given state.
57          *
58          * @param state
59          *            The state to copy the exception from
60          * @return A failed state
61          */
62         public static FailedState from(State state) {
63                 if (state instanceof FailedState) {
64                         return (FailedState) state;
65                 }
66                 return new FailedState(state.exception());
67         }
68
69         //
70         // OBJECT METHODS
71         //
72
73         /**
74          * {@inheritDoc}
75          */
76         @Override
77         public String toString() {
78                 return String.format("%s[exception=%s]", getClass().getSimpleName(), exception());
79         }
80
81 }