|
| 1 | +/* |
| 2 | + * * Copyright 2018 github.com/ReflxctionDev |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package net.reflxction.simplejson.configuration; |
| 17 | + |
| 18 | +import com.google.gson.Gson; |
| 19 | +import com.google.gson.JsonObject; |
| 20 | +import net.reflxction.simplejson.json.JsonFile; |
| 21 | +import net.reflxction.simplejson.json.JsonWriter; |
| 22 | +import net.reflxction.simplejson.json.Lockable; |
| 23 | +import net.reflxction.simplejson.utils.Checks; |
| 24 | +import net.reflxction.simplejson.utils.Gsons; |
| 25 | +import net.reflxction.simplejson.utils.JsonUtils; |
| 26 | +import net.reflxction.simplejson.utils.ObjectUtils; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.lang.reflect.Type; |
| 30 | +import java.math.BigDecimal; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Map; |
| 33 | +import java.util.Objects; |
| 34 | +import java.util.function.Consumer; |
| 35 | + |
| 36 | +/** |
| 37 | + * Represents a configuration file which makes <i>direct</i> contact with the |
| 38 | + * configuration. Direct contact is achieved from <i>getter</i> and <i>setter</i> methods |
| 39 | + * and eventually saves with {@link DirectConfiguration#save(Consumer)} |
| 40 | + * <p> |
| 41 | + * All methods which update or get a value are done on a local {@link JsonObject}, which is derived |
| 42 | + * on creation of this configuration. When {@link #save(Consumer)} is invoked, the content is saved |
| 43 | + * on the JSON file. |
| 44 | + * |
| 45 | + * @see net.reflxction.simplejson.configuration.select.SelectableConfiguration |
| 46 | + */ |
| 47 | +public class DirectConfiguration implements Lockable { |
| 48 | + |
| 49 | + /** |
| 50 | + * The JSON writer used to cache content and write to the file |
| 51 | + */ |
| 52 | + private final JsonWriter writer; |
| 53 | + |
| 54 | + /** |
| 55 | + * The cached JSON content as a JsonObject |
| 56 | + */ |
| 57 | + private JsonObject content; |
| 58 | + |
| 59 | + /** |
| 60 | + * Whether to allow calls to {@link #setFile(JsonFile)} or not |
| 61 | + */ |
| 62 | + private final boolean locked; |
| 63 | + |
| 64 | + /** |
| 65 | + * Initiates a new configuration for given JSON file. |
| 66 | + * |
| 67 | + * @param file JSON file to contact |
| 68 | + * @param locked Whether to allow calls to {@link #setFile(JsonFile)} or not |
| 69 | + * @throws IOException I/O exception while connecting with the file |
| 70 | + */ |
| 71 | + public DirectConfiguration(JsonFile file, boolean locked) throws IOException { |
| 72 | + Objects.requireNonNull(file, "JsonFile (file) cannot be null"); |
| 73 | + writer = new JsonWriter(file); |
| 74 | + this.locked = locked; |
| 75 | + content = writer.getCachedContentAsObject(); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Initiates a new configuration for the given addon name. |
| 80 | + * |
| 81 | + * @param file JSON file to contact |
| 82 | + * @throws IOException I/O exception while connecting with the file |
| 83 | + */ |
| 84 | + public DirectConfiguration(JsonFile file) throws IOException { |
| 85 | + this(file, false); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Returns a {@link String} from the associated key. |
| 90 | + * |
| 91 | + * @param key Key to fetch from |
| 92 | + * @return The associated string |
| 93 | + */ |
| 94 | + public final String getString(String key) { |
| 95 | + Checks.notNull(key); |
| 96 | + return content.get(key).getAsString(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Returns a {@code int} from the associated key. |
| 101 | + * |
| 102 | + * @param key Key to fetch from |
| 103 | + * @return The associated integer |
| 104 | + */ |
| 105 | + public final int getInt(String key) { |
| 106 | + Checks.notNull(key); |
| 107 | + return content.get(key).getAsInt(); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Returns a {@code double} from the associated key. |
| 112 | + * |
| 113 | + * @param key Key to fetch from |
| 114 | + * @return The associated double |
| 115 | + */ |
| 116 | + public final double getDouble(String key) { |
| 117 | + Checks.notNull(key); |
| 118 | + return content.get(key).getAsDouble(); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Returns a {@code long} from the associated key. |
| 123 | + * |
| 124 | + * @param key Key to fetch from |
| 125 | + * @return The associated long |
| 126 | + */ |
| 127 | + public final long getLong(String key) { |
| 128 | + Checks.notNull(key); |
| 129 | + return content.get(key).getAsLong(); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Returns a {@code float} from the associated key. |
| 134 | + * |
| 135 | + * @param key Key to fetch from |
| 136 | + * @return The associated float |
| 137 | + */ |
| 138 | + public final float getFloat(String key) { |
| 139 | + Checks.notNull(key); |
| 140 | + return content.get(key).getAsFloat(); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Returns a {@code boolean} from the associated boolean |
| 145 | + * |
| 146 | + * @param key Key to fetch from |
| 147 | + * @return The associated boolean |
| 148 | + */ |
| 149 | + public final boolean getBoolean(String key) { |
| 150 | + Checks.notNull(key); |
| 151 | + return content.get(key).getAsBoolean(); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Returns a {@link BigDecimal} from the associated key. |
| 156 | + * |
| 157 | + * @param key Key to fetch from |
| 158 | + * @return The associated decimal |
| 159 | + */ |
| 160 | + public final BigDecimal getBigDecimal(String key) { |
| 161 | + Checks.notNull(key); |
| 162 | + return content.get(key).getAsBigDecimal(); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Returns a {@link List} of objects from the associated key |
| 167 | + * |
| 168 | + * @param key Key to fetch from |
| 169 | + * @return The associated List |
| 170 | + */ |
| 171 | + public final List<Object> getList(String key) { |
| 172 | + Checks.notNull(key); |
| 173 | + return JsonUtils.toList(content.get(key).toString()); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Returns a {@link Map} with values linked appropriately to their keys in order. |
| 178 | + * |
| 179 | + * @param key Key to fetch from |
| 180 | + * @return The associated Map |
| 181 | + */ |
| 182 | + public final Map<String, Object> getMap(String key) { |
| 183 | + Checks.notNull(key); |
| 184 | + return JsonUtils.toMap(content.get(key).toString()); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Returns a deserialized instance of the given class assignment. |
| 189 | + * |
| 190 | + * @param key Key to fetch from |
| 191 | + * @param type Type to return an instance of, as a deserialized object |
| 192 | + * @param <T> Class object assignment |
| 193 | + * @return The deserialized object |
| 194 | + */ |
| 195 | + public final <T> T get(String key, Type type) { |
| 196 | + Checks.notNull(key); |
| 197 | + return get(key, type, Gsons.PRETTY_PRINTING); |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * Returns a deserialized instance of the given class assignment, using the given GSON profile |
| 202 | + * |
| 203 | + * @param key Key to fetch from |
| 204 | + * @param type Type to return an instance of, as a deserialized object |
| 205 | + * @param gson Gson profile to use |
| 206 | + * @param <T> Class object assignment |
| 207 | + * @return The deserialized object |
| 208 | + */ |
| 209 | + public final <T> T get(String key, Type type, Gson gson) { |
| 210 | + Checks.notNull(key); |
| 211 | + return gson.fromJson(content.get(key), type); |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Returns the content of the JSON writer |
| 216 | + * |
| 217 | + * @return The JSON content |
| 218 | + */ |
| 219 | + public final JsonObject getContent() { |
| 220 | + return content; |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * Assigns the given element to the key, and writes it to the JSON file. |
| 225 | + * |
| 226 | + * @param key Key to assign to |
| 227 | + * @param value Value to assign to the key |
| 228 | + */ |
| 229 | + public final void set(String key, Object value) { |
| 230 | + set(key, value, Gsons.PRETTY_PRINTING); |
| 231 | + } |
| 232 | + |
| 233 | + /** |
| 234 | + * Assigns the given element to the key, and writes it to the JSON file. |
| 235 | + * |
| 236 | + * @param key Key to assign to |
| 237 | + * @param value Value to assign to the key |
| 238 | + * @param gson Gson profile to use |
| 239 | + */ |
| 240 | + public final void set(String key, Object value, Gson gson) { |
| 241 | + Checks.notNull(key); |
| 242 | + Checks.notNull(value); |
| 243 | + Checks.notNull(gson); |
| 244 | + content.add(key, gson.toJsonTree(value)); |
| 245 | + } |
| 246 | + |
| 247 | + /** |
| 248 | + * Removes the given key from the JSON file. |
| 249 | + * <p> |
| 250 | + * This will have no effect if the given key doesn't exist. |
| 251 | + * |
| 252 | + * @param key Key to remove |
| 253 | + */ |
| 254 | + public final void remove(String key) { |
| 255 | + Checks.notNull(key); |
| 256 | + content.remove(key); |
| 257 | + } |
| 258 | + |
| 259 | + /** |
| 260 | + * Saves the configuration |
| 261 | + * |
| 262 | + * @param onException The task to execute on exception. If no exception handling is required, |
| 263 | + * this may be left null. |
| 264 | + */ |
| 265 | + public final void save(Consumer<IOException> onException) { |
| 266 | + try { |
| 267 | + writer.writeAndOverride(content, true); |
| 268 | + } catch (IOException e) { |
| 269 | + ObjectUtils.ifNotNull(onException, task -> task.accept(e)); |
| 270 | + } |
| 271 | + } |
| 272 | + |
| 273 | + /** |
| 274 | + * Sets the new file. Implementation of this method should also update any content |
| 275 | + * this component controls. |
| 276 | + * |
| 277 | + * @param file New JSON file to use. Must not be null |
| 278 | + */ |
| 279 | + @Override |
| 280 | + public final void setFile(JsonFile file) { |
| 281 | + checkLocked("Cannot invoke #setFile() on a locked DirectConfiguration!"); |
| 282 | + Checks.notNull(file); |
| 283 | + writer.setFile(file); |
| 284 | + content = writer.getCachedContentAsObject(); |
| 285 | + } |
| 286 | + |
| 287 | + /** |
| 288 | + * Returns whether the current component is locked or not. This will control whether |
| 289 | + * {@link #setFile(JsonFile)} can be used or not. |
| 290 | + * |
| 291 | + * @return Whether the current component is locked or not. |
| 292 | + */ |
| 293 | + @Override |
| 294 | + public boolean isLocked() { |
| 295 | + return locked; |
| 296 | + } |
| 297 | + |
| 298 | + /** |
| 299 | + * Returns a new {@link DirectConfiguration} and throws unchecked exceptions if there were any IO exceptions |
| 300 | + * |
| 301 | + * @param file JSON file to contact |
| 302 | + * @return The DirectConfiguration object |
| 303 | + */ |
| 304 | + public static DirectConfiguration of(JsonFile file) { |
| 305 | + try { |
| 306 | + return new DirectConfiguration(file); |
| 307 | + } catch (IOException e) { |
| 308 | + throw new RuntimeException(e); |
| 309 | + } |
| 310 | + } |
| 311 | + |
| 312 | + /** |
| 313 | + * Returns a new {@link DirectConfiguration} and throws unchecked exceptions if there were any IO exceptions |
| 314 | + * |
| 315 | + * @param file JSON file to contact |
| 316 | + * @param locked Whether to allow calls to {@link #setFile(JsonFile)} or not |
| 317 | + * @return The DirectConfiguration object |
| 318 | + */ |
| 319 | + public static DirectConfiguration of(JsonFile file, boolean locked) { |
| 320 | + try { |
| 321 | + return new DirectConfiguration(file, locked); |
| 322 | + } catch (IOException e) { |
| 323 | + throw new RuntimeException(e); |
| 324 | + } |
| 325 | + } |
| 326 | + |
| 327 | +} |
0 commit comments