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;
20 import static java.util.Optional.empty;
21 import static java.util.Optional.ofNullable;
24 import java.io.IOException;
25 import java.util.Optional;
27 import net.pterodactylus.rhynodge.State;
29 import org.apache.log4j.Logger;
31 import com.fasterxml.jackson.core.JsonGenerationException;
32 import com.fasterxml.jackson.core.JsonParseException;
33 import com.fasterxml.jackson.databind.JsonMappingException;
34 import com.fasterxml.jackson.databind.ObjectMapper;
37 * Loads and saves {@link State}s.
39 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41 public class StateManager {
44 private static final Logger logger = Logger.getLogger(StateManager.class);
46 /** Jackson object mapper. */
47 private final ObjectMapper objectMapper = new ObjectMapper();
49 /** The directory in which to store states. */
50 private final String directory;
53 * Creates a new state manager. The given directory is assumed to exist.
56 * The directory to store states in
58 public StateManager(String directory) {
59 this.directory = directory;
67 * Loads the last state with the given name.
70 * The name of the reaction
71 * @return The loaded state, or {@link Optional#empty()} if the state could not be
74 public Optional<State> loadLastState(String reactionName) {
75 return loadLastState(reactionName, false);
79 * Loads the last state with the given name.
82 * The name of the reaction
83 * @return The loaded state, or {@link Optional#empty()} if the state could not be
86 public Optional<State> loadLastSuccessfulState(String reactionName) {
87 return loadLastState(reactionName, true);
91 * Saves the given state under the given name.
94 * The name of the reaction
98 public void saveState(String reactionName, State state) {
99 File stateFile = null;
101 stateFile = stateFile(reactionName, "last");
102 objectMapper.writeValue(stateFile, state);
103 if (state.success()) {
104 stateFile = stateFile(reactionName, "success");
105 objectMapper.writeValue(stateFile, state);
107 } catch (JsonGenerationException jge1) {
108 logger.warn(String.format("State for Reaction “%s” could not be generated.", reactionName), jge1);
110 } catch (JsonMappingException jme1) {
111 logger.warn(String.format("State for Reaction “%s” could not be generated.", reactionName), jme1);
113 } catch (IOException ioe1) {
114 logger.warn(String.format("State for Reaction “%s” could not be written.", reactionName));
124 * Returns the file for the state with the given name.
126 * @param reactionName
127 * The name of the reaction
129 * An additional suffix (may be {@code null}
130 * @return The file for the state
132 private File stateFile(String reactionName, String suffix) {
133 return new File(directory, reactionName + ((suffix != null) ? "." + suffix : "") + ".json");
137 * Load the given state for the reaction with the given name.
139 * @param reactionName
140 * The name of the reaction
142 * {@code true} to load the last successful state, {@code false}
143 * to load the last state
144 * @return The loaded state, or {@link Optional#empty()} if the state could not be
147 private Optional<State> loadLastState(String reactionName, boolean successful) {
148 File stateFile = stateFile(reactionName, successful ? "success" : "last");
150 State state = objectMapper.readValue(stateFile, AbstractState.class);
151 return ofNullable(state);
152 } catch (JsonParseException jpe1) {
153 logger.warn(String.format("State for Reaction “%s” could not be parsed.", reactionName), jpe1);
154 } catch (JsonMappingException jme1) {
155 logger.warn(String.format("State for Reaction “%s” could not be parsed.", reactionName), jme1);
156 } catch (IOException ioe1) {
157 logger.info(String.format("State for Reaction “%s” could not be found.", reactionName));