2 * Rhynodge - 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.rhynodge.states;
21 import java.io.IOException;
23 import net.pterodactylus.rhynodge.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 last 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 loadLastState(String reactionName) {
71 return loadLastState(reactionName, false);
75 * Loads the last state with the given name.
78 * The name of the reaction
79 * @return The loaded state, or {@code null} if the state could not be
82 public State loadLastSuccessfulState(String reactionName) {
83 return loadLastState(reactionName, true);
87 * Saves the given state under the given name.
90 * The name of the reaction
94 public void saveState(String reactionName, State state) {
96 File stateFile = stateFile(reactionName, "last");
97 objectMapper.writeValue(stateFile, state);
98 if (state.success()) {
99 stateFile = stateFile(reactionName, "success");
100 objectMapper.writeValue(stateFile, state);
102 } catch (JsonGenerationException jge1) {
103 logger.warn(String.format("State for Reaction “%s” could not be generated.", reactionName), jge1);
104 } catch (JsonMappingException jme1) {
105 logger.warn(String.format("State for Reaction “%s” could not be generated.", reactionName), jme1);
106 } catch (IOException ioe1) {
107 logger.warn(String.format("State for Reaction “%s” could not be written.", reactionName));
116 * Returns the file for the state with the given name.
118 * @param reactionName
119 * The name of the reaction
121 * An additional suffix (may be {@code null}
122 * @return The file for the state
124 private File stateFile(String reactionName, String suffix) {
125 return new File(directory, reactionName + ((suffix != null) ? "." + suffix : "") + ".json");
129 * Load the given state for the reaction with the given name.
131 * @param reactionName
132 * The name of the reaction
134 * {@code true} to load the last successful state, {@code false}
135 * to load the last state
136 * @return The loaded state, or {@code null} if the state could not be
139 private State loadLastState(String reactionName, boolean successful) {
140 File stateFile = stateFile(reactionName, successful ? "success" : "last");
142 State state = objectMapper.readValue(stateFile, AbstractState.class);
144 } catch (JsonParseException jpe1) {
145 logger.warn(String.format("State for Reaction “%s” could not be parsed.", reactionName), jpe1);
146 } catch (JsonMappingException jme1) {
147 logger.warn(String.format("State for Reaction “%s” could not be parsed.", reactionName), jme1);
148 } catch (IOException ioe1) {
149 logger.info(String.format("State for Reaction “%s” could not be found.", reactionName));