mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 18:48:20 +08:00
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6a007c5187 | |||
| 43b00f2b69 | |||
| 2e61e66cdb | |||
| 39f946c28e | |||
| 25931ffd7e | |||
| de103da879 | |||
| 457c22d461 | |||
| aa4225dbba | |||
| ddd33154be | |||
| 727c26a2fb | |||
| 9c95a16d90 | |||
| 92c05f1a59 | |||
| 739ed41885 | |||
| a66da01996 |
+3
-3
@@ -5,12 +5,12 @@
|
||||
<parent>
|
||||
<artifactId>easyconfiguration-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>3.4.0</version>
|
||||
<version>3.6.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
</properties>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package cc.carm.lib.configuration.core.builder;
|
||||
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||
import cc.carm.lib.configuration.core.value.ValueManifest;
|
||||
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -66,4 +67,11 @@ public abstract class AbstractConfigBuilder<T, B extends AbstractConfigBuilder<T
|
||||
return defaults(defaultValueSupplier.get());
|
||||
}
|
||||
|
||||
protected @NotNull ValueManifest<T> buildManifest() {
|
||||
return ValueManifest.of(
|
||||
this.provider, this.path,
|
||||
this.headerComments, this.inlineComment, this.defaultValue
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package cc.carm.lib.configuration.core.builder;
|
||||
|
||||
import cc.carm.lib.configuration.core.builder.list.ConfigListBuilder;
|
||||
import cc.carm.lib.configuration.core.builder.map.ConfigMapBuilder;
|
||||
import cc.carm.lib.configuration.core.builder.map.ConfigMapCreator;
|
||||
import cc.carm.lib.configuration.core.builder.value.ConfigValueBuilder;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -19,24 +20,24 @@ public class ConfigBuilder {
|
||||
return new ConfigListBuilder<>(valueClass);
|
||||
}
|
||||
|
||||
public <K, V> @NotNull ConfigMapBuilder<LinkedHashMap<K, V>, K, V> asMap(@NotNull Class<K> keyClass,
|
||||
@NotNull Class<V> valueClass) {
|
||||
return new ConfigMapBuilder<>(LinkedHashMap::new, keyClass, valueClass);
|
||||
public <K, V> @NotNull ConfigMapCreator<K, V> asMap(@NotNull Class<K> keyClass,
|
||||
@NotNull Class<V> valueClass) {
|
||||
return new ConfigMapCreator<>(keyClass, valueClass);
|
||||
}
|
||||
|
||||
public <K, V> @NotNull ConfigMapBuilder<HashMap<K, V>, K, V> asHashMap(@NotNull Class<K> keyClass,
|
||||
@NotNull Class<V> valueClass) {
|
||||
return asMap(keyClass, valueClass).supplier(HashMap::new);
|
||||
@NotNull Class<V> valueClass) {
|
||||
return asMap(keyClass, valueClass).asHashMap();
|
||||
}
|
||||
|
||||
public <K, V> @NotNull ConfigMapBuilder<LinkedHashMap<K, V>, K, V> asLinkedMap(@NotNull Class<K> keyClass,
|
||||
@NotNull Class<V> valueClass) {
|
||||
return asMap(keyClass, valueClass);
|
||||
@NotNull Class<V> valueClass) {
|
||||
return asMap(keyClass, valueClass).asLinkedMap();
|
||||
}
|
||||
|
||||
public <K extends Comparable<K>, V> @NotNull ConfigMapBuilder<TreeMap<K, V>, K, V> asTreeMap(@NotNull Class<K> keyClass,
|
||||
@NotNull Class<V> valueClass) {
|
||||
return asMap(keyClass, valueClass).supplier(TreeMap::new);
|
||||
@NotNull Class<V> valueClass) {
|
||||
return asMap(keyClass, valueClass).asTreeMap();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+6
-3
@@ -7,6 +7,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public class SourceListBuilder<S, V> extends CommonConfigBuilder<List<V>, SourceListBuilder<S, V>> {
|
||||
@@ -37,6 +38,10 @@ public class SourceListBuilder<S, V> extends CommonConfigBuilder<List<V>, Source
|
||||
return defaults(new ArrayList<>(Arrays.asList(values)));
|
||||
}
|
||||
|
||||
public final @NotNull SourceListBuilder<S, V> defaults(@NotNull Collection<V> values) {
|
||||
return defaults(new ArrayList<>(values));
|
||||
}
|
||||
|
||||
public @NotNull SourceListBuilder<S, V> parseSource(ConfigDataFunction<Object, S> sourceParser) {
|
||||
this.sourceParser = sourceParser;
|
||||
return this;
|
||||
@@ -65,9 +70,7 @@ public class SourceListBuilder<S, V> extends CommonConfigBuilder<List<V>, Source
|
||||
@Override
|
||||
public @NotNull ConfiguredList<V> build() {
|
||||
return new ConfiguredList<>(
|
||||
this.provider, this.path,
|
||||
this.headerComments, this.inlineComment,
|
||||
this.valueClass, this.defaultValue,
|
||||
buildManifest(), this.valueClass,
|
||||
this.sourceParser.andThen(this.valueParser),
|
||||
this.valueSerializer.andThen(sourceSerializer)
|
||||
);
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package cc.carm.lib.configuration.core.builder.map;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class ConfigMapCreator<K, V> {
|
||||
|
||||
protected final @NotNull Class<K> keyClass;
|
||||
protected final @NotNull Class<V> valueClass;
|
||||
|
||||
public ConfigMapCreator(@NotNull Class<K> keyClass, @NotNull Class<V> valueClass) {
|
||||
this.keyClass = keyClass;
|
||||
this.valueClass = valueClass;
|
||||
}
|
||||
|
||||
public <M extends Map<K, V>> @NotNull ConfigMapBuilder<M, K, V> asMap(Supplier<M> mapSuppler) {
|
||||
return new ConfigMapBuilder<>(mapSuppler, keyClass, valueClass);
|
||||
}
|
||||
|
||||
public @NotNull ConfigMapBuilder<HashMap<K, V>, K, V> asHashMap() {
|
||||
return asMap(HashMap::new);
|
||||
}
|
||||
|
||||
public @NotNull ConfigMapBuilder<LinkedHashMap<K, V>, K, V> asLinkedMap() {
|
||||
return asMap(LinkedHashMap::new);
|
||||
}
|
||||
|
||||
public @NotNull ConfigMapBuilder<TreeMap<K, V>, K, V> asTreeMap() {
|
||||
return asMap(TreeMap::new);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package cc.carm.lib.configuration.core.builder.map;
|
||||
|
||||
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder;
|
||||
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
||||
import cc.carm.lib.configuration.core.value.ValueManifest;
|
||||
import cc.carm.lib.configuration.core.value.type.ConfiguredMap;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -96,10 +97,8 @@ public class SourceMapBuilder<M extends Map<K, V>, S, K, V> extends CommonConfig
|
||||
@Override
|
||||
public @NotNull ConfiguredMap<K, V> build() {
|
||||
return new ConfiguredMap<>(
|
||||
this.provider, this.path,
|
||||
this.headerComments, this.inlineComment,
|
||||
this.defaultValue, this.supplier,
|
||||
this.keyClass, this.keyParser,
|
||||
new ValueManifest<>(provider, path, headerComments, inlineComment, defaultValue),
|
||||
this.supplier, this.keyClass, this.keyParser,
|
||||
this.valueClass, this.sourceParser.andThen(this.valueParser),
|
||||
this.keySerializer, this.valueSerializer.andThen(this.sourceSerializer)
|
||||
);
|
||||
|
||||
+1
-6
@@ -44,12 +44,7 @@ public class SectionValueBuilder<V>
|
||||
|
||||
@Override
|
||||
public @NotNull ConfiguredSection<V> build() {
|
||||
return new ConfiguredSection<>(
|
||||
this.provider, this.path,
|
||||
this.headerComments, this.inlineComment,
|
||||
this.valueClass, this.defaultValue,
|
||||
this.parser, this.serializer
|
||||
);
|
||||
return new ConfiguredSection<>(buildManifest(), this.valueClass, this.parser, this.serializer);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-3
@@ -57,9 +57,7 @@ public class SourceValueBuilder<S, V> extends CommonConfigBuilder<V, SourceValue
|
||||
@Override
|
||||
public @NotNull ConfiguredValue<V> build() {
|
||||
return new ConfiguredValue<>(
|
||||
this.provider, this.path,
|
||||
this.headerComments, this.inlineComment,
|
||||
this.valueClass, this.defaultValue,
|
||||
buildManifest(), this.valueClass,
|
||||
this.valueParser.compose(this.sourceParser),
|
||||
this.valueSerializer.andThen(sourceSerializer)
|
||||
);
|
||||
|
||||
@@ -62,6 +62,94 @@ public interface ConfigDataFunction<T, R> {
|
||||
};
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
static <V> @NotNull ConfigDataFunction<String, V> parseString(Class<V> valueClass) {
|
||||
return (input) -> {
|
||||
if (valueClass.isInstance(input)) return valueClass.cast(input);
|
||||
else throw new IllegalArgumentException("Cannot cast string to " + valueClass.getName());
|
||||
};
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
static @NotNull ConfigDataFunction<Object, Integer> intValue() {
|
||||
return (input) -> {
|
||||
if (input instanceof Integer) {
|
||||
return (Integer) input;
|
||||
} else if (input instanceof Number) {
|
||||
return ((Number) input).intValue();
|
||||
} else throw new IllegalArgumentException("Cannot cast value to " + Integer.class.getName());
|
||||
};
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
static @NotNull ConfigDataFunction<Object, Short> shortValue() {
|
||||
return (input) -> {
|
||||
if (input instanceof Short) {
|
||||
return (Short) input;
|
||||
} else if (input instanceof Number) {
|
||||
return ((Number) input).shortValue();
|
||||
} else throw new IllegalArgumentException("Cannot cast value to " + Short.class.getName());
|
||||
};
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
static @NotNull ConfigDataFunction<Object, Double> doubleValue() {
|
||||
return (input) -> {
|
||||
if (input instanceof Double) {
|
||||
return (Double) input;
|
||||
} else if (input instanceof Number) {
|
||||
return ((Number) input).doubleValue();
|
||||
} else throw new IllegalArgumentException("Cannot cast value to " + Double.class.getName());
|
||||
};
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
static @NotNull ConfigDataFunction<Object, Byte> byteValue() {
|
||||
return (input) -> {
|
||||
if (input instanceof Byte) {
|
||||
return (Byte) input;
|
||||
} else if (input instanceof Number) {
|
||||
return ((Number) input).byteValue();
|
||||
} else throw new IllegalArgumentException("Cannot cast value to " + Byte.class.getName());
|
||||
};
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
static @NotNull ConfigDataFunction<Object, Float> floatValue() {
|
||||
return (input) -> {
|
||||
if (input instanceof Float) {
|
||||
return (Float) input;
|
||||
} else if (input instanceof Number) {
|
||||
return ((Number) input).floatValue();
|
||||
} else throw new IllegalArgumentException("Cannot cast value to " + Float.class.getName());
|
||||
};
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
static @NotNull ConfigDataFunction<Object, Long> longValue() {
|
||||
return (input) -> {
|
||||
if (input instanceof Long) {
|
||||
return (Long) input;
|
||||
} else if (input instanceof Number) {
|
||||
return ((Number) input).longValue();
|
||||
} else throw new IllegalArgumentException("Cannot cast value to " + Long.class.getName());
|
||||
};
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
static @NotNull ConfigDataFunction<Object, Boolean> booleanValue() {
|
||||
return (input) -> {
|
||||
if (input instanceof Boolean) {
|
||||
return (Boolean) input;
|
||||
} else if (input instanceof String) {
|
||||
String s = (String) input;
|
||||
return Boolean.parseBoolean(s) || "yes".equalsIgnoreCase(s);
|
||||
} else if (input instanceof Integer) {
|
||||
return ((Integer) input) == 1;
|
||||
} else throw new IllegalArgumentException("Cannot cast value to " + Boolean.class.getName());
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -90,92 +90,39 @@ public interface ConfigValueParser<T, R> {
|
||||
};
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
static @NotNull <T> ConfigValueParser<T, String> castToString(Class<T> clazz) {
|
||||
return (input, defaultValue) -> {
|
||||
if (input instanceof String) return (String) input;
|
||||
else return input.toString();
|
||||
};
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
static @NotNull ConfigValueParser<Object, Integer> intValue() {
|
||||
return (input, defaultValue) -> {
|
||||
if (input instanceof Integer) {
|
||||
return (Integer) input;
|
||||
} else if (input instanceof Number) {
|
||||
return ((Number) input).intValue();
|
||||
} else throw new IllegalArgumentException("Cannot cast value to " + Integer.class.getName());
|
||||
};
|
||||
return (input, defaultValue) -> ConfigDataFunction.intValue().parse(input);
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
static @NotNull ConfigValueParser<Object, Short> shortValue() {
|
||||
return (input, defaultValue) -> {
|
||||
if (input instanceof Short) {
|
||||
return (Short) input;
|
||||
} else if (input instanceof Number) {
|
||||
return ((Number) input).shortValue();
|
||||
} else throw new IllegalArgumentException("Cannot cast value to " + Short.class.getName());
|
||||
};
|
||||
return (input, defaultValue) -> ConfigDataFunction.shortValue().parse(input);
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
static @NotNull ConfigValueParser<Object, Double> doubleValue() {
|
||||
return (input, defaultValue) -> {
|
||||
if (input instanceof Double) {
|
||||
return (Double) input;
|
||||
} else if (input instanceof Number) {
|
||||
return ((Number) input).doubleValue();
|
||||
} else throw new IllegalArgumentException("Cannot cast value to " + Double.class.getName());
|
||||
};
|
||||
return (input, defaultValue) -> ConfigDataFunction.doubleValue().parse(input);
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
static @NotNull ConfigValueParser<Object, Byte> byteValue() {
|
||||
return (input, defaultValue) -> {
|
||||
if (input instanceof Byte) {
|
||||
return (Byte) input;
|
||||
} else if (input instanceof Number) {
|
||||
return ((Number) input).byteValue();
|
||||
} else throw new IllegalArgumentException("Cannot cast value to " + Byte.class.getName());
|
||||
};
|
||||
return (input, defaultValue) -> ConfigDataFunction.byteValue().parse(input);
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
static @NotNull ConfigValueParser<Object, Float> floatValue() {
|
||||
return (input, defaultValue) -> {
|
||||
if (input instanceof Float) {
|
||||
return (Float) input;
|
||||
} else if (input instanceof Number) {
|
||||
return ((Number) input).floatValue();
|
||||
} else throw new IllegalArgumentException("Cannot cast value to " + Float.class.getName());
|
||||
};
|
||||
return (input, defaultValue) -> ConfigDataFunction.floatValue().parse(input);
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
static @NotNull ConfigValueParser<Object, Long> longValue() {
|
||||
return (input, defaultValue) -> {
|
||||
if (input instanceof Long) {
|
||||
return (Long) input;
|
||||
} else if (input instanceof Number) {
|
||||
return ((Number) input).longValue();
|
||||
} else throw new IllegalArgumentException("Cannot cast value to " + Long.class.getName());
|
||||
};
|
||||
return (input, defaultValue) -> ConfigDataFunction.longValue().parse(input);
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
static @NotNull ConfigValueParser<Object, Boolean> booleanValue() {
|
||||
return (input, defaultValue) -> {
|
||||
if (input instanceof Boolean) {
|
||||
return (Boolean) input;
|
||||
} else if (input instanceof String) {
|
||||
String s = (String) input;
|
||||
return Boolean.parseBoolean(s) || "yes".equalsIgnoreCase(s);
|
||||
} else if (input instanceof Integer) {
|
||||
return ((Integer) input) == 1;
|
||||
} else throw new IllegalArgumentException("Cannot cast value to " + Boolean.class.getName());
|
||||
};
|
||||
return (input, defaultValue) -> ConfigDataFunction.booleanValue().parse(input);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import org.jetbrains.annotations.Unmodifiable;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public abstract class ConfigurationComments {
|
||||
public class ConfigurationComments {
|
||||
|
||||
protected final @NotNull Map<String, List<String>> headerComments = new HashMap<>();
|
||||
protected final @NotNull Map<String, String> inlineComments = new HashMap<>();
|
||||
|
||||
@@ -11,11 +11,16 @@ import org.jetbrains.annotations.Unmodifiable;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 配置文件提供者,用于为 {@link ConfigValue} 提供配置文件的源,以便实现读取、保存等操作。
|
||||
*
|
||||
* @param <W> 配置文件的原生功能类
|
||||
*/
|
||||
public abstract class ConfigurationProvider<W extends ConfigurationWrapper<?>> {
|
||||
|
||||
protected long updateTime;
|
||||
|
||||
public ConfigurationProvider() {
|
||||
protected ConfigurationProvider() {
|
||||
this.updateTime = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@ public abstract class FileConfigProvider<W extends ConfigurationWrapper<?>> exte
|
||||
|
||||
protected final @NotNull File file;
|
||||
|
||||
public FileConfigProvider(@NotNull File file) {
|
||||
protected FileConfigProvider(@NotNull File file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,62 +2,42 @@ package cc.carm.lib.configuration.core.value;
|
||||
|
||||
import cc.carm.lib.configuration.core.builder.ConfigBuilder;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.Unmodifiable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
public abstract class ConfigValue<T> {
|
||||
public abstract class ConfigValue<T> extends ValueManifest<T> {
|
||||
|
||||
public static @NotNull ConfigBuilder builder() {
|
||||
return new ConfigBuilder();
|
||||
}
|
||||
|
||||
protected @Nullable ConfigurationProvider<?> provider;
|
||||
protected @Nullable String configPath;
|
||||
protected ConfigValue(@NotNull ValueManifest<T> manifest) {
|
||||
super(manifest.provider, manifest.configPath, manifest.headerComments, manifest.inlineComment, manifest.defaultValue);
|
||||
}
|
||||
|
||||
protected @Nullable List<String> headerComments;
|
||||
protected @Nullable String inlineComments;
|
||||
|
||||
protected @Nullable T defaultValue;
|
||||
|
||||
public ConfigValue(@Nullable ConfigurationProvider<?> provider, @Nullable String configPath,
|
||||
@Nullable List<String> headerComments, @Nullable String inlineComments,
|
||||
@Nullable T defaultValue) {
|
||||
this.provider = provider;
|
||||
this.configPath = configPath;
|
||||
this.headerComments = headerComments;
|
||||
this.inlineComments = inlineComments;
|
||||
this.defaultValue = defaultValue;
|
||||
/**
|
||||
* @param provider 配置文件提供者
|
||||
* @param configPath 配置路径
|
||||
* @param headerComments 头部注释内容
|
||||
* @param inlineComments 行内注释内容
|
||||
* @param defaultValue 默认参数值
|
||||
* @deprecated 请使用 {@link #ConfigValue(ValueManifest)} 构造器。
|
||||
*/
|
||||
@Deprecated
|
||||
protected ConfigValue(@Nullable ConfigurationProvider<?> provider, @Nullable String configPath,
|
||||
@Nullable List<String> headerComments, @Nullable String inlineComments,
|
||||
@Nullable T defaultValue) {
|
||||
super(provider, configPath, headerComments, inlineComments, defaultValue);
|
||||
}
|
||||
|
||||
public void initialize(@NotNull ConfigurationProvider<?> provider, boolean saveDefault, @NotNull String configPath,
|
||||
@Nullable List<String> headerComments, @Nullable String inlineComments) {
|
||||
if (this.provider == null) this.provider = provider;
|
||||
if (this.configPath == null) this.configPath = configPath;
|
||||
if (this.headerComments == null) this.headerComments = headerComments;
|
||||
if (this.inlineComments == null) this.inlineComments = inlineComments;
|
||||
this.initialize(provider, configPath, headerComments, inlineComments);
|
||||
if (saveDefault) setDefault();
|
||||
|
||||
if (getHeaderComments() != null) {
|
||||
this.provider.setHeaderComment(getConfigPath(), getHeaderComments());
|
||||
}
|
||||
if (getInlineComments() != null) {
|
||||
this.provider.setInlineComment(getConfigPath(), getInlineComments());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public @Nullable T getDefaultValue() {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public void setDefaultValue(@Nullable T defaultValue) {
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -131,39 +111,4 @@ public abstract class ConfigValue<T> {
|
||||
else return false;
|
||||
}
|
||||
|
||||
public @NotNull ConfigurationProvider<?> getProvider() {
|
||||
return Optional.ofNullable(this.provider)
|
||||
.orElseThrow(() -> new IllegalStateException("Value(" + configPath + ") does not have a provider."));
|
||||
}
|
||||
|
||||
public final @NotNull ConfigurationWrapper<?> getConfiguration() {
|
||||
try {
|
||||
return getProvider().getConfiguration();
|
||||
} catch (Exception ex) {
|
||||
throw new IllegalStateException("Value(" + configPath + ") has not been initialized", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public @NotNull String getConfigPath() {
|
||||
return Optional.ofNullable(this.configPath)
|
||||
.orElseThrow(() -> new IllegalStateException("No section path provided."));
|
||||
}
|
||||
|
||||
protected Object getValue() {
|
||||
return getConfiguration().get(getConfigPath());
|
||||
}
|
||||
|
||||
protected void setValue(@Nullable Object value) {
|
||||
getConfiguration().set(getConfigPath(), value);
|
||||
}
|
||||
|
||||
public @Nullable String getInlineComments() {
|
||||
return inlineComments;
|
||||
}
|
||||
|
||||
@Unmodifiable
|
||||
public @Nullable List<String> getHeaderComments() {
|
||||
return headerComments;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
package cc.carm.lib.configuration.core.value;
|
||||
|
||||
import cc.carm.lib.configuration.core.ConfigInitializer;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.Unmodifiable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 配置值描述清单。用于描述一个配置值的相关基础信息。
|
||||
*
|
||||
* @param <T> 配置值类型
|
||||
* @author CarmJos
|
||||
*/
|
||||
public class ValueManifest<T> {
|
||||
|
||||
public static <V> ValueManifest<V> of(@Nullable ConfigurationProvider<?> provider, @Nullable String configPath,
|
||||
@Nullable List<String> headerComments, @Nullable String inlineComments) {
|
||||
return new ValueManifest<>(provider, configPath, headerComments, inlineComments, null);
|
||||
}
|
||||
|
||||
public static <V> ValueManifest<V> of(@Nullable ConfigurationProvider<?> provider, @Nullable String configPath,
|
||||
@Nullable List<String> headerComments, @Nullable String inlineComments,
|
||||
@Nullable V defaultValue) {
|
||||
return new ValueManifest<>(provider, configPath, headerComments, inlineComments, defaultValue);
|
||||
}
|
||||
|
||||
protected @Nullable ConfigurationProvider<?> provider;
|
||||
protected @Nullable String configPath;
|
||||
|
||||
protected @Nullable List<String> headerComments;
|
||||
protected @Nullable String inlineComment;
|
||||
|
||||
protected @Nullable T defaultValue;
|
||||
|
||||
/**
|
||||
* @param provider 配置文件提供者
|
||||
* @param configPath 配置路径
|
||||
* @param headerComments 头部注释内容
|
||||
* @param inlineComment 行内注释内容
|
||||
* @param defaultValue 默认参数值
|
||||
*/
|
||||
public ValueManifest(@Nullable ConfigurationProvider<?> provider, @Nullable String configPath,
|
||||
@Nullable List<String> headerComments, @Nullable String inlineComment,
|
||||
@Nullable T defaultValue) {
|
||||
this.provider = provider;
|
||||
this.configPath = configPath;
|
||||
this.headerComments = headerComments;
|
||||
this.inlineComment = inlineComment;
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 此方法提供给 {@link ConfigInitializer},以便对配置值的相关信息进行统一初始化操作。
|
||||
*
|
||||
* @param provider 配置文件提供者
|
||||
* @param configPath 配置路径
|
||||
* @param headerComments 头部注释内容
|
||||
* @param inlineComment 行内注释内容
|
||||
*/
|
||||
protected void initialize(@NotNull ConfigurationProvider<?> provider, @NotNull String configPath,
|
||||
@Nullable List<String> headerComments, @Nullable String inlineComment) {
|
||||
if (this.provider == null) this.provider = provider;
|
||||
if (this.configPath == null) this.configPath = configPath;
|
||||
if (this.headerComments == null) this.headerComments = headerComments;
|
||||
if (this.inlineComment == null) this.inlineComment = inlineComment;
|
||||
|
||||
if (getHeaderComments() != null) {
|
||||
this.provider.setHeaderComment(getConfigPath(), getHeaderComments());
|
||||
}
|
||||
if (getInlineComment() != null) {
|
||||
this.provider.setInlineComment(getConfigPath(), getInlineComment());
|
||||
}
|
||||
}
|
||||
|
||||
public @Nullable T getDefaultValue() {
|
||||
return this.defaultValue;
|
||||
}
|
||||
|
||||
public void setDefaultValue(@Nullable T defaultValue) {
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
public @NotNull ConfigurationProvider<?> getProvider() {
|
||||
return Optional.ofNullable(this.provider)
|
||||
.orElseThrow(() -> new IllegalStateException("Value(" + configPath + ") does not have a provider."));
|
||||
}
|
||||
|
||||
public final @NotNull ConfigurationWrapper<?> getConfiguration() {
|
||||
try {
|
||||
return getProvider().getConfiguration();
|
||||
} catch (Exception ex) {
|
||||
throw new IllegalStateException("Value(" + configPath + ") has not been initialized", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public @NotNull String getConfigPath() {
|
||||
return Optional.ofNullable(this.configPath)
|
||||
.orElseThrow(() -> new IllegalStateException("No section path provided."));
|
||||
}
|
||||
|
||||
protected Object getValue() {
|
||||
String path = getConfigPath(); // 当未指定路径时,优先抛出异常
|
||||
return getConfiguration().get(path);
|
||||
}
|
||||
|
||||
protected void setValue(@Nullable Object value) {
|
||||
getConfiguration().set(getConfigPath(), value);
|
||||
}
|
||||
|
||||
public @Nullable String getInlineComment() {
|
||||
return inlineComment;
|
||||
}
|
||||
|
||||
@Unmodifiable
|
||||
public @Nullable List<String> getHeaderComments() {
|
||||
return headerComments;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+17
-12
@@ -1,21 +1,19 @@
|
||||
package cc.carm.lib.configuration.core.value.impl;
|
||||
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||
import cc.carm.lib.configuration.core.value.ValueManifest;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class CachedConfigValue<T> extends ConfigValue<T> {
|
||||
|
||||
|
||||
protected @Nullable T cachedValue;
|
||||
protected long parsedTime = -1;
|
||||
|
||||
public CachedConfigValue(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
|
||||
@Nullable List<String> headerComments, @Nullable String inlineComments,
|
||||
@Nullable T defaultValue) {
|
||||
super(provider, sectionPath, headerComments, inlineComments, defaultValue);
|
||||
protected CachedConfigValue(@NotNull ValueManifest<T> manifest) {
|
||||
super(manifest);
|
||||
}
|
||||
|
||||
protected T updateCache(T value) {
|
||||
@@ -32,12 +30,19 @@ public abstract class CachedConfigValue<T> extends ConfigValue<T> {
|
||||
return this.parsedTime <= 0 || getProvider().isExpired(this.parsedTime);
|
||||
}
|
||||
|
||||
protected final T useDefault() {
|
||||
return useOrDefault(null);
|
||||
}
|
||||
|
||||
protected final T useOrDefault(@Nullable T value) {
|
||||
protected final T getDefaultFirst(@Nullable T value) {
|
||||
return updateCache(this.defaultValue == null ? value : this.defaultValue);
|
||||
}
|
||||
|
||||
protected @Nullable T getCachedOrDefault() {
|
||||
return getCachedOrDefault(null);
|
||||
}
|
||||
|
||||
@Contract("!null->!null")
|
||||
protected T getCachedOrDefault(@Nullable T emptyValue) {
|
||||
if (getCachedValue() != null) return getCachedValue();
|
||||
else if (getDefaultValue() != null) return getDefaultValue();
|
||||
else return emptyValue;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package cc.carm.lib.configuration.core.value.type;
|
||||
|
||||
import cc.carm.lib.configuration.core.builder.list.ConfigListBuilder;
|
||||
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||
import cc.carm.lib.configuration.core.value.ValueManifest;
|
||||
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -13,21 +13,28 @@ import java.util.function.Function;
|
||||
|
||||
public class ConfiguredList<V> extends CachedConfigValue<List<V>> implements List<V> {
|
||||
|
||||
public static <V> @NotNull ConfigListBuilder<V> builder(@NotNull Class<V> valueClass) {
|
||||
public static <V> @NotNull ConfigListBuilder<V> builderOf(@NotNull Class<V> valueClass) {
|
||||
return builder().asList(valueClass);
|
||||
}
|
||||
|
||||
public static <V> @NotNull ConfiguredList<V> of(@NotNull Class<V> valueClass, @NotNull Collection<V> defaults) {
|
||||
return builderOf(valueClass).fromObject().defaults(defaults).build();
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public static <V> @NotNull ConfiguredList<V> of(@NotNull Class<V> valueClass, @NotNull V... defaults) {
|
||||
return builderOf(valueClass).fromObject().defaults(defaults).build();
|
||||
}
|
||||
|
||||
protected final @NotNull Class<V> valueClass;
|
||||
|
||||
protected final @NotNull ConfigDataFunction<Object, V> parser;
|
||||
protected final @NotNull ConfigDataFunction<V, Object> serializer;
|
||||
|
||||
public ConfiguredList(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
|
||||
@Nullable List<String> headerComments, @Nullable String inlineComments,
|
||||
@NotNull Class<V> valueClass, @Nullable List<V> defaultValue,
|
||||
public ConfiguredList(@NotNull ValueManifest<List<V>> manifest, @NotNull Class<V> valueClass,
|
||||
@NotNull ConfigDataFunction<Object, V> parser,
|
||||
@NotNull ConfigDataFunction<V, Object> serializer) {
|
||||
super(provider, sectionPath, headerComments, inlineComments, defaultValue);
|
||||
super(manifest);
|
||||
this.valueClass = valueClass;
|
||||
this.parser = parser;
|
||||
this.serializer = serializer;
|
||||
@@ -35,23 +42,41 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>> implements Lis
|
||||
|
||||
@Override
|
||||
public @NotNull List<V> get() {
|
||||
if (isExpired()) { // 已过时的数据,需要重新解析一次。
|
||||
List<V> list = new ArrayList<>();
|
||||
List<?> data = getConfiguration().contains(getConfigPath()) ?
|
||||
getConfiguration().getList(getConfigPath()) : null;
|
||||
if (data == null) return useOrDefault(list);
|
||||
for (Object dataVal : data) {
|
||||
if (dataVal == null) continue;
|
||||
try {
|
||||
list.add(parser.parse(dataVal));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (!isExpired()) return getCachedOrDefault(new ArrayList<>());
|
||||
|
||||
// 已过时的数据,需要重新解析一次。
|
||||
List<V> list = new ArrayList<>();
|
||||
List<?> data = getConfiguration().contains(getConfigPath()) ?
|
||||
getConfiguration().getList(getConfigPath()) : null;
|
||||
if (data == null) return getDefaultFirst(list);
|
||||
for (Object dataVal : data) {
|
||||
if (dataVal == null) continue;
|
||||
try {
|
||||
list.add(parser.parse(dataVal));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return updateCache(list);
|
||||
} else if (getCachedValue() != null) return getCachedValue();
|
||||
else if (getDefaultValue() != null) return getDefaultValue();
|
||||
else return new ArrayList<>();
|
||||
}
|
||||
return updateCache(list);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get(int index) {
|
||||
return get().get(index);
|
||||
}
|
||||
|
||||
public <T> @NotNull T handle(Function<List<V>, T> function) {
|
||||
List<V> list = get();
|
||||
T result = function.apply(list);
|
||||
set(list);
|
||||
return result;
|
||||
}
|
||||
|
||||
public @NotNull List<V> modify(Consumer<List<V>> consumer) {
|
||||
List<V> list = get();
|
||||
consumer.accept(list);
|
||||
set(list);
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -72,18 +97,9 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>> implements Lis
|
||||
}
|
||||
}
|
||||
|
||||
public <T> @NotNull T modifyValue(Function<List<V>, T> function) {
|
||||
List<V> list = get();
|
||||
T result = function.apply(list);
|
||||
set(list);
|
||||
return result;
|
||||
}
|
||||
|
||||
public @NotNull List<V> modifyList(Consumer<List<V>> consumer) {
|
||||
List<V> list = get();
|
||||
consumer.accept(list);
|
||||
set(list);
|
||||
return list;
|
||||
@Override
|
||||
public V set(int index, V element) {
|
||||
return handle(list -> list.set(index, element));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -119,65 +135,55 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>> implements Lis
|
||||
return get().toArray(a);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(V v) {
|
||||
modifyValue(list -> list.add(v));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
return modifyValue(list -> list.remove(o));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAll(@NotNull Collection<?> c) {
|
||||
return new HashSet<>(get()).containsAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(@NotNull Collection<? extends V> c) {
|
||||
return modifyValue(list -> list.addAll(c));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(int index, @NotNull Collection<? extends V> c) {
|
||||
return modifyValue(list -> list.addAll(index, c));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(@NotNull Collection<?> c) {
|
||||
return modifyValue(list -> list.removeAll(c));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(@NotNull Collection<?> c) {
|
||||
return modifyValue(list -> list.retainAll(c));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
modifyList(List::clear);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get(int index) {
|
||||
return get().get(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V set(int index, V element) {
|
||||
return modifyValue(list -> list.set(index, element));
|
||||
public boolean add(V v) {
|
||||
handle(list -> list.add(v));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(int index, V element) {
|
||||
modifyList(list -> list.add(index, element));
|
||||
modify(list -> list.add(index, element));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(@NotNull Collection<? extends V> c) {
|
||||
return handle(list -> list.addAll(c));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(int index, @NotNull Collection<? extends V> c) {
|
||||
return handle(list -> list.addAll(index, c));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
return handle(list -> list.remove(o));
|
||||
}
|
||||
|
||||
@Override
|
||||
public V remove(int index) {
|
||||
return modifyValue(list -> list.remove(index));
|
||||
return handle(list -> list.remove(index));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(@NotNull Collection<?> c) {
|
||||
return handle(list -> list.removeAll(c));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(@NotNull Collection<?> c) {
|
||||
return handle(list -> list.retainAll(c));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
modify(List::clear);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,23 +1,26 @@
|
||||
package cc.carm.lib.configuration.core.value.type;
|
||||
|
||||
import cc.carm.lib.configuration.core.builder.map.ConfigMapBuilder;
|
||||
import cc.carm.lib.configuration.core.builder.map.ConfigMapCreator;
|
||||
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||
import cc.carm.lib.configuration.core.value.ValueManifest;
|
||||
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.Unmodifiable;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class ConfiguredMap<K, V> extends CachedConfigValue<Map<K, V>> implements Map<K, V> {
|
||||
|
||||
public static <K, V> @NotNull ConfigMapBuilder<LinkedHashMap<K, V>, K, V> builder(@NotNull Class<K> keyClass,
|
||||
@NotNull Class<V> valueClass) {
|
||||
public static <K, V> @NotNull ConfigMapCreator<K, V> builderOf(@NotNull Class<K> keyClass,
|
||||
@NotNull Class<V> valueClass) {
|
||||
return builder().asMap(keyClass, valueClass);
|
||||
}
|
||||
|
||||
@@ -32,15 +35,15 @@ public class ConfiguredMap<K, V> extends CachedConfigValue<Map<K, V>> implements
|
||||
protected final @NotNull ConfigDataFunction<K, String> keySerializer;
|
||||
protected final @NotNull ConfigDataFunction<V, Object> valueSerializer;
|
||||
|
||||
public ConfiguredMap(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
|
||||
@Nullable List<String> headerComments, @Nullable String inlineComments,
|
||||
@Nullable Map<K, V> defaultValue, @NotNull Supplier<? extends Map<K, V>> supplier,
|
||||
|
||||
public ConfiguredMap(@NotNull ValueManifest<Map<K, V>> manifest,
|
||||
@NotNull Supplier<? extends Map<K, V>> mapObjSupplier,
|
||||
@NotNull Class<K> keyClass, @NotNull ConfigDataFunction<String, K> keyParser,
|
||||
@NotNull Class<V> valueClass, @NotNull ConfigDataFunction<Object, V> valueParser,
|
||||
@NotNull ConfigDataFunction<K, String> keySerializer,
|
||||
@NotNull ConfigDataFunction<V, Object> valueSerializer) {
|
||||
super(provider, sectionPath, headerComments, inlineComments, defaultValue);
|
||||
this.supplier = supplier;
|
||||
super(manifest);
|
||||
this.supplier = mapObjSupplier;
|
||||
this.keyClass = keyClass;
|
||||
this.valueClass = valueClass;
|
||||
this.keyParser = keyParser;
|
||||
@@ -75,31 +78,35 @@ public class ConfiguredMap<K, V> extends CachedConfigValue<Map<K, V>> implements
|
||||
|
||||
@Override
|
||||
public @NotNull Map<K, V> get() {
|
||||
if (isExpired()) { // 已过时的数据,需要重新解析一次。
|
||||
Map<K, V> map = supplier.get();
|
||||
if (!isExpired()) return getCachedOrDefault(supplier.get());
|
||||
|
||||
ConfigurationWrapper<?> section = getConfiguration().getConfigurationSection(getConfigPath());
|
||||
if (section == null) return useOrDefault(map);
|
||||
// 已过时的数据,需要重新解析一次。
|
||||
Map<K, V> map = supplier.get();
|
||||
|
||||
Set<String> keys = section.getKeys(false);
|
||||
if (keys.isEmpty()) return useOrDefault(map);
|
||||
ConfigurationWrapper<?> section = getConfiguration().getConfigurationSection(getConfigPath());
|
||||
if (section == null) return getDefaultFirst(map);
|
||||
|
||||
for (String dataKey : keys) {
|
||||
Object dataVal = section.get(dataKey);
|
||||
if (dataVal == null) continue;
|
||||
try {
|
||||
K key = keyParser.parse(dataKey);
|
||||
V value = valueParser.parse(dataVal);
|
||||
map.put(key, value);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Set<String> keys = section.getKeys(false);
|
||||
if (keys.isEmpty()) return getDefaultFirst(map);
|
||||
|
||||
for (String dataKey : keys) {
|
||||
Object dataVal = section.get(dataKey);
|
||||
if (dataVal == null) continue;
|
||||
try {
|
||||
K key = keyParser.parse(dataKey);
|
||||
V value = valueParser.parse(dataVal);
|
||||
map.put(key, value);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return updateCache(map);
|
||||
} else if (getCachedValue() != null) return getCachedValue();
|
||||
else if (getDefaultValue() != null) return getDefaultValue();
|
||||
else return supplier.get();
|
||||
return updateCache(map);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get(Object key) {
|
||||
return get().get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -156,11 +163,6 @@ public class ConfiguredMap<K, V> extends CachedConfigValue<Map<K, V>> implements
|
||||
return get().containsValue(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get(Object key) {
|
||||
return get().get(key);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public V put(K key, V value) {
|
||||
|
||||
+19
-20
@@ -3,19 +3,17 @@ package cc.carm.lib.configuration.core.value.type;
|
||||
import cc.carm.lib.configuration.core.builder.value.SectionValueBuilder;
|
||||
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
||||
import cc.carm.lib.configuration.core.function.ConfigValueParser;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||
import cc.carm.lib.configuration.core.value.ValueManifest;
|
||||
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public class ConfiguredSection<V> extends CachedConfigValue<V> {
|
||||
|
||||
public static <V> @NotNull SectionValueBuilder<V> builder(@NotNull Class<V> valueClass) {
|
||||
public static <V> @NotNull SectionValueBuilder<V> builderOf(@NotNull Class<V> valueClass) {
|
||||
return builder().asValue(valueClass).fromSection();
|
||||
}
|
||||
|
||||
@@ -24,12 +22,10 @@ public class ConfiguredSection<V> extends CachedConfigValue<V> {
|
||||
protected final @NotNull ConfigValueParser<ConfigurationWrapper<?>, V> parser;
|
||||
protected final @NotNull ConfigDataFunction<V, ? extends Map<String, Object>> serializer;
|
||||
|
||||
public ConfiguredSection(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
|
||||
@Nullable List<String> headerComments, @Nullable String inlineComments,
|
||||
@NotNull Class<V> valueClass, @Nullable V defaultValue,
|
||||
public ConfiguredSection(@NotNull ValueManifest<V> manifest, @NotNull Class<V> valueClass,
|
||||
@NotNull ConfigValueParser<ConfigurationWrapper<?>, V> parser,
|
||||
@NotNull ConfigDataFunction<V, ? extends Map<String, Object>> serializer) {
|
||||
super(provider, sectionPath, headerComments, inlineComments, defaultValue);
|
||||
super(manifest);
|
||||
this.valueClass = valueClass;
|
||||
this.parser = parser;
|
||||
this.serializer = serializer;
|
||||
@@ -49,18 +45,21 @@ public class ConfiguredSection<V> extends CachedConfigValue<V> {
|
||||
|
||||
@Override
|
||||
public @Nullable V get() {
|
||||
if (isExpired()) { // 已过时的数据,需要重新解析一次。
|
||||
ConfigurationWrapper<?> section = getConfiguration().getConfigurationSection(getConfigPath());
|
||||
if (section == null) return useDefault();
|
||||
try {
|
||||
// 若未出现错误,则直接更新缓存并返回。
|
||||
return updateCache(this.parser.parse(section, this.defaultValue));
|
||||
} catch (Exception e) {
|
||||
// 出现了解析错误,提示并返回默认值。
|
||||
e.printStackTrace();
|
||||
return useDefault();
|
||||
}
|
||||
} else return Optional.ofNullable(getCachedValue()).orElse(defaultValue);
|
||||
if (!isExpired()) return getCachedOrDefault();
|
||||
// 已过时的数据,需要重新解析一次。
|
||||
|
||||
ConfigurationWrapper<?> section = getConfiguration().getConfigurationSection(getConfigPath());
|
||||
if (section == null) return getDefaultValue();
|
||||
|
||||
try {
|
||||
// 若未出现错误,则直接更新缓存并返回。
|
||||
return updateCache(this.parser.parse(section, this.defaultValue));
|
||||
} catch (Exception e) {
|
||||
// 出现了解析错误,提示并返回默认值。
|
||||
e.printStackTrace();
|
||||
return getDefaultValue();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -3,17 +3,14 @@ package cc.carm.lib.configuration.core.value.type;
|
||||
import cc.carm.lib.configuration.core.builder.value.ConfigValueBuilder;
|
||||
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
||||
import cc.carm.lib.configuration.core.function.ConfigValueParser;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||
import cc.carm.lib.configuration.core.value.ValueManifest;
|
||||
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class ConfiguredValue<V> extends CachedConfigValue<V> {
|
||||
|
||||
public static <V> ConfigValueBuilder<V> builder(Class<V> valueClass) {
|
||||
public static <V> ConfigValueBuilder<V> builderOf(Class<V> valueClass) {
|
||||
return builder().asValue(valueClass);
|
||||
}
|
||||
|
||||
@@ -22,7 +19,7 @@ public class ConfiguredValue<V> extends CachedConfigValue<V> {
|
||||
}
|
||||
|
||||
public static <V> ConfiguredValue<V> of(Class<V> valueClass, @Nullable V defaultValue) {
|
||||
return builder(valueClass).fromObject().defaults(defaultValue).build();
|
||||
return builderOf(valueClass).fromObject().defaults(defaultValue).build();
|
||||
}
|
||||
|
||||
protected final @NotNull Class<V> valueClass;
|
||||
@@ -30,13 +27,10 @@ public class ConfiguredValue<V> extends CachedConfigValue<V> {
|
||||
protected final @NotNull ConfigValueParser<Object, V> parser;
|
||||
protected final @NotNull ConfigDataFunction<V, Object> serializer;
|
||||
|
||||
public ConfiguredValue(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
|
||||
@Nullable List<String> headerComments, @Nullable String inlineComments,
|
||||
@NotNull Class<V> valueClass, @Nullable V defaultValue,
|
||||
public ConfiguredValue(@NotNull ValueManifest<V> manifest, @NotNull Class<V> valueClass,
|
||||
@NotNull ConfigValueParser<Object, V> parser,
|
||||
@NotNull ConfigDataFunction<V, Object> serializer) {
|
||||
|
||||
super(provider, sectionPath, headerComments, inlineComments, defaultValue);
|
||||
super(manifest);
|
||||
this.valueClass = valueClass;
|
||||
this.parser = parser;
|
||||
this.serializer = serializer;
|
||||
@@ -52,18 +46,20 @@ public class ConfiguredValue<V> extends CachedConfigValue<V> {
|
||||
|
||||
@Override
|
||||
public V get() {
|
||||
if (isExpired()) { // 已过时的数据,需要重新解析一次。
|
||||
Object value = getValue();
|
||||
if (value == null) return useDefault(); // 获取的值不存在,直接使用默认值。
|
||||
try {
|
||||
// 若未出现错误,则直接更新缓存并返回。
|
||||
return updateCache(this.parser.parse(value, this.defaultValue));
|
||||
} catch (Exception e) {
|
||||
// 出现了解析错误,提示并返回默认值。
|
||||
e.printStackTrace();
|
||||
return useDefault();
|
||||
}
|
||||
} else return Optional.ofNullable(getCachedValue()).orElse(defaultValue);
|
||||
if (!isExpired()) return getCachedOrDefault();
|
||||
// 已过时的数据,需要重新解析一次。
|
||||
|
||||
Object value = getValue();
|
||||
if (value == null) return getDefaultValue(); // 获取的值不存在,直接使用默认值。
|
||||
try {
|
||||
// 若未出现错误,则直接更新缓存并返回。
|
||||
return updateCache(this.parser.parse(value, this.defaultValue));
|
||||
} catch (Exception e) {
|
||||
// 出现了解析错误,提示并返回默认值。
|
||||
e.printStackTrace();
|
||||
return getDefaultValue();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+3
-3
@@ -5,12 +5,12 @@
|
||||
<parent>
|
||||
<artifactId>easyconfiguration-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>3.4.0</version>
|
||||
<version>3.6.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
</properties>
|
||||
|
||||
@@ -42,7 +42,7 @@ public class ConfigurationTest {
|
||||
|
||||
System.out.println("> Clear List:");
|
||||
System.out.println(" Before: size :" + DemoConfiguration.Sub.That.OPERATORS.size());
|
||||
DemoConfiguration.Sub.That.OPERATORS.modifyList(List::clear);
|
||||
DemoConfiguration.Sub.That.OPERATORS.modify(List::clear);
|
||||
System.out.println(" After size :" + DemoConfiguration.Sub.That.OPERATORS.size());
|
||||
|
||||
System.out.println("> Test Section:");
|
||||
|
||||
+8
-10
@@ -16,10 +16,7 @@ import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
|
||||
@HeaderComment({
|
||||
"此处内容将显示在配置文件的最上方",
|
||||
""/*添加一个空行与其他配置的注释分割*/
|
||||
})
|
||||
@HeaderComment({"此处内容将显示在配置文件的最上方"})
|
||||
public class DemoConfiguration extends ConfigurationRoot {
|
||||
|
||||
@ConfigPath(root = true)
|
||||
@@ -36,17 +33,18 @@ public class DemoConfiguration extends ConfigurationRoot {
|
||||
public static final Class<?> OTHER = OtherConfiguration.class;
|
||||
|
||||
@ConfigPath("user") // 通过注解规定配置文件中的路径,若不进行注解则以变量名自动生成。
|
||||
@HeaderComment({"", "Section类型数据测试"}) // 通过注解给配置添加注释。
|
||||
@HeaderComment({"Section类型数据测试"}) // 通过注解给配置添加注释。
|
||||
@InlineComment("Section数据也支持InlineComment注释")
|
||||
public static final ConfigValue<TestModel> MODEL_TEST = ConfiguredSection
|
||||
.builder(TestModel.class)
|
||||
.builderOf(TestModel.class)
|
||||
.defaults(new TestModel("Carm", UUID.randomUUID()))
|
||||
.parseValue((section, defaultValue) -> TestModel.deserialize(section))
|
||||
.serializeValue(TestModel::serialize).build();
|
||||
|
||||
@HeaderComment({"", "[ID - UUID]对照表", "", "用于测试Map类型的解析与序列化保存"})
|
||||
@HeaderComment({"[ID - UUID]对照表", "", "用于测试Map类型的解析与序列化保存"})
|
||||
public static final ConfiguredMap<Integer, UUID> USERS = ConfiguredMap
|
||||
.builder(Integer.class, UUID.class).fromString()
|
||||
.builderOf(Integer.class, UUID.class)
|
||||
.asLinkedMap().fromString()
|
||||
.parseKey(Integer::parseInt)
|
||||
.parseValue(v -> Objects.requireNonNull(UUID.fromString(v)))
|
||||
.build();
|
||||
@@ -61,14 +59,14 @@ public class DemoConfiguration extends ConfigurationRoot {
|
||||
@ConfigPath(value = "uuid-value", root = true)
|
||||
@InlineComment("This is an inline comment")
|
||||
public static final ConfigValue<UUID> UUID_CONFIG_VALUE = ConfiguredValue
|
||||
.builder(UUID.class).fromString()
|
||||
.builderOf(UUID.class).fromString()
|
||||
.parseValue((data, defaultValue) -> UUID.fromString(data))
|
||||
.build();
|
||||
|
||||
public static class That extends ConfigurationRoot {
|
||||
|
||||
public static final ConfiguredList<UUID> OPERATORS = ConfiguredList
|
||||
.builder(UUID.class).fromString()
|
||||
.builderOf(UUID.class).fromString()
|
||||
.parseValue(s -> Objects.requireNonNull(UUID.fromString(s)))
|
||||
.build();
|
||||
|
||||
|
||||
@@ -18,10 +18,10 @@ public class TestConfiguration extends ConfigurationRoot {
|
||||
public final ConfigValue<Double> CLASS_VALUE = ConfiguredValue.of(Double.class, 1.0D);
|
||||
|
||||
@ConfigPath("test.user") // 通过注解规定配置文件中的路径,若不进行注解则以变量名自动生成。
|
||||
@HeaderComment({"", "Section类型数据测试"}) // 通过注解给配置添加注释。
|
||||
@HeaderComment({"Section类型数据测试"}) // 通过注解给配置添加注释。
|
||||
@InlineComment("Section数据也支持InlineComment注释")
|
||||
public final ConfigValue<TestModel> TEST_MODEL = ConfiguredSection
|
||||
.builder(TestModel.class)
|
||||
.builderOf(TestModel.class)
|
||||
.defaults(new TestModel("Carm", UUID.randomUUID()))
|
||||
.parseValue((section, defaultValue) -> TestModel.deserialize(section))
|
||||
.serializeValue(TestModel::serialize).build();
|
||||
|
||||
+3
-3
@@ -5,13 +5,13 @@
|
||||
<parent>
|
||||
<artifactId>easyconfiguration-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>3.4.0</version>
|
||||
<version>3.6.0</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
</properties>
|
||||
|
||||
+3
-3
@@ -5,13 +5,13 @@
|
||||
<parent>
|
||||
<artifactId>easyconfiguration-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>3.4.0</version>
|
||||
<version>3.6.0</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
|
||||
|
||||
+11
-5
@@ -5,13 +5,13 @@
|
||||
<parent>
|
||||
<artifactId>easyconfiguration-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>3.4.0</version>
|
||||
<version>3.6.0</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
</properties>
|
||||
@@ -28,17 +28,23 @@
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>yamlcommentwriter</artifactId>
|
||||
<version>1.0.1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<artifactId>easyconfiguration-demo</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.bspfsystems</groupId>
|
||||
<artifactId>yamlconfiguration</artifactId>
|
||||
<version>1.3.1</version>
|
||||
<version>1.3.2</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
@@ -1,111 +0,0 @@
|
||||
package cc.carm.lib.configuration.yaml;
|
||||
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationComments;
|
||||
import org.bspfsystems.yamlconfiguration.configuration.ConfigurationSection;
|
||||
import org.bspfsystems.yamlconfiguration.file.FileConfiguration;
|
||||
import org.bspfsystems.yamlconfiguration.file.YamlConfiguration;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.StringJoiner;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static cc.carm.lib.configuration.yaml.YAMLConfigProvider.SEPARATOR;
|
||||
|
||||
@SuppressWarnings("SpellCheckingInspection")
|
||||
public class YAMLComments extends ConfigurationComments {
|
||||
|
||||
public @Nullable String buildHeaderComments(@Nullable String path, @NotNull String indents) {
|
||||
List<String> comments = getHeaderComment(path);
|
||||
if (comments == null || comments.size() == 0) return null;
|
||||
|
||||
StringJoiner joiner = new StringJoiner("\n");
|
||||
for (String comment : comments) {
|
||||
if (comment.length() == 0) joiner.add(" ");
|
||||
else joiner.add(indents + "# " + comment);
|
||||
}
|
||||
return joiner + "\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* 从一个文件读取配置并写入注释到某个写入器中。
|
||||
* 该方法的部分源代码借鉴自 tchristofferson/ConfigUpdater 项目。
|
||||
*
|
||||
* @param source 源配置文件
|
||||
* @param writer 配置写入器
|
||||
* @throws IOException 当写入发生错误时抛出
|
||||
*/
|
||||
public void writeComments(@NotNull YamlConfiguration source, @NotNull BufferedWriter writer) throws IOException {
|
||||
FileConfiguration temp = new YamlConfiguration(); // 该对象用于临时记录配置内容
|
||||
|
||||
String configHeader = buildHeaderComments(null, "");
|
||||
if (configHeader != null) writer.write(configHeader);
|
||||
|
||||
for (String fullKey : source.getKeys(true)) {
|
||||
Object currentValue = source.get(fullKey);
|
||||
|
||||
String indents = getIndents(fullKey);
|
||||
String headerComments = buildHeaderComments(fullKey, indents);
|
||||
String inlineComment = getInlineComment(fullKey);
|
||||
|
||||
if (headerComments != null) writer.write(headerComments);
|
||||
|
||||
String[] splitFullKey = fullKey.split("[" + SEPARATOR + "]");
|
||||
String trailingKey = splitFullKey[splitFullKey.length - 1];
|
||||
|
||||
if (currentValue instanceof ConfigurationSection) {
|
||||
ConfigurationSection section = (ConfigurationSection) currentValue;
|
||||
writer.write(indents + trailingKey + ":");
|
||||
if (inlineComment != null && inlineComment.length() > 0) {
|
||||
writer.write(" # " + inlineComment);
|
||||
}
|
||||
if (!section.getKeys(false).isEmpty()) {
|
||||
writer.write("\n");
|
||||
} else {
|
||||
writer.write(" {}\n");
|
||||
if (indents.length() == 0) writer.write("\n");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
temp.set(trailingKey, currentValue);
|
||||
String yaml = temp.saveToString();
|
||||
temp.set(trailingKey, null);
|
||||
|
||||
yaml = yaml.substring(0, yaml.length() - 1);
|
||||
|
||||
if (inlineComment != null && inlineComment.length() > 0) {
|
||||
if (yaml.contains("\n")) {
|
||||
// section为多行内容,需要 InlineComment 加在首行末尾
|
||||
String[] splitLine = yaml.split("\n", 2);
|
||||
yaml = splitLine[0] + " # " + inlineComment + "\n" + splitLine[1];
|
||||
} else {
|
||||
// 其他情况下就直接加载后面就好。
|
||||
yaml += " # " + inlineComment;
|
||||
}
|
||||
}
|
||||
|
||||
writer.write(indents + yaml.replace("\n", "\n" + indents) + "\n");
|
||||
if (indents.length() == 0) writer.write("\n");
|
||||
}
|
||||
|
||||
writer.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到一个键的缩进。
|
||||
* 该方法的源代码来自 tchristofferson/ConfigUpdater 项目。
|
||||
*
|
||||
* @param key 键
|
||||
* @return 该键的缩进文本
|
||||
*/
|
||||
protected static String getIndents(String key) {
|
||||
String[] splitKey = key.split("[" + YAMLConfigProvider.SEPARATOR + "]");
|
||||
return IntStream.range(1, splitKey.length).mapToObj(i -> " ").collect(Collectors.joining());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,22 +3,21 @@ package cc.carm.lib.configuration.yaml;
|
||||
import cc.carm.lib.configuration.core.ConfigInitializer;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationComments;
|
||||
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
|
||||
import cc.carm.lib.yamlcommentupdater.CommentedYAML;
|
||||
import cc.carm.lib.yamlcommentupdater.CommentedYAMLWriter;
|
||||
import org.bspfsystems.yamlconfiguration.configuration.ConfigurationSection;
|
||||
import org.bspfsystems.yamlconfiguration.file.FileConfiguration;
|
||||
import org.bspfsystems.yamlconfiguration.file.YamlConfiguration;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class YAMLConfigProvider extends FileConfigProvider<YAMLSectionWrapper> {
|
||||
public class YAMLConfigProvider extends FileConfigProvider<YAMLSectionWrapper> implements CommentedYAML {
|
||||
|
||||
protected static final char SEPARATOR = '.';
|
||||
|
||||
protected final @NotNull YAMLComments comments = new YAMLComments();
|
||||
protected final @NotNull ConfigurationComments comments = new ConfigurationComments();
|
||||
protected YamlConfiguration configuration;
|
||||
protected ConfigInitializer<YAMLConfigProvider> initializer;
|
||||
|
||||
@@ -42,27 +41,18 @@ public class YAMLConfigProvider extends FileConfigProvider<YAMLSectionWrapper> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable ConfigurationComments getComments() {
|
||||
public @NotNull ConfigurationComments getComments() {
|
||||
return this.comments;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("SpellCheckingInspection")
|
||||
public void save() throws Exception {
|
||||
configuration.save(getFile());
|
||||
|
||||
// tchristofferson/ConfigUpdater start
|
||||
StringWriter writer = new StringWriter();
|
||||
this.comments.writeComments(configuration, new BufferedWriter(writer));
|
||||
String value = writer.toString(); // config contents
|
||||
|
||||
Path toUpdatePath = getFile().toPath();
|
||||
if (!value.equals(new String(Files.readAllBytes(toUpdatePath), StandardCharsets.UTF_8))) {
|
||||
// if updated contents are not the same as current file contents, update
|
||||
Files.write(toUpdatePath, value.getBytes(StandardCharsets.UTF_8));
|
||||
try {
|
||||
CommentedYAMLWriter.writeWithComments(this, this.file);
|
||||
} catch (Exception ex) {
|
||||
configuration.save(file);
|
||||
throw ex;
|
||||
}
|
||||
// tchristofferson/ConfigUpdater end
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -70,4 +60,31 @@ public class YAMLConfigProvider extends FileConfigProvider<YAMLSectionWrapper> {
|
||||
return this.initializer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String serializeValue(@NotNull String key, @NotNull Object value) {
|
||||
FileConfiguration temp = new YamlConfiguration();
|
||||
temp.set(key, value);
|
||||
return temp.saveToString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getKeys(@Nullable String sectionKey, boolean deep) {
|
||||
if (sectionKey == null) return configuration.getKeys(deep);
|
||||
|
||||
ConfigurationSection section = configuration.getConfigurationSection(sectionKey);
|
||||
if (section == null) return null;
|
||||
|
||||
return section.getKeys(deep);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Object getValue(@NotNull String key) {
|
||||
return configuration.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable List<String> getHeaderComments(@Nullable String key) {
|
||||
return comments.getHeaderComment(key);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
package cc.carm.lib.configuration.yaml;
|
||||
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||
import cc.carm.lib.configuration.core.value.ValueManifest;
|
||||
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
||||
import cc.carm.lib.configuration.yaml.builder.YAMLConfigBuilder;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class YAMLValue<T> extends CachedConfigValue<T> {
|
||||
|
||||
@@ -14,10 +12,8 @@ public abstract class YAMLValue<T> extends CachedConfigValue<T> {
|
||||
return new YAMLConfigBuilder();
|
||||
}
|
||||
|
||||
public YAMLValue(@Nullable YAMLConfigProvider provider, @Nullable String configPath,
|
||||
@Nullable List<String> headerComments, @Nullable String inlineComments,
|
||||
@Nullable T defaultValue) {
|
||||
super(provider, configPath, headerComments, inlineComments, defaultValue);
|
||||
public YAMLValue(@NotNull ValueManifest<T> manifest) {
|
||||
super(manifest);
|
||||
}
|
||||
|
||||
public YAMLConfigProvider getYAMLProvider() {
|
||||
|
||||
+1
-1
@@ -21,7 +21,7 @@ public class SerializableBuilder<T extends ConfigurationSerializable>
|
||||
|
||||
@Override
|
||||
public @NotNull ConfiguredSerializable<T> build() {
|
||||
return new ConfiguredSerializable<>(this.provider, this.path, this.headerComments, this.inlineComment, this.valueClass, this.defaultValue);
|
||||
return new ConfiguredSerializable<>(buildManifest(), this.valueClass);
|
||||
}
|
||||
|
||||
|
||||
|
||||
+14
-18
@@ -1,14 +1,11 @@
|
||||
package cc.carm.lib.configuration.yaml.value;
|
||||
|
||||
import cc.carm.lib.configuration.yaml.YAMLConfigProvider;
|
||||
import cc.carm.lib.configuration.core.value.ValueManifest;
|
||||
import cc.carm.lib.configuration.yaml.YAMLValue;
|
||||
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerializable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class ConfiguredSerializable<T extends ConfigurationSerializable> extends YAMLValue<T> {
|
||||
|
||||
public static <V extends ConfigurationSerializable> ConfiguredSerializable<V> of(@NotNull Class<V> valueClass) {
|
||||
@@ -22,25 +19,24 @@ public class ConfiguredSerializable<T extends ConfigurationSerializable> extends
|
||||
|
||||
protected final @NotNull Class<T> valueClass;
|
||||
|
||||
public ConfiguredSerializable(@Nullable YAMLConfigProvider provider, @Nullable String configPath,
|
||||
@Nullable List<String> headerComments, @Nullable String inlineComments,
|
||||
@NotNull Class<T> valueClass, @Nullable T defaultValue) {
|
||||
super(provider, configPath, headerComments, inlineComments, defaultValue);
|
||||
public ConfiguredSerializable(@NotNull ValueManifest<T> manifest, @NotNull Class<T> valueClass) {
|
||||
super(manifest);
|
||||
this.valueClass = valueClass;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable T get() {
|
||||
if (isExpired()) { // 已过时的数据,需要重新解析一次。
|
||||
try {
|
||||
// 若未出现错误,则直接更新缓存并返回。
|
||||
return updateCache(getYAMLConfig().getSerializable(getConfigPath(), valueClass, getDefaultValue()));
|
||||
} catch (Exception e) {
|
||||
// 出现了解析错误,提示并返回默认值。
|
||||
e.printStackTrace();
|
||||
return useDefault();
|
||||
}
|
||||
} else return Optional.ofNullable(getCachedValue()).orElse(defaultValue);
|
||||
if (!isExpired()) return getCachedOrDefault();
|
||||
|
||||
try {
|
||||
// 若未出现错误,则直接更新缓存并返回。
|
||||
return updateCache(getYAMLConfig().getSerializable(getConfigPath(), valueClass, getDefaultValue()));
|
||||
} catch (Exception e) {
|
||||
// 出现了解析错误,提示并返回默认值。
|
||||
e.printStackTrace();
|
||||
return getDefaultValue();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -21,6 +21,7 @@ public class DemoConfigTest {
|
||||
|
||||
@Test
|
||||
public void onTest() {
|
||||
|
||||
ConfigurationTest.testDemo(this.provider);
|
||||
ConfigurationTest.testInner(this.provider);
|
||||
|
||||
@@ -40,6 +41,8 @@ public class DemoConfigTest {
|
||||
AbstractModel anyModel = ModelConfiguration.ANY_MODEL.get();
|
||||
if (anyModel != null) System.out.println(anyModel.getName());
|
||||
|
||||
ModelConfiguration.MODELS.forEach(System.out::println);
|
||||
|
||||
System.out.println("----------------------------------------------------");
|
||||
}
|
||||
|
||||
|
||||
@@ -2,12 +2,17 @@ package config.source;
|
||||
|
||||
import cc.carm.lib.configuration.core.ConfigurationRoot;
|
||||
import cc.carm.lib.configuration.core.annotation.ConfigPath;
|
||||
import cc.carm.lib.configuration.core.annotation.HeaderComment;
|
||||
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
|
||||
import cc.carm.lib.configuration.demo.tests.model.AbstractModel;
|
||||
import cc.carm.lib.configuration.yaml.value.ConfiguredSerializable;
|
||||
import config.model.AnyModel;
|
||||
import config.model.SomeModel;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@HeaderComment("以下内容用于测试序列化")
|
||||
@ConfigPath("model-test")
|
||||
public class ModelConfiguration extends ConfigurationRoot {
|
||||
|
||||
@@ -19,4 +24,11 @@ public class ModelConfiguration extends ConfigurationRoot {
|
||||
AnyModel.class, AnyModel.random()
|
||||
);
|
||||
|
||||
public static final ConfiguredList<AnyModel> MODELS = ConfiguredList.builderOf(AnyModel.class)
|
||||
.fromObject()
|
||||
.parseValue((section) -> AnyModel.deserialize((Map<String, ?>) section))
|
||||
.serializeValue(AnyModel::serialize)
|
||||
.defaults(AnyModel.random(), AnyModel.random(), AnyModel.random())
|
||||
.build();
|
||||
|
||||
}
|
||||
|
||||
@@ -1,2 +1 @@
|
||||
# Test Header
|
||||
version: 1.0
|
||||
@@ -5,9 +5,9 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
<project.jdk.version>1.8</project.jdk.version>
|
||||
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
</properties>
|
||||
@@ -15,7 +15,7 @@
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easyconfiguration-parent</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<version>3.4.0</version>
|
||||
<version>3.6.0</version>
|
||||
<modules>
|
||||
<module>core</module>
|
||||
<module>demo</module>
|
||||
@@ -109,7 +109,7 @@
|
||||
<dependency>
|
||||
<groupId>org.jetbrains</groupId>
|
||||
<artifactId>annotations</artifactId>
|
||||
<version>24.0.0</version>
|
||||
<version>24.0.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
@@ -122,7 +122,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.2</version>
|
||||
<version>3.1.0</version>
|
||||
<configuration>
|
||||
<useSystemClassLoader>false</useSystemClassLoader>
|
||||
</configuration>
|
||||
@@ -130,7 +130,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-gpg-plugin</artifactId>
|
||||
<version>3.0.1</version>
|
||||
<version>3.1.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>sign-artifacts</id>
|
||||
@@ -150,7 +150,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-release-plugin</artifactId>
|
||||
<version>2.5.3</version>
|
||||
<version>3.0.0</version>
|
||||
<configuration>
|
||||
<autoVersionSubmodules>true</autoVersionSubmodules>
|
||||
<useReleaseProfile>false</useReleaseProfile>
|
||||
@@ -193,10 +193,10 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.10.1</version>
|
||||
<version>3.11.0</version>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
<source>${project.jdk.version}</source>
|
||||
<target>${project.jdk.version}</target>
|
||||
<encoding>UTF-8</encoding>
|
||||
<compilerArgument>-parameters</compilerArgument>
|
||||
</configuration>
|
||||
|
||||
Reference in New Issue
Block a user