aboutsummaryrefslogtreecommitdiff
path: root/common/src/main/java/dev/isxander/yacl3/impl
diff options
context:
space:
mode:
authorCrendgrim <Crendgrim@users.noreply.github.com>2023-07-21 23:57:05 +0200
committerGitHub <noreply@github.com>2023-07-21 22:57:05 +0100
commit8a4d6df67917fee4b600cfad1afb7d39a75d75f1 (patch)
treec89cad79d18a5a86e5709e42b8d6129b43f54d7f /common/src/main/java/dev/isxander/yacl3/impl
parenta25e2d754ae856c0c9d2c8ebb9ea16a7a458cf04 (diff)
downloadYetAnotherConfigLib-8a4d6df67917fee4b600cfad1afb7d39a75d75f1.tar.gz
YetAnotherConfigLib-8a4d6df67917fee4b600cfad1afb7d39a75d75f1.tar.bz2
YetAnotherConfigLib-8a4d6df67917fee4b600cfad1afb7d39a75d75f1.zip
Small improvements to ListOption (#89)
Diffstat (limited to 'common/src/main/java/dev/isxander/yacl3/impl')
-rw-r--r--common/src/main/java/dev/isxander/yacl3/impl/ListOptionImpl.java53
1 files changed, 49 insertions, 4 deletions
diff --git a/common/src/main/java/dev/isxander/yacl3/impl/ListOptionImpl.java b/common/src/main/java/dev/isxander/yacl3/impl/ListOptionImpl.java
index 82f5576..25ff7ba 100644
--- a/common/src/main/java/dev/isxander/yacl3/impl/ListOptionImpl.java
+++ b/common/src/main/java/dev/isxander/yacl3/impl/ListOptionImpl.java
@@ -25,12 +25,15 @@ public final class ListOptionImpl<T> implements ListOption<T> {
private final List<ListOptionEntry<T>> entries;
private final boolean collapsed;
private boolean available;
+ private final int minimumNumberOfEntries;
+ private final int maximumNumberOfEntries;
+ private final boolean insertEntriesAtEnd;
private final ImmutableSet<OptionFlag> flags;
private final EntryFactory entryFactory;
private final List<BiConsumer<Option<List<T>>, List<T>>> listeners;
private final List<Runnable> refreshListeners;
- public ListOptionImpl(@NotNull Component name, @NotNull OptionDescription description, @NotNull Binding<List<T>> binding, @NotNull T initialValue, @NotNull Function<ListOptionEntry<T>, Controller<T>> controllerFunction, ImmutableSet<OptionFlag> flags, boolean collapsed, boolean available, Collection<BiConsumer<Option<List<T>>, List<T>>> listeners) {
+ public ListOptionImpl(@NotNull Component name, @NotNull OptionDescription description, @NotNull Binding<List<T>> binding, @NotNull T initialValue, @NotNull Function<ListOptionEntry<T>, Controller<T>> controllerFunction, ImmutableSet<OptionFlag> flags, boolean collapsed, boolean available, int minimumNumberOfEntries, int maximumNumberOfEntries, boolean insertEntriesAtEnd, Collection<BiConsumer<Option<List<T>>, List<T>>> listeners) {
this.name = name;
this.description = description;
this.binding = binding;
@@ -40,6 +43,9 @@ public final class ListOptionImpl<T> implements ListOption<T> {
this.collapsed = collapsed;
this.flags = flags;
this.available = available;
+ this.minimumNumberOfEntries = minimumNumberOfEntries;
+ this.maximumNumberOfEntries = maximumNumberOfEntries;
+ this.insertEntriesAtEnd = insertEntriesAtEnd;
this.listeners = new ArrayList<>();
this.listeners.addAll(listeners);
this.refreshListeners = new ArrayList<>();
@@ -98,9 +104,14 @@ public final class ListOptionImpl<T> implements ListOption<T> {
}
@Override
- public ListOptionEntry<T> insertNewEntryToTop() {
+ public ListOptionEntry<T> insertNewEntry() {
ListOptionEntry<T> newEntry = entryFactory.create(initialValue);
- entries.add(0, newEntry);
+ if (insertEntriesAtEnd) {
+ entries.add(newEntry);
+ } else {
+ // insert at top
+ entries.add(0, newEntry);
+ }
onRefresh();
return newEntry;
}
@@ -163,6 +174,19 @@ public final class ListOptionImpl<T> implements ListOption<T> {
}
@Override
+ public int numberOfEntries() {
+ return this.entries.size();
+ }
+ @Override
+ public int maximumNumberOfEntries() {
+ return this.maximumNumberOfEntries;
+ }
+ @Override
+ public int minimumNumberOfEntries() {
+ return this.minimumNumberOfEntries;
+ }
+
+ @Override
public void addListener(BiConsumer<Option<List<T>>, List<T>> changedListener) {
this.listeners.add(changedListener);
}
@@ -213,6 +237,9 @@ public final class ListOptionImpl<T> implements ListOption<T> {
private T initialValue;
private boolean collapsed = false;
private boolean available = true;
+ private int minimumNumberOfEntries = 0;
+ private int maximumNumberOfEntries = Integer.MAX_VALUE;
+ private boolean insertEntriesAtEnd = false;
private final List<BiConsumer<Option<List<T>>, List<T>>> listeners = new ArrayList<>();
@Override
@@ -280,6 +307,24 @@ public final class ListOptionImpl<T> implements ListOption<T> {
}
@Override
+ public Builder<T> minimumNumberOfEntries(int number) {
+ this.minimumNumberOfEntries = number;
+ return this;
+ }
+
+ @Override
+ public Builder<T> maximumNumberOfEntries(int number) {
+ this.maximumNumberOfEntries = number;
+ return this;
+ }
+
+ @Override
+ public Builder<T> insertEntriesAtEnd(boolean insertAtEnd) {
+ this.insertEntriesAtEnd = insertAtEnd;
+ return this;
+ }
+
+ @Override
public Builder<T> flag(@NotNull OptionFlag... flag) {
Validate.notNull(flag, "`flag` must not be null");
@@ -319,7 +364,7 @@ public final class ListOptionImpl<T> implements ListOption<T> {
Validate.notNull(binding, "`binding` must not be null");
Validate.notNull(initialValue, "`initialValue` must not be null");
- return new ListOptionImpl<>(name, description, binding, initialValue, controllerFunction, ImmutableSet.copyOf(flags), collapsed, available, listeners);
+ return new ListOptionImpl<>(name, description, binding, initialValue, controllerFunction, ImmutableSet.copyOf(flags), collapsed, available, minimumNumberOfEntries, maximumNumberOfEntries, insertEntriesAtEnd, listeners);
}
}
}