فهرست منبع

Code improvements

Lortseam 4 سال پیش
والد
کامیت
cfe4dc9f46

+ 8 - 8
src/main/java/me/lortseam/completeconfig/ConfigManager.java

@@ -20,6 +20,11 @@ import java.util.function.Supplier;
 
 public final class ConfigManager {
 
+    private static final Gson GSON = new GsonBuilder()
+            .registerTypeAdapter(CollectionSerializer.TYPE, new CollectionSerializer())
+            .registerTypeAdapter(EntrySerializer.TYPE, new EntrySerializer())
+            .setPrettyPrinting()
+            .create();
     private static final Logger LOGGER = LogManager.getLogger();
 
     private final String modID;
@@ -37,7 +42,7 @@ public final class ConfigManager {
     private JsonElement load() {
         if(Files.exists(jsonPath)) {
             try {
-                return new Gson().fromJson(new FileReader(jsonPath.toString()), JsonElement.class);
+                return GSON.fromJson(new FileReader(jsonPath.toString()), JsonElement.class);
             } catch (FileNotFoundException e) {
                 throw new RuntimeException(e);
             } catch (JsonSyntaxException e) {
@@ -90,13 +95,8 @@ public final class ConfigManager {
                 throw new RuntimeException(e);
             }
         }
-        try(Writer writer = new FileWriter(jsonPath.toString())) {
-            new GsonBuilder()
-                    .registerTypeAdapter(CollectionSerializer.TYPE, new CollectionSerializer())
-                    .registerTypeAdapter(EntrySerializer.TYPE, new EntrySerializer())
-                    .setPrettyPrinting()
-                    .create()
-                    .toJson(config, writer);
+        try(Writer writer = Files.newBufferedWriter(jsonPath)) {
+            GSON.toJson(config, writer);
         } catch (IOException e) {
             throw new RuntimeException(e);
         }

+ 2 - 1
src/main/java/me/lortseam/completeconfig/serialization/CollectionMapDeserializer.java

@@ -11,6 +11,7 @@ import java.util.Map;
 
 public class CollectionMapDeserializer implements JsonDeserializer<CollectionMap> {
 
+    private static final Gson GSON = new Gson();
     public static final Type TYPE = new TypeToken<CollectionMap>() {}.getType();
 
     private final Map<String, Collection> configMap;
@@ -27,7 +28,7 @@ public class CollectionMapDeserializer implements JsonDeserializer<CollectionMap
 
     @Override
     public CollectionMap deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
-        LinkedHashMap<String, JsonElement> map = new Gson().fromJson(json, new TypeToken<LinkedHashMap<String, JsonElement>>() {}.getType());
+        LinkedHashMap<String, JsonElement> map = GSON.fromJson(json, new TypeToken<LinkedHashMap<String, JsonElement>>() {}.getType());
         if (collectionID == null) {
             map.forEach(this::deserialize);
         } else {

+ 2 - 1
src/main/java/me/lortseam/completeconfig/serialization/EntryMapDeserializer.java

@@ -12,13 +12,14 @@ import java.util.LinkedHashMap;
 @RequiredArgsConstructor
 public class EntryMapDeserializer implements JsonDeserializer<EntryMap> {
 
+    private static final Gson GSON = new Gson();
     public static final Type TYPE = new TypeToken<EntryMap>() {}.getType();
 
     private final EntryMap configMap;
 
     @Override
     public EntryMap deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
-        LinkedHashMap<String, JsonElement> map = new Gson().fromJson(json, new TypeToken<LinkedHashMap<String, JsonElement>>() {}.getType());
+        LinkedHashMap<String, JsonElement> map = GSON.fromJson(json, new TypeToken<LinkedHashMap<String, JsonElement>>() {}.getType());
         map.forEach((entryID, element) -> {
             Entry<?> entry = configMap.get(entryID);
             if (entry == null) {