22
33import ch .jalu .configme .exception .ConfigMeException ;
44import ch .jalu .configme .internal .PathUtils ;
5-
65import org .jetbrains .annotations .NotNull ;
76import org .jetbrains .annotations .Nullable ;
87import org .yaml .snakeyaml .Yaml ;
1716import java .nio .file .Path ;
1817import java .util .Collections ;
1918import java .util .LinkedHashSet ;
20- import java .util .List ;
2119import java .util .Map ;
2220import java .util .Set ;
2321import java .util .stream .Collectors ;
2422
2523/**
2624 * YAML file reader.
2725 */
28- public class YamlFileReader implements PropertyReader {
26+ public class YamlFileReader implements PropertyReader , PathProvider {
2927
3028 private final Path path ;
3129 private final Charset charset ;
@@ -54,7 +52,7 @@ public YamlFileReader(@NotNull Path path, @NotNull Charset charset) {
5452 }
5553
5654 @ Override
57- public @ Nullable Object getObject (@ NotNull String path ) {
55+ public @ Nullable Object getValue (@ NotNull String path ) {
5856 if (path .isEmpty ()) {
5957 return root ;
6058 }
@@ -71,54 +69,23 @@ public YamlFileReader(@NotNull Path path, @NotNull Charset charset) {
7169 }
7270
7371 @ Override
74- public @ Nullable String getString (@ NotNull String path ) {
75- return getTypedObject (path , String .class );
76- }
77-
78- @ Override
79- public @ Nullable Integer getInt (@ NotNull String path ) {
80- Number n = getTypedObject (path , Number .class );
81- return (n == null )
82- ? null
83- : n .intValue ();
84- }
85-
86- @ Override
87- public @ Nullable Double getDouble (@ NotNull String path ) {
88- Number n = getTypedObject (path , Number .class );
89- return (n == null )
90- ? null
91- : n .doubleValue ();
92- }
93-
94- @ Override
95- public @ Nullable Boolean getBoolean (@ NotNull String path ) {
96- return getTypedObject (path , Boolean .class );
97- }
98-
99- @ Override
100- public @ Nullable List <?> getList (@ NotNull String path ) {
101- return getTypedObject (path , List .class );
72+ public boolean contains (@ NotNull String path ) {
73+ return getValue (path ) != null ;
10274 }
10375
10476 @ Override
105- public boolean contains ( @ NotNull String path ) {
106- return getObject ( path ) != null ;
77+ public @ NotNull Set < String > getPaths ( ) {
78+ return collectPaths ( false ) ;
10779 }
10880
10981 @ Override
110- public @ NotNull Set <String > getKeys (boolean onlyLeafNodes ) {
111- if (root == null ) {
112- return Collections .emptySet ();
113- }
114- Set <String > allKeys = new LinkedHashSet <>();
115- collectKeysIntoSet ("" , root , allKeys , onlyLeafNodes );
116- return allKeys ;
82+ public @ NotNull Set <String > getLeafPaths () {
83+ return collectPaths (true );
11784 }
11885
11986 @ Override
120- public @ NotNull Set <String > getChildKeys (@ NotNull String path ) {
121- Object object = getObject (path );
87+ public @ NotNull Set <String > getChildPaths (@ NotNull String path ) {
88+ Object object = getValue (path );
12289 if (object instanceof Map ) {
12390 String pathPrefix = path .isEmpty () ? "" : path + "." ;
12491 return ((Map <String , Object >) object ).keySet ().stream ()
@@ -128,24 +95,33 @@ public boolean contains(@NotNull String path) {
12895 return Collections .emptySet ();
12996 }
13097
98+ private @ NotNull Set <String > collectPaths (boolean onlyLeafNodes ) {
99+ if (root == null ) {
100+ return Collections .emptySet ();
101+ }
102+ Set <String > allPaths = new LinkedHashSet <>();
103+ collectPathsIntoSet ("" , root , allPaths , onlyLeafNodes );
104+ return allPaths ;
105+ }
106+
131107 /**
132- * Recursively collects keys from maps into the given set .
108+ * Recursively collects keys from maps and adds them as paths to {@code result} .
133109 *
134- * @param path the path of the given map
110+ * @param path the path to the given map
135111 * @param map the map to process recursively
136- * @param result set to save keys to
112+ * @param result set to save paths to
137113 * @param onlyLeafNodes whether only leaf nodes should be added to the result set
138114 */
139- private void collectKeysIntoSet (@ NotNull String path , @ NotNull Map <String , Object > map , @ NotNull Set < String > result ,
140- boolean onlyLeafNodes ) {
115+ private static void collectPathsIntoSet (@ NotNull String path , @ NotNull Map <String , Object > map ,
116+ @ NotNull Set < String > result , boolean onlyLeafNodes ) {
141117 for (Map .Entry <String , Object > entry : map .entrySet ()) {
142118 String childPath = PathUtils .concat (path , entry .getKey ());
143119 if (!onlyLeafNodes || isLeafValue (entry .getValue ())) {
144120 result .add (childPath );
145121 }
146122
147123 if (entry .getValue () instanceof Map ) {
148- collectKeysIntoSet (childPath , (Map ) entry .getValue (), result , onlyLeafNodes );
124+ collectPathsIntoSet (childPath , (Map ) entry .getValue (), result , onlyLeafNodes );
149125 }
150126 }
151127 }
@@ -183,6 +159,9 @@ private static boolean isLeafValue(@Nullable Object o) {
183159 return new MapNormalizer ().normalizeMap (map );
184160 }
185161
162+ /**
163+ * @return the file this reader read from
164+ */
186165 protected final @ NotNull Path getPath () {
187166 return path ;
188167 }
@@ -196,23 +175,6 @@ private static boolean isLeafValue(@Nullable Object o) {
196175 return root ;
197176 }
198177
199- /**
200- * Gets the object at the given path and safely casts it to the given class's type. Returns null
201- * if no value is available or if it cannot be cast.
202- *
203- * @param path the path to retrieve
204- * @param clazz the class to cast to
205- * @param <T> the class type
206- * @return cast value at the given path, null if not applicable
207- */
208- protected <T > @ Nullable T getTypedObject (@ NotNull String path , @ NotNull Class <T > clazz ) {
209- Object value = getObject (path );
210- if (clazz .isInstance (value )) {
211- return clazz .cast (value );
212- }
213- return null ;
214- }
215-
216178 private static @ Nullable Object getEntryIfIsMap (@ NotNull String key , @ Nullable Object value ) {
217179 if (value instanceof Map <?, ?>) {
218180 return ((Map <?, ?>) value ).get (key );
0 commit comments