Skip to content

Commit 6b62d09

Browse files
committed
Add processed category collection util
1 parent f4d9d24 commit 6b62d09

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

common/src/main/java/io/github/notenoughupdates/moulconfig/processor/ProcessedCategory.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@
2020

2121
package io.github.notenoughupdates.moulconfig.processor;
2222

23+
import io.github.notenoughupdates.moulconfig.Config;
24+
import io.github.notenoughupdates.moulconfig.gui.MoulConfigEditor;
25+
import io.github.notenoughupdates.moulconfig.internal.Warnings;
26+
import lombok.var;
2327
import org.jetbrains.annotations.Nullable;
2428
import org.jetbrains.annotations.Unmodifiable;
2529

30+
import java.util.LinkedHashMap;
2631
import java.util.List;
2732
import java.util.Map;
2833

@@ -40,4 +45,28 @@ public interface ProcessedCategory extends HasDebugLocation {
4045

4146
@Unmodifiable
4247
Map<Integer, ProcessedOption> getAccordionAnchors();
48+
49+
/**
50+
* Collect a list of categories into a map that can be used by {@link MoulConfigEditor#MoulConfigEditor(LinkedHashMap, Config)}.
51+
* Also checks that all ids are unique and all categories with a parent are ordered correctly
52+
*/
53+
static <T extends ProcessedCategory> @Unmodifiable LinkedHashMap<String, T> collect(Iterable<T> categories) {
54+
var map = new LinkedHashMap<String, T>();
55+
String lastParentId = null;
56+
for (T category : categories) {
57+
if (map.containsKey(category.getIdentifier())) {
58+
Warnings.warn("Category list contains multiple categories with identifier " + category.getIdentifier());
59+
}
60+
if (category.getParentCategoryId() == null) {
61+
lastParentId = category.getParentCategoryId();
62+
} else {
63+
if (!category.getParentCategoryId().equals(lastParentId)) {
64+
Warnings.warn("Out of order child category " + category + " has parent with id " + category.getParentCategoryId() + " but the last parent was " + lastParentId);
65+
}
66+
}
67+
map.put(category.getIdentifier(), category);
68+
}
69+
return map;
70+
}
71+
4372
}

common/src/main/java/io/github/notenoughupdates/moulconfig/processor/ProcessedCategoryImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,9 @@ public ProcessedCategoryImpl(Field field, String name, String desc, @Nullable St
6363
this.parent = parent;
6464
this.desc = desc;
6565
}
66+
67+
@Override
68+
public String toString() {
69+
return "ProcessedCategory {" + getIdentifier() + "}";
70+
}
6671
}

0 commit comments

Comments
 (0)