mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-05 02:58:20 +08:00
feat(map): Finished Map value builders.
This commit is contained in:
@@ -49,8 +49,8 @@ public class ConfigurationTest {
|
||||
System.out.println(" After size :" + DemoConfiguration.SUB.That.OPERATORS.size());
|
||||
|
||||
System.out.println("> Test Section:");
|
||||
System.out.println(DemoConfiguration.USERS.get());
|
||||
DemoConfiguration.USERS.add(UserRecord.random());
|
||||
System.out.println(DemoConfiguration.ALLOWLISTS.get());
|
||||
DemoConfiguration.ALLOWLISTS.add(UserRecord.random());
|
||||
|
||||
// System.out.println("> Test Maps:");
|
||||
// DemoConfiguration.USERS.forEach((k, v) -> System.out.println(k + ": " + v));
|
||||
|
||||
+24
-8
@@ -6,18 +6,26 @@ import cc.carm.lib.configuration.annotation.FooterComments;
|
||||
import cc.carm.lib.configuration.annotation.HeaderComments;
|
||||
import cc.carm.lib.configuration.annotation.InlineComment;
|
||||
import cc.carm.lib.configuration.demo.DatabaseConfiguration;
|
||||
import cc.carm.lib.configuration.demo.tests.model.ItemStack;
|
||||
import cc.carm.lib.configuration.demo.tests.model.UserRecord;
|
||||
import cc.carm.lib.configuration.value.ConfigValue;
|
||||
import cc.carm.lib.configuration.value.standard.ConfiguredList;
|
||||
import cc.carm.lib.configuration.value.standard.ConfiguredMap;
|
||||
import cc.carm.lib.configuration.value.standard.ConfiguredValue;
|
||||
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
@ConfigPath(root = true)
|
||||
@HeaderComments({"此处内容将显示在配置文件的最上方"})
|
||||
@FooterComments({"此处内容将显示在配置文件的最下方", "可用于显示版权信息等"})
|
||||
@FooterComments({
|
||||
"------------------------------------------------",
|
||||
"此处内容将显示在配置文件的最下方",
|
||||
"可用于显示版权信息等",
|
||||
"感谢您使用 https://github.com/CarmJos/EasyConfiguration !"
|
||||
})
|
||||
public interface DemoConfiguration extends Configuration {
|
||||
|
||||
@ConfigPath(root = true)
|
||||
@@ -40,16 +48,24 @@ public interface DemoConfiguration extends Configuration {
|
||||
@InlineComment("默认地注释会加到Section的首行末尾") // 通过注解给配置添加注释。
|
||||
@InlineComment(value = "用户名(匹配注释)", regex = "name") // 通过注解给配置添加注释。
|
||||
@InlineComment(value = "信息", regex = "info.*") // 通过注解给配置添加注释。
|
||||
ConfiguredList<UserRecord> USERS = ConfiguredList.builderOf(UserRecord.class).fromSection()
|
||||
ConfiguredList<UserRecord> ALLOWLISTS = ConfiguredList.builderOf(UserRecord.class).fromSection()
|
||||
.parse(UserRecord::deserialize).serialize(UserRecord::serialize)
|
||||
.defaults(UserRecord.CARM).build();
|
||||
|
||||
// @HeaderComment({"[ID - UUID]对照表", "", "用于测试Map类型的解析与序列化保存"})
|
||||
// ConfiguredMap<Integer, UUID> USERS = ConfiguredMap.builderOf(Integer.class, UUID.class)
|
||||
// .asLinkedMap().fromString()
|
||||
// .parseKey(Integer::parseInt)
|
||||
// .parseValue(v -> Objects.requireNonNull(UUID.fromString(v)))
|
||||
// .build();
|
||||
@HeaderComments({
|
||||
"------------------------------------------------",
|
||||
"[ID - ItemStack]对照表", "", "用于测试Map类型的解析与序列化保存"
|
||||
})
|
||||
@FooterComments("------------------------------------------------")
|
||||
ConfiguredMap<Integer, ItemStack> ITEMS = ConfiguredMap.builderOf(Integer.class, ItemStack.class)
|
||||
.asLinkedMap().fromSection()
|
||||
.parseKey(data -> Integer.parseInt(data))
|
||||
.parse(ItemStack::deserialize).serialize(ItemStack::serialize)
|
||||
.defaults(m -> {
|
||||
m.put(1, new ItemStack("stone", 64));
|
||||
m.put(2, new ItemStack("iron", 64, "铁锭", Arrays.asList("一些铁锭", "可以制造东西")));
|
||||
})
|
||||
.build();
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package cc.carm.lib.configuration.demo.tests.model;
|
||||
|
||||
import cc.carm.lib.configuration.source.section.ConfigureSection;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class ItemStack {
|
||||
|
||||
protected String material;
|
||||
protected int amount;
|
||||
protected @Nullable String name;
|
||||
protected @Nullable List<String> lore;
|
||||
|
||||
public ItemStack(String material, int amount) {
|
||||
this(material, amount, null, null);
|
||||
}
|
||||
|
||||
public ItemStack(String material, int amount, @Nullable String name, @Nullable List<String> lore) {
|
||||
this.material = material;
|
||||
this.amount = amount;
|
||||
this.name = name;
|
||||
this.lore = lore;
|
||||
}
|
||||
|
||||
public String getMaterial() {
|
||||
return material;
|
||||
}
|
||||
|
||||
public int getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public List<String> getLore() {
|
||||
return lore;
|
||||
}
|
||||
|
||||
public void setMaterial(String material) {
|
||||
this.material = material;
|
||||
}
|
||||
|
||||
public void setAmount(int amount) {
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setLore(List<String> lore) {
|
||||
this.lore = lore;
|
||||
}
|
||||
|
||||
|
||||
public Map<String, Object> serialize() {
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
map.put("material", material);
|
||||
if (amount != 1) map.put("amount", amount);
|
||||
if (name != null) map.put("name", name);
|
||||
if (lore != null) map.put("lore", lore);
|
||||
return map;
|
||||
}
|
||||
|
||||
public static ItemStack deserialize(ConfigureSection section) {
|
||||
return new ItemStack(
|
||||
section.getString("material"),
|
||||
section.getInt("amount", 1),
|
||||
section.getString("name"),
|
||||
section.getStringList("lore")
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user