mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 10:38:19 +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 org.jetbrains.annotations.UnmodifiableView;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.function.Supplier;
|
|
||||||
|
|
||||||
public class ShadedSection implements ConfigureSection {
|
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 @NotNull ConfigureSection template;
|
||||||
protected @Nullable ConfigureSection source;
|
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.template = template;
|
||||||
this.source = source;
|
this.source = source;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @Nullable ConfigureSection parent() {
|
public @Nullable ConfigureSection parent() {
|
||||||
return null;
|
return this.parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull @UnmodifiableView Map<String, Object> getValues(boolean deep) {
|
public @NotNull @UnmodifiableView Map<String, Object> getValues(boolean deep) {
|
||||||
if (source == null) {
|
if (source == null) return template.getValues(deep);
|
||||||
return template.getValues(deep);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 本函数为,当 getValues 时,递归合并 source 和 template
|
// 本函数为,当 getValues 时,递归合并 source 和 template
|
||||||
return merge(template, source).getValues(deep);
|
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) {
|
private ConfigureSection merge(ConfigureSection templateSection, ConfigureSection valueSection) {
|
||||||
MemorySection merged = MemorySection.of();
|
MemorySection merged = MemorySection.of();
|
||||||
List<String> existingKey = new ArrayList<>();
|
Set<String> existingKey = new HashSet<>();
|
||||||
|
|
||||||
for (Map.Entry<String, Object> entry : valueSection.getValues(false).entrySet()) {
|
for (Map.Entry<String, Object> entry : valueSection.getValues(false).entrySet()) {
|
||||||
String key = entry.getKey();
|
String key = entry.getKey();
|
||||||
@@ -42,7 +54,7 @@ public class ShadedSection implements ConfigureSection {
|
|||||||
if (value instanceof ConfigureSection) {
|
if (value instanceof ConfigureSection) {
|
||||||
ConfigureSection subSectionFromValue = (ConfigureSection) value;
|
ConfigureSection subSectionFromValue = (ConfigureSection) value;
|
||||||
ConfigureSection subSectionFromTemplate = (ConfigureSection) templateSection.get(key);
|
ConfigureSection subSectionFromTemplate = (ConfigureSection) templateSection.get(key);
|
||||||
if(subSectionFromTemplate == null) {
|
if (subSectionFromTemplate == null) {
|
||||||
merged.set(key, value);
|
merged.set(key, value);
|
||||||
} else {
|
} else {
|
||||||
merged.set(key, merge(subSectionFromTemplate, subSectionFromValue));
|
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()) {
|
for (Map.Entry<String, Object> entry : templateSection.getValues(false).entrySet()) {
|
||||||
if (existingKey.contains(entry.getKey())) {
|
if (existingKey.contains(entry.getKey())) continue;
|
||||||
continue;
|
|
||||||
}
|
|
||||||
merged.set(entry.getKey(), entry.getValue());
|
merged.set(entry.getKey(), entry.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,20 +78,20 @@ public class ShadedSection implements ConfigureSection {
|
|||||||
if (value instanceof ConfigureSection) {
|
if (value instanceof ConfigureSection) {
|
||||||
ConfigureSection targetSection = (ConfigureSection) value;
|
ConfigureSection targetSection = (ConfigureSection) value;
|
||||||
for (Map.Entry<String, Object> entry : targetSection.getValues(true).entrySet()) {
|
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;
|
return;
|
||||||
} else if (Objects.equals(get(path), value)){
|
} else if (Objects.equals(get(path), value)) {
|
||||||
remove(path);
|
remove(path);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional.ofNullable(source).ifPresent(source -> source.set(path, value));
|
Optional.ofNullable(source).ifPresent(s -> s.set(path, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void remove(@NotNull String path) {
|
public void remove(@NotNull String path) {
|
||||||
Optional.ofNullable(source).ifPresent(source -> source.remove(path));
|
Optional.ofNullable(source).ifPresent(s -> s.remove(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -104,7 +114,7 @@ public class ShadedSection implements ConfigureSection {
|
|||||||
if (templateSection == null) {
|
if (templateSection == null) {
|
||||||
return value;
|
return value;
|
||||||
} else {
|
} else {
|
||||||
return new ShadedSection(templateSection, (ConfigureSection) value);
|
return new ShadedSection(this, templateSection, (ConfigureSection) value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
@@ -113,13 +123,13 @@ public class ShadedSection implements ConfigureSection {
|
|||||||
public @Nullable Object getFromTemplate(@NotNull String path) {
|
public @Nullable Object getFromTemplate(@NotNull String path) {
|
||||||
Object value = template.get(path);
|
Object value = template.get(path);
|
||||||
if (value instanceof ConfigureSection) {
|
if (value instanceof ConfigureSection) {
|
||||||
return new ShadedSection((ConfigureSection) value, null);
|
return new ShadedSection(this, (ConfigureSection) value, null);
|
||||||
} else {
|
} else {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConfigureSection getSource() {
|
public @Nullable ConfigureSection getSource() {
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -41,36 +41,36 @@ public class ShadeTest {
|
|||||||
data.put("cards", Arrays.asList("33333", "55555")); // 应当直接覆盖原先的List
|
data.put("cards", Arrays.asList("33333", "55555")); // 应当直接覆盖原先的List
|
||||||
});
|
});
|
||||||
|
|
||||||
ShadedSection root = new ShadedSection(template, source);
|
ShadedSection root = new ShadedSection(null, template, source);
|
||||||
System.out.println("age: "+root.get("age"));
|
System.out.println("age: " + root.get("age"));
|
||||||
System.out.println("addresses: ");
|
System.out.println("addresses: ");
|
||||||
for (Map.Entry<String, Object> entry : root.getSection("addresses").getValues(false).entrySet()) {
|
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) {
|
if (entry.getValue() instanceof ConfigureSection) {
|
||||||
for(Map.Entry<String, Object> inner : ((ConfigureSection) entry.getValue()).getValues(false).entrySet()) {
|
for (Map.Entry<String, Object> inner : ((ConfigureSection) entry.getValue()).getValues(false).entrySet()) {
|
||||||
System.out.println(" "+inner.getKey()+": "+inner.getValue());
|
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("\n----------------------\n");
|
||||||
System.out.println("Deep Search Test");
|
System.out.println("Deep Search Test");
|
||||||
System.out.println("addresses: ");
|
System.out.println("addresses: ");
|
||||||
for (Map.Entry<String, Object> entry : root.getSection("addresses").getValues(true).entrySet()) {
|
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");
|
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("Hotel", "Nanjing Road 101");
|
||||||
map.put("Store", "Beijing Road banned");
|
map.put("Store", "Beijing Road banned");
|
||||||
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
for (Map.Entry<String, Object> entry : source.getValues(true).entrySet()) {
|
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