This repository has been 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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ValueProvider observers and PID fixes (#53)
- Loading branch information
Showing
27 changed files
with
847 additions
and
415 deletions.
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 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 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.