Skip to content

Commit

Permalink
feat(section): Implement more sections
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmJos committed Feb 20, 2025
1 parent 3f1ffad commit 5c16e98
Showing 1 changed file with 32 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package cc.carm.lib.configuration.source.section;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;

public class ShadedSection extends MapSection<ShadedSection> {
private final ConfigureSection template;

// 构造方法
public ShadedSection(@Nullable ShadedSection parent, @Nullable ConfigureSection template) {
private final @Nullable Supplier<@Nullable ConfigureSection> templateSupplier;

public ShadedSection(@Nullable ShadedSection parent, @Nullable Supplier<ConfigureSection> templateSupplier) {
super(parent);
this.template = template;
this.templateSupplier = templateSupplier;
}

public ShadedSection(@NotNull Map<?, ?> data, @Nullable ShadedSection parent, @Nullable ConfigureSection template) {
public ShadedSection(@NotNull Map<?, ?> data, @Nullable ShadedSection parent, @Nullable Supplier<ConfigureSection> templateSupplier) {
super(parent);
this.template = template;
this.templateSupplier = templateSupplier;
migrate(data);
}

Expand All @@ -32,6 +33,20 @@ public ShadedSection(@NotNull Map<?, ?> data, @Nullable ShadedSection parent, @N
return new ShadedSection(data, this, null);
}

protected @Nullable ConfigureSection template() {
return templateSupplier != null ? templateSupplier.get() : null;
}

@Nullable
protected <T> T template(@NotNull Function<ConfigureSection, T> function) {
return template(null, function);
}

@Contract("!null, _ -> !null")
protected <T> T template(@Nullable T defaults, @NotNull Function<ConfigureSection, T> function) {
return Optional.ofNullable(template()).map(function).orElse(defaults);
}

@Override
public @Nullable Object get(@NotNull String path) {
// 优先从当前section获取
Expand All @@ -41,8 +56,8 @@ public ShadedSection(@NotNull Map<?, ?> data, @Nullable ShadedSection parent, @N
}

// 当前section无数据时从模板获取
if (template != null) {
Object templateValue = template.get(path);
if (templateSupplier != null) {
Object templateValue = template((template) -> template.get(path));
return wrapTemplateNestedValues(path, templateValue);
}

Expand All @@ -51,16 +66,10 @@ public ShadedSection(@NotNull Map<?, ?> data, @Nullable ShadedSection parent, @N

private Object wrapNestedValues(String path, Object value) {
if (value instanceof ConfigureSection) {
// 包装子Section,继承当前模板的对应子Section
ConfigureSection templateChild = template != null ? template.getSection(path) : null;
return new ShadedSection(
((ConfigureSection) value).getValues(false),
this,
templateChild
this, () -> template(s -> s.getSection(path))
);
} else if (value instanceof List) {
// 处理列表中的嵌套结构
return processList(path, (List<?>) value);
}
return value;
}
Expand All @@ -80,26 +89,6 @@ private Object wrapTemplateNestedValues(String path, Object templateValue) {
return templateValue;
}

private List<Object> processList(String path, List<?> list) {
List<Object> processed = new ArrayList<>();
for (Object item : list) {
if (item instanceof Map) {
processed.add(new ShadedSection(
(Map<?, ?>) item, this,
template != null ? template.getSection(path) : null
));
} else if (item instanceof ConfigureSection) {
processed.add(new ShadedSection(
((ConfigureSection) item).getValues(false), this,
template != null ? template.getSection(path) : null
));
} else {
processed.add(item);
}
}
return processed;
}

private List<Object> processTemplateList(String path, List<?> list) {
List<Object> processed = new ArrayList<>();
for (Object item : list) {
Expand All @@ -118,7 +107,7 @@ private List<Object> processTemplateList(String path, List<?> list) {
@Override
public void set(@NotNull String path, @Nullable Object value) {
// 获取模板值并深度比较
Object templateValue = template != null ? template.get(path) : null;
Object templateValue = templateSupplier != null ? templateSupplier.get(path) : null;
Object processedValue = preprocessValue(path, value);

if (isDeepEqual(processedValue, templateValue)) {
Expand All @@ -132,7 +121,7 @@ private Object preprocessValue(String path, Object value) {
if (value instanceof Map) {
return new ShadedSection(
(Map<?, ?>) value, this,
template != null ? template.getSection(path) : null
() -> template(s -> s.getSection(path))
);
} else if (value instanceof List) {
return processList(path, (List<?>) value);
Expand All @@ -147,15 +136,9 @@ private boolean isDeepEqual(Object a, Object b) {
if (a instanceof ConfigureSection && b instanceof ConfigureSection) {
return ((ConfigureSection) a).getValues(true)
.equals(((ConfigureSection) b).getValues(true));
} else if (a instanceof List && b instanceof List) {
List<?> listA = (List<?>) a;
List<?> listB = (List<?>) b;
if (listA.size() != listB.size()) return false;
for (int i = 0; i < listA.size(); i++) {
if (!isDeepEqual(listA.get(i), listB.get(i))) return false;
}
return true;
}
return a.equals(b);

return Objects.equals(a, b);
}

}

0 comments on commit 5c16e98

Please sign in to comment.