From 6cd724b33ff7c745f663c6784842ebba362d61dc Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 6 Jan 2013 11:54:15 +0100 Subject: [PATCH] Add manager for state (uses Jackson to read/write JSON). --- pom.xml | 15 +++ .../pterodactylus/reactor/states/StateManager.java | 121 +++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 src/main/java/net/pterodactylus/reactor/states/StateManager.java diff --git a/pom.xml b/pom.xml index a6ca554..28d464d 100644 --- a/pom.xml +++ b/pom.xml @@ -55,5 +55,20 @@ jaxb 2.1.9 + + com.fasterxml.jackson.core + jackson-core + 2.1.2 + + + com.fasterxml.jackson.core + jackson-annotations + 2.1.2 + + + com.fasterxml.jackson.core + jackson-databind + 2.1.2 + diff --git a/src/main/java/net/pterodactylus/reactor/states/StateManager.java b/src/main/java/net/pterodactylus/reactor/states/StateManager.java new file mode 100644 index 0000000..cd0af2c --- /dev/null +++ b/src/main/java/net/pterodactylus/reactor/states/StateManager.java @@ -0,0 +1,121 @@ +/* + * Reactor - StateManager.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 java.io.File; +import java.io.IOException; + +import net.pterodactylus.reactor.State; + +import org.apache.log4j.Logger; + +import com.fasterxml.jackson.core.JsonGenerationException; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * Loads and saves {@link State}s. + * + * @author David ‘Bombe’ Roden + */ +public class StateManager { + + /** The logger. */ + private static final Logger logger = Logger.getLogger(StateManager.class); + + /** Jackson object mapper. */ + private final ObjectMapper objectMapper = new ObjectMapper(); + + /** 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 + * The directory to store states in + */ + public StateManager(String directory) { + this.directory = directory; + } + + // + // ACTIONS + // + + /** + * Loads the state with the given name. + * + * @param reactionName + * The name of the reaction + * @return The loaded state, or {@code null} if the state could not be + * loaded + */ + public State loadState(String reactionName) { + File stateFile = stateFile(reactionName); + try { + State state = objectMapper.readValue(stateFile, AbstractState.class); + return state; + } catch (JsonParseException jpe1) { + logger.warn(String.format("State for Reaction “%s” could not be parsed.", reactionName), jpe1); + } catch (JsonMappingException jme1) { + logger.warn(String.format("State for Reaction “%s” could not be parsed.", reactionName), jme1); + } catch (IOException ioe1) { + logger.info(String.format("State for Reaction “%s” could not be found.", reactionName)); + } + return null; + } + + /** + * Saves the given state under the given name. + * + * @param reactionName + * The name of the reaction + * @param state + * The state to save + */ + public void saveState(String reactionName, State state) { + try { + File stateFile = stateFile(reactionName); + objectMapper.writeValue(stateFile, state); + } catch (JsonGenerationException jge1) { + logger.warn(String.format("State for Reaction “%s” could not be generated.", reactionName), jge1); + } catch (JsonMappingException jme1) { + logger.warn(String.format("State for Reaction “%s” could not be generated.", reactionName), jme1); + } catch (IOException ioe1) { + logger.warn(String.format("State for Reaction “%s” could not be written.", reactionName)); + } + } + + // + // PRIVATE METHODS + // + + /** + * Returns the file for the state with the given name. + * + * @param reactionName + * The name of the reaction + * @return The file for the state + */ + private File stateFile(String reactionName) { + return new File(directory, reactionName + ".json"); + } + +} -- 2.7.4