This repository was archived by the owner on Jan 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
ValueProvider observers and PID fixes #53
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
153 changes: 78 additions & 75 deletions
153
src/main/java/com/team766/config/AbstractConfigValue.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,95 +1,98 @@ | ||
| package com.team766.config; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import com.team766.library.AbstractObservable; | ||
| import com.team766.library.SettableValueProvider; | ||
| import com.team766.logging.Category; | ||
| import com.team766.logging.Logger; | ||
| import com.team766.logging.LoggerExceptionUtils; | ||
| import com.team766.logging.Severity; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.Optional; | ||
|
|
||
| public abstract class AbstractConfigValue<E> implements SettableValueProvider<E> { | ||
| protected String m_key; | ||
| private E m_cachedValue; | ||
| private boolean m_cachedHasValue; | ||
| private int m_cachedGeneration = -1; | ||
| public abstract class AbstractConfigValue<E> extends AbstractObservable<Optional<E>> | ||
| implements SettableValueProvider<E> { | ||
| protected String m_key; | ||
| private E m_cachedValue; | ||
| private boolean m_cachedHasValue; | ||
|
|
||
| private static ArrayList<AbstractConfigValue<?>> c_accessedValues = new ArrayList<AbstractConfigValue<?>>(); | ||
| private static ArrayList<AbstractConfigValue<?>> c_accessedValues = | ||
| new ArrayList<AbstractConfigValue<?>>(); | ||
|
|
||
| static Collection<AbstractConfigValue<?>> accessedValues() { | ||
| return Collections.unmodifiableCollection(c_accessedValues); | ||
| } | ||
| static Collection<AbstractConfigValue<?>> accessedValues() { | ||
| return Collections.unmodifiableCollection(c_accessedValues); | ||
| } | ||
|
|
||
| static void resetStatics() { | ||
| c_accessedValues.clear(); | ||
| } | ||
| static void resetStatics() { | ||
| c_accessedValues.clear(); | ||
| } | ||
|
|
||
| protected AbstractConfigValue(final String key) { | ||
| m_key = key; | ||
| c_accessedValues.add(this); | ||
| // Querying for this config setting's key will add a placeholder entry | ||
| // in the config file if this setting does not already exist there. | ||
| ConfigFileReader.instance.getRawValue(m_key); | ||
| } | ||
| protected AbstractConfigValue(final String key) { | ||
| m_key = key; | ||
| c_accessedValues.add(this); | ||
| // Querying for this config setting's key will add a placeholder entry | ||
| // in the config file if this setting does not already exist there. | ||
| ConfigFileReader.instance.getRawValue(m_key); | ||
| update(); | ||
| } | ||
|
|
||
| private void sync() { | ||
| if (ConfigFileReader.instance.getGeneration() != m_cachedGeneration) { | ||
| m_cachedGeneration = ConfigFileReader.instance.getGeneration(); | ||
| var rawValue = ConfigFileReader.instance.getRawValue(m_key); | ||
| m_cachedHasValue = rawValue != null; | ||
| if (m_cachedHasValue) { | ||
| try { | ||
| m_cachedValue = parseJsonValue(rawValue); | ||
| } catch (Exception ex) { | ||
| Logger.get(Category.CONFIGURATION).logRaw(Severity.ERROR, | ||
| "Failed to parse " + m_key + " from the config file: " | ||
| + LoggerExceptionUtils.exceptionToString(ex)); | ||
| m_cachedValue = null; | ||
| m_cachedHasValue = false; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| void update() { | ||
| var rawValue = ConfigFileReader.instance.getRawValue(m_key); | ||
| m_cachedHasValue = rawValue != null; | ||
| if (m_cachedHasValue) { | ||
| try { | ||
| m_cachedValue = parseJsonValue(rawValue); | ||
| } catch (Exception ex) { | ||
| Logger.get(Category.CONFIGURATION) | ||
| .logRaw( | ||
| Severity.ERROR, | ||
| "Failed to parse " | ||
| + m_key | ||
| + " from the config file: " | ||
| + LoggerExceptionUtils.exceptionToString(ex)); | ||
| m_cachedValue = null; | ||
| m_cachedHasValue = false; | ||
| } | ||
| } | ||
| notifyObservers(m_cachedHasValue ? Optional.of(m_cachedValue) : Optional.empty()); | ||
| } | ||
|
|
||
| public String getKey() { | ||
| return m_key; | ||
| } | ||
| public String getKey() { | ||
| return m_key; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasValue() { | ||
| sync(); | ||
| return m_cachedHasValue; | ||
| } | ||
| @Override | ||
| public boolean hasValue() { | ||
| return m_cachedHasValue; | ||
| } | ||
|
|
||
| @Override | ||
| public E get() { | ||
| sync(); | ||
| if (!m_cachedHasValue) { | ||
| throw new IllegalArgumentException(m_key + " not found in the config file"); | ||
| } | ||
| return m_cachedValue; | ||
| } | ||
| @Override | ||
| public E get() { | ||
| if (!m_cachedHasValue) { | ||
| throw new IllegalArgumentException(m_key + " not found in the config file"); | ||
| } | ||
| return m_cachedValue; | ||
| } | ||
|
|
||
| public void set(final E value) { | ||
| ConfigFileReader.instance.setValue(m_key, value); | ||
| } | ||
| public void set(final E value) { | ||
| ConfigFileReader.instance.setValue(m_key, value); | ||
| } | ||
|
|
||
| public void clear() { | ||
| ConfigFileReader.instance.setValue(m_key, null); | ||
| } | ||
| public void clear() { | ||
| ConfigFileReader.instance.setValue(m_key, null); | ||
| } | ||
|
|
||
| protected abstract E parseJsonValue(Object configValue); | ||
| protected abstract E parseJsonValue(Object configValue); | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| sync(); | ||
| if (!m_cachedHasValue) { | ||
| return "<unset>"; | ||
| } | ||
| if (m_cachedValue == null) { | ||
| return "<null>"; | ||
| } | ||
| return m_cachedValue.toString(); | ||
| } | ||
| } | ||
| @Override | ||
| public String toString() { | ||
| if (!m_cachedHasValue) { | ||
| return "<unset>"; | ||
| } | ||
| if (m_cachedValue == null) { | ||
| return "<null>"; | ||
| } | ||
| return m_cachedValue.toString(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.