소스 검색

Create AbstractListListEntry and AbstractListCell to build off of and reduce duplicated code

Mitchell Skaggs 5 년 전
부모
커밋
8455deb088
1개의 변경된 파일75개의 추가작업 그리고 0개의 파일을 삭제
  1. 75 0
      src/main/java/me/shedaniel/clothconfig2/gui/entries/AbstractListListEntry.java

+ 75 - 0
src/main/java/me/shedaniel/clothconfig2/gui/entries/AbstractListListEntry.java

@@ -0,0 +1,75 @@
+package me.shedaniel.clothconfig2.gui.entries;
+
+import java.util.List;
+import java.util.Optional;
+import java.util.function.BiFunction;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+
+/**
+ * @param <T>    the configuration object type
+ * @param <C>    the cell type
+ * @param <SELF> the "curiously recurring template pattern" type parameter
+ * @see BaseListEntry
+ */
+abstract class AbstractListListEntry<T, C extends AbstractListListEntry.AbstractListCell<T, C, SELF>, SELF extends AbstractListListEntry<T, C, SELF>>
+        extends BaseListEntry<T, C, SELF> {
+
+    protected final BiFunction<T, SELF, C> createNewCell;
+    protected Function<T, Optional<String>> cellErrorSupplier;
+
+    @Deprecated
+    public AbstractListListEntry(String fieldName, List<T> value, boolean defaultExpended, Supplier<Optional<String[]>> tooltipSupplier, Consumer<List<T>> saveConsumer, Supplier<List<T>> defaultValue, String resetButtonKey) {
+        this(fieldName, value, defaultExpended, tooltipSupplier, saveConsumer, defaultValue, resetButtonKey, false, null);
+    }
+
+    @Deprecated
+    public AbstractListListEntry(String fieldName, List<T> value, boolean defaultExpended, Supplier<Optional<String[]>> tooltipSupplier, Consumer<List<T>> saveConsumer, Supplier<List<T>> defaultValue, String resetButtonKey, boolean requiresRestart, BiFunction<T, SELF, C> createNewCell) {
+        super(fieldName, tooltipSupplier, defaultValue, abstractListListEntry -> createNewCell.apply(null, abstractListListEntry), saveConsumer, resetButtonKey, requiresRestart);
+        this.createNewCell = createNewCell;
+        for (T f : value)
+            cells.add(createNewCell.apply(f, this.self()));
+        this.widgets.addAll(cells);
+        expended = defaultExpended;
+    }
+
+    public Function<T, Optional<String>> getCellErrorSupplier() {
+        return cellErrorSupplier;
+    }
+
+    public void setCellErrorSupplier(Function<T, Optional<String>> cellErrorSupplier) {
+        this.cellErrorSupplier = cellErrorSupplier;
+    }
+
+    @Override
+    public List<T> getValue() {
+        return cells.stream().map(AbstractListCell::getValue).collect(Collectors.toList());
+    }
+
+    @Override
+    protected C getFromValue(T value) {
+        return createNewCell.apply(value, this.self());
+    }
+
+    /**
+     * @param <T>           the configuration object type
+     * @param <SELF>        the "curiously recurring template pattern" type parameter for this class
+     * @param <OUTER_SELF>> the "curiously recurring template pattern" type parameter for the outer class
+     * @see AbstractListListEntry
+     */
+    static abstract class AbstractListCell<T, SELF extends AbstractListCell<T, SELF, OUTER_SELF>, OUTER_SELF extends AbstractListListEntry<T, SELF, OUTER_SELF>> extends BaseListCell {
+        protected final T value;
+        protected final AbstractListListEntry<T, SELF, OUTER_SELF> listListEntry;
+
+        public AbstractListCell(T value, AbstractListListEntry<T, SELF, OUTER_SELF> listListEntry) {
+            this.value = value;
+            this.listListEntry = listListEntry;
+        }
+
+        public abstract T getValue();
+
+    }
+
+}