[core] Introduce arity for structured sub entries
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package de.siphalor.tweed5.core.api;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Getter
|
||||
public enum Arity {
|
||||
SINGLE(1, 1),
|
||||
OPTIONAL(0, 1),
|
||||
MULTIPLE(1, Integer.MAX_VALUE),
|
||||
ANY(0, Integer.MAX_VALUE),
|
||||
;
|
||||
|
||||
private final int min;
|
||||
private final int max;
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package de.siphalor.tweed5.core.api.entry;
|
||||
|
||||
import de.siphalor.tweed5.core.api.Arity;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
import java.util.Collection;
|
||||
@@ -14,6 +15,19 @@ public interface CollectionConfigEntry<E, T extends Collection<E>> extends Struc
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
default void visitInOrder(ConfigEntryVisitor visitor) {
|
||||
if (visitor.enterStructuredEntry(this)) {
|
||||
subEntries().forEach((key, entry) -> {
|
||||
if (visitor.enterStructuredSubEntry(key, Arity.ANY)) {
|
||||
entry.visitInOrder(visitor);
|
||||
visitor.leaveStructuredSubEntry(key, Arity.ANY);
|
||||
}
|
||||
});
|
||||
visitor.leaveStructuredEntry(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
default void visitInOrder(ConfigEntryValueVisitor visitor, @Nullable T value) {
|
||||
if (value == null) {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package de.siphalor.tweed5.core.api.entry;
|
||||
|
||||
import de.siphalor.tweed5.core.api.Arity;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public interface CompoundConfigEntry<T> extends StructuredConfigEntry<T> {
|
||||
@@ -13,4 +15,17 @@ public interface CompoundConfigEntry<T> extends StructuredConfigEntry<T> {
|
||||
<V> V get(T compoundValue, String key);
|
||||
|
||||
T instantiateCompoundValue();
|
||||
|
||||
@Override
|
||||
default void visitInOrder(ConfigEntryVisitor visitor) {
|
||||
if (visitor.enterStructuredEntry(this)) {
|
||||
subEntries().forEach((key, entry) -> {
|
||||
if (visitor.enterStructuredSubEntry(key, Arity.SINGLE)) {
|
||||
entry.visitInOrder(visitor);
|
||||
visitor.leaveStructuredSubEntry(key, Arity.SINGLE);
|
||||
}
|
||||
});
|
||||
visitor.leaveStructuredEntry(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package de.siphalor.tweed5.core.api.entry;
|
||||
|
||||
import de.siphalor.tweed5.core.api.Arity;
|
||||
|
||||
public interface ConfigEntryVisitor {
|
||||
void visitEntry(ConfigEntry<?> entry);
|
||||
|
||||
@@ -8,11 +10,11 @@ public interface ConfigEntryVisitor {
|
||||
return true;
|
||||
}
|
||||
|
||||
default boolean enterStructuredSubEntry(String key) {
|
||||
default boolean enterStructuredSubEntry(String key, Arity arity) {
|
||||
return true;
|
||||
}
|
||||
|
||||
default void leaveStructuredSubEntry(String key) {
|
||||
default void leaveStructuredSubEntry(String key, Arity arity) {
|
||||
}
|
||||
|
||||
default void leaveStructuredEntry(ConfigEntry<?> entry) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package de.siphalor.tweed5.core.api.entry;
|
||||
|
||||
import de.siphalor.tweed5.core.api.Arity;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
@@ -14,4 +15,17 @@ public interface NullableConfigEntry<T extends @Nullable Object> extends Structu
|
||||
}
|
||||
|
||||
ConfigEntry<T> nonNullEntry();
|
||||
|
||||
@Override
|
||||
default void visitInOrder(ConfigEntryVisitor visitor) {
|
||||
if (visitor.enterStructuredEntry(this)) {
|
||||
subEntries().forEach((key, entry) -> {
|
||||
if (visitor.enterStructuredSubEntry(key, Arity.OPTIONAL)) {
|
||||
entry.visitInOrder(visitor);
|
||||
visitor.leaveStructuredSubEntry(key, Arity.OPTIONAL);
|
||||
}
|
||||
});
|
||||
visitor.leaveStructuredEntry(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,18 +10,5 @@ public interface StructuredConfigEntry<T> extends ConfigEntry<T> {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
default void visitInOrder(ConfigEntryVisitor visitor) {
|
||||
if (visitor.enterStructuredEntry(this)) {
|
||||
subEntries().forEach((key, entry) -> {
|
||||
if (visitor.enterStructuredSubEntry(key)) {
|
||||
entry.visitInOrder(visitor);
|
||||
visitor.leaveStructuredSubEntry(key);
|
||||
}
|
||||
});
|
||||
visitor.leaveStructuredEntry(this);
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, ConfigEntry<?>> subEntries();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package de.siphalor.tweed5.defaultextensions.pather.api;
|
||||
|
||||
import de.siphalor.tweed5.core.api.Arity;
|
||||
import de.siphalor.tweed5.core.api.entry.ConfigEntry;
|
||||
import de.siphalor.tweed5.core.api.entry.ConfigEntryVisitor;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -20,8 +21,8 @@ public class PathTrackingConfigEntryVisitor implements ConfigEntryVisitor {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean enterStructuredSubEntry(String key) {
|
||||
boolean enter = delegate.enterStructuredSubEntry(key);
|
||||
public boolean enterStructuredSubEntry(String key, Arity arity) {
|
||||
boolean enter = delegate.enterStructuredSubEntry(key, arity);
|
||||
if (enter) {
|
||||
pathTracking.pushPathPart(key);
|
||||
}
|
||||
@@ -29,8 +30,8 @@ public class PathTrackingConfigEntryVisitor implements ConfigEntryVisitor {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void leaveStructuredSubEntry(String key) {
|
||||
delegate.leaveStructuredSubEntry(key);
|
||||
public void leaveStructuredSubEntry(String key, Arity arity) {
|
||||
delegate.leaveStructuredSubEntry(key, arity);
|
||||
pathTracking.popPathPart();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user