mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 18:48:20 +08:00
feat: Implement more sections functions
This commit is contained in:
@@ -18,7 +18,7 @@ import java.util.stream.Stream;
|
|||||||
* @author Carm
|
* @author Carm
|
||||||
* @since 4.0.0
|
* @since 4.0.0
|
||||||
*/
|
*/
|
||||||
public interface ConfigureSection extends Cloneable {
|
public interface ConfigureSection {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the parent section of this section.
|
* Gets the parent section of this section.
|
||||||
@@ -30,6 +30,24 @@ public interface ConfigureSection extends Cloneable {
|
|||||||
@Contract(pure = true)
|
@Contract(pure = true)
|
||||||
@Nullable ConfigureSection parent();
|
@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.
|
* Gets a set containing all keys in this section.
|
||||||
* <p>
|
* <p>
|
||||||
@@ -190,7 +208,10 @@ public interface ConfigureSection extends Cloneable {
|
|||||||
* @return The section if the path exists and is a section, otherwise null.
|
* @return The section if the path exists and is a section, otherwise null.
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@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.
|
* Get the origin value of the path.
|
||||||
@@ -686,7 +707,8 @@ public interface ConfigureSection extends Cloneable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static <T, C extends Collection<T>> @NotNull C parseCollection(
|
static <T, C extends Collection<T>> @NotNull C parseCollection(
|
||||||
@Nullable List<?> data, @NotNull Supplier<C> constructor,
|
@Nullable List<?> data,
|
||||||
|
@NotNull Supplier<C> constructor,
|
||||||
@NotNull DataFunction<Object, T> parser
|
@NotNull DataFunction<Object, T> parser
|
||||||
) {
|
) {
|
||||||
C values = constructor.get();
|
C values = constructor.get();
|
||||||
|
|||||||
@@ -108,11 +108,6 @@ public abstract class ConfigureSource<
|
|||||||
section().remove(path);
|
section().remove(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable ConfigureSection getSection(@NotNull String path) {
|
|
||||||
return section().getSection(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @Nullable Object get(@NotNull String path) {
|
public @Nullable Object get(@NotNull String path) {
|
||||||
return section().get(path);
|
return section().get(path);
|
||||||
|
|||||||
+5
-14
@@ -8,14 +8,6 @@ import java.util.*;
|
|||||||
|
|
||||||
public abstract class MapSection<R extends MapSection<R>> implements ConfigureSection {
|
public abstract class MapSection<R extends MapSection<R>> 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<String, Object> data;
|
protected final @NotNull Map<String, Object> data;
|
||||||
protected final @Nullable R parent;
|
protected final @Nullable R parent;
|
||||||
|
|
||||||
@@ -57,6 +49,11 @@ public abstract class MapSection<R extends MapSection<R>> implements ConfigureSe
|
|||||||
return this.data;
|
return this.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return data.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
@UnmodifiableView
|
@UnmodifiableView
|
||||||
public @NotNull Map<String, Object> rawMap() {
|
public @NotNull Map<String, Object> rawMap() {
|
||||||
Map<String, Object> output = new LinkedHashMap<>();
|
Map<String, Object> output = new LinkedHashMap<>();
|
||||||
@@ -118,12 +115,6 @@ public abstract class MapSection<R extends MapSection<R>> implements ConfigureSe
|
|||||||
return section == this ? data.get(path) : section.get(childPath(path));
|
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")
|
@SuppressWarnings("unchecked")
|
||||||
private R getSectionFor(String path) {
|
private R getSectionFor(String path) {
|
||||||
int index = path.indexOf(pathSeparator());
|
int index = path.indexOf(pathSeparator());
|
||||||
|
|||||||
+18
-1
@@ -3,10 +3,27 @@ package cc.carm.lib.configuration.source.section;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class RawMapSection extends MapSection<RawMapSection> {
|
public class RawMapSection extends MapSection<RawMapSection> {
|
||||||
|
|
||||||
|
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) {
|
protected RawMapSection(@NotNull Map<?, ?> raw, @Nullable RawMapSection parent) {
|
||||||
super(parent);
|
super(parent);
|
||||||
migrate(raw);
|
migrate(raw);
|
||||||
|
|||||||
Reference in New Issue
Block a user