Skip to content

Commit 809462c

Browse files
committed
#372 Extract path methods from PropertyReader, rename "key" to "path"
1 parent 128f987 commit 809462c

4 files changed

Lines changed: 94 additions & 49 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package ch.jalu.configme.resource;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.util.Set;
6+
7+
/**
8+
* Exposes the paths available in a configuration resource.
9+
* <p>
10+
* This interface is an optional extension for {@link PropertyReader} implementations that support path enumeration.
11+
*/
12+
public interface PathProvider {
13+
14+
/**
15+
* Returns all paths available in the resource, including intermediate paths.
16+
* <p>
17+
* For a configuration like:
18+
* <pre>
19+
* header:
20+
* title: Hello
21+
* font:
22+
* color: red
23+
* size: 12
24+
* </pre>
25+
* this method returns {@code ["header", "header.title", "header.font", "header.font.color", "header.font.size"]}.
26+
*
27+
* @return all paths (in encounter order)
28+
*/
29+
@NotNull Set<String> getPaths();
30+
31+
/**
32+
* Returns all leaf paths in the resource.
33+
* <p>
34+
* For the configuration example in {@link #getPaths()}, this method returns
35+
* {@code ["header.title", "header.font.color", "header.font.size"]}.
36+
*
37+
* @return all leaf paths (in encounter order)
38+
*/
39+
@NotNull Set<String> getLeafPaths();
40+
41+
/**
42+
* Returns the direct child paths of the given path. Returns an empty set
43+
* if the path does not exist or has no children.
44+
* <p>
45+
* For the configuration example in {@link #getPaths()},
46+
* {@code getChildPaths("header")} returns {@code ["header.title", "header.font"]}.
47+
* <p>
48+
* If {@code path} is empty, the top-level paths are returned.
49+
*
50+
* @param path the path whose direct child paths should be looked up
51+
* @return all direct child paths (in encounter order, never null)
52+
*/
53+
@NotNull Set<String> getChildPaths(@NotNull String path);
54+
55+
}

src/main/java/ch/jalu/configme/resource/PropertyReader.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import org.jetbrains.annotations.Nullable;
1010

1111
import java.util.List;
12-
import java.util.Set;
1312

1413
/**
1514
* A property reader provides values from a resource (e.g. a YAML file) based on whose data the values of properties
@@ -37,24 +36,6 @@ public interface PropertyReader {
3736
*/
3837
@Nullable Object getValue(@NotNull String path);
3938

40-
/**
41-
* Returns the keys available in the file. Depending on the parameter either all keys are returned,
42-
* or only the keys of the leaf nodes are considered.
43-
*
44-
* @param onlyLeafNodes true if only the paths of leaf nodes should be returned (no intermediate paths)
45-
* @return set of all existing keys (ordered)
46-
*/
47-
@NotNull Set<String> getKeys(boolean onlyLeafNodes);
48-
49-
/**
50-
* Returns the direct children of the given path which are available in the file. Returns an empty set
51-
* if the path does not exist in the file (never null).
52-
*
53-
* @param path the path whose direct child paths should be looked up
54-
* @return set of all direct children (ordered, never null)
55-
*/
56-
@NotNull Set<String> getChildKeys(@NotNull String path);
57-
5839
/**
5940
* Returns the object at the given path, or null if absent.
6041
*

src/main/java/ch/jalu/configme/resource/YamlFileReader.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
/**
2424
* YAML file reader.
2525
*/
26-
public class YamlFileReader implements PropertyReader {
26+
public class YamlFileReader implements PropertyReader, PathProvider {
2727

2828
private final Path path;
2929
private final Charset charset;
@@ -74,17 +74,17 @@ public boolean contains(@NotNull String path) {
7474
}
7575

7676
@Override
77-
public @NotNull Set<String> getKeys(boolean onlyLeafNodes) {
78-
if (root == null) {
79-
return Collections.emptySet();
80-
}
81-
Set<String> allKeys = new LinkedHashSet<>();
82-
collectKeysIntoSet("", root, allKeys, onlyLeafNodes);
83-
return allKeys;
77+
public @NotNull Set<String> getPaths() {
78+
return collectPaths(false);
79+
}
80+
81+
@Override
82+
public @NotNull Set<String> getLeafPaths() {
83+
return collectPaths(true);
8484
}
8585

8686
@Override
87-
public @NotNull Set<String> getChildKeys(@NotNull String path) {
87+
public @NotNull Set<String> getChildPaths(@NotNull String path) {
8888
Object object = getValue(path);
8989
if (object instanceof Map) {
9090
String pathPrefix = path.isEmpty() ? "" : path + ".";
@@ -95,6 +95,15 @@ public boolean contains(@NotNull String path) {
9595
return Collections.emptySet();
9696
}
9797

98+
private @NotNull Set<String> collectPaths(boolean onlyLeafNodes) {
99+
if (root == null) {
100+
return Collections.emptySet();
101+
}
102+
Set<String> allKeys = new LinkedHashSet<>();
103+
collectKeysIntoSet("", root, allKeys, onlyLeafNodes);
104+
return allKeys;
105+
}
106+
98107
/**
99108
* Recursively collects keys from maps into the given set.
100109
*
@@ -103,8 +112,8 @@ public boolean contains(@NotNull String path) {
103112
* @param result set to save keys to
104113
* @param onlyLeafNodes whether only leaf nodes should be added to the result set
105114
*/
106-
private void collectKeysIntoSet(@NotNull String path, @NotNull Map<String, Object> map, @NotNull Set<String> result,
107-
boolean onlyLeafNodes) {
115+
private static void collectKeysIntoSet(@NotNull String path, @NotNull Map<String, Object> map,
116+
@NotNull Set<String> result, boolean onlyLeafNodes) {
108117
for (Map.Entry<String, Object> entry : map.entrySet()) {
109118
String childPath = PathUtils.concat(path, entry.getKey());
110119
if (!onlyLeafNodes || isLeafValue(entry.getValue())) {

src/test/java/ch/jalu/configme/resource/YamlFileReaderTest.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ void shouldHandleEmptyFile() {
204204
// then
205205
assertThat(result, sameInstance(configFile));
206206
assertThat(reader.getRoot(), nullValue());
207-
assertThat(reader.getKeys(true), empty());
208-
assertThat(reader.getChildKeys(""), empty());
207+
assertThat(reader.getLeafPaths(), empty());
208+
assertThat(reader.getChildPaths(""), empty());
209209
}
210210

211211
@Test
@@ -232,16 +232,16 @@ void shouldReadWithCustomCharset() {
232232
}
233233

234234
@Test
235-
void shouldReturnKeysOfFile() {
235+
void shouldReturnPathsOfFile() {
236236
// given
237237
Path file = copyFileFromResources(COMPLETE_FILE);
238238
YamlFileReader reader = new YamlFileReader(file);
239239

240240
// when
241-
Set<String> keys = reader.getKeys(false);
241+
Set<String> paths = reader.getPaths();
242242

243243
// then
244-
assertThat(keys, contains("test", "test.duration", "test.systemName",
244+
assertThat(paths, contains("test", "test.duration", "test.systemName",
245245
"sample", "sample.ratio", "sample.ratio.order", "sample.ratio.fields",
246246
"version",
247247
"features", "features.boring", "features.boring.skip", "features.boring.colors", "features.boring.dustLevel",
@@ -250,16 +250,16 @@ void shouldReturnKeysOfFile() {
250250
}
251251

252252
@Test
253-
void shouldReturnLeafNodeKeysInFile() {
253+
void shouldReturnLeafPathsInFile() {
254254
// given
255255
Path file = copyFileFromResources(COMPLETE_FILE);
256256
YamlFileReader reader = new YamlFileReader(file);
257257

258258
// when
259-
Set<String> keys = reader.getKeys(true);
259+
Set<String> paths = reader.getLeafPaths();
260260

261261
// then
262-
assertThat(keys, contains("test.duration", "test.systemName",
262+
assertThat(paths, contains("test.duration", "test.systemName",
263263
"sample.ratio.order", "sample.ratio.fields",
264264
"version",
265265
"features.boring.skip", "features.boring.colors", "features.boring.dustLevel",
@@ -274,11 +274,11 @@ void shouldTreatEmptyMapsAsLeafNodes() {
274274
YamlFileReader reader = new YamlFileReader(file);
275275

276276
// when
277-
Set<String> keys = reader.getKeys(true);
277+
Set<String> paths = reader.getLeafPaths();
278278

279279
// then
280-
assertThat(keys, hasSize(24));
281-
assertThat(keys, hasItems(
280+
assertThat(paths, hasSize(24));
281+
assertThat(paths, hasItems(
282282
"message-key.conditionalElem.conditionals.low.conditionals", // empty map
283283
"message-key.conditionalElem.conditionals.med.color",
284284
"message-key.conditionalElem.conditionals.high.conditionalElem.bold",
@@ -290,29 +290,29 @@ void shouldTreatEmptyMapsAsLeafNodes() {
290290
}
291291

292292
@Test
293-
void shouldReturnChildrenPathsOfGivenPath() {
293+
void shouldReturnChildPathsOfGivenPath() {
294294
// given
295295
Path file = copyFileFromResources(COMPLETE_FILE);
296296
YamlFileReader reader = new YamlFileReader(file);
297297

298298
// when
299-
Set<String> keys = reader.getChildKeys("features.boring");
299+
Set<String> paths = reader.getChildPaths("features.boring");
300300

301301
// then
302-
assertThat(keys, contains("features.boring.skip", "features.boring.colors", "features.boring.dustLevel"));
302+
assertThat(paths, contains("features.boring.skip", "features.boring.colors", "features.boring.dustLevel"));
303303
}
304304

305305
@Test
306-
void shouldReturnChildrenPathsOfRoot() {
306+
void shouldReturnChildPathsOfRoot() {
307307
// given
308308
Path file = copyFileFromResources(COMPLETE_FILE);
309309
YamlFileReader reader = new YamlFileReader(file);
310310

311311
// when
312-
Set<String> keys = reader.getChildKeys("");
312+
Set<String> paths = reader.getChildPaths("");
313313

314314
// then
315-
assertThat(keys, contains("test", "sample", "version", "features", "security"));
315+
assertThat(paths, contains("test", "sample", "version", "features", "security"));
316316
}
317317

318318
@Test
@@ -322,8 +322,8 @@ void shouldReturnEmptySetForNonExistentOrLeafValue() {
322322
YamlFileReader reader = new YamlFileReader(file);
323323

324324
// when
325-
Set<String> bogusChildren = reader.getChildKeys("bogus");
326-
Set<String> leafChildren = reader.getChildKeys("features.boring.colors");
325+
Set<String> bogusChildren = reader.getChildPaths("bogus");
326+
Set<String> leafChildren = reader.getChildPaths("features.boring.colors");
327327

328328
// then
329329
assertThat(bogusChildren, empty());

0 commit comments

Comments
 (0)