Skip to content

Commit 426ca5b

Browse files
Add API methods to get config entries and paths from config
1 parent 920a84a commit 426ca5b

File tree

7 files changed

+46
-30
lines changed

7 files changed

+46
-30
lines changed

core/src/main/java/io/github/almightysatan/jaskl/Config.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@
2323
import org.jetbrains.annotations.NotNull;
2424
import org.jetbrains.annotations.Nullable;
2525
import org.jetbrains.annotations.Unmodifiable;
26+
import org.jetbrains.annotations.UnmodifiableView;
2627

2728
import java.io.IOException;
29+
import java.util.Collection;
30+
import java.util.Map;
2831
import java.util.Set;
2932

3033
public interface Config extends AutoCloseable {
@@ -105,4 +108,25 @@ default void strip() throws IOException, UnsupportedOperationException {
105108
* @throws IOException if an I/O exception occurs
106109
*/
107110
boolean isReadOnly() throws IOException;
111+
112+
/**
113+
* Returns a map of all paths with their {@link ConfigEntry}.
114+
*
115+
* @return a map of all paths with their {@link ConfigEntry}
116+
*/
117+
@UnmodifiableView @NotNull Map<@NotNull String, @NotNull ConfigEntry<?>> getEntryMap();
118+
119+
/**
120+
* Returns a collection of all paths.
121+
*
122+
* @return a collection of all paths
123+
*/
124+
@UnmodifiableView @NotNull Collection<@NotNull String> getPaths();
125+
126+
/**
127+
* Returns a collection of all {@link ConfigEntry config entries}.
128+
*
129+
* @return a collection of all {@link ConfigEntry config entries}
130+
*/
131+
@UnmodifiableView @NotNull Collection<@NotNull ConfigEntry<?>> getEntries();
108132
}

core/src/main/java/io/github/almightysatan/jaskl/impl/ConfigImpl.java

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.github.almightysatan.jaskl.*;
2424
import org.jetbrains.annotations.NotNull;
2525
import org.jetbrains.annotations.Nullable;
26+
import org.jetbrains.annotations.UnmodifiableView;
2627

2728
import java.io.IOException;
2829
import java.util.*;
@@ -74,36 +75,24 @@ public boolean isReadOnly() throws IOException {
7475
return false;
7576
}
7677

77-
/**
78-
* Returns a map of all paths with their config entries.
79-
*
80-
* @return a map of all paths with their config entries
81-
*/
82-
public @NotNull Map<String, ConfigEntry<?>> getEntries() {
83-
return entries;
78+
@Override
79+
public @UnmodifiableView @NotNull Map<@NotNull String, @NotNull ConfigEntry<?>> getEntryMap() {
80+
return Collections.unmodifiableMap(this.entries);
8481
}
8582

86-
/**
87-
* Returns a set of all the paths of the config entries.
88-
*
89-
* @return a set of all the paths of the config entries
90-
*/
91-
public @NotNull Set<String> getPaths() {
92-
return this.getEntries().keySet();
83+
@Override
84+
public @UnmodifiableView @NotNull Collection<@NotNull String> getPaths() {
85+
return Collections.unmodifiableCollection(this.getEntryMap().keySet());
9386
}
9487

95-
/**
96-
* Returns a collection of all config entries.
97-
*
98-
* @return a collection of all config entries
99-
*/
100-
public @NotNull Collection<ConfigEntry<?>> getValues() {
101-
return this.getEntries().values();
88+
@Override
89+
public @UnmodifiableView @NotNull Collection<@NotNull ConfigEntry<?>> getEntries() {
90+
return Collections.unmodifiableCollection(this.getEntryMap().values());
10291
}
10392

10493
@SuppressWarnings({"unchecked", "rawtypes"})
10594
protected @NotNull Collection<WritableConfigEntry<?>> getCastedValues() {
106-
return (Collection<WritableConfigEntry<?>>) (Collection) getEntries().values();
95+
return (Collection<WritableConfigEntry<?>>) (Collection) getEntryMap().values();
10796
}
10897

10998
public ExceptionHandler getExceptionHandler() {

hocon/src/main/java/io/github/almightysatan/jaskl/hocon/HoconConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void load() throws IllegalStateException, IOException {
7070
public void reload() throws IllegalStateException {
7171
if (this.config == null)
7272
throw new IllegalStateException();
73-
for (ConfigEntry<?> uncastedConfigEntry : this.getValues()) {
73+
for (ConfigEntry<?> uncastedConfigEntry : this.getEntries()) {
7474
WritableConfigEntry<?> configEntry = (WritableConfigEntry<?>) uncastedConfigEntry;
7575
try {
7676
Object value = this.config.getValue(configEntry.getPath()).unwrapped();
@@ -139,7 +139,7 @@ protected void writeIfNecessary(@NotNull Config config, boolean setDescription)
139139
}
140140
}
141141

142-
protected void resolvePathsToStrip(@NotNull String path, @NotNull ConfigObject node, @NotNull Set<String> paths, @NotNull Set<String> toRemove, @NotNull Set<String> valuePathsRemoved) {
142+
protected void resolvePathsToStrip(@NotNull String path, @NotNull ConfigObject node, @NotNull Collection<String> paths, @NotNull Set<String> toRemove, @NotNull Set<String> valuePathsRemoved) {
143143
for (Map.Entry<String, ConfigValue> entry : node.entrySet()) {
144144
String fieldPath = (path.isEmpty() ? "" : path + ".") + entry.getKey();
145145
if (entry.getValue() instanceof ConfigObject) {

jackson/src/main/java/io/github/almightysatan/jaskl/jackson/JacksonConfigImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ protected void putNode(@NotNull String path, @NotNull JsonNode value) {
141141
node.set(pathSplit[pathSplit.length - 1], value);
142142
}
143143

144-
protected boolean stripNodes(@NotNull String path, @NotNull ObjectNode node, @NotNull Set<String> paths, @NotNull Set<String> pathsRemoved) {
144+
protected boolean stripNodes(@NotNull String path, @NotNull ObjectNode node, @NotNull Collection<String> paths, @NotNull Set<String> pathsRemoved) {
145145
boolean changed = false;
146146
List<String> toRemove = new ArrayList<>();
147147
for (Entry<String, JsonNode> field : node.properties()) {

mongodb/src/main/java/io/github/almightysatan/jaskl/mongodb/MongodbConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public void write() throws IOException {
129129

130130
Set<String> pathsRemoved = new HashSet<>();
131131
try {
132-
Set<String> paths = this.getPaths();
132+
Collection<String> paths = this.getPaths();
133133
List<WriteModel<? extends Document>> writeModels = new ArrayList<>();
134134
FindIterable<Document> documents = this.mongoCollection.find();
135135
for (Document document : documents) {

properties/src/main/java/io/github/almightysatan/jaskl/properties/PropertiesConfig.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
import org.jetbrains.annotations.Nullable;
3232
import org.jetbrains.annotations.Unmodifiable;
3333

34-
import java.io.*;
34+
import java.io.File;
35+
import java.io.IOException;
36+
import java.io.Reader;
37+
import java.io.Writer;
3538
import java.util.*;
3639
import java.util.Map.Entry;
3740

@@ -91,7 +94,7 @@ public void write() throws IOException {
9194
return Collections.emptySet();
9295

9396
Properties stripped = new Properties();
94-
Set<String> paths = this.getPaths();
97+
Collection<String> paths = this.getPaths();
9598
Set<String> pathsRemoved = new HashSet<>();
9699
for (Entry<Object, Object> entry : this.config.entrySet()) {
97100
String key = (String) entry.getKey();

yaml/src/main/java/io/github/almightysatan/jaskl/yaml/YamlConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ protected void loadValues(@NotNull String path, @NotNull MappingNode node) {
164164
* @return true if the entry exists
165165
*/
166166
protected boolean loadValueIfEntryExists(String path, Node node) {
167-
WritableConfigEntry<?> entry = (WritableConfigEntry<?>) this.getEntries().get(path);
167+
WritableConfigEntry<?> entry = (WritableConfigEntry<?>) this.getEntryMap().get(path);
168168
if (entry == null)
169169
return false;
170170
Object value = CONSTRUCTOR.constructObject(node);
@@ -223,7 +223,7 @@ protected void setComment(@NotNull Node node, @Nullable String comment) {
223223
node.setBlockComments(Collections.emptyList()); // Remove comment
224224
}
225225

226-
protected boolean stripNodes(@NotNull String path, @NotNull MappingNode node, @NotNull Set<String> paths, @NotNull Set<String> removedPaths) {
226+
protected boolean stripNodes(@NotNull String path, @NotNull MappingNode node, @NotNull Collection<String> paths, @NotNull Set<String> removedPaths) {
227227
boolean changed = false;
228228
List<NodeTuple> toRemove = new ArrayList<>();
229229
for (NodeTuple tuple : node.getValue()) {

0 commit comments

Comments
 (0)