@@ -57,19 +57,15 @@ public static StructuraBuilder builder() {
5757
5858 /**
5959 * Builder class for creating a {@link StructuraProcessor} with custom configurations.
60- * This allows you to enable or disable validation during parsing.
60+ *
61+ * <p>Default settings:</p>
62+ * <ul>
63+ * <li>Validation is enabled during parsing</li>
64+ * </ul>
6165 */
6266 public static class StructuraBuilder {
6367 private boolean validateOnParse = true ;
6468
65- /**
66- * Constructs a new {@link StructuraBuilder} with default settings.
67- * By default, validation is enabled during parsing.
68- */
69- public StructuraBuilder () {
70- // Default constructor
71- }
72-
7369 /**
7470 * Sets whether to validate the configuration during parsing.
7571 *
@@ -101,6 +97,44 @@ public static void with(StructuraProcessor processor) {
10197 PROCESSOR = processor ;
10298 }
10399
100+ /**
101+ * Reads content from various sources (Path, File, or resource).
102+ *
103+ * @param source the source to read from (Path, File, or String for resources)
104+ * @return the content as a String
105+ * @throws IOException if reading fails
106+ */
107+ private static String readContent (Object source ) throws IOException {
108+ return switch (source ) {
109+ case Path path -> Files .readString (path );
110+ case File file -> Files .readString (file .toPath ());
111+ case String resourcePath -> {
112+ try (var stream = Structura .class .getResourceAsStream (resourcePath )) {
113+ if (stream == null ) {
114+ throw new StructuraException ("Resource not found: " + resourcePath );
115+ }
116+ yield new String (stream .readAllBytes ());
117+ }
118+ }
119+ default -> throw new IllegalArgumentException ("Unsupported source type: " + source .getClass ().getName ());
120+ };
121+ }
122+
123+ /**
124+ * Gets a user-friendly name for a source object.
125+ *
126+ * @param source the source object
127+ * @return a descriptive name for error messages
128+ */
129+ private static String getSourceName (Object source ) {
130+ return switch (source ) {
131+ case Path p -> p .toAbsolutePath ().toString ();
132+ case File f -> f .getAbsolutePath ();
133+ case String s -> s ;
134+ default -> "unknown" ;
135+ };
136+ }
137+
104138 /**
105139 * Parses a YAML content string into an enum of type E.
106140 *
@@ -122,13 +156,7 @@ public static <E extends Enum<E> & Loadable> void parseEnum(String yamlContent,
122156 * @throws StructuraException if there is an error during file reading or parsing
123157 */
124158 public static <E extends Enum <E > & Loadable > void loadEnum (Path filePath , Class <E > enumClass ) {
125-
126- try {
127- String content = Files .readString (filePath );
128- parseEnum (content , enumClass );
129- } catch (IOException e ) {
130- throw new StructuraException ("Unable to read file: " + filePath .toAbsolutePath (), e );
131- }
159+ loadEnumInternal (filePath , enumClass );
132160 }
133161
134162 /**
@@ -140,12 +168,7 @@ public static <E extends Enum<E> & Loadable> void loadEnum(Path filePath, Class<
140168 * @throws StructuraException if there is an error during file reading or parsing
141169 */
142170 public static <E extends Enum <E > & Loadable > void loadEnum (File file , Class <E > enumClass ) {
143- try {
144- String content = Files .readString (file .toPath ());
145- parseEnum (content , enumClass );
146- } catch (IOException e ) {
147- throw new StructuraException ("Unable to read file " + file .getAbsolutePath (), e );
148- }
171+ loadEnumInternal (file , enumClass );
149172 }
150173
151174 /**
@@ -157,14 +180,23 @@ public static <E extends Enum<E> & Loadable> void loadEnum(File file, Class<E> e
157180 * @throws StructuraException if there is an error during resource reading or parsing
158181 */
159182 public static <E extends Enum <E > & Loadable > void loadEnumFromResource (String resourcePath , Class <E > enumClass ) {
160- try (var stream = Structura .class .getResourceAsStream (resourcePath )) {
161- if (stream == null ) {
162- throw new StructuraException ("Resource not found: " + resourcePath );
163- }
164- String content = new String (stream .readAllBytes ());
183+ loadEnumInternal (resourcePath , enumClass );
184+ }
185+
186+ /**
187+ * Internal implementation for loading enums from various sources.
188+ *
189+ * @param source the source to load from (Path, File, or String for resources)
190+ * @param enumClass the class of the enum to load
191+ * @param <E> the type of the enum, which must implement {@link Loadable}
192+ * @throws StructuraException if there is an error during reading or parsing
193+ */
194+ private static <E extends Enum <E > & Loadable > void loadEnumInternal (Object source , Class <E > enumClass ) {
195+ try {
196+ String content = readContent (source );
165197 parseEnum (content , enumClass );
166198 } catch (IOException e ) {
167- throw new StructuraException ("Unable to read ressource " + resourcePath , e );
199+ throw new StructuraException ("Unable to read file: " + getSourceName ( source ) , e );
168200 }
169201 }
170202
@@ -191,12 +223,7 @@ public static <T extends Loadable> T parse(String yamlContent, Class<T> configCl
191223 * @throws StructuraException if there is an error during file reading or parsing
192224 */
193225 public static <T extends Loadable > T load (Path filePath , Class <T > configClass ) {
194- try {
195- String content = Files .readString (filePath );
196- return parse (content , configClass );
197- } catch (IOException e ) {
198- throw new StructuraException ("Unable to read file: " + filePath .toAbsolutePath (), e );
199- }
226+ return loadInternal (filePath , configClass );
200227 }
201228
202229 /**
@@ -209,12 +236,7 @@ public static <T extends Loadable> T load(Path filePath, Class<T> configClass) {
209236 * @throws StructuraException if there is an error during file reading or parsing
210237 */
211238 public static <T extends Loadable > T load (File file , Class <T > configClass ) {
212- try {
213- String content = Files .readString (file .toPath ());
214- return parse (content , configClass );
215- } catch (IOException e ) {
216- throw new StructuraException ("Unable to read file: " + file .getAbsolutePath (), e );
217- }
239+ return loadInternal (file , configClass );
218240 }
219241
220242 /**
@@ -227,14 +249,24 @@ public static <T extends Loadable> T load(File file, Class<T> configClass) {
227249 * @throws StructuraException if there is an error during resource reading or parsing
228250 */
229251 public static <T extends Loadable > T loadFromResource (String resourcePath , Class <T > configClass ) {
230- try (var stream = Structura .class .getResourceAsStream (resourcePath )) {
231- if (stream == null ) {
232- throw new StructuraException ("Resource not found: " + resourcePath );
233- }
234- String content = new String (stream .readAllBytes ());
252+ return loadInternal (resourcePath , configClass );
253+ }
254+
255+ /**
256+ * Internal implementation for loading configurations from various sources.
257+ *
258+ * @param source the source to load from (Path, File, or String for resources)
259+ * @param configClass the class of the configuration to load
260+ * @param <T> the type of the configuration, which must implement {@link Loadable}
261+ * @return an instance of T populated with the parsed data
262+ * @throws StructuraException if there is an error during reading or parsing
263+ */
264+ private static <T extends Loadable > T loadInternal (Object source , Class <T > configClass ) {
265+ try {
266+ String content = readContent (source );
235267 return parse (content , configClass );
236268 } catch (IOException e ) {
237- throw new StructuraException ("Unable to read ressource " + resourcePath , e );
269+ throw new StructuraException ("Unable to read file: " + getSourceName ( source ) , e );
238270 }
239271 }
240272}
0 commit comments