Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Apply Spotless to everything in main #124

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions src/main/java/com/team766/config/AbstractConfigMultiValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@
import org.json.JSONArray;

abstract class AbstractConfigMultiValue<E> extends AbstractConfigValue<E[]> {
private final IntFunction<E[]> m_arrayFactory;
private final IntFunction<E[]> m_arrayFactory;

@SuppressWarnings("unchecked")
protected AbstractConfigMultiValue(final String key, final Class<E> elementClass) {
super(key);
m_arrayFactory = (int length) -> (E[]) Array.newInstance(elementClass, length);
}
@SuppressWarnings("unchecked")
protected AbstractConfigMultiValue(final String key, final Class<E> elementClass) {
super(key);
m_arrayFactory = (int length) -> (E[]) Array.newInstance(elementClass, length);
}

@Override
protected final E[] parseJsonValue(final Object configValue) {
JSONArray jsonArray;
try {
jsonArray = (JSONArray) configValue;
} catch (ClassCastException ex) {
final E[] valueArray = m_arrayFactory.apply(1);
valueArray[0] = parseJsonElement(configValue);
return valueArray;
}
final int length = jsonArray.length();
final E[] valueArray = m_arrayFactory.apply(length);
for (int i = 0; i < length; ++i) {
valueArray[i] = parseJsonElement(jsonArray.get(i));
}
return valueArray;
}
@Override
protected final E[] parseJsonValue(final Object configValue) {
JSONArray jsonArray;
try {
jsonArray = (JSONArray) configValue;
} catch (ClassCastException ex) {
final E[] valueArray = m_arrayFactory.apply(1);
valueArray[0] = parseJsonElement(configValue);
return valueArray;
}
final int length = jsonArray.length();
final E[] valueArray = m_arrayFactory.apply(length);
for (int i = 0; i < length; ++i) {
valueArray[i] = parseJsonElement(jsonArray.get(i));
}
return valueArray;
}

protected abstract E parseJsonElement(Object configElement);
}
protected abstract E parseJsonElement(Object configElement);
}
153 changes: 79 additions & 74 deletions src/main/java/com/team766/config/AbstractConfigValue.java
Original file line number Diff line number Diff line change
@@ -1,95 +1,100 @@
package com.team766.config;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
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;

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;
protected String m_key;
private E m_cachedValue;
private boolean m_cachedHasValue;
private int m_cachedGeneration = -1;

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);
}

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;
}
}
}
}
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;
}
}
}
}

public String getKey() {
return m_key;
}
public String getKey() {
return m_key;
}

@Override
public boolean hasValue() {
sync();
return m_cachedHasValue;
}
@Override
public boolean hasValue() {
sync();
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() {
sync();
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() {
sync();
if (!m_cachedHasValue) {
return "<unset>";
}
if (m_cachedValue == null) {
return "<null>";
}
return m_cachedValue.toString();
}
}
Loading
Loading