diff --git a/core/src/main/java/net/silthus/configmapper/ConfigMap.java b/core/src/main/java/net/silthus/configmapper/ConfigMap.java index a52def2..62d6ae5 100644 --- a/core/src/main/java/net/silthus/configmapper/ConfigMap.java +++ b/core/src/main/java/net/silthus/configmapper/ConfigMap.java @@ -23,13 +23,51 @@ import java.util.Map; import java.util.function.Supplier; +/** + * The ConfigMap holds information about all fields and their type inside your config class. + *

Use the {@link ConfigMap#of(Class)} method to create a new ConfigMap from your class. + *

This will create a new instance of your class and search all fields annotated with @{@link ConfigOption}. + * The fields are then stored as {@link ConfigFieldInformation} and mapped to their keys. + *

Use the {@link #with(KeyValuePair...)} method to load the actual config values into the ConfigMap. + * This is required before you can apply the config to your object. + *

Then apply the config to an intance of your config class with {@link #applyTo(Object)}. + *

{@code
+ * ConfigMap.of(MyConfig.class)
+ *      .with(KeyValuePair.of("key", "value")
+ *      .applyTo(new MyConfig());
+ * }
+ */ public interface ConfigMap { + /** + * Tries to fetch all annotated config fields of the given class. + *

The given class must have a parameterless public constructor. + * Use the {@link #of(Class, Supplier)} method if you class has a different signature. + *

Only fields annotated with @{@link ConfigOption} will be cataloged or sub classes that + * have the {@code @ConfigOption} annotation. + * + * @param configClass the config class that should be analyzed for configured fiels + * @return a ConfigMap of all fields inside the given class + * @throws ConfigurationException if the class cannot be instantiated (e.g. no public constructor) + * or if a mapping failed + */ static ConfigMap of(Class configClass) throws ConfigurationException { return of(ConfigUtil.getConfigFields(configClass)); } + /** + * Creates a new ConfigMap using the given supplier to create the config object. + *

This is the alternative to the {@link #of(Class)} method that does not require a + * public parameterless constructor. + * + * @param configClass the config class that should be analyzed for configured fiels + * @param supplier the supplier used to create a fresh instance of the config class + * @param the type of the config + * @return a ConfigMap of all fields inside the given class + * @throws ConfigurationException if the config class contains invalid field mappings + * @see #of(Class) + */ static ConfigMap of(Class configClass, Supplier supplier) throws ConfigurationException { return of(ConfigUtil.getConfigFields(configClass, supplier.get())); diff --git a/core/src/main/java/net/silthus/configmapper/ConfigUtil.java b/core/src/main/java/net/silthus/configmapper/ConfigUtil.java index 65effa2..4037b90 100644 --- a/core/src/main/java/net/silthus/configmapper/ConfigUtil.java +++ b/core/src/main/java/net/silthus/configmapper/ConfigUtil.java @@ -34,7 +34,7 @@ import java.util.Set; import java.util.stream.Collectors; -@Log(topic = "net/silthus/configmapper") +@Log(topic = "configmapper") public final class ConfigUtil { public static Map getConfigFields(Class configClass, FieldNameFormatter formatter) throws ConfigurationException {