X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Frhynodge%2Fstates%2FStateManager.java;h=8e0c72eb1177efb568744ddd2aa587116aa99a61;hb=e426b888068677738269eb378c24a6f62db3614d;hp=35125bb8b5a0d91ac731ed177c65ce310fbe6647;hpb=1f272e712b8a1142dd6cf6db3cc43bb0bc63b9bc;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 35125bb..8e0c72e 100644 --- a/src/main/java/net/pterodactylus/rhynodge/states/StateManager.java +++ b/src/main/java/net/pterodactylus/rhynodge/states/StateManager.java @@ -17,15 +17,18 @@ package net.pterodactylus.rhynodge.states; -import static com.google.common.base.Optional.absent; -import static com.google.common.base.Optional.fromNullable; +import static java.util.Optional.empty; +import static java.util.Optional.ofNullable; import java.io.File; import java.io.IOException; +import java.util.Optional; + +import javax.inject.Inject; +import javax.inject.Singleton; import net.pterodactylus.rhynodge.State; -import com.google.common.base.Optional; import org.apache.log4j.Logger; import com.fasterxml.jackson.core.JsonGenerationException; @@ -38,6 +41,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; * * @author David ‘Bombe’ Roden */ +@Singleton public class StateManager { /** The logger. */ @@ -52,11 +56,12 @@ public class StateManager { /** * Creates a new state manager. The given directory is assumed to exist. * - * @param directory + * @param stateDirectory * The directory to store states in */ - public StateManager(String directory) { - this.directory = directory; + @Inject + public StateManager(StateDirectory stateDirectory) { + this.directory = stateDirectory.getDirectory(); } // @@ -68,7 +73,7 @@ public class StateManager { * * @param reactionName * The name of the reaction - * @return The loaded state, or {@link Optional#absent()} if the state could not be + * @return The loaded state, or {@link Optional#empty()} if the state could not be * loaded */ public Optional loadLastState(String reactionName) { @@ -80,7 +85,7 @@ public class StateManager { * * @param reactionName * The name of the reaction - * @return The loaded state, or {@link Optional#absent()} if the state could not be + * @return The loaded state, or {@link Optional#empty()} if the state could not be * loaded */ public Optional loadLastSuccessfulState(String reactionName) { @@ -96,8 +101,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"); @@ -105,10 +111,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(); } } @@ -137,14 +146,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 {@link Optional#absent()} if the state could not be + * @return The loaded state, or {@link Optional#empty()} if the state could not be * loaded */ private Optional loadLastState(String reactionName, boolean successful) { File stateFile = stateFile(reactionName, successful ? "success" : "last"); try { State state = objectMapper.readValue(stateFile, AbstractState.class); - return fromNullable(state); + return ofNullable(state); } catch (JsonParseException jpe1) { logger.warn(String.format("State for Reaction “%s” could not be parsed.", reactionName), jpe1); } catch (JsonMappingException jme1) { @@ -152,7 +161,25 @@ public class StateManager { } catch (IOException ioe1) { logger.info(String.format("State for Reaction “%s” could not be found.", reactionName)); } - return absent(); + return empty(); + } + + public static class StateDirectory { + + private final String directory; + + private StateDirectory(String directory) { + this.directory = directory; + } + + public String getDirectory() { + return directory; + } + + public static StateDirectory of(String directory) { + return new StateDirectory(directory); + } + } }