PageRenderTime 88ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/IngeniousFrame/src/main/java/za/ac/sun/cs/ingenious/core/configuration/MatchSetting.java

https://bitbucket.org/ElanVB/ingenious-framework
Java | 290 lines | 169 code | 28 blank | 93 comment | 10 complexity | 2473aedf68214bd832b2696d1dade036 MD5 | raw file
  1. package za.ac.sun.cs.ingenious.core.configuration;
  2. import com.google.gson.Gson;
  3. import com.google.gson.JsonElement;
  4. import java.io.IOException;
  5. import java.io.ObjectInputStream;
  6. import java.io.ObjectOutputStream;
  7. import java.io.Serializable;
  8. import java.nio.charset.StandardCharsets;
  9. import java.nio.file.Files;
  10. import java.nio.file.Paths;
  11. import java.util.List;
  12. import java.util.Map;
  13. import za.ac.sun.cs.ingenious.core.exception.BadMatchSetting;
  14. import za.ac.sun.cs.ingenious.core.exception.IncorrectSettingTypeException;
  15. import za.ac.sun.cs.ingenious.core.exception.MissingSettingException;
  16. /**
  17. * Objects of this class represent the settings for one instance of some game. The class supports
  18. * reading and modifying these settings for different setting types (int, boolean, ...).
  19. * The available settings will vary from game to game, but three settings are fixed:
  20. * LobbyName, numPlayers and gameName (used with the RefereeFactory to create a referee object)
  21. */
  22. public final class MatchSetting implements Serializable {
  23. /**
  24. * Determines if a de-serialized file is compatible with this class.
  25. */
  26. private static final long serialVersionUID = -3834774530442103753L;
  27. public static final int UNSET_POSITIVE_SCALAR = -1;
  28. private static final Gson gson = new Gson();
  29. private Map<String, Object> data;
  30. /**
  31. * Load a JSON file containing match settings
  32. * @param pathToMatchFile Complete path to a JSON file containing the match settings
  33. * @throws IOException if file could not be opened
  34. */
  35. public MatchSetting(String pathToMatchFile) throws IOException {
  36. byte[] encoded = Files.readAllBytes(Paths.get(pathToMatchFile));
  37. String jsonObject = new String(encoded, StandardCharsets.UTF_8);
  38. Object o = gson.fromJson(jsonObject, Object.class);
  39. data = (Map<String, Object>) o;
  40. }
  41. /**
  42. * Read match settings from the given JSON object
  43. * @param matchSettings JSON object containing the match settings
  44. */
  45. public MatchSetting(JsonElement matchSettings) {
  46. Object o = gson.fromJson(matchSettings, Object.class);
  47. data = (Map<String, Object>) o;
  48. }
  49. /**
  50. * @return Value of setting "LobbyName" or "" if absent
  51. */
  52. public String getLobbyName() {
  53. try {
  54. return getSettingAsString("LobbyName");
  55. } catch (MissingSettingException | IncorrectSettingTypeException e) {
  56. return "";
  57. }
  58. }
  59. public void setLobbyName(String newLobbyName) {
  60. data.put("LobbyName", newLobbyName);
  61. }
  62. /**
  63. * @return Value of setting "numPlayers" or MatchSetting.UNSET_POSITIVE_SCALAR if absent
  64. */
  65. public int getNumPlayers() {
  66. try {
  67. return getSettingAsInt("numPlayers");
  68. } catch (MissingSettingException | IncorrectSettingTypeException e) {
  69. return UNSET_POSITIVE_SCALAR;
  70. }
  71. }
  72. public void setNumPlayers(int newNumPlayers) {
  73. data.put("numPlayers", new Double(newNumPlayers));
  74. }
  75. /**
  76. * @return Value of setting "gameName" or "" if absent
  77. */
  78. public String getGameName() {
  79. try {
  80. return getSettingAsString("gameName");
  81. } catch (MissingSettingException | IncorrectSettingTypeException e) {
  82. return "";
  83. }
  84. }
  85. public void setGameName(String newGameName) {
  86. data.put("gameName", newGameName);
  87. }
  88. private Object getSetting(String settingName) throws MissingSettingException {
  89. if (!data.containsKey(settingName)) {
  90. throw new MissingSettingException(settingName);
  91. }
  92. return data.get(settingName);
  93. }
  94. /**
  95. * @param settingName Key of the setting to read.
  96. * @return Value of the setting with key settingName as a String, if it exists.
  97. * @throws MissingSettingException If no setting with the given key exists.
  98. * @throws IncorrectSettingTypeException If a setting with settingName exists but is not of type String.
  99. */
  100. public String getSettingAsString(String settingName) throws MissingSettingException, IncorrectSettingTypeException {
  101. Object val = getSetting(settingName);
  102. if (!(val instanceof String)) {
  103. throw new IncorrectSettingTypeException(settingName, "String");
  104. }
  105. return (String) val;
  106. }
  107. /**
  108. * @param settingName Key of the setting to read.
  109. * @param altValue Value to return if the given setting does not exist or is of the wrong type.
  110. * @return Value of the setting with key settingName as a String, if it exists, else altValue.
  111. */
  112. public String getSettingAsString(String settingName, String altValue) {
  113. String ret;
  114. try {
  115. ret = getSettingAsString(settingName);
  116. } catch (MissingSettingException | IncorrectSettingTypeException e) {
  117. ret = altValue;
  118. }
  119. return ret;
  120. }
  121. /**
  122. * @param settingName Key of the setting to read.
  123. * @return Value of the setting with key settingName as a double, if it exists.
  124. * @throws MissingSettingException If no setting with the given key exists.
  125. * @throws IncorrectSettingTypeException If a setting with settingName exists but is not of type double.
  126. */
  127. public double getSettingAsDouble(String settingName) throws MissingSettingException, IncorrectSettingTypeException {
  128. Object val = getSetting(settingName);
  129. if (!(val instanceof Double)) {
  130. throw new IncorrectSettingTypeException(settingName, "Double");
  131. }
  132. return ((Double) val).doubleValue();
  133. }
  134. /**
  135. * @param settingName Key of the setting to read.
  136. * @param altValue Value to return if the given setting does not exist or is of the wrong type.
  137. * @return Value of the setting with key settingName as a double, if it exists, else altValue.
  138. */
  139. public double getSettingAsDouble(String settingName, double altValue) {
  140. double ret;
  141. try {
  142. ret = getSettingAsDouble(settingName);
  143. } catch (MissingSettingException | IncorrectSettingTypeException e) {
  144. ret = altValue;
  145. }
  146. return ret;
  147. }
  148. /**
  149. * @param settingName Key of the setting to read.
  150. * @return Value of the setting with key settingName as a int, if it exists.
  151. * @throws MissingSettingException If no setting with the given key exists.
  152. * @throws IncorrectSettingTypeException If a setting with settingName exists but is not of type int.
  153. */
  154. public int getSettingAsInt(String settingName) throws MissingSettingException, IncorrectSettingTypeException {
  155. Object val = getSetting(settingName);
  156. if (!(val instanceof Double)) {
  157. throw new IncorrectSettingTypeException(settingName, "Double");
  158. }
  159. return ((Double) val).intValue();
  160. }
  161. /**
  162. * @param settingName Key of the setting to read.
  163. * @param altValue Value to return if the given setting does not exist or is of the wrong type.
  164. * @return Value of the setting with key settingName as a int, if it exists, else altValue.
  165. */
  166. public int getSettingAsInt(String settingName, int altValue) {
  167. int ret;
  168. try {
  169. ret = getSettingAsInt(settingName);
  170. } catch (MissingSettingException | IncorrectSettingTypeException e) {
  171. ret = altValue;
  172. }
  173. return ret;
  174. }
  175. /**
  176. * @param settingName Key of the setting to read.
  177. * @return Value of the setting with key settingName as a boolean, if it exists.
  178. * @throws MissingSettingException If no setting with the given key exists.
  179. * @throws IncorrectSettingTypeException If a setting with settingName exists but is not of type boolean.
  180. */
  181. public boolean getSettingAsBoolean(String settingName) throws MissingSettingException, IncorrectSettingTypeException {
  182. Object val = getSetting(settingName);
  183. if (!(val instanceof Boolean)) {
  184. throw new IncorrectSettingTypeException(settingName, "Boolean");
  185. }
  186. return ((Boolean) val).booleanValue();
  187. }
  188. /**
  189. * @param settingName Key of the setting to read.
  190. * @param altValue Value to return if the given setting does not exist or is of the wrong type.
  191. * @return Value of the setting with key settingName as a boolean, if it exists, else altValue.
  192. */
  193. public boolean getSettingAsBoolean(String settingName, boolean altValue) {
  194. boolean ret;
  195. try {
  196. ret = getSettingAsBoolean(settingName);
  197. } catch (MissingSettingException | IncorrectSettingTypeException e) {
  198. ret = altValue;
  199. }
  200. return ret;
  201. }
  202. /**
  203. * @param settingName Key of the setting to read.
  204. * @return Value of the setting with key settingName as a List, if it exists.
  205. * @throws MissingSettingException If no setting with the given key exists.
  206. * @throws IncorrectSettingTypeException If a setting with settingName exists but is not of type List.
  207. */
  208. public List<Object> getSettingAsList(String settingName) throws MissingSettingException, IncorrectSettingTypeException {
  209. Object val = getSetting(settingName);
  210. if (!(val instanceof List)) {
  211. throw new IncorrectSettingTypeException(settingName, "List");
  212. }
  213. return (List<Object>) val;
  214. }
  215. /**
  216. * @param settingName Key of the setting to read.
  217. * @return Value of the setting with key settingName as a Map, if it exists.
  218. * @throws MissingSettingException If no setting with the given key exists.
  219. * @throws IncorrectSettingTypeException If a setting with settingName exists but is not of type Map.
  220. */
  221. public Map<String,Object> getSettingAsMap(String settingName) throws MissingSettingException, IncorrectSettingTypeException {
  222. Object val = getSetting(settingName);
  223. if (!(val instanceof Map)) {
  224. throw new IncorrectSettingTypeException(settingName, "Map");
  225. }
  226. return (Map<String,Object>) val;
  227. }
  228. /**
  229. * @param settingName Key of the setting to assign a value to.
  230. * @param value Value to assign
  231. */
  232. public void setSetting(String settingName, Object value) {
  233. data.put(settingName, value);
  234. }
  235. /**
  236. * Verifies that the loaded match settings are correct
  237. */
  238. public void checkConfiguration() throws BadMatchSetting {
  239. if (getNumPlayers()<=0) {
  240. throw new BadMatchSetting("Number of players must be > 0.");
  241. }
  242. if (getLobbyName().isEmpty()) {
  243. throw new BadMatchSetting("Lobby name cannot be empty.");
  244. }
  245. if (getGameName().isEmpty()) {
  246. throw new BadMatchSetting("Game name cannot be empty.");
  247. }
  248. }
  249. /*
  250. * The following two methods are for serialization support with ObjectOutput/InputStreams
  251. */
  252. private void writeObject(ObjectOutputStream out) throws IOException {
  253. Gson gson = new Gson();
  254. String data = gson.toJson(this.data);
  255. out.writeObject(data);
  256. }
  257. private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
  258. Gson gson = new Gson();
  259. String data = (String) in.readObject();
  260. this.data = (Map<String, Object>) gson.fromJson(data, Object.class);
  261. }
  262. }