Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linted #29

Merged
merged 13 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
27 changes: 16 additions & 11 deletions .github/linters/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@

<!-- Miscellaneous other checks. -->
<!-- See https://checkstyle.org/checks/misc/index.html -->
<module name="RegexpSingleline">
<!--<module name="RegexpSingleline">
<property name="format" value="\s+$"/>
<property name="minimum" value="0"/>
<property name="maximum" value="0"/>
<property name="message" value="Line has trailing spaces."/>
</module>
</module>-->

<!-- Checks for Headers -->
<!-- See https://checkstyle.org/checks/header/index.html -->
Expand All @@ -90,8 +90,9 @@

<module name="LineLength">
<property name="fileExtensions" value="java"/>
<property name="max" value="250"/>
<property name="max" value="500"/>
<property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>
<property name="severity" value="warning"/>
</module>

<module name="TreeWalker">
Expand Down Expand Up @@ -123,7 +124,7 @@
<module name="IllegalImport"/> <!-- defaults to sun.* packages -->
<module name="RedundantImport"/>
<module name="UnusedImports">
<property name="processJavadoc" value="false"/>
<property name="severity" value="warning"/>
</module>

<!-- Checks for Size Violations. -->
Expand Down Expand Up @@ -151,7 +152,7 @@

<!-- Checks for blocks. You know, those {}'s -->
<!-- See https://checkstyle.org/checks/blocks/index.html -->
<module name="AvoidNestedBlocks"/>
<!--<module name="AvoidNestedBlocks"/>-->
<module name="EmptyBlock"/>
<module name="LeftCurly"/>
<module name="NeedBraces"/>
Expand All @@ -160,10 +161,12 @@
<!-- Checks for common coding problems -->
<!-- See https://checkstyle.org/checks/coding/index.html -->
<module name="EmptyStatement"/>
<module name="EqualsHashCode"/>
<module name="HiddenField"/>
<!--<module name="EqualsHashCode"/>-->
<module name="HiddenField">
<property name="severity" value="warning"/>
</module>
<module name="IllegalInstantiation"/>
<module name="InnerAssignment"/>
<!--<module name="InnerAssignment"/>-->
<!--<module name="MagicNumber"/>-->
<module name="MissingSwitchDefault"/>
<module name="MultipleVariableDeclarations"/>
Expand All @@ -173,16 +176,18 @@
<!-- Checks for class design -->
<!-- See https://checkstyle.org/checks/design/index.html -->
<!--<module name="DesignForExtension"/>-->
<module name="FinalClass"/>
<module name="FinalClass">
<property name="severity" value="warning"/>
</module>
<!--<module name="HideUtilityClassConstructor"/>-->
<module name="InterfaceIsType"/>
<!--<module name="VisibilityModifier"/>-->

<!-- Miscellaneous other checks. -->
<!-- See https://checkstyle.org/checks/misc/index.html -->
<module name="ArrayTypeStyle"/>
<module name="FinalParameters"/>
<module name="TodoComment"/>
<!--<module name="FinalParameters"/>-->
<!--<module name="TodoComment"/>-->
<module name="UpperEll"/>

<!-- https://checkstyle.org/filters/suppressionxpathfilter.html -->
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ jobs:
DEFAULT_BRANCH: master
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
IGNORE_GITIGNORED_FILES: true
VALIDATE_JAVA_CHECKSTYLE: true
VALIDATE_GOOGLE_JAVA_FORMAT: false
VALIDATE_JSCPD: false
VALIDATE_MARKDOWN: false
JAVA_FILE_NAME: 'checkstyle.xml'
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ abstract class AbstractConfigMultiValue<E> extends AbstractConfigValue<E[]> {
private final IntFunction<E[]> m_arrayFactory;

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

@Override
protected final E[] parseJsonValue(Object configValue) {
protected final E[] parseJsonValue(final Object configValue) {
JSONArray jsonArray;
try {
jsonArray = (JSONArray)configValue;
jsonArray = (JSONArray) configValue;
} catch (ClassCastException ex) {
final E[] valueArray = m_arrayFactory.apply(1);
valueArray[0] = parseJsonElement(configValue);
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/com/team766/config/AbstractConfigValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ public abstract class AbstractConfigValue<E> implements SettableValueProvider<E>
private E m_cachedValue;
private boolean m_cachedHasValue;
private int m_cachedGeneration = -1;

private static ArrayList<AbstractConfigValue<?>> c_accessedValues = new ArrayList<AbstractConfigValue<?>>();

static Collection<AbstractConfigValue<?>> accessedValues() {
return Collections.unmodifiableCollection(c_accessedValues);
}

static void resetStatics() {
c_accessedValues.clear();
}
protected AbstractConfigValue(String 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();
Expand All @@ -42,7 +42,9 @@ private void sync() {
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));
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;
}
Expand All @@ -53,13 +55,13 @@ private void sync() {
public String getKey() {
return m_key;
}

@Override
public boolean hasValue() {
sync();
return m_cachedHasValue;
}

@Override
public E get() {
sync();
Expand All @@ -69,16 +71,16 @@ public E get() {
return m_cachedValue;
}

public void set(E value) {
public void set(final E value) {
ConfigFileReader.instance.setValue(m_key, value);
}

public void clear() {
ConfigFileReader.instance.setValue(m_key, null);
}

protected abstract E parseJsonValue(Object configValue);

@Override
public String toString() {
sync();
Expand Down
95 changes: 47 additions & 48 deletions src/main/java/com/team766/config/ConfigFileReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,31 @@
/**
* Class for loading in config from the Config file.
* Constants that need to be tuned / changed
*
*
* Data is read from a file in JSON format
*
*
* @author Brett Levenson
*/
public class ConfigFileReader {
// this.getClass().getClassLoader().getResource(fileName).getPath()

public static ConfigFileReader instance;

private static final String KEY_DELIMITER = ".";

// This is incremented each time the config file is reloaded to ensure that ConfigValues use the most recent setting.
private int m_generation = 0;

private String m_fileName;
private JSONObject m_values = new JSONObject();

public static ConfigFileReader getInstance() {
return instance;
}
public ConfigFileReader(String fileName) {

public ConfigFileReader(final String fileName) {
m_fileName = fileName;

try {
reloadFromFile();
} catch (Exception e) {
Expand All @@ -50,14 +50,14 @@ public ConfigFileReader(String fileName) {
LoggerExceptionUtils.logException(new IOException("Failed to load config file!", e));
}
}

public void reloadFromFile() throws IOException {
System.out.println("Loading config file: " + m_fileName);
String jsonString = Files.readString(Paths.get(m_fileName));
reloadFromJson(jsonString);
}

public void reloadFromJson(String jsonString) {
public void reloadFromJson(final String jsonString) {
JSONObject newValues;
try (StringReader reader = new StringReader(jsonString)) {
newValues = new JSONObject(new JSONTokener(reader));
Expand All @@ -70,70 +70,69 @@ public void reloadFromJson(String jsonString) {
try {
param.parseJsonValue(rawValue);
} catch (Exception ex) {
throw new ConfigValueParseException("Could not parse config value for " + param.getKey(), ex);
throw new ConfigValueParseException(
"Could not parse config value for " + param.getKey(), ex);
}
}
m_values = newValues;
++m_generation;
}

public int getGeneration() {
return m_generation;
}
public boolean containsKey(String key) {

public boolean containsKey(final String key) {
return getRawValue(key) != null;
}
public SettableValueProvider<Integer[]> getInts(String key) {

public SettableValueProvider<Integer[]> getInts(final String key) {
return new IntegerConfigMultiValue(key);
}
public SettableValueProvider<Integer> getInt(String key) {

public SettableValueProvider<Integer> getInt(final String key) {
return new IntegerConfigValue(key);
}
public SettableValueProvider<Double[]> getDoubles(String key) {

public SettableValueProvider<Double[]> getDoubles(final String key) {
return new DoubleConfigMultiValue(key);
}
public SettableValueProvider<Double> getDouble(String key) {

public SettableValueProvider<Double> getDouble(final String key) {
return new DoubleConfigValue(key);
}
public SettableValueProvider<Boolean> getBoolean(String key) {

public SettableValueProvider<Boolean> getBoolean(final String key) {
return new BooleanConfigValue(key);
}
public SettableValueProvider<String> getString(String key) {

public SettableValueProvider<String> getString(final String key) {
return new StringConfigValue(key);
}

public <E extends Enum<E>> SettableValueProvider<E> getEnum(Class<E> enumClass, String key) {
public <E extends Enum<E>> SettableValueProvider<E> getEnum(final Class<E> enumClass,
final String key) {
return new EnumConfigValue<E>(enumClass, key);
}

public <E> void setValue(String key, E value) {
public <E> void setValue(final String key, final E value) {
String[] keyParts = splitKey(key);
JSONObject parentObj = getParent(m_values, keyParts);
parentObj.putOpt(
keyParts[keyParts.length - 1],
value == null ? JSONObject.NULL : value);
parentObj.putOpt(keyParts[keyParts.length - 1], value == null ? JSONObject.NULL : value);
}

Object getRawValue(String key) {
Object getRawValue(final String key) {
return getRawValue(m_values, key);
}

private static Object getRawValue(JSONObject obj, String key) {
private static Object getRawValue(final JSONObject obj, final String key) {
String[] keyParts = splitKey(key);
JSONObject parentObj = getParent(obj, keyParts);
var rawValue = parentObj.opt(keyParts[keyParts.length - 1]);
if (rawValue instanceof JSONObject) {
throw new IllegalArgumentException(
"The config file cannot store both a single config " +
"setting and a group of config settings with the name " +
key + " Please pick a different name");
throw new IllegalArgumentException("The config file cannot store both a single config "
+ "setting and a group of config settings with the name " + key
+ " Please pick a different name");
}
if (rawValue == null) {
parentObj.put(keyParts[keyParts.length - 1], JSONObject.NULL);
Expand All @@ -144,21 +143,21 @@ private static Object getRawValue(JSONObject obj, String key) {
return rawValue;
}

private static String[] splitKey(String key) {
private static String[] splitKey(final String key) {
return key.split(Pattern.quote(KEY_DELIMITER));
}

private static JSONObject getParent(JSONObject obj, String[] keyParts) {
private static JSONObject getParent(JSONObject obj, final String[] keyParts) {
for (int i = 0; i < keyParts.length - 1; ++i) {
JSONObject subObj;
try {
subObj = (JSONObject)obj.opt(keyParts[i]);
subObj = (JSONObject) obj.opt(keyParts[i]);
} catch (ClassCastException ex) {
throw new IllegalArgumentException(
"The config file cannot store both a single config " +
"setting and a group of config settings with the name " +
String.join(KEY_DELIMITER, keyParts) +
" Please pick a different name for one of them.");
"The config file cannot store both a single config "
+ "setting and a group of config settings with the name "
+ String.join(KEY_DELIMITER, keyParts)
+ " Please pick a different name for one of them.");
}
if (subObj == null) {
subObj = new JSONObject();
Expand All @@ -172,9 +171,9 @@ private static JSONObject getParent(JSONObject obj, String[] keyParts) {
public String getJsonString() {
return m_values.toString(2);
}
public void saveFile(String jsonString) throws IOException {
try(FileWriter writer = new FileWriter(m_fileName)) {

public void saveFile(final String jsonString) throws IOException {
try (FileWriter writer = new FileWriter(m_fileName)) {
writer.write(jsonString);
}
Logger.get(Category.CONFIGURATION).logRaw(Severity.INFO, "Config file written to " + m_fileName);
Expand Down
Loading
Loading