Skip to content

Commit

Permalink
Add processed category collection util
Browse files Browse the repository at this point in the history
  • Loading branch information
nea89o committed Nov 12, 2024
1 parent f4d9d24 commit 6b62d09
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@

package io.github.notenoughupdates.moulconfig.processor;

import io.github.notenoughupdates.moulconfig.Config;
import io.github.notenoughupdates.moulconfig.gui.MoulConfigEditor;
import io.github.notenoughupdates.moulconfig.internal.Warnings;
import lombok.var;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

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

@Unmodifiable
Map<Integer, ProcessedOption> getAccordionAnchors();

/**
* Collect a list of categories into a map that can be used by {@link MoulConfigEditor#MoulConfigEditor(LinkedHashMap, Config)}.
* Also checks that all ids are unique and all categories with a parent are ordered correctly
*/
static <T extends ProcessedCategory> @Unmodifiable LinkedHashMap<String, T> collect(Iterable<T> categories) {
var map = new LinkedHashMap<String, T>();
String lastParentId = null;
for (T category : categories) {
if (map.containsKey(category.getIdentifier())) {
Warnings.warn("Category list contains multiple categories with identifier " + category.getIdentifier());
}
if (category.getParentCategoryId() == null) {
lastParentId = category.getParentCategoryId();
} else {
if (!category.getParentCategoryId().equals(lastParentId)) {
Warnings.warn("Out of order child category " + category + " has parent with id " + category.getParentCategoryId() + " but the last parent was " + lastParentId);
}
}
map.put(category.getIdentifier(), category);
}
return map;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,9 @@ public ProcessedCategoryImpl(Field field, String name, String desc, @Nullable St
this.parent = parent;
this.desc = desc;
}

@Override
public String toString() {
return "ProcessedCategory {" + getIdentifier() + "}";
}
}

0 comments on commit 6b62d09

Please sign in to comment.