From 0f374aad8fcd9328411d80ed06e0215df4462f1f Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 2 Jan 2013 18:58:43 +0100 Subject: [PATCH] Add failed state. --- .../pterodactylus/reactor/states/FailedState.java | 81 ++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/main/java/net/pterodactylus/reactor/states/FailedState.java diff --git a/src/main/java/net/pterodactylus/reactor/states/FailedState.java b/src/main/java/net/pterodactylus/reactor/states/FailedState.java new file mode 100644 index 0000000..cfcbeef --- /dev/null +++ b/src/main/java/net/pterodactylus/reactor/states/FailedState.java @@ -0,0 +1,81 @@ +/* + * Reactor - FailedState.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.reactor.states; + +import net.pterodactylus.reactor.State; + +/** + * {@link State} implementation that signals failure. + * + * @author David ‘Bombe’ Roden + */ +public class FailedState extends AbstractState { + + /** A failed state instance without an exception. */ + public static final State INSTANCE = new FailedState(); + + /** + * Creates a new failed state. + */ + public FailedState() { + super(false); + } + + /** + * Creates a new failed state with the given exception + * + * @param exception + * The exception of the state + */ + public FailedState(Throwable exception) { + super(exception); + } + + // + // STATIC METHODS + // + + /** + * Returns a failed state for the given state. The failed state will be + * unsuccessful ({@link #success()} returns false) and it will contain the + * same {@link #exception()} as the given state. + * + * @param state + * The state to copy the exception from + * @return A failed state + */ + public static FailedState from(State state) { + if (state instanceof FailedState) { + return (FailedState) state; + } + return new FailedState(state.exception()); + } + + // + // OBJECT METHODS + // + + /** + * {@inheritDoc} + */ + @Override + public String toString() { + return String.format("%s[exception=%s]", getClass().getSimpleName(), exception()); + } + +} -- 2.7.4