mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 18:48:20 +08:00
初始版本完成
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package cc.carm.lib.configuration;
|
||||
|
||||
import cc.carm.lib.configuration.yaml.YamlConfigProvider;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class EasyConfiguration {
|
||||
|
||||
public static YamlConfigProvider from(File file, String source) {
|
||||
YamlConfigProvider provider = new YamlConfigProvider(file);
|
||||
try {
|
||||
provider.initializeFile(source);
|
||||
provider.initialize();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return provider;
|
||||
}
|
||||
|
||||
public static YamlConfigProvider from(File file) {
|
||||
return from(file, file.getName());
|
||||
}
|
||||
|
||||
public static YamlConfigProvider from(String fileName) {
|
||||
return from(fileName, fileName);
|
||||
}
|
||||
|
||||
public static YamlConfigProvider from(String fileName, String source) {
|
||||
return from(new File(fileName), source);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package cc.carm.lib.configuration.yaml;
|
||||
|
||||
import org.bspfsystems.yamlconfiguration.commented.CommentsProvider;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class YamlComments implements CommentsProvider {
|
||||
|
||||
Map<String, String[]> comments = new HashMap<>();
|
||||
|
||||
protected Map<String, String[]> getComments() {
|
||||
return comments;
|
||||
}
|
||||
|
||||
public void set(@NotNull String path, @NotNull String... comments) {
|
||||
if (comments.length == 0) {
|
||||
getComments().remove(path);
|
||||
} else {
|
||||
getComments().put(path, comments);
|
||||
}
|
||||
}
|
||||
|
||||
public @Nullable String[] get(@NotNull String path) {
|
||||
return getComments().get(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] apply(String s) {
|
||||
return get(s);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package cc.carm.lib.configuration.yaml;
|
||||
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
|
||||
import org.bspfsystems.yamlconfiguration.commented.CommentedYamlConfiguration;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class YamlConfigProvider extends FileConfigProvider {
|
||||
|
||||
YamlComments comments = new YamlComments();
|
||||
CommentedYamlConfiguration configuration;
|
||||
|
||||
public YamlConfigProvider(@NotNull File file) {
|
||||
super(file);
|
||||
}
|
||||
|
||||
public void initialize() {
|
||||
this.configuration = CommentedYamlConfiguration.loadConfiguration(comments, file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfigurationWrapper getConfiguration() {
|
||||
return YamlSectionWrapper.of(configuration);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reload() throws Exception {
|
||||
configuration.load(getFile());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() throws Exception {
|
||||
configuration.save(getFile());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setComments(@NotNull String path, @NotNull String... comments) {
|
||||
this.comments.set(path, comments);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String[] getComments(@NotNull String path) {
|
||||
return this.comments.get(path);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package cc.carm.lib.configuration.yaml;
|
||||
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||
import org.bspfsystems.yamlconfiguration.configuration.ConfigurationSection;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class YamlSectionWrapper implements ConfigurationWrapper {
|
||||
|
||||
private final ConfigurationSection section;
|
||||
|
||||
private YamlSectionWrapper(ConfigurationSection section) {
|
||||
this.section = section;
|
||||
}
|
||||
|
||||
@Contract("!null->!null")
|
||||
public static @Nullable YamlSectionWrapper of(@Nullable ConfigurationSection section) {
|
||||
return section == null ? null : new YamlSectionWrapper(section);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Set<String> getKeys(boolean deep) {
|
||||
return new LinkedHashSet<>(section.getKeys(deep));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Map<String, Object> getValues(boolean deep) {
|
||||
return section.getValues(deep);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(@NotNull String path, @Nullable Object value) {
|
||||
this.section.set(path, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(@NotNull String path) {
|
||||
return this.section.contains(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Object get(@NotNull String path) {
|
||||
return this.section.get(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isList(@NotNull String path) {
|
||||
return this.section.isList(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable List<?> getList(@NotNull String path) {
|
||||
return this.section.getList(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConfigurationSection(@NotNull String path) {
|
||||
return this.section.isConfigurationSection(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable ConfigurationWrapper getConfigurationSection(@NotNull String path) {
|
||||
return of(this.section.getConfigurationSection(path));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package config;
|
||||
|
||||
import cc.carm.lib.configuration.EasyConfiguration;
|
||||
import cc.carm.lib.configuration.core.ConfigInitializer;
|
||||
import cc.carm.lib.configuration.yaml.YamlConfigProvider;
|
||||
import config.misc.TestUser;
|
||||
import config.source.TestConfiguration;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class ConfigTester {
|
||||
|
||||
@Test
|
||||
public void onTest() {
|
||||
|
||||
YamlConfigProvider provider = EasyConfiguration.from("target/config.yml", "config.yml");
|
||||
ConfigInitializer.initialize(provider, TestConfiguration.class, true);
|
||||
|
||||
System.out.println("before: " + TestConfiguration.Sub.UUID_CONFIG_VALUE.get());
|
||||
TestConfiguration.Sub.UUID_CONFIG_VALUE.set(UUID.randomUUID());
|
||||
System.out.println("after: " + TestConfiguration.Sub.UUID_CONFIG_VALUE.get());
|
||||
|
||||
|
||||
TestConfiguration.Sub.That.Operators.getNotNull().forEach(System.out::println);
|
||||
List<UUID> operators = IntStream.range(0, 5).mapToObj(i -> UUID.randomUUID()).collect(Collectors.toList());
|
||||
TestConfiguration.Sub.That.Operators.set(operators);
|
||||
|
||||
System.out.println(TestConfiguration.USER.get());
|
||||
TestUser b = new TestUser(UUID.randomUUID().toString().substring(0, 3), UUID.randomUUID());
|
||||
TestConfiguration.USER.set(b);
|
||||
|
||||
TestConfiguration.USERS.getNotNull().forEach((k, v) -> System.out.println(k + ": " + v));
|
||||
LinkedHashMap<Integer, UUID> data = new LinkedHashMap<>();
|
||||
for (int i = 0; i < 5; i++) {
|
||||
data.put((int) (1000 * Math.random()), UUID.randomUUID());
|
||||
}
|
||||
TestConfiguration.USERS.set(data);
|
||||
|
||||
try {
|
||||
provider.save();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package config.misc;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class TestUser {
|
||||
|
||||
public String name;
|
||||
public UUID uuid;
|
||||
|
||||
public TestUser(String name, UUID uuid) {
|
||||
this.name = name;
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setUuid(UUID uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public UUID getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TestUser{" +
|
||||
"name='" + name + '\'' +
|
||||
", uuid=" + uuid +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package config.source;
|
||||
|
||||
import cc.carm.lib.configuration.core.ConfigurationRoot;
|
||||
import cc.carm.lib.configuration.core.annotation.ConfigComment;
|
||||
import cc.carm.lib.configuration.core.annotation.ConfigPath;
|
||||
import cc.carm.lib.configuration.core.builder.ConfigBuilder;
|
||||
import cc.carm.lib.configuration.core.util.MapFactory;
|
||||
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||
import config.misc.TestUser;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
public class TestConfiguration extends ConfigurationRoot {
|
||||
|
||||
@ConfigComment({"User测试"})
|
||||
public static final ConfigValue<TestUser> USER = ConfigBuilder
|
||||
.asValue(TestUser.class).fromSection()
|
||||
.defaults(new TestUser("Carm", UUID.randomUUID()))
|
||||
.parseValue((section, defaultValue) -> new TestUser(
|
||||
section.getString("name"),
|
||||
UUID.fromString(section.getString("user.uuid", UUID.randomUUID().toString()))
|
||||
)).serializeValue(user -> MapFactory.<String, Object>linkedMap()
|
||||
.put("name", user.getName())
|
||||
.put("user.uuid", user.getUuid().toString())
|
||||
.build()
|
||||
).build();
|
||||
|
||||
@ConfigComment({"[ID-UUID] 对照表", "", "用于测试Map类型的解析与序列化保存"})
|
||||
public static final ConfigValue<Map<Integer, UUID>> USERS = ConfigBuilder
|
||||
.asMap(Integer.class, UUID.class).fromString()
|
||||
.parseKey(Integer::parseInt)
|
||||
.parseValue(v -> Objects.requireNonNull(UUID.fromString(v)))
|
||||
.build();
|
||||
|
||||
public static class Sub {
|
||||
|
||||
@ConfigPath("uuid")
|
||||
public static final ConfigValue<UUID> UUID_CONFIG_VALUE = ConfigBuilder
|
||||
.asValue(UUID.class).fromString()
|
||||
.parseValue((data, defaultValue) -> UUID.fromString(data))
|
||||
.build();
|
||||
|
||||
@ConfigPath("nothing")
|
||||
public static class That {
|
||||
|
||||
public static final ConfigValue<List<UUID>> Operators = ConfigBuilder.asList(UUID.class).fromString()
|
||||
.parseValue(s -> Objects.requireNonNull(UUID.fromString(s))).build();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
something: 123123
|
||||
Reference in New Issue
Block a user