-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigSection.java
170 lines (136 loc) · 4.96 KB
/
ConfigSection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package dev.manere.inscript;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import dev.manere.inscript.node.ConfigNode;
import dev.manere.inscript.node.RootSectionNode;
import dev.manere.inscript.node.ScalarNode;
import dev.manere.inscript.node.SectionNode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.util.*;
import java.util.function.Consumer;
public interface ConfigSection {
@NotNull
@Unmodifiable
default Set<ConfigNode> getChildren() {
return new LinkedHashSet<>(getSection().getChildren());
}
@NotNull
@CanIgnoreReturnValue
default ConfigSection copy(final @NotNull ConfigSection other) {
getSection().getChildren().addAll(other.getChildren());
return this;
}
@NotNull
@Unmodifiable
default Set<String> getKeys() {
final Set<String> keys = new LinkedHashSet<>();
for (final ConfigNode node : getChildren()) {
keys.add(node.getKey());
}
return keys;
}
default boolean isRoot() {
return getSection() instanceof RootSectionNode;
}
@NotNull
default Optional<ConfigNode> getNode(final @NotNull String key) {
for (final ConfigNode node : getChildren()) if (node.getKey().equals(key)) return Optional.of(node);
return Optional.empty();
}
default boolean isSection(final @NotNull String key) {
return getNode(key).orElse(null) instanceof SectionNode;
}
default boolean isScalar(final @NotNull String key) {
return getNode(key).orElse(null) instanceof ScalarNode<?>;
}
@NotNull
SectionNode getSection();
@NotNull
Optional<ConfigSection> getSection(final @NotNull String key);
@NotNull
ConfigSection createSection(final @NotNull String key);
@NotNull
@CanIgnoreReturnValue
default ConfigSection section(final @NotNull String key, final @NotNull Consumer<ConfigSection> handler) {
handler.accept(getSection(key).orElse(createSection(key)));
return this;
}
@NotNull
<T> Optional<T> get(final @NotNull String key, final @NotNull Class<? extends T> ignoredType);
@NotNull
<T> List<T> getList(final @NotNull String key, final @NotNull Class<? extends T> ignoredType);
@NotNull
@CanIgnoreReturnValue
<T> ConfigSection set(final @NotNull String key, final @Nullable T value);
default boolean has(final @NotNull String key) {
return contains(key);
}
default boolean contains(final @NotNull String key) {
return getNode(key).isPresent();
}
@NotNull
@CanIgnoreReturnValue
default ConfigSection unset(final @NotNull String key) {
getNode(key).ifPresent(node -> getSection().getChildren().remove(node));
return this;
}
@NotNull
@CanIgnoreReturnValue
default ConfigSection reset() {
getSection().getChildren().clear();
return this;
}
@NotNull
@CanIgnoreReturnValue
default ConfigSection forEachSection(final @NotNull Consumer<ConfigSection> sectionConsumer) {
for (final ConfigNode node : getSection().getChildren()) {
getSection(node.getKey()).ifPresent(sectionConsumer);
}
return this;
}
@NotNull
@CanIgnoreReturnValue
default ConfigSection forEachScalar(final @NotNull Consumer<ScalarNode<?>> scalarConsumer) {
for (final ConfigNode node : getSection().getChildren()) {
if (node instanceof ScalarNode<?> scalar) scalarConsumer.accept(scalar);
}
return this;
}
@NotNull
@CanIgnoreReturnValue
default ConfigSection forEach(final @NotNull Consumer<ScalarNode<?>> scalarConsumer, final @NotNull Consumer<ConfigSection> sectionConsumer) {
for (final ConfigNode node : getSection().getChildren()) {
if (node instanceof ScalarNode<?> scalar) {
scalarConsumer.accept(scalar);
} else if (node instanceof SectionNode section) {
sectionConsumer.accept(new SimpleConfigSection(section));
}
}
return this;
}
@NotNull
@CanIgnoreReturnValue
default ConfigSection comment(final @NotNull String key, final @NotNull Collection<? extends String> comments) {
getNode(key).ifPresent(node -> {
node.getComments().clear();
node.getComments().addAll(comments);
});
return this;
}
@NotNull
default Collection<String> getComments(final @NotNull String key) {
final ConfigNode node = getNode(key).orElse(null);
if (node == null) return Set.of();
return Set.copyOf(node.getComments());
}
@NotNull
@CanIgnoreReturnValue
default ConfigSection comment(final @NotNull String key, final @NotNull String @NotNull ... comments) {
return comment(key, Arrays.asList(comments));
}
@NotNull
default String getKey() {
return getSection().getKey();
}
}