Explorar o código

Code and performance improvements

Lortseam %!s(int64=4) %!d(string=hai) anos
pai
achega
21d6f44c55

+ 1 - 2
src/main/java/me/lortseam/completeconfig/data/CollectionMap.java

@@ -1,6 +1,5 @@
 package me.lortseam.completeconfig.data;
 
-import me.lortseam.completeconfig.ConfigMap;
 import me.lortseam.completeconfig.api.ConfigCategory;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
@@ -20,7 +19,7 @@ public class CollectionMap extends ConfigMap<Collection> {
             LOGGER.warn("[CompleteConfig] Category " + categoryID + " is empty!");
             return false;
         }
-        put(categoryID, collection);
+        putUnique(categoryID, collection);
         return true;
     }
 

+ 3 - 15
src/main/java/me/lortseam/completeconfig/ConfigMap.java → src/main/java/me/lortseam/completeconfig/data/ConfigMap.java

@@ -1,36 +1,24 @@
-package me.lortseam.completeconfig;
+package me.lortseam.completeconfig.data;
 
 import lombok.AccessLevel;
 import lombok.RequiredArgsConstructor;
 import org.apache.commons.lang3.StringUtils;
 
 import java.util.LinkedHashMap;
-import java.util.Map;
 
 @RequiredArgsConstructor(access = AccessLevel.PROTECTED)
 public abstract class ConfigMap<T> extends LinkedHashMap<String, T> {
 
     protected final String modTranslationKey;
 
-    @Override
-    public T put(String id, T value) {
-        check(id, value);
-        return super.put(id, value);
-    }
-
-    @Override
-    public void putAll(Map<? extends String, ? extends T> m) {
-        m.forEach(this::check);
-        super.putAll(m);
-    }
-
-    private void check(String id, T value) {
+    void putUnique(String id, T value) {
         if (StringUtils.isBlank(id)) {
             throw new IllegalArgumentException("ID of type " + value.getClass().getSimpleName() + " must not be null or blank");
         }
         if (containsKey(id)) {
             throw new UnsupportedOperationException("A value of type " + value.getClass().getSimpleName() + " with ID " + id + " already exists in the corresponding structure");
         }
+        put(id, value);
     }
 
 }

+ 3 - 3
src/main/java/me/lortseam/completeconfig/data/EntryMap.java

@@ -1,7 +1,6 @@
 package me.lortseam.completeconfig.data;
 
 import com.google.common.base.CaseFormat;
-import me.lortseam.completeconfig.ConfigMap;
 import me.lortseam.completeconfig.api.ConfigEntry;
 import me.lortseam.completeconfig.api.ConfigEntryContainer;
 import me.lortseam.completeconfig.api.ConfigEntryListener;
@@ -15,7 +14,6 @@ import java.lang.reflect.Modifier;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
-import java.util.stream.Collectors;
 
 public class EntryMap extends ConfigMap<Entry> {
 
@@ -134,7 +132,9 @@ public class EntryMap extends ConfigMap<Entry> {
             });
             containerEntries.addAll(0, clazzEntries);
         }
-        putAll(containerEntries.stream().collect(Collectors.toMap(Entry::getID, entry -> entry)));
+        for (Entry<?> entry : containerEntries) {
+            putUnique(entry.getID(), entry);
+        }
     }
 
 }

+ 10 - 7
src/main/java/me/lortseam/completeconfig/gui/cloth/ClothGuiBuilder.java

@@ -3,6 +3,7 @@ package me.lortseam.completeconfig.gui.cloth;
 import lombok.Getter;
 import me.lortseam.completeconfig.Config;
 import me.lortseam.completeconfig.data.Collection;
+import me.lortseam.completeconfig.data.Entry;
 import me.lortseam.completeconfig.gui.GuiBuilder;
 import me.shedaniel.clothconfig2.api.AbstractConfigListEntry;
 import me.shedaniel.clothconfig2.api.ConfigBuilder;
@@ -40,25 +41,27 @@ public class ClothGuiBuilder implements GuiBuilder {
         builder.setParentScreen(parentScreen)
                 .setTitle(new TranslatableText(config.getModTranslationKey() + ".title"))
                 .setSavingRunnable(savingRunnable);
-        config.values().forEach(collection -> {
+        for(Collection collection : config.values()) {
             ConfigCategory configCategory = builder.getOrCreateCategory(collection.getText());
             for (AbstractConfigListEntry<?> entry : buildCollection(collection)) {
                 configCategory.addEntry(entry);
             }
-        });
+        }
         return builder.build();
     }
 
     private List<AbstractConfigListEntry> buildCollection(Collection collection) {
         List<AbstractConfigListEntry> collectionGui = new ArrayList<>();
-        collection.getEntries().values().forEach(entry -> collectionGui.add(((Optional<GuiProvider>) registry.getProvider(entry)).orElseGet(() -> {
-            throw new UnsupportedOperationException("Could not find gui provider for field " + entry.getField());
-        }).build(entry.getText(), entry.getField(), entry.getValue(), entry.getDefaultValue(), entry.getTooltip(), entry.getExtras(), entry::setValue, entry.requiresRestart())));
-        collection.getCollections().values().forEach(c -> {
+        for (Entry entry : collection.getEntries().values()) {
+            collectionGui.add(((Optional<GuiProvider>) registry.getProvider(entry)).orElseGet(() -> {
+                throw new UnsupportedOperationException("Could not find gui provider for field " + entry.getField());
+            }).build(entry.getText(), entry.getField(), entry.getValue(), entry.getDefaultValue(), entry.getTooltip(), entry.getExtras(), entry::setValue, entry.requiresRestart()));
+        }
+        for (Collection c : collection.getCollections().values()) {
             SubCategoryBuilder subBuilder = ConfigEntryBuilder.create().startSubCategory(c.getText());
             subBuilder.addAll(buildCollection(c));
             collectionGui.add(subBuilder.build());
-        });
+        }
         return collectionGui;
     }