diff options
author | isXander <xandersmith2008@gmail.com> | 2022-11-12 11:05:16 +0000 |
---|---|---|
committer | isXander <xandersmith2008@gmail.com> | 2022-11-12 11:05:16 +0000 |
commit | 558b120e2d4924a84860c3f84415ab573040a293 (patch) | |
tree | d1a6cf8508d2aadf5b1a22f663c3d4fb7867fdd2 /src/main/java/dev/isxander/yacl/config/ConfigInstance.java | |
parent | fe404957974b127363e95aa27c4745afc91760aa (diff) | |
download | YetAnotherConfigLib-558b120e2d4924a84860c3f84415ab573040a293.tar.gz YetAnotherConfigLib-558b120e2d4924a84860c3f84415ab573040a293.tar.bz2 YetAnotherConfigLib-558b120e2d4924a84860c3f84415ab573040a293.zip |
REAL config library
Diffstat (limited to 'src/main/java/dev/isxander/yacl/config/ConfigInstance.java')
-rw-r--r-- | src/main/java/dev/isxander/yacl/config/ConfigInstance.java | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/main/java/dev/isxander/yacl/config/ConfigInstance.java b/src/main/java/dev/isxander/yacl/config/ConfigInstance.java new file mode 100644 index 0000000..e85a645 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/config/ConfigInstance.java @@ -0,0 +1,48 @@ +package dev.isxander.yacl.config; + +import dev.isxander.yacl.api.YetAnotherConfigLib; + +import java.lang.reflect.InvocationTargetException; +import java.util.function.BiFunction; + +public abstract class ConfigInstance<T> { + private final Class<T> configClass; + private final T defaultInstance; + private T instance; + + public ConfigInstance(Class<T> configClass) { + this.configClass = configClass; + + try { + this.defaultInstance = this.instance = configClass.getConstructor().newInstance(); + } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { + throw new IllegalStateException(String.format("Could not create default instance of config for %s. Make sure there is a default constructor!", this.configClass.getSimpleName())); + } + + } + + public T getConfig() { + return this.instance; + } + + protected void setConfig(T instance) { + this.instance = instance; + } + + public T getDefaults() { + return this.defaultInstance; + } + + public Class<T> getConfigClass() { + return this.configClass; + } + + public YetAnotherConfigLib buildConfig(BiFunction<ConfigInstance<T>, YetAnotherConfigLib.Builder, YetAnotherConfigLib.Builder> builder) { + return builder.apply(this, YetAnotherConfigLib.createBuilder()) + .save(this::save) + .build(); + } + + public abstract void save(); + public abstract void load(); +} |