X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Frhynodge%2Fstates%2FStateManager.java;h=a52e74d4bcc63a3d84dd6283e3940f8b4ef4f640;hb=4fea375ff20fbbbaaad6feba574d4b6c6380ed93;hp=19eccba3a4c92e230a16bb219f7395ae2a627b71;hpb=6ec36ef950c23c135bf0e112d932c5b7068189b8;p=rhynodge.git diff --git a/src/main/java/net/pterodactylus/rhynodge/states/StateManager.java b/src/main/java/net/pterodactylus/rhynodge/states/StateManager.java index 19eccba..a52e74d 100644 --- a/src/main/java/net/pterodactylus/rhynodge/states/StateManager.java +++ b/src/main/java/net/pterodactylus/rhynodge/states/StateManager.java @@ -17,11 +17,15 @@ package net.pterodactylus.rhynodge.states; +import static com.google.common.base.Optional.absent; +import static com.google.common.base.Optional.fromNullable; + import java.io.File; import java.io.IOException; import net.pterodactylus.rhynodge.State; +import com.google.common.base.Optional; import org.apache.log4j.Logger; import com.fasterxml.jackson.core.JsonGenerationException; @@ -64,10 +68,10 @@ public class StateManager { * * @param reactionName * The name of the reaction - * @return The loaded state, or {@code null} if the state could not be + * @return The loaded state, or {@link Optional#absent()} if the state could not be * loaded */ - public State loadLastState(String reactionName) { + public Optional loadLastState(String reactionName) { return loadLastState(reactionName, false); } @@ -76,10 +80,10 @@ public class StateManager { * * @param reactionName * The name of the reaction - * @return The loaded state, or {@code null} if the state could not be + * @return The loaded state, or {@link Optional#absent()} if the state could not be * loaded */ - public State loadLastSuccessfulState(String reactionName) { + public Optional loadLastSuccessfulState(String reactionName) { return loadLastState(reactionName, true); } @@ -92,8 +96,9 @@ public class StateManager { * The state to save */ public void saveState(String reactionName, State state) { + File stateFile = null; try { - File stateFile = stateFile(reactionName, "last"); + stateFile = stateFile(reactionName, "last"); objectMapper.writeValue(stateFile, state); if (state.success()) { stateFile = stateFile(reactionName, "success"); @@ -101,10 +106,13 @@ public class StateManager { } } catch (JsonGenerationException jge1) { logger.warn(String.format("State for Reaction “%s” could not be generated.", reactionName), jge1); + stateFile.delete(); } catch (JsonMappingException jme1) { logger.warn(String.format("State for Reaction “%s” could not be generated.", reactionName), jme1); + stateFile.delete(); } catch (IOException ioe1) { logger.warn(String.format("State for Reaction “%s” could not be written.", reactionName)); + stateFile.delete(); } } @@ -133,14 +141,14 @@ public class StateManager { * @param successful * {@code true} to load the last successful state, {@code false} * to load the last state - * @return The loaded state, or {@code null} if the state could not be + * @return The loaded state, or {@link Optional#absent()} if the state could not be * loaded */ - private State loadLastState(String reactionName, boolean successful) { + private Optional loadLastState(String reactionName, boolean successful) { File stateFile = stateFile(reactionName, successful ? "success" : "last"); try { State state = objectMapper.readValue(stateFile, AbstractState.class); - return state; + return fromNullable(state); } catch (JsonParseException jpe1) { logger.warn(String.format("State for Reaction “%s” could not be parsed.", reactionName), jpe1); } catch (JsonMappingException jme1) { @@ -148,7 +156,7 @@ public class StateManager { } catch (IOException ioe1) { logger.info(String.format("State for Reaction “%s” could not be found.", reactionName)); } - return null; + return absent(); } }