1
mirror of https://github.com/CarmJos/EasyConfiguration.git synced 2026-06-04 18:48:20 +08:00

feat(section): Add #path and #fullPath for sections

This commit is contained in:
2025-02-25 00:04:24 +08:00
parent 842cd78ce3
commit 6f28abebb9
20 changed files with 181 additions and 66 deletions
@@ -10,9 +10,11 @@ public abstract class AbstractMapSection<R extends AbstractMapSection<R>> implem
protected final @NotNull Map<String, Object> data;
protected final @Nullable R parent;
protected final @NotNull String path;
protected AbstractMapSection(@Nullable R parent) {
protected AbstractMapSection(@Nullable R parent, @NotNull String path) {
this.parent = parent;
this.path = path;
this.data = new LinkedHashMap<>();
}
@@ -20,15 +22,17 @@ public abstract class AbstractMapSection<R extends AbstractMapSection<R>> implem
for (Map.Entry<?, ?> entry : data.entrySet()) {
String key = (entry.getKey() == null) ? "" : entry.getKey().toString();
if (entry.getValue() instanceof Map) {
this.data.put(key, createSection((Map<?, ?>) entry.getValue()));
this.data.put(key, createSection(key, (Map<?, ?>) entry.getValue()));
} else if (entry.getValue() instanceof List) {
List<Object> list = new ArrayList<>();
int index = 0;
for (Object obj : (List<?>) entry.getValue()) {
if (obj instanceof Map) {
list.add(createSection((Map<?, ?>) obj));
list.add(createSection(key + "[" + index + "]", (Map<?, ?>) obj));
} else {
list.add(obj);
}
index++;
}
this.data.put(key, list);
} else {
@@ -40,11 +44,11 @@ public abstract class AbstractMapSection<R extends AbstractMapSection<R>> implem
public abstract @NotNull R self();
@Override
public abstract @NotNull R createSection(@NotNull Map<?, ?> data);
public abstract @NotNull R createSection(@NotNull String path, @NotNull Map<?, ?> data);
@Override
public @NotNull R createSection() {
return createSection(new LinkedHashMap<>());
public @NotNull String path() {
return this.path;
}
@Override
@@ -63,7 +67,12 @@ public abstract class AbstractMapSection<R extends AbstractMapSection<R>> implem
@Override
public boolean isEmpty() {
return data.isEmpty();
return this.data.isEmpty();
}
@Override
public int size(boolean deep) {
return deep ? getKeys(true).size() : this.data.size();
}
@UnmodifiableView
@@ -86,12 +95,12 @@ public abstract class AbstractMapSection<R extends AbstractMapSection<R>> implem
@Override
public @NotNull Map<String, Object> getValues(boolean deep) {
return Collections.unmodifiableMap(deep ? mappingValues(this, null, true) : data());
return Collections.unmodifiableMap(deep ? mappingValues(this, null, true, String.valueOf(pathSeparator())) : data());
}
@Override
public void set(@NotNull String path, @Nullable Object value) {
if (value instanceof Map) value = createSection((Map<?, ?>) value);
if (value instanceof Map) value = createSection(path, (Map<?, ?>) value);
R section = getSectionFor(path);
if (section == this) {
@@ -125,7 +134,7 @@ public abstract class AbstractMapSection<R extends AbstractMapSection<R>> implem
if (index == -1) return self();
String root = path.substring(0, index);
return (R) data().computeIfAbsent(root, k -> createSection());
return (R) computeSection(root);
}
/**
@@ -135,14 +144,14 @@ public abstract class AbstractMapSection<R extends AbstractMapSection<R>> implem
* @param parent The parent path
* @param deep If the mapping should be deep
*/
protected Map<String, Object> mappingValues(@NotNull AbstractMapSection<?> section, @Nullable String parent, boolean deep) {
protected static Map<String, Object> mappingValues(@NotNull AbstractMapSection<?> section, @Nullable String parent, boolean deep, String pathSeparator) {
Map<String, Object> output = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : section.data().entrySet()) {
String path = (parent == null ? "" : parent + pathSeparator()) + entry.getKey();
String path = (parent == null ? "" : parent + pathSeparator) + entry.getKey();
output.remove(path);
output.put(path, entry.getValue());
if (deep && entry.getValue() instanceof AbstractMapSection<?>) {
output.putAll(mappingValues((AbstractMapSection<?>) entry.getValue(), path, true));
output.putAll(mappingValues((AbstractMapSection<?>) entry.getValue(), path, true, pathSeparator));
}
}
return output;
@@ -32,6 +32,11 @@ public class ImmutableSection implements ConfigureSection {
return this.parent;
}
@Override
public @NotNull String path() {
return section().path();
}
private @NotNull ConfigureSection section() {
return section;
}
@@ -52,8 +57,8 @@ public class ImmutableSection implements ConfigureSection {
}
@Override
public @NotNull ConfigureSection createSection(@NotNull Map<?, ?> data) {
return new ImmutableSection(this, section().createSection(data));
public @NotNull ImmutableSection createSection(@NotNull String path, @NotNull Map<?, ?> data) {
return new ImmutableSection(this, section().createSection(path, data));
}
@Override
@@ -11,7 +11,7 @@ import java.util.function.Supplier;
public class MemorySection extends AbstractMapSection<MemorySection> {
public static MemorySection of() {
return of((MemorySection) null);
return of(new LinkedHashMap<>());
}
public static MemorySection of(@NotNull Consumer<Map<String, Object>> data) {
@@ -23,23 +23,23 @@ public class MemorySection extends AbstractMapSection<MemorySection> {
}
public static MemorySection of(@NotNull Supplier<Map<?, ?>> data) {
return of(data.get(), null);
return of(data.get());
}
public static MemorySection of(@NotNull Map<?, ?> data) {
return of(data, null);
return of(data, null, "");
}
public static MemorySection of(@Nullable MemorySection parent) {
return of(new LinkedHashMap<>(), parent);
public static MemorySection of(@Nullable MemorySection parent, @NotNull String path) {
return of(new LinkedHashMap<>(), parent, path);
}
public static MemorySection of(@NotNull Map<?, ?> data, @Nullable MemorySection parent) {
return new MemorySection(data, parent);
public static MemorySection of(@NotNull Map<?, ?> data, @Nullable MemorySection parent, @NotNull String path) {
return new MemorySection(data, parent, path);
}
public MemorySection(@NotNull Map<?, ?> raw, @Nullable MemorySection parent) {
super(parent);
public MemorySection(@NotNull Map<?, ?> raw, @Nullable MemorySection parent, @NotNull String path) {
super(parent, path);
migrate(raw);
}
@@ -49,8 +49,8 @@ public class MemorySection extends AbstractMapSection<MemorySection> {
}
@Override
public @NotNull MemorySection createSection(@NotNull Map<?, ?> data) {
return new MemorySection(data, this);
public @NotNull MemorySection createSection(@NotNull String path, @NotNull Map<?, ?> data) {
return new MemorySection(data, this, path);
}
}
@@ -28,6 +28,11 @@ public class ShadedSection implements ConfigureSection {
return this.parent;
}
@Override
public @NotNull String path() {
return this.template.path();
}
@Override
public @NotNull @UnmodifiableView Map<String, Object> getValues(boolean deep) {
if (source == null) return template.getValues(deep);
@@ -95,11 +100,11 @@ public class ShadedSection implements ConfigureSection {
}
@Override
public @NotNull ConfigureSection createSection(@NotNull Map<?, ?> data) {
public @NotNull ConfigureSection createSection(@NotNull String path, @NotNull Map<?, ?> data) {
if (source == null) {
return new ShadedSection(this, template, MemorySection.of(data));
} else {
ConfigureSection section = source.createSection(data);
ConfigureSection section = source.computeSection(path, data);
return new ShadedSection(this, template, section);
}
}
@@ -9,19 +9,19 @@ import java.util.Map;
public class SourcedSection extends AbstractMapSection<SourcedSection> {
public static @NotNull SourcedSection root(@NotNull ConfigureSource<? extends SourcedSection, ?, ?> source) {
return new SourcedSection(source, new LinkedHashMap<>(), null);
return new SourcedSection(source, new LinkedHashMap<>(), null, "");
}
public static @NotNull SourcedSection root(@NotNull ConfigureSource<? extends SourcedSection, ?, ?> source,
@Nullable Map<?, ?> raw) {
return new SourcedSection(source, raw == null ? new LinkedHashMap<>() : raw, null);
return new SourcedSection(source, raw == null ? new LinkedHashMap<>() : raw, null, "");
}
protected final @NotNull ConfigureSource<? extends SourcedSection, ?, ?> source;
public SourcedSection(@NotNull ConfigureSource<? extends SourcedSection, ?, ?> source,
@NotNull Map<?, ?> raw, @Nullable SourcedSection parent) {
super(parent);
@NotNull Map<?, ?> raw, @Nullable SourcedSection parent, @NotNull String path) {
super(parent, path);
this.source = source;
migrate(raw);
}
@@ -41,8 +41,8 @@ public class SourcedSection extends AbstractMapSection<SourcedSection> {
}
@Override
public @NotNull SourcedSection createSection(@NotNull Map<?, ?> data) {
return new SourcedSection(source(), data, this);
public @NotNull SourcedSection createSection(@NotNull String path, @NotNull Map<?, ?> data) {
return new SourcedSection(source(), data, this, path);
}
}