From: David ‘Bombe’ Roden Date: Wed, 2 Jan 2013 17:58:43 +0000 (+0100) Subject: Add failed state. X-Git-Tag: 0.1~101 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=0f374aad8fcd9328411d80ed06e0215df4462f1f;p=rhynodge.git Add failed state. --- 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()); + } + +}