Add manager for state (uses Jackson to read/write JSON).
[rhynodge.git] / src / main / java / net / pterodactylus / reactor / states / StateManager.java
1 /*
2  * Reactor - 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.reactor.states;
19
20 import java.io.File;
21 import java.io.IOException;
22
23 import net.pterodactylus.reactor.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 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 loadState(String reactionName) {
71                 File stateFile = stateFile(reactionName);
72                 try {
73                         State state = objectMapper.readValue(stateFile, AbstractState.class);
74                         return state;
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));
81                 }
82                 return null;
83         }
84
85         /**
86          * Saves the given state under the given name.
87          *
88          * @param reactionName
89          *            The name of the reaction
90          * @param state
91          *            The state to save
92          */
93         public void saveState(String reactionName, State state) {
94                 try {
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));
103                 }
104         }
105
106         //
107         // PRIVATE METHODS
108         //
109
110         /**
111          * Returns the file for the state with the given name.
112          *
113          * @param reactionName
114          *            The name of the reaction
115          * @return The file for the state
116          */
117         private File stateFile(String reactionName) {
118                 return new File(directory, reactionName + ".json");
119         }
120
121 }