From d81855697c22499edf49d1a6aeac13e4c1f6c057 Mon Sep 17 00:00:00 2001 From: Carm Date: Fri, 21 Feb 2025 11:27:03 +0800 Subject: [PATCH] feat: Implement more sections functions --- .../source/section/ConfigureSection.java | 28 +++++++++++++++++-- .../source/section/ConfigureSource.java | 5 ---- .../source/section/MapSection.java | 19 ++++--------- .../source/section/RawMapSection.java | 19 ++++++++++++- 4 files changed, 48 insertions(+), 23 deletions(-) diff --git a/core/src/main/java/cc/carm/lib/configuration/source/section/ConfigureSection.java b/core/src/main/java/cc/carm/lib/configuration/source/section/ConfigureSection.java index 52ad20a..2bd49e9 100644 --- a/core/src/main/java/cc/carm/lib/configuration/source/section/ConfigureSection.java +++ b/core/src/main/java/cc/carm/lib/configuration/source/section/ConfigureSection.java @@ -18,7 +18,7 @@ import java.util.stream.Stream; * @author Carm * @since 4.0.0 */ -public interface ConfigureSection extends Cloneable { +public interface ConfigureSection { /** * Gets the parent section of this section. @@ -30,6 +30,24 @@ public interface ConfigureSection extends Cloneable { @Contract(pure = true) @Nullable ConfigureSection parent(); + /** + * Gets if this section is a root section. + * + * @return True if this section is a root section, false otherwise. + */ + default boolean isRoot() { + return parent() == null; + } + + /** + * Gets if this section is empty. + * + * @return True if this section is empty, false otherwise. + */ + default boolean isEmpty() { + return getValues(false).isEmpty(); + } + /** * Gets a set containing all keys in this section. *

@@ -190,7 +208,10 @@ public interface ConfigureSection extends Cloneable { * @return The section if the path exists and is a section, otherwise null. */ @Nullable - ConfigureSection getSection(@NotNull String path); + default ConfigureSection getSection(@NotNull String path) { + Object val = get(path); + return (val instanceof ConfigureSection) ? (ConfigureSection) val : null; + } /** * Get the origin value of the path. @@ -686,7 +707,8 @@ public interface ConfigureSection extends Cloneable { } static > @NotNull C parseCollection( - @Nullable List data, @NotNull Supplier constructor, + @Nullable List data, + @NotNull Supplier constructor, @NotNull DataFunction parser ) { C values = constructor.get(); diff --git a/core/src/main/java/cc/carm/lib/configuration/source/section/ConfigureSource.java b/core/src/main/java/cc/carm/lib/configuration/source/section/ConfigureSource.java index dde17cf..6d55738 100644 --- a/core/src/main/java/cc/carm/lib/configuration/source/section/ConfigureSource.java +++ b/core/src/main/java/cc/carm/lib/configuration/source/section/ConfigureSource.java @@ -108,11 +108,6 @@ public abstract class ConfigureSource< section().remove(path); } - @Override - public @Nullable ConfigureSection getSection(@NotNull String path) { - return section().getSection(path); - } - @Override public @Nullable Object get(@NotNull String path) { return section().get(path); diff --git a/features/section/src/main/java/cc/carm/lib/configuration/source/section/MapSection.java b/features/section/src/main/java/cc/carm/lib/configuration/source/section/MapSection.java index 347e6db..0b66c76 100644 --- a/features/section/src/main/java/cc/carm/lib/configuration/source/section/MapSection.java +++ b/features/section/src/main/java/cc/carm/lib/configuration/source/section/MapSection.java @@ -8,14 +8,6 @@ import java.util.*; public abstract class MapSection> implements ConfigureSection { - public static RawMapSection of() { - return new RawMapSection(new LinkedHashMap<>(), null); - } - - public static RawMapSection of(@NotNull Map data) { - return new RawMapSection(data, null); - } - protected final @NotNull Map data; protected final @Nullable R parent; @@ -57,6 +49,11 @@ public abstract class MapSection> implements ConfigureSe return this.data; } + @Override + public boolean isEmpty() { + return data.isEmpty(); + } + @UnmodifiableView public @NotNull Map rawMap() { Map output = new LinkedHashMap<>(); @@ -118,12 +115,6 @@ public abstract class MapSection> implements ConfigureSe return section == this ? data.get(path) : section.get(childPath(path)); } - @Override - public @Nullable ConfigureSection getSection(@NotNull String path) { - Object val = get(path); - return (val instanceof ConfigureSection) ? (ConfigureSection) val : null; - } - @SuppressWarnings("unchecked") private R getSectionFor(String path) { int index = path.indexOf(pathSeparator()); diff --git a/features/section/src/main/java/cc/carm/lib/configuration/source/section/RawMapSection.java b/features/section/src/main/java/cc/carm/lib/configuration/source/section/RawMapSection.java index f23d5e6..074f735 100644 --- a/features/section/src/main/java/cc/carm/lib/configuration/source/section/RawMapSection.java +++ b/features/section/src/main/java/cc/carm/lib/configuration/source/section/RawMapSection.java @@ -3,10 +3,27 @@ package cc.carm.lib.configuration.source.section; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.LinkedHashMap; import java.util.Map; public class RawMapSection extends MapSection { - + + public static RawMapSection of() { + return of((RawMapSection) null); + } + + public static RawMapSection of(@NotNull Map data) { + return of(data, null); + } + + public static RawMapSection of(@Nullable RawMapSection parent) { + return of(new LinkedHashMap<>(), parent); + } + + public static RawMapSection of(@NotNull Map data, @Nullable RawMapSection parent) { + return new RawMapSection(data, parent); + } + protected RawMapSection(@NotNull Map raw, @Nullable RawMapSection parent) { super(parent); migrate(raw);