diff options
author | isXander <xander@isxander.dev> | 2024-10-19 19:22:45 +0100 |
---|---|---|
committer | isXander <xander@isxander.dev> | 2024-10-19 19:22:45 +0100 |
commit | e73a08e6672fb380cab8db71340158969c5ef56b (patch) | |
tree | dd08a311f4eff9a91b465ef1854caa1286fc6f9a /src/main/java/dev/isxander/yacl3/impl/InstantStateManager.java | |
parent | 519ac2fc0e23587defcf4a8259979961d35d0ce2 (diff) | |
download | YetAnotherConfigLib-e73a08e6672fb380cab8db71340158969c5ef56b.tar.gz YetAnotherConfigLib-e73a08e6672fb380cab8db71340158969c5ef56b.tar.bz2 YetAnotherConfigLib-e73a08e6672fb380cab8db71340158969c5ef56b.zip |
3.6.0
Diffstat (limited to 'src/main/java/dev/isxander/yacl3/impl/InstantStateManager.java')
-rw-r--r-- | src/main/java/dev/isxander/yacl3/impl/InstantStateManager.java | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/main/java/dev/isxander/yacl3/impl/InstantStateManager.java b/src/main/java/dev/isxander/yacl3/impl/InstantStateManager.java new file mode 100644 index 0000000..8806617 --- /dev/null +++ b/src/main/java/dev/isxander/yacl3/impl/InstantStateManager.java @@ -0,0 +1,68 @@ +package dev.isxander.yacl3.impl; + +import dev.isxander.yacl3.api.Binding; +import dev.isxander.yacl3.api.StateManager; + +public class InstantStateManager<T> implements StateManager<T>, ProvidesBindingForDeprecation<T> { + private final Binding<T> binding; + private StateListener<T> stateListener; + + public InstantStateManager(Binding<T> binding) { + this.binding = binding; + this.stateListener = StateListener.noop(); + } + + @Override + public void set(T value) { + boolean changed = !this.get().equals(value); + + this.binding.setValue(value); + + if (changed) stateListener.onStateChange(this.get(), value); + } + + @Override + public T get() { + return this.binding.getValue(); + } + + @Override + public void apply() { + // no-op, state is always applied + } + + @Override + public void resetToDefault(ResetAction action) { + this.set(binding.defaultValue()); + } + + @Override + public void sync() { + // no-op, state is always synced + } + + @Override + public boolean isSynced() { + return true; + } + + @Override + public boolean isAlwaysSynced() { + return true; + } + + @Override + public boolean isDefault() { + return binding.defaultValue().equals(this.get()); + } + + @Override + public void addListener(StateListener<T> stateListener) { + this.stateListener = this.stateListener.andThen(stateListener); + } + + @Override + public Binding<T> getBinding() { + return binding; + } +} |