🔀 Merge branch 'website/epic-games' into next
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / states / FailedState.java
1 /*
2  * Rhynodge - 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.rhynodge.states;
19
20 import java.io.IOException;
21 import java.io.PrintWriter;
22 import java.io.StringWriter;
23 import java.io.Writer;
24 import javax.annotation.Nonnull;
25
26 import net.pterodactylus.rhynodge.State;
27
28 /**
29  * {@link State} implementation that signals failure.
30  *
31  * @author <a href="mailto:bombe@pterodactylus.net">David â€˜Bombe’ Roden</a>
32  */
33 public class FailedState extends AbstractState {
34
35         /** A failed state instance without an exception. */
36         public static final State INSTANCE = new FailedState();
37
38         /**
39          * Creates a new failed state.
40          */
41         public FailedState() {
42                 super(false);
43         }
44
45         /**
46          * Creates a new failed state with the given exception
47          *
48          * @param exception
49          *            The exception of the state
50          */
51         public FailedState(Throwable exception) {
52                 super(exception);
53         }
54
55         @Override
56         public boolean isEmpty() {
57                 return true;
58         }
59
60         @Nonnull
61         @Override
62         protected String plainText() {
63                 if (exception() == null) {
64                         return "Failed";
65                 }
66                 try (Writer stringWriter = new StringWriter();
67                          PrintWriter printWriter = new PrintWriter(stringWriter)) {
68                         exception().printStackTrace(printWriter);
69                         return "Failed: " + stringWriter.toString();
70                 } catch (IOException ioe1) {
71                         return "Failed while rendering exception";
72                 }
73         }
74
75         //
76         // STATIC METHODS
77         //
78
79         /**
80          * Returns a failed state for the given state. The failed state will be
81          * unsuccessful ({@link #success()} returns false) and it will contain the
82          * same {@link #exception()} as the given state.
83          *
84          * @param state
85          *            The state to copy the exception from
86          * @return A failed state
87          */
88         public static FailedState from(State state) {
89                 if (state instanceof FailedState) {
90                         return (FailedState) state;
91                 }
92                 return new FailedState(state.exception());
93         }
94
95         //
96         // OBJECT METHODS
97         //
98
99         /**
100          * {@inheritDoc}
101          */
102         @Override
103         public String toString() {
104                 return String.format("%s[exception=%s]", getClass().getSimpleName(), exception());
105         }
106
107 }