refactoring

This commit is contained in:
00asdf 2023-03-29 17:38:50 +02:00
parent 3353b4deb7
commit 57cd27162d
4 changed files with 45 additions and 47 deletions

View File

@ -5,17 +5,10 @@
</component>
<component name="ChangeListManager">
<list default="true" id="d8897652-66f5-4f30-9dbf-a6b280944ebf" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Confidibus.iml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/dev/asdf00/confidibus/Confidibus.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/dev/asdf00/confidibus/ConfigurationException.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/dev/asdf00/confidibus/annotations/Config.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/dev/asdf00/confidibus/annotations/Section.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/dev/asdf00/confidibus/annotations/Value.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/dev/asdf00/confidibus/Confidibus.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/dev/asdf00/confidibus/Configurator.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/dev/asdf00/confidibus/annotations/Config.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/dev/asdf00/confidibus/annotations/Configuration.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/dev/asdf00/confidibus/annotations/Value.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/dev/asdf00/confidibus/annotations/Value.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -39,15 +32,16 @@
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"project.structure.last.edited": "Artifacts",
"project.structure.proportion": "0.0",
"project.structure.side.proportion": "0.2"
<component name="PropertiesComponent">{
&quot;keyToString&quot;: {
&quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;last_opened_file_path&quot;: &quot;C:/Users/chris/Documents/HomeAuto/Confindibus&quot;,
&quot;project.structure.last.edited&quot;: &quot;Artifacts&quot;,
&quot;project.structure.proportion&quot;: &quot;0.0&quot;,
&quot;project.structure.side.proportion&quot;: &quot;0.2&quot;
}
}]]></component>
}</component>
<component name="RecentsManager">
<key name="MoveClassesOrPackagesDialog.RECENTS_KEY">
<recent name="dev.asdf00.confidibus.config" />
@ -74,20 +68,4 @@
</task>
<servers />
</component>
<component name="XDebuggerManager">
<breakpoint-manager>
<breakpoints>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/dev/asdf00/confidibus/Confidibus.java</url>
<line>180</line>
<option name="timeStamp" value="3" />
</line-breakpoint>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/dev/asdf00/confidibus/Confidibus.java</url>
<line>252</line>
<option name="timeStamp" value="4" />
</line-breakpoint>
</breakpoints>
</breakpoint-manager>
</component>
</project>

View File

@ -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()) {

View File

@ -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
*/

View File

@ -15,5 +15,5 @@ public @interface Value {
*/
String name() default "";
String _default();
String standard();
}