Lortseam 4 年之前
父節點
當前提交
38b3c3f544

+ 2 - 2
lib/src/main/java/me/lortseam/completeconfig/data/Collection.java → lib/src/main/java/me/lortseam/completeconfig/data/Cluster.java

@@ -17,9 +17,9 @@ import java.util.Arrays;
 
 @Log4j2(topic = "CompleteConfig")
 @RequiredArgsConstructor(access = AccessLevel.PACKAGE)
-public final class Collection extends BaseCollection implements Identifiable, TooltipSupplier {
+public final class Cluster extends Parent implements Identifiable, TooltipSupplier {
 
-    private final BaseCollection parent;
+    private final Parent parent;
     private final ConfigGroup group;
     @Environment(EnvType.CLIENT)
     private TranslationKey translation;

+ 20 - 0
lib/src/main/java/me/lortseam/completeconfig/data/ClusterSet.java

@@ -0,0 +1,20 @@
+package me.lortseam.completeconfig.data;
+
+import lombok.extern.log4j.Log4j2;
+import me.lortseam.completeconfig.api.ConfigGroup;
+
+@Log4j2(topic = "CompleteConfig")
+public class ClusterSet extends SortedSet<Cluster> {
+
+    protected ClusterSet(Parent parent) {
+        super(parent);
+    }
+
+    void resolve(ConfigGroup group) {
+        Cluster cluster = new Cluster(parent, group);
+        cluster.resolveContainer(group);
+        if (cluster.isEmpty()) return;
+        add(cluster);
+    }
+
+}

+ 0 - 20
lib/src/main/java/me/lortseam/completeconfig/data/CollectionSet.java

@@ -1,20 +0,0 @@
-package me.lortseam.completeconfig.data;
-
-import lombok.extern.log4j.Log4j2;
-import me.lortseam.completeconfig.api.ConfigGroup;
-
-@Log4j2(topic = "CompleteConfig")
-public class CollectionSet extends DataSet<Collection> {
-
-    protected CollectionSet(BaseCollection parent) {
-        super(parent);
-    }
-
-    void resolve(ConfigGroup group) {
-        Collection collection = new Collection(parent, group);
-        collection.resolveContainer(group);
-        if (collection.isEmpty()) return;
-        add(collection);
-    }
-
-}

+ 1 - 1
lib/src/main/java/me/lortseam/completeconfig/data/Config.java

@@ -29,7 +29,7 @@ import java.util.Objects;
 @Log4j2(topic = "CompleteConfig")
 @EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = false)
 @ToString(onlyExplicitlyIncluded = true)
-public class Config extends BaseCollection {
+public class Config extends Parent {
 
     @EqualsAndHashCode.Include
     @ToString.Include

+ 1 - 1
lib/src/main/java/me/lortseam/completeconfig/data/Entry.java

@@ -41,7 +41,7 @@ public class Entry<T> implements StructurePart, Identifiable, Translatable, Tool
         }
     }
 
-    static Entry<?> of(BaseCollection parent, Field field, ConfigContainer object) {
+    static Entry<?> of(Parent parent, Field field, ConfigContainer object) {
         EntryOrigin origin = new EntryOrigin(parent, field, object);
         return ConfigRegistry.getTransformations().stream().filter(transformation -> {
             return transformation.test(origin);

+ 2 - 2
lib/src/main/java/me/lortseam/completeconfig/data/EntryOrigin.java

@@ -18,7 +18,7 @@ import java.util.Optional;
 public final class EntryOrigin {
 
     @Getter(AccessLevel.PACKAGE)
-    private final BaseCollection parent;
+    private final Parent parent;
     @Getter
     @EqualsAndHashCode.Include
     private final Field field;
@@ -28,7 +28,7 @@ public final class EntryOrigin {
     @EqualsAndHashCode.Include
     private final ConfigContainer object;
 
-    EntryOrigin(BaseCollection parent, Field field, ConfigContainer object) {
+    EntryOrigin(Parent parent, Field field, ConfigContainer object) {
         this.parent = parent;
         this.field = field;
         type = ReflectionUtils.getFieldType(field);

+ 2 - 2
lib/src/main/java/me/lortseam/completeconfig/data/EntrySet.java

@@ -10,9 +10,9 @@ import java.lang.reflect.Modifier;
 import java.util.Arrays;
 
 @Log4j2(topic = "CompleteConfig")
-public class EntrySet extends DataSet<Entry> {
+public class EntrySet extends SortedSet<Entry> {
 
-    EntrySet(BaseCollection parent) {
+    EntrySet(Parent parent) {
         super(parent);
     }
 

+ 35 - 11
lib/src/main/java/me/lortseam/completeconfig/data/BaseCollection.java → lib/src/main/java/me/lortseam/completeconfig/data/Parent.java

@@ -1,31 +1,34 @@
 package me.lortseam.completeconfig.data;
 
-import com.google.common.collect.Iterables;
 import me.lortseam.completeconfig.api.ConfigContainer;
 import me.lortseam.completeconfig.api.ConfigGroup;
-import me.lortseam.completeconfig.data.structure.ParentStructurePart;
+import me.lortseam.completeconfig.data.structure.Identifiable;
 import me.lortseam.completeconfig.data.structure.StructurePart;
 import me.lortseam.completeconfig.data.structure.client.Translatable;
 import me.lortseam.completeconfig.exception.IllegalAnnotationTargetException;
 import me.lortseam.completeconfig.util.ReflectionUtils;
 import org.apache.commons.lang3.ArrayUtils;
+import org.spongepowered.configurate.CommentedConfigurationNode;
 
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Modifier;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
+import java.util.function.BiConsumer;
+import java.util.function.Predicate;
 
-abstract class BaseCollection implements ParentStructurePart, Translatable {
+abstract class Parent implements StructurePart, Translatable {
 
     private final EntrySet entries = new EntrySet(this);
-    private final CollectionSet collections = new CollectionSet(this);
+    private final ClusterSet clusters = new ClusterSet(this);
 
-    public final java.util.Collection<Entry> getEntries() {
+    public final Collection<Entry> getEntries() {
         return Collections.unmodifiableCollection(entries);
     }
 
-    public final java.util.Collection<Collection> getCollections() {
-        return Collections.unmodifiableCollection(collections);
+    public final Collection<Cluster> getClusters() {
+        return Collections.unmodifiableCollection(clusters);
     }
 
     final void resolveContainer(ConfigContainer container) {
@@ -76,7 +79,7 @@ abstract class BaseCollection implements ParentStructurePart, Translatable {
     final void resolve(ConfigContainer... containers) {
         for (ConfigContainer container : containers) {
             if (container instanceof ConfigGroup) {
-                collections.resolve((ConfigGroup) container);
+                clusters.resolve((ConfigGroup) container);
             } else {
                 resolveContainer(container);
             }
@@ -84,12 +87,33 @@ abstract class BaseCollection implements ParentStructurePart, Translatable {
     }
 
     @Override
-    public final Iterable<StructurePart> getChildren() {
-        return Iterables.concat(entries, collections);
+    public final void apply(CommentedConfigurationNode node) {
+        propagateToChildren(entries, node, childNode -> !childNode.isNull(), StructurePart::apply);
+        propagateToChildren(clusters, node, childNode -> !childNode.isNull(), StructurePart::apply);
+    }
+
+    @Override
+    public void fetch(CommentedConfigurationNode node) {
+        propagateToChildren(entries, node, StructurePart::fetch);
+        propagateToChildren(clusters, node, StructurePart::fetch);
+    }
+
+    private <C extends StructurePart & Identifiable> void propagateToChildren(Collection<C> children, CommentedConfigurationNode node, Predicate<CommentedConfigurationNode> childNodeCondition, BiConsumer<C, CommentedConfigurationNode> function) {
+        for (C child : children) {
+            CommentedConfigurationNode childNode = node.node(child.getId());
+            if (!childNodeCondition.test(childNode)) {
+                continue;
+            }
+            function.accept(child, childNode);
+        }
+    }
+
+    private <C extends StructurePart & Identifiable> void propagateToChildren(Collection<C> children, CommentedConfigurationNode node, BiConsumer<C, CommentedConfigurationNode> function) {
+        propagateToChildren(children, node, childNode -> true, function);
     }
 
     final boolean isEmpty() {
-        return Iterables.size(getChildren()) == 0;
+        return entries.isEmpty() && clusters.isEmpty();
     }
 
 }

+ 2 - 2
lib/src/main/java/me/lortseam/completeconfig/data/DataSet.java → lib/src/main/java/me/lortseam/completeconfig/data/SortedSet.java

@@ -12,9 +12,9 @@ import java.util.LinkedHashMap;
 import java.util.Map;
 
 @RequiredArgsConstructor(access = AccessLevel.PROTECTED)
-abstract class DataSet<T extends StructurePart & Identifiable> extends AbstractSet<T> {
+abstract class SortedSet<T extends StructurePart & Identifiable> extends AbstractSet<T> {
 
-    protected final BaseCollection parent;
+    protected final Parent parent;
     private final Map<String, T> map = new LinkedHashMap<>();
 
     @Override

+ 0 - 32
lib/src/main/java/me/lortseam/completeconfig/data/structure/ParentStructurePart.java

@@ -1,32 +0,0 @@
-package me.lortseam.completeconfig.data.structure;
-
-import org.spongepowered.configurate.CommentedConfigurationNode;
-
-import java.util.function.BiConsumer;
-import java.util.function.Predicate;
-
-public interface ParentStructurePart<C extends StructurePart & Identifiable> extends StructurePart {
-
-    Iterable<C> getChildren();
-
-    @Override
-    default void apply(CommentedConfigurationNode node) {
-        propagateToChildren(childNode -> !childNode.isNull(), StructurePart::apply, node);
-    }
-
-    @Override
-    default void fetch(CommentedConfigurationNode node) {
-        propagateToChildren(childNode -> true, StructurePart::fetch, node);
-    }
-
-    default void propagateToChildren(Predicate<CommentedConfigurationNode> childNodeCondition, BiConsumer<C, CommentedConfigurationNode> function, CommentedConfigurationNode node) {
-        for (C child : getChildren()) {
-            CommentedConfigurationNode childNode = node.node(child.getId());
-            if (!childNodeCondition.test(childNode)) {
-                continue;
-            }
-            function.accept(child, childNode);
-        }
-    }
-
-}

+ 15 - 15
lib/src/main/java/me/lortseam/completeconfig/gui/cloth/ClothConfigScreenBuilder.java

@@ -2,7 +2,7 @@ package me.lortseam.completeconfig.gui.cloth;
 
 import lombok.Getter;
 import lombok.NonNull;
-import me.lortseam.completeconfig.data.Collection;
+import me.lortseam.completeconfig.data.Cluster;
 import me.lortseam.completeconfig.data.Config;
 import me.lortseam.completeconfig.data.Entry;
 import me.lortseam.completeconfig.text.TranslationKey;
@@ -51,10 +51,10 @@ public final class ClothConfigScreenBuilder extends ConfigScreenBuilder {
                 category.addEntry(buildEntry(entry));
             }
         }
-        for(Collection collection : config.getCollections()) {
-            ConfigCategory category = builder.getOrCreateCategory(collection.getText());
-            category.setDescription(() -> collection.getTooltip().map(lines -> Arrays.stream(lines).map(line -> (StringVisitable) line).toArray(StringVisitable[]::new)));
-            for (AbstractConfigListEntry<?> entry : buildCollection(collection)) {
+        for(Cluster cluster : config.getClusters()) {
+            ConfigCategory category = builder.getOrCreateCategory(cluster.getText());
+            category.setDescription(() -> cluster.getTooltip().map(lines -> Arrays.stream(lines).map(line -> (StringVisitable) line).toArray(StringVisitable[]::new)));
+            for (AbstractConfigListEntry<?> entry : buildCluster(cluster)) {
                 category.addEntry(entry);
             }
         }
@@ -67,19 +67,19 @@ public final class ClothConfigScreenBuilder extends ConfigScreenBuilder {
         }).build(entry);
     }
 
-    private List<AbstractConfigListEntry> buildCollection(Collection collection) {
-        List<AbstractConfigListEntry> collectionGui = new ArrayList<>();
-        for (Entry<?> entry : collection.getEntries()) {
-            collectionGui.add(buildEntry(entry));
+    private List<AbstractConfigListEntry> buildCluster(Cluster cluster) {
+        List<AbstractConfigListEntry> clusterGui = new ArrayList<>();
+        for (Entry<?> entry : cluster.getEntries()) {
+            clusterGui.add(buildEntry(entry));
         }
-        for (Collection subCollection : collection.getCollections()) {
+        for (Cluster subCluster : cluster.getClusters()) {
             SubCategoryBuilder subBuilder = ConfigEntryBuilder.create()
-                    .startSubCategory(subCollection.getText())
-                    .setTooltip(subCollection.getTooltip());
-            subBuilder.addAll(buildCollection(subCollection));
-            collectionGui.add(subBuilder.build());
+                    .startSubCategory(subCluster.getText())
+                    .setTooltip(subCluster.getTooltip());
+            subBuilder.addAll(buildCluster(subCluster));
+            clusterGui.add(subBuilder.build());
         }
-        return collectionGui;
+        return clusterGui;
     }
 
 }

+ 2 - 2
lib/src/test/java/me/lortseam/completeconfig/data/EntryTest.java

@@ -16,7 +16,7 @@ import static org.mockito.Mockito.when;
 
 public class EntryTest implements ConfigContainer {
 
-    private static final BaseCollection PARENT;
+    private static final Parent PARENT;
     private static final boolean REQUIRES_RESTART = true;
     private static final String COMMENT = "Comment";
     private static final String CUSTOM_ID = "customId", CUSTOM_TRANSLATION_KEY = "customTranslationKey";
@@ -27,7 +27,7 @@ public class EntryTest implements ConfigContainer {
         Config config = mock(Config.class);
         when(config.getMod()).thenReturn(modMetadata);
         TranslationKey parentTranslation = TranslationKey.from(config).append("subKey");
-        PARENT = new BaseCollection() {
+        PARENT = new Parent() {
             @Override
             public TranslationKey getTranslation() {
                 return parentTranslation;

+ 31 - 31
lib/src/test/java/me/lortseam/completeconfig/data/BaseCollectionTest.java → lib/src/test/java/me/lortseam/completeconfig/data/ParentTest.java

@@ -13,13 +13,13 @@ import org.junit.jupiter.api.Test;
 import static org.junit.jupiter.api.Assertions.*;
 import static org.mockito.Mockito.mock;
 
-public class BaseCollectionTest {
+public class ParentTest {
 
-    private BaseCollection baseCollection;
+    private Parent parent;
 
     @BeforeEach
     public void beforeEach() {
-        baseCollection = new BaseCollection() {
+        parent = new Parent() {
             @Override
             public TranslationKey getTranslation() {
                 return mock(TranslationKey.class);
@@ -29,93 +29,93 @@ public class BaseCollectionTest {
 
     @Test
     public void resolve_includeFieldIfAnnotated() {
-        baseCollection.resolve(new ContainerWithEntry(), new ContainerWithContainerWithEntry(), new ContainerWithGroupWithEntry());
-        assertEquals(2, baseCollection.getEntries().size());
-        assertEquals(1, baseCollection.getCollections().size());
+        parent.resolve(new ContainerWithEntry(), new ContainerWithContainerWithEntry(), new ContainerWithGroupWithEntry());
+        assertEquals(2, parent.getEntries().size());
+        assertEquals(1, parent.getClusters().size());
     }
 
     @Test
     public void resolve_excludeFieldIfNotAnnotated() {
-        baseCollection.resolve(new ContainerWithField());
-        assertTrue(baseCollection.isEmpty());
+        parent.resolve(new ContainerWithField());
+        assertTrue(parent.isEmpty());
     }
 
     @Test
     public void resolve_includeFieldInEntries() {
-        baseCollection.resolve(new EntriesContainerWithEntry());
-        assertEquals(1, baseCollection.getEntries().size());
+        parent.resolve(new EntriesContainerWithEntry());
+        assertEquals(1, parent.getEntries().size());
     }
 
     @Test
     public void resolve_excludeFieldInEntriesIfContainer() {
-        baseCollection.resolve(new EntriesContainerWithEmptyContainer());
-        assertTrue(baseCollection.isEmpty());
+        parent.resolve(new EntriesContainerWithEmptyContainer());
+        assertTrue(parent.isEmpty());
     }
 
     @Test
     public void resolve_excludeFieldInEntriesIfIgnoreAnnotated() {
-        baseCollection.resolve(new EntriesContainerWithIgnoredField());
-        assertTrue(baseCollection.isEmpty());
+        parent.resolve(new EntriesContainerWithIgnoredField());
+        assertTrue(parent.isEmpty());
     }
 
     @Test
     public void resolve_excludeFieldInEntriesIfTransient() {
-        baseCollection.resolve(new EntriesContainerWithTransientField());
-        assertTrue(baseCollection.isEmpty());
+        parent.resolve(new EntriesContainerWithTransientField());
+        assertTrue(parent.isEmpty());
     }
 
     @Test
     public void resolve_includeSuperclassFieldIfNonStatic() {
-        baseCollection.resolve(new SubclassOfContainerWithEntry(), new SubclassOfContainerWithContainerWithEntry());
-        assertEquals(2, baseCollection.getEntries().size());
+        parent.resolve(new SubclassOfContainerWithEntry(), new SubclassOfContainerWithContainerWithEntry());
+        assertEquals(2, parent.getEntries().size());
     }
 
     @Test
     public void resolve_excludeSuperclassFieldIfStatic() {
-        baseCollection.resolve(new SubclassOfContainerWithStaticEntry(), new SubclassOfContainerWithStaticContainerWithEntry());
-        assertTrue(baseCollection.isEmpty());
+        parent.resolve(new SubclassOfContainerWithStaticEntry(), new SubclassOfContainerWithStaticContainerWithEntry());
+        assertTrue(parent.isEmpty());
     }
 
     @Test
     public void resolve_includeFromMethod() {
-        baseCollection.resolve(new ContainerIncludingContainerWithEntry(), new ContainerIncludingGroupWithEntry());
-        assertEquals(1, baseCollection.getEntries().size());
-        assertEquals(1, baseCollection.getCollections().size());
+        parent.resolve(new ContainerIncludingContainerWithEntry(), new ContainerIncludingGroupWithEntry());
+        assertEquals(1, parent.getEntries().size());
+        assertEquals(1, parent.getClusters().size());
     }
 
     @Test
     public void resolve_includeNestedIfStatic() {
-        baseCollection.resolve(new ContainerNestingStaticContainerWithEntry());
-        assertEquals(1, baseCollection.getEntries().size());
+        parent.resolve(new ContainerNestingStaticContainerWithEntry());
+        assertEquals(1, parent.getEntries().size());
     }
 
     @Test
     public void resolve_throwIfNestedNonContainer() {
-        IllegalAnnotationTargetException exception = assertThrows(IllegalAnnotationTargetException.class, () -> baseCollection.resolve(new ContainerNestingStaticClass()));
+        IllegalAnnotationTargetException exception = assertThrows(IllegalAnnotationTargetException.class, () -> parent.resolve(new ContainerNestingStaticClass()));
         assertEquals("Transitive " + ContainerNestingStaticClass.Class.class + " must implement " + ConfigContainer.class.getSimpleName(), exception.getMessage());
     }
 
     @Test
     public void resolve_throwIfNestedNonStatic() {
-        IllegalAnnotationTargetException exception = assertThrows(IllegalAnnotationTargetException.class, () -> baseCollection.resolve(new ContainerNestingContainerWithEntry()));
+        IllegalAnnotationTargetException exception = assertThrows(IllegalAnnotationTargetException.class, () -> parent.resolve(new ContainerNestingContainerWithEntry()));
         assertEquals("Transitive " + ContainerNestingContainerWithEntry.ContainerWithEntry.class + " must be static", exception.getMessage());
     }
 
     @Test
     public void resolve_listenSetter() {
         SetterListener listener = new SetterListener();
-        baseCollection.resolve(listener);
+        parent.resolve(listener);
         boolean value = !listener.getValue();
-        Iterables.getOnlyElement(baseCollection.getEntries()).setValue(value);
+        Iterables.getOnlyElement(parent.getEntries()).setValue(value);
         assertEquals(value, listener.getValue());
     }
 
     @Test
     public void resolve_doNotUpdateListenerField() {
         EmptyListener listener = new EmptyListener();
-        baseCollection.resolve(listener);
+        parent.resolve(listener);
         boolean oldValue = listener.getValue();
-        Iterables.getOnlyElement(baseCollection.getEntries()).setValue(!oldValue);
+        Iterables.getOnlyElement(parent.getEntries()).setValue(!oldValue);
         assertEquals(oldValue, listener.getValue());
     }