refactoring
This commit is contained in:
+30
-10
@@ -1,6 +1,6 @@
|
||||
package dev.asdf00.confidibus;
|
||||
|
||||
import dev.asdf00.confidibus.annotations.Config;
|
||||
import dev.asdf00.confidibus.annotations.Configuration;
|
||||
import dev.asdf00.confidibus.annotations.Section;
|
||||
import dev.asdf00.confidibus.annotations.Value;
|
||||
|
||||
@@ -18,19 +18,39 @@ import java.util.Iterator;
|
||||
/**
|
||||
* Helper class to initialize and read a configuration file. Call {@code init()} to parse the configuration file.
|
||||
* <br/>
|
||||
* The {@code configClass} needs to be annotated with @Config and @Section. It may contain inner classes annotated
|
||||
* The {@code configClass} needs to be annotated with @Configuration and @Section. It may contain inner classes annotated
|
||||
* with @Section. Every value needs to be public, static and annotated with @Value.
|
||||
* <br/>
|
||||
* This config only allows primitive types and strings.
|
||||
* <br/>
|
||||
* <br/>
|
||||
* Example config class:
|
||||
* <pre>{@code
|
||||
* @Configuration(path = "example.conf")
|
||||
* @Section(title = "Example Configuration", comment = "This is an example")
|
||||
* public class Config {
|
||||
* @Value(standard = "1")
|
||||
* public static int SIMPLE_VAL;
|
||||
*
|
||||
* @Value(name = "VAR_NAME_IN_FILE", standard = "someDefaultValue")
|
||||
* public static String INTERNAL_VAR_NAME;
|
||||
*
|
||||
* @Section(title = "subsection A")
|
||||
* public static class Subsection1 {
|
||||
* @Value(comment = "enables debug mode", standard = "false")
|
||||
* public static boolean DEBUG;
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
public class Confidibus {
|
||||
public class Configurator {
|
||||
private final Class<?> _class;
|
||||
private final PrintStream _debug;
|
||||
private StringBuilder sb = new StringBuilder();
|
||||
private int indent = 0;
|
||||
|
||||
|
||||
private Confidibus(Class<?> configClass, PrintStream debugMsgStream) {
|
||||
private Configurator(Class<?> configClass, PrintStream debugMsgStream) {
|
||||
_class = configClass;
|
||||
_debug = debugMsgStream;
|
||||
}
|
||||
@@ -42,8 +62,8 @@ public class Confidibus {
|
||||
* @param debugMsgStream {@link PrintStream} where debug output shall be written to.
|
||||
* Can be {@code null} if no debug output should be written
|
||||
*/
|
||||
public static Confidibus of(Class<?> configClass, PrintStream debugMsgStream) {
|
||||
return new Confidibus(configClass, debugMsgStream);
|
||||
public static Configurator of(Class<?> configClass, PrintStream debugMsgStream) {
|
||||
return new Configurator(configClass, debugMsgStream);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,7 +71,7 @@ public class Confidibus {
|
||||
* @param createIfMissing permission to create new config file if none is found
|
||||
*/
|
||||
public void init(boolean createIfMissing) {
|
||||
Config cAnn = _class.getAnnotation(Config.class);
|
||||
Configuration cAnn = _class.getAnnotation(Configuration.class);
|
||||
if (cAnn == null) {
|
||||
throw new ConfigurationException("missing @Config annotation for %s!", _class.getSimpleName());
|
||||
}
|
||||
@@ -160,9 +180,9 @@ public class Confidibus {
|
||||
}
|
||||
String type = f.getType().equals(String.class) ? "String" : f.getType().getTypeName();
|
||||
sb.append(" ".repeat(indent)).append('[').append(type).append("] ").append(name).append(": ")
|
||||
.append(v._default()).append('\n');
|
||||
setConfigValue(f, v._default());
|
||||
printDebug("initialized %s(%s) with %s!", f.getName(), name, v._default());
|
||||
.append(v.standard()).append('\n');
|
||||
setConfigValue(f, v.standard());
|
||||
printDebug("initialized %s(%s) with %s!", f.getName(), name, v.standard());
|
||||
}
|
||||
}
|
||||
for (Class<?> subSection : section.getDeclaredClasses()) {
|
||||
+1
-1
@@ -7,7 +7,7 @@ import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Config {
|
||||
public @interface Configuration {
|
||||
/**
|
||||
* path of the configuration file
|
||||
*/
|
||||
@@ -15,5 +15,5 @@ public @interface Value {
|
||||
*/
|
||||
String name() default "";
|
||||
|
||||
String _default();
|
||||
String standard();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user