1
mirror of https://github.com/CarmJos/EasyConfiguration.git synced 2024-09-19 20:25:51 +00:00

feat(source): 为 ConfigurationWrapper 添加 getSource() 方法以获取源实现内容。

BREAKING-CHANGE: ConfigurationWrapper 更改为泛型类,并新增 “getSource” 方法需要实现。
This commit is contained in:
Carm Jos 2022-09-10 00:36:46 +08:00
parent 4a17089da0
commit 0fddfe28af
18 changed files with 55 additions and 92 deletions

View File

@ -19,7 +19,7 @@ public class ConfigValueBuilder<V> {
return fromSection(ConfigValueParser.required(), ConfigDataFunction.required());
}
public @NotNull SectionValueBuilder<V> fromSection(@NotNull ConfigValueParser<ConfigurationWrapper, V> valueParser,
public @NotNull SectionValueBuilder<V> fromSection(@NotNull ConfigValueParser<ConfigurationWrapper<?>, V> valueParser,
@NotNull ConfigDataFunction<V, ? extends Map<String, Object>> valueSerializer) {
return new SectionValueBuilder<>(this.valueClass, valueParser, valueSerializer);
}

View File

@ -15,11 +15,11 @@ public class SectionValueBuilder<V>
protected final @NotNull Class<V> valueClass;
protected @NotNull ConfigValueParser<ConfigurationWrapper, V> parser;
protected @NotNull ConfigValueParser<ConfigurationWrapper<?>, V> parser;
protected @NotNull ConfigDataFunction<V, ? extends Map<String, Object>> serializer;
public SectionValueBuilder(@NotNull Class<V> valueClass,
@NotNull ConfigValueParser<ConfigurationWrapper, V> parser,
@NotNull ConfigValueParser<ConfigurationWrapper<?>, V> parser,
@NotNull ConfigDataFunction<V, ? extends Map<String, Object>> serializer) {
this.valueClass = valueClass;
this.parser = parser;
@ -32,7 +32,7 @@ public class SectionValueBuilder<V>
return this;
}
public @NotNull SectionValueBuilder<V> parseValue(ConfigValueParser<ConfigurationWrapper, V> valueParser) {
public @NotNull SectionValueBuilder<V> parseValue(ConfigValueParser<ConfigurationWrapper<?>, V> valueParser) {
this.parser = valueParser;
return this;
}

View File

@ -7,8 +7,9 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.util.List;
import java.util.Optional;
public abstract class ConfigurationProvider<W extends ConfigurationWrapper> {
public abstract class ConfigurationProvider<W extends ConfigurationWrapper<?>> {
protected long updateTime;
@ -21,7 +22,7 @@ public abstract class ConfigurationProvider<W extends ConfigurationWrapper> {
}
public boolean isExpired(long time) {
return this.updateTime > time;
return getUpdateTime() > time;
}
public abstract @NotNull W getConfiguration();
@ -35,15 +36,27 @@ public abstract class ConfigurationProvider<W extends ConfigurationWrapper> {
protected abstract void onReload() throws Exception;
public abstract void setHeaderComment(@Nullable String path, @Nullable List<String> comments);
public abstract @Nullable ConfigurationComments getComments();
public abstract void setInlineComment(@NotNull String path, @Nullable String comment);
public void setHeaderComment(@Nullable String path, @Nullable List<String> comments) {
if (getComments() == null) return;
getComments().setHeaderComments(path, comments);
}
public void setInlineComment(@NotNull String path, @Nullable String comment) {
if (getComments() == null) return;
getComments().setInlineComment(path, comment);
}
@Nullable
@Unmodifiable
public abstract List<String> getHeaderComment(@Nullable String path);
public List<String> getHeaderComment(@Nullable String path) {
return Optional.ofNullable(getComments()).map(c -> c.getHeaderComment(path)).orElse(null);
}
public abstract @Nullable String getInlineComment(@NotNull String path);
public @Nullable String getInlineComment(@NotNull String path) {
return Optional.ofNullable(getComments()).map(c -> c.getInlineComment(path)).orElse(null);
}
public abstract @NotNull ConfigInitializer<? extends ConfigurationProvider<W>> getInitializer();

View File

@ -10,7 +10,7 @@ import java.net.URL;
import java.net.URLConnection;
import java.util.Objects;
public abstract class FileConfigProvider<W extends ConfigurationWrapper> extends ConfigurationProvider<W> {
public abstract class FileConfigProvider<W extends ConfigurationWrapper<?>> extends ConfigurationProvider<W> {
protected final @NotNull File file;

View File

@ -136,7 +136,7 @@ public abstract class ConfigValue<T> {
.orElseThrow(() -> new IllegalStateException("Value(" + configPath + ") does not have a provider."));
}
public final @NotNull ConfigurationWrapper getConfiguration() {
public final @NotNull ConfigurationWrapper<?> getConfiguration() {
try {
return getProvider().getConfiguration();
} catch (Exception ex) {

View File

@ -78,7 +78,7 @@ public class ConfiguredMap<K, V> extends CachedConfigValue<Map<K, V>> {
if (isExpired()) { // 已过时的数据需要重新解析一次
Map<K, V> map = supplier.get();
ConfigurationWrapper section = getConfiguration().getConfigurationSection(getConfigPath());
ConfigurationWrapper<?> section = getConfiguration().getConfigurationSection(getConfigPath());
if (section == null) return useOrDefault(map);
Set<String> keys = section.getKeys(false);

View File

@ -21,13 +21,13 @@ public class ConfiguredSection<V> extends CachedConfigValue<V> {
protected final @NotNull Class<V> valueClass;
protected final @NotNull ConfigValueParser<ConfigurationWrapper, V> parser;
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,
@NotNull ConfigValueParser<ConfigurationWrapper, V> parser,
@NotNull ConfigValueParser<ConfigurationWrapper<?>, V> parser,
@NotNull ConfigDataFunction<V, ? extends Map<String, Object>> serializer) {
super(provider, sectionPath, headerComments, inlineComments, defaultValue);
this.valueClass = valueClass;
@ -39,7 +39,7 @@ public class ConfiguredSection<V> extends CachedConfigValue<V> {
return valueClass;
}
public @NotNull ConfigValueParser<ConfigurationWrapper, V> getParser() {
public @NotNull ConfigValueParser<ConfigurationWrapper<?>, V> getParser() {
return parser;
}
@ -50,7 +50,7 @@ public class ConfiguredSection<V> extends CachedConfigValue<V> {
@Override
public @Nullable V get() {
if (isExpired()) { // 已过时的数据需要重新解析一次
ConfigurationWrapper section = getConfiguration().getConfigurationSection(getConfigPath());
ConfigurationWrapper<?> section = getConfiguration().getConfigurationSection(getConfigPath());
if (section == null) return useDefault();
try {
// 若未出现错误则直接更新缓存并返回

View File

@ -1,6 +1,7 @@
package cc.carm.lib.configuration.json;
import cc.carm.lib.configuration.core.ConfigInitializer;
import cc.carm.lib.configuration.core.source.ConfigurationComments;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
import com.google.gson.Gson;
@ -8,12 +9,10 @@ import com.google.gson.GsonBuilder;
import com.google.gson.JsonSerializer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.List;
/**
* Some code comes from BungeeCord's implementation of the JsonConfiguration.
@ -62,6 +61,11 @@ public class JSONConfigProvider extends FileConfigProvider<JSONConfigWrapper> {
initializeConfig();
}
@Override
public @Nullable ConfigurationComments getComments() {
return null;
}
@Override
public void save() throws Exception {
try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
@ -69,26 +73,6 @@ public class JSONConfigProvider extends FileConfigProvider<JSONConfigWrapper> {
}
}
@Override
public void setHeaderComment(@Nullable String path, @Nullable List<String> comments) {
// JSON doesn't support comments;
}
@Override
public void setInlineComment(@NotNull String path, @Nullable String comment) {
// JSON doesn't support comments;
}
@Override
public @Nullable @Unmodifiable List<String> getHeaderComment(@Nullable String path) {
return null;
}
@Override
public @Nullable String getInlineComment(@NotNull String path) {
return null;
}
@Override
public @NotNull ConfigInitializer<? extends ConfigurationProvider<JSONConfigWrapper>> getInitializer() {
return this.initializer;

View File

@ -8,7 +8,7 @@ import java.util.Map;
public class SomeModel extends AbstractModel {
int num;
public final int num;
public SomeModel(@NotNull String name, int num) {
super(name);

View File

@ -33,7 +33,7 @@ public class TestModel extends AbstractModel {
return map;
}
public static TestModel deserialize(ConfigurationWrapper section) {
public static TestModel deserialize(ConfigurationWrapper<?> section) {
String name = section.getString("name");
if (name == null) throw new NullPointerException("name is null");
String uuidString = section.getString("info.uuid");

View File

@ -1,12 +1,10 @@
package cc.carm.lib.configuration.sql;
import cc.carm.lib.configuration.core.ConfigInitializer;
import cc.carm.lib.configuration.core.source.ConfigurationComments;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.util.List;
public class SQLConfigProvider extends ConfigurationProvider<SQLSectionWrapper> {
@ -27,22 +25,7 @@ public class SQLConfigProvider extends ConfigurationProvider<SQLSectionWrapper>
}
@Override
public void setHeaderComment(@Nullable String path, @Nullable List<String> comments) {
}
@Override
public void setInlineComment(@NotNull String path, @Nullable String comment) {
}
@Override
public @Nullable @Unmodifiable List<String> getHeaderComment(@Nullable String path) {
return null;
}
@Override
public @Nullable String getInlineComment(@NotNull String path) {
public @Nullable ConfigurationComments getComments() {
return null;
}

View File

@ -16,6 +16,7 @@ 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) {

View File

@ -1,11 +1,11 @@
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 org.bspfsystems.yamlconfiguration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.io.BufferedWriter;
import java.io.File;
@ -13,7 +13,6 @@ import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
public class YAMLConfigProvider extends FileConfigProvider<YAMLSectionWrapper> {
@ -43,6 +42,12 @@ public class YAMLConfigProvider extends FileConfigProvider<YAMLSectionWrapper> {
}
@Override
public @Nullable ConfigurationComments getComments() {
return this.comments;
}
@Override
@SuppressWarnings("SpellCheckingInspection")
public void save() throws Exception {
configuration.save(getFile());
@ -60,28 +65,6 @@ public class YAMLConfigProvider extends FileConfigProvider<YAMLSectionWrapper> {
}
@Override
public void setHeaderComment(@Nullable String path, @Nullable List<String> comments) {
this.comments.setHeaderComments(path, comments);
}
@Override
public void setInlineComment(@NotNull String path, @Nullable String comment) {
this.comments.setInlineComment(path, comment);
}
@Override
@Nullable
@Unmodifiable
public List<String> getHeaderComment(@Nullable String path) {
return this.comments.getHeaderComment(path);
}
@Override
public @Nullable String getInlineComment(@NotNull String path) {
return this.comments.getInlineComment(path);
}
@Override
public @NotNull ConfigInitializer<YAMLConfigProvider> getInitializer() {
return this.initializer;

View File

@ -16,7 +16,7 @@ import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class ConfigTester {
public class DemoConfigTest {
static {
ConfigurationSerialization.registerClass(TestModel.class);

View File

@ -3,17 +3,16 @@ package config;
import config.offset.FieldOffset;
import config.offset.OffsetUtil;
import config.source.DemoConfiguration;
import org.junit.Test;
import java.util.List;
public class OffsetTest {
@Test
// @Test
public void test() {
//
// output(OffsetUtil.getClassMemberOffset(DemoConfiguration.class));
// output(OffsetUtil.getClassMemberOffset(DemoConfiguration.Sub.class));
output(OffsetUtil.getClassMemberOffset(DemoConfiguration.class));
output(OffsetUtil.getClassMemberOffset(DemoConfiguration.Sub.class));
}

View File

@ -11,7 +11,7 @@ import java.util.Map;
@SerializableAs("SomeModel")
public class SomeModel extends AbstractModel implements ConfigurationSerializable {
int num;
public final int num;
public SomeModel(@NotNull String name, int num) {
super(name);

View File

@ -38,7 +38,7 @@ public class TestModel extends AbstractModel implements ConfigurationSerializabl
return map;
}
public static TestModel deserialize(ConfigurationWrapper section) {
public static TestModel deserialize(ConfigurationWrapper<?> section) {
String name = section.getString("name");
if (name == null) throw new NullPointerException("name is null");
String uuidString = section.getString("info.uuid");

View File

@ -91,7 +91,7 @@
<downloadUrl>https://github.com/CarmJos/EasyConfiguration/releases</downloadUrl>
<site>
<id>javadoc</id>
<name>EasyConfiguration JavaDoc (on Github Pages)</name>
<name>EasyConfiguration JavaDoc (on GitHub Pages)</name>
<url>https://CarmJos.github.io/EasyConfiguration</url>
</site>
</distributionManagement>