⬆️ Update a bunch of dependencies
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / states / StateManager.java
index 19eccba..9b46feb 100644 (file)
 
 package net.pterodactylus.rhynodge.states;
 
+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 com.fasterxml.jackson.databind.SerializationFeature;
+import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
+import com.fasterxml.jackson.module.kotlin.KotlinModule;
+import jakarta.inject.Inject;
+import jakarta.inject.Singleton;
 
 import net.pterodactylus.rhynodge.State;
 
@@ -34,6 +44,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
+@Singleton
 public class StateManager {
 
        /** The logger. */
@@ -42,17 +53,24 @@ public class StateManager {
        /** Jackson object mapper. */
        private final ObjectMapper objectMapper = new ObjectMapper();
 
+       {
+               objectMapper.registerModule(new KotlinModule.Builder().build());
+               objectMapper.registerModule(new JavaTimeModule());
+               objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
+       }
+
        /** The directory in which to store states. */
        private final String directory;
 
        /**
         * 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();
        }
 
        //
@@ -64,10 +82,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#empty()} if the state could not be
         *         loaded
         */
-       public State loadLastState(String reactionName) {
+       public Optional<State> loadLastState(String reactionName) {
                return loadLastState(reactionName, false);
        }
 
@@ -76,10 +94,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#empty()} if the state could not be
         *         loaded
         */
-       public State loadLastSuccessfulState(String reactionName) {
+       public Optional<State> loadLastSuccessfulState(String reactionName) {
                return loadLastState(reactionName, true);
        }
 
@@ -92,8 +110,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 +120,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 +155,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#empty()} if the state could not be
         *         loaded
         */
-       private State loadLastState(String reactionName, boolean successful) {
+       private Optional<State> loadLastState(String reactionName, boolean successful) {
                File stateFile = stateFile(reactionName, successful ? "success" : "last");
                try {
                        State state = objectMapper.readValue(stateFile, AbstractState.class);
-                       return state;
+                       return ofNullable(state);
                } catch (JsonParseException jpe1) {
                        logger.warn(String.format("State for Reaction “%s” could not be parsed.", reactionName), jpe1);
                } catch (JsonMappingException jme1) {
@@ -148,7 +170,25 @@ public class StateManager {
                } catch (IOException ioe1) {
                        logger.info(String.format("State for Reaction “%s” could not be found.", reactionName));
                }
-               return null;
+               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);
+               }
+
        }
 
 }