2 * Reactor - StateManager.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.reactor.states;
21 import java.io.IOException;
23 import net.pterodactylus.reactor.State;
25 import org.apache.log4j.Logger;
27 import com.fasterxml.jackson.core.JsonGenerationException;
28 import com.fasterxml.jackson.core.JsonParseException;
29 import com.fasterxml.jackson.databind.JsonMappingException;
30 import com.fasterxml.jackson.databind.ObjectMapper;
33 * Loads and saves {@link State}s.
35 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37 public class StateManager {
40 private static final Logger logger = Logger.getLogger(StateManager.class);
42 /** Jackson object mapper. */
43 private final ObjectMapper objectMapper = new ObjectMapper();
45 /** The directory in which to store states. */
46 private final String directory;
49 * Creates a new state manager. The given directory is assumed to exist.
52 * The directory to store states in
54 public StateManager(String directory) {
55 this.directory = directory;
63 * Loads the state with the given name.
66 * The name of the reaction
67 * @return The loaded state, or {@code null} if the state could not be
70 public State loadState(String reactionName) {
71 File stateFile = stateFile(reactionName);
73 State state = objectMapper.readValue(stateFile, AbstractState.class);
75 } catch (JsonParseException jpe1) {
76 logger.warn(String.format("State for Reaction “%s” could not be parsed.", reactionName), jpe1);
77 } catch (JsonMappingException jme1) {
78 logger.warn(String.format("State for Reaction “%s” could not be parsed.", reactionName), jme1);
79 } catch (IOException ioe1) {
80 logger.info(String.format("State for Reaction “%s” could not be found.", reactionName));
86 * Saves the given state under the given name.
89 * The name of the reaction
93 public void saveState(String reactionName, State state) {
95 File stateFile = stateFile(reactionName);
96 objectMapper.writeValue(stateFile, state);
97 } catch (JsonGenerationException jge1) {
98 logger.warn(String.format("State for Reaction “%s” could not be generated.", reactionName), jge1);
99 } catch (JsonMappingException jme1) {
100 logger.warn(String.format("State for Reaction “%s” could not be generated.", reactionName), jme1);
101 } catch (IOException ioe1) {
102 logger.warn(String.format("State for Reaction “%s” could not be written.", reactionName));
111 * Returns the file for the state with the given name.
113 * @param reactionName
114 * The name of the reaction
115 * @return The file for the state
117 private File stateFile(String reactionName) {
118 return new File(directory, reactionName + ".json");