Rename project to “Rhynodge.”
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / states / StateManager.java
1 /*
2  * Rhynodge - StateManager.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.rhynodge.states;
19
20 import java.io.File;
21 import java.io.IOException;
22
23 import net.pterodactylus.rhynodge.State;
24
25 import org.apache.log4j.Logger;
26
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;
31
32 /**
33  * Loads and saves {@link State}s.
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public class StateManager {
38
39         /** The logger. */
40         private static final Logger logger = Logger.getLogger(StateManager.class);
41
42         /** Jackson object mapper. */
43         private final ObjectMapper objectMapper = new ObjectMapper();
44
45         /** The directory in which to store states. */
46         private final String directory;
47
48         /**
49          * Creates a new state manager. The given directory is assumed to exist.
50          *
51          * @param directory
52          *            The directory to store states in
53          */
54         public StateManager(String directory) {
55                 this.directory = directory;
56         }
57
58         //
59         // ACTIONS
60         //
61
62         /**
63          * Loads the last state with the given name.
64          *
65          * @param reactionName
66          *            The name of the reaction
67          * @return The loaded state, or {@code null} if the state could not be
68          *         loaded
69          */
70         public State loadLastState(String reactionName) {
71                 return loadLastState(reactionName, false);
72         }
73
74         /**
75          * Loads the last state with the given name.
76          *
77          * @param reactionName
78          *            The name of the reaction
79          * @return The loaded state, or {@code null} if the state could not be
80          *         loaded
81          */
82         public State loadLastSuccessfulState(String reactionName) {
83                 return loadLastState(reactionName, true);
84         }
85
86         /**
87          * Saves the given state under the given name.
88          *
89          * @param reactionName
90          *            The name of the reaction
91          * @param state
92          *            The state to save
93          */
94         public void saveState(String reactionName, State state) {
95                 try {
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);
101                         }
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));
108                 }
109         }
110
111         //
112         // PRIVATE METHODS
113         //
114
115         /**
116          * Returns the file for the state with the given name.
117          *
118          * @param reactionName
119          *            The name of the reaction
120          * @param suffix
121          *            An additional suffix (may be {@code null}
122          * @return The file for the state
123          */
124         private File stateFile(String reactionName, String suffix) {
125                 return new File(directory, reactionName + ((suffix != null) ? "." + suffix : "") + ".json");
126         }
127
128         /**
129          * Load the given state for the reaction with the given name.
130          *
131          * @param reactionName
132          *            The name of the reaction
133          * @param successful
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
137          *         loaded
138          */
139         private State loadLastState(String reactionName, boolean successful) {
140                 File stateFile = stateFile(reactionName, successful ? "success" : "last");
141                 try {
142                         State state = objectMapper.readValue(stateFile, AbstractState.class);
143                         return state;
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));
150                 }
151                 return null;
152         }
153
154 }