mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-05 02:58:20 +08:00
feat(section): Implement shaded sections
This commit is contained in:
+29
-19
@@ -5,36 +5,48 @@ import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.UnmodifiableView;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class ShadedSection implements ConfigureSection {
|
||||
|
||||
public static ShadedSection create(@NotNull ConfigureSection template, @Nullable ConfigureSection source) {
|
||||
return new ShadedSection(null, template, source);
|
||||
}
|
||||
|
||||
protected final @Nullable ShadedSection parent;
|
||||
protected @NotNull ConfigureSection template;
|
||||
protected @Nullable ConfigureSection source;
|
||||
|
||||
public ShadedSection(@NotNull ConfigureSection template, @Nullable ConfigureSection source) {
|
||||
public ShadedSection(@Nullable ShadedSection parent,
|
||||
@NotNull ConfigureSection template, @Nullable ConfigureSection source) {
|
||||
this.parent = parent;
|
||||
this.template = template;
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable ConfigureSection parent() {
|
||||
return null;
|
||||
return this.parent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull @UnmodifiableView Map<String, Object> getValues(boolean deep) {
|
||||
if (source == null) {
|
||||
return template.getValues(deep);
|
||||
}
|
||||
|
||||
if (source == null) return template.getValues(deep);
|
||||
// 本函数为,当 getValues 时,递归合并 source 和 template
|
||||
return merge(template, source).getValues(deep);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull @UnmodifiableView Set<String> getKeys(boolean deep) {
|
||||
Set<String> keys = new HashSet<>(template.getKeys(deep));
|
||||
if (source != null) {
|
||||
keys.addAll(source.getKeys(deep));
|
||||
}
|
||||
return Collections.unmodifiableSet(keys);
|
||||
}
|
||||
|
||||
private ConfigureSection merge(ConfigureSection templateSection, ConfigureSection valueSection) {
|
||||
MemorySection merged = MemorySection.of();
|
||||
List<String> existingKey = new ArrayList<>();
|
||||
Set<String> existingKey = new HashSet<>();
|
||||
|
||||
for (Map.Entry<String, Object> entry : valueSection.getValues(false).entrySet()) {
|
||||
String key = entry.getKey();
|
||||
@@ -42,7 +54,7 @@ public class ShadedSection implements ConfigureSection {
|
||||
if (value instanceof ConfigureSection) {
|
||||
ConfigureSection subSectionFromValue = (ConfigureSection) value;
|
||||
ConfigureSection subSectionFromTemplate = (ConfigureSection) templateSection.get(key);
|
||||
if(subSectionFromTemplate == null) {
|
||||
if (subSectionFromTemplate == null) {
|
||||
merged.set(key, value);
|
||||
} else {
|
||||
merged.set(key, merge(subSectionFromTemplate, subSectionFromValue));
|
||||
@@ -54,9 +66,7 @@ public class ShadedSection implements ConfigureSection {
|
||||
}
|
||||
|
||||
for (Map.Entry<String, Object> entry : templateSection.getValues(false).entrySet()) {
|
||||
if (existingKey.contains(entry.getKey())) {
|
||||
continue;
|
||||
}
|
||||
if (existingKey.contains(entry.getKey())) continue;
|
||||
merged.set(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
@@ -68,20 +78,20 @@ public class ShadedSection implements ConfigureSection {
|
||||
if (value instanceof ConfigureSection) {
|
||||
ConfigureSection targetSection = (ConfigureSection) value;
|
||||
for (Map.Entry<String, Object> entry : targetSection.getValues(true).entrySet()) {
|
||||
set(path+pathSeparator()+entry.getKey(), entry.getValue());
|
||||
set(path + pathSeparator() + entry.getKey(), entry.getValue());
|
||||
}
|
||||
return;
|
||||
} else if (Objects.equals(get(path), value)){
|
||||
} else if (Objects.equals(get(path), value)) {
|
||||
remove(path);
|
||||
return;
|
||||
}
|
||||
|
||||
Optional.ofNullable(source).ifPresent(source -> source.set(path, value));
|
||||
Optional.ofNullable(source).ifPresent(s -> s.set(path, value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(@NotNull String path) {
|
||||
Optional.ofNullable(source).ifPresent(source -> source.remove(path));
|
||||
Optional.ofNullable(source).ifPresent(s -> s.remove(path));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -104,7 +114,7 @@ public class ShadedSection implements ConfigureSection {
|
||||
if (templateSection == null) {
|
||||
return value;
|
||||
} else {
|
||||
return new ShadedSection(templateSection, (ConfigureSection) value);
|
||||
return new ShadedSection(this, templateSection, (ConfigureSection) value);
|
||||
}
|
||||
}
|
||||
return value;
|
||||
@@ -113,13 +123,13 @@ public class ShadedSection implements ConfigureSection {
|
||||
public @Nullable Object getFromTemplate(@NotNull String path) {
|
||||
Object value = template.get(path);
|
||||
if (value instanceof ConfigureSection) {
|
||||
return new ShadedSection((ConfigureSection) value, null);
|
||||
return new ShadedSection(this, (ConfigureSection) value, null);
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
public ConfigureSection getSource() {
|
||||
public @Nullable ConfigureSection getSource() {
|
||||
return source;
|
||||
}
|
||||
}
|
||||
@@ -41,36 +41,36 @@ public class ShadeTest {
|
||||
data.put("cards", Arrays.asList("33333", "55555")); // 应当直接覆盖原先的List
|
||||
});
|
||||
|
||||
ShadedSection root = new ShadedSection(template, source);
|
||||
System.out.println("age: "+root.get("age"));
|
||||
ShadedSection root = new ShadedSection(null, template, source);
|
||||
System.out.println("age: " + root.get("age"));
|
||||
System.out.println("addresses: ");
|
||||
for (Map.Entry<String, Object> entry : root.getSection("addresses").getValues(false).entrySet()) {
|
||||
System.out.println(" "+entry.getKey()+": "+entry.getValue());
|
||||
System.out.println(" " + entry.getKey() + ": " + entry.getValue());
|
||||
if (entry.getValue() instanceof ConfigureSection) {
|
||||
for(Map.Entry<String, Object> inner : ((ConfigureSection) entry.getValue()).getValues(false).entrySet()) {
|
||||
System.out.println(" "+inner.getKey()+": "+inner.getValue());
|
||||
for (Map.Entry<String, Object> inner : ((ConfigureSection) entry.getValue()).getValues(false).entrySet()) {
|
||||
System.out.println(" " + inner.getKey() + ": " + inner.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("cards: "+root.getList("cards"));
|
||||
System.out.println("cards: " + root.getList("cards"));
|
||||
|
||||
System.out.println("\n----------------------\n");
|
||||
System.out.println("Deep Search Test");
|
||||
System.out.println("addresses: ");
|
||||
for (Map.Entry<String, Object> entry : root.getSection("addresses").getValues(true).entrySet()) {
|
||||
System.out.println(" "+entry.getKey()+": "+entry.getValue());
|
||||
System.out.println(" " + entry.getKey() + ": " + entry.getValue());
|
||||
}
|
||||
|
||||
System.out.println("\n----------------------\n");
|
||||
|
||||
root.set("addresses", MemorySection.of(map->{
|
||||
root.set("addresses", MemorySection.of(map -> {
|
||||
map.put("Hotel", "Nanjing Road 101");
|
||||
map.put("Store", "Beijing Road banned");
|
||||
|
||||
}));
|
||||
|
||||
for (Map.Entry<String, Object> entry : source.getValues(true).entrySet()) {
|
||||
System.out.println(" "+entry.getKey()+": "+entry.getValue());
|
||||
System.out.println(" " + entry.getKey() + ": " + entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user