From fa14b9a77e7fc41eca8f6b8f0a6757fe55967184 Mon Sep 17 00:00:00 2001 From: nathan Date: Mon, 9 Sep 2024 19:01:38 -0400 Subject: [PATCH] Actual Added Spotless --- parameter_tools/build.gradle | 37 ++++----- .../coppercore/parameter_tools/JSONSync.java | 75 ++++++++++++------- .../src/test/java/ExampleJsonSyncClass.java | 36 +++++---- .../src/test/java/JSONSyncTests.java | 28 +++---- 4 files changed, 103 insertions(+), 73 deletions(-) diff --git a/parameter_tools/build.gradle b/parameter_tools/build.gradle index 6f5be79..f99af0c 100644 --- a/parameter_tools/build.gradle +++ b/parameter_tools/build.gradle @@ -6,40 +6,41 @@ */ plugins { - // Apply the java-library plugin for API and implementation separation. - id 'java-library' + // Apply the java-library plugin for API and implementation separation. + id 'java-library' + id "com.diffplug.spotless" version "6.24.0" } repositories { - // Use Maven Central for resolving dependencies. - mavenCentral() + // Use Maven Central for resolving dependencies. + mavenCentral() } dependencies { - // Use JUnit Jupiter for testing. - testImplementation libs.junit.jupiter + // Use JUnit Jupiter for testing. + testImplementation libs.junit.jupiter - testRuntimeOnly 'org.junit.platform:junit-platform-launcher' + testRuntimeOnly 'org.junit.platform:junit-platform-launcher' - // This dependency is exported to consumers, that is to say found on their compile classpath. - api libs.commons.math3 + // This dependency is exported to consumers, that is to say found on their compile classpath. + api libs.commons.math3 - // This dependency is used internally, and not exposed to consumers on their own compile classpath. - implementation libs.guava - implementation 'com.google.code.gson:gson:2.11.0' + // This dependency is used internally, and not exposed to consumers on their own compile classpath. + implementation libs.guava + implementation 'com.google.code.gson:gson:2.11.0' } // Apply a specific Java toolchain to ease working on different environments. java { - toolchain { - languageVersion = JavaLanguageVersion.of(17) - } + toolchain { + languageVersion = JavaLanguageVersion.of(17) + } } tasks.named('test') { - // Use JUnit Platform for unit tests. - useJUnitPlatform() + // Use JUnit Platform for unit tests. + useJUnitPlatform() } spotless { @@ -67,4 +68,4 @@ java { } // Automatically format code on build. -compileJava.dependsOn 'spotlessApply' \ No newline at end of file +compileJava.dependsOn 'spotlessApply' diff --git a/parameter_tools/src/main/java/coppercore/parameter_tools/JSONSync.java b/parameter_tools/src/main/java/coppercore/parameter_tools/JSONSync.java index 74ecb9d..b028e32 100644 --- a/parameter_tools/src/main/java/coppercore/parameter_tools/JSONSync.java +++ b/parameter_tools/src/main/java/coppercore/parameter_tools/JSONSync.java @@ -1,8 +1,8 @@ package coppercore.parameter_tools; import com.google.gson.*; -import java.io.FileReader; import java.io.FileNotFoundException; +import java.io.FileReader; public class JSONSync { @@ -14,47 +14,60 @@ public class JSONSync { private final JSONSyncConfig config; - public T getObject(){ + public T getObject() { return instance; } - private FileReader getFileReader(String path){ + private FileReader getFileReader(String path) { try { return new FileReader(path); - }catch (FileNotFoundException e){ - throw new RuntimeException("File not found "+path, e); + } catch (FileNotFoundException e) { + throw new RuntimeException("File not found " + path, e); } } - + @SuppressWarnings("unchecked") - public void loadData(){ + public void loadData() { instance = gson.fromJson(getFileReader(file), (Class) instance.getClass()); } - public void setFile(String newFilePath){ + public void setFile(String newFilePath) { file = newFilePath; } - private Gson generateGson(){ + private Gson generateGson() { return new GsonBuilder().serializeNulls().create(); } - public JSONSync(T instance, String file, JSONSyncConfig config){ + public JSONSync(T instance, String file, JSONSyncConfig config) { this.instance = instance; this.gson = generateGson(); - this.config = (config == null)? new JSONSyncConfigBuilder().build() : config; + this.config = (config == null) ? new JSONSyncConfigBuilder().build() : config; this.file = file; } - private static record JSONSyncConfig(boolean serializeNulls, boolean prettyPrinting, boolean excludeFieldsWithoutExposeAnnotation, FieldNamingPolicy namingPolicy, - ToNumberPolicy numberToNumberPolicy, ToNumberPolicy objectToNumberPolicy, LongSerializationPolicy longSerializationPolicy, boolean autoReload) { - public JSONSyncConfig(JSONSyncConfigBuilder builder){ - this(builder.serializeNulls, builder.prettyPrinting, builder.excludeFieldsWithoutExposeAnnotation, builder.namingPolicy, builder.numberToNumberPolicy, - builder.objectToNumberPolicy, builder.longSerializationPolicy, builder.autoReload); + private static record JSONSyncConfig( + boolean serializeNulls, + boolean prettyPrinting, + boolean excludeFieldsWithoutExposeAnnotation, + FieldNamingPolicy namingPolicy, + ToNumberPolicy numberToNumberPolicy, + ToNumberPolicy objectToNumberPolicy, + LongSerializationPolicy longSerializationPolicy, + boolean autoReload) { + public JSONSyncConfig(JSONSyncConfigBuilder builder) { + this( + builder.serializeNulls, + builder.prettyPrinting, + builder.excludeFieldsWithoutExposeAnnotation, + builder.namingPolicy, + builder.numberToNumberPolicy, + builder.objectToNumberPolicy, + builder.longSerializationPolicy, + builder.autoReload); } } - public static class JSONSyncConfigBuilder { public boolean serializeNulls = false; public boolean prettyPrinting = false; @@ -66,49 +79,61 @@ public static class JSONSyncConfigBuilder { public boolean keepOldValuesWhenNotPresent = false; public boolean autoReload = false; - //public JSONSyncConfigBuilder newInstance(){ + // public JSONSyncConfigBuilder newInstance(){ // return new JSONSyncConfigBuilder(); - //} - //public JSONSyncConfigBuilder(){ + // } + // public JSONSyncConfigBuilder(){ // - //} + // } public JSONSyncConfigBuilder setSerializeNulls(boolean serializeNulls) { this.serializeNulls = serializeNulls; return this; } + public JSONSyncConfigBuilder setPrettyPrinting(boolean prettyPrinting) { this.prettyPrinting = prettyPrinting; return this; } - public JSONSyncConfigBuilder setExcludeFieldsWithoutExposeAnnotation(boolean excludeFieldsWithoutExposeAnnotation) { + + public JSONSyncConfigBuilder setExcludeFieldsWithoutExposeAnnotation( + boolean excludeFieldsWithoutExposeAnnotation) { this.excludeFieldsWithoutExposeAnnotation = excludeFieldsWithoutExposeAnnotation; return this; } + public JSONSyncConfigBuilder setNamingPolicy(FieldNamingPolicy namingPolicy) { this.namingPolicy = namingPolicy; return this; } + public JSONSyncConfigBuilder setNumberToNumberPolicy(ToNumberPolicy numberToNumberPolicy) { this.numberToNumberPolicy = numberToNumberPolicy; return this; } + public JSONSyncConfigBuilder setObjectToNumberPolicy(ToNumberPolicy objectToNumberPolicy) { this.objectToNumberPolicy = objectToNumberPolicy; return this; } - public JSONSyncConfigBuilder setLongSerializationPolicy(LongSerializationPolicy longSerializationPolicy) { + + public JSONSyncConfigBuilder setLongSerializationPolicy( + LongSerializationPolicy longSerializationPolicy) { this.longSerializationPolicy = longSerializationPolicy; return this; } - public JSONSyncConfigBuilder setKeepOldValuesWhenNotPresent(boolean keepOldValuesWhenNotPresent) { + + public JSONSyncConfigBuilder setKeepOldValuesWhenNotPresent( + boolean keepOldValuesWhenNotPresent) { this.keepOldValuesWhenNotPresent = keepOldValuesWhenNotPresent; return this; } + public JSONSyncConfigBuilder setAutoReload(boolean autoReload) { this.autoReload = autoReload; return this; } - public JSONSyncConfig build(){ + + public JSONSyncConfig build() { return new JSONSyncConfig(this); } } diff --git a/parameter_tools/src/test/java/ExampleJsonSyncClass.java b/parameter_tools/src/test/java/ExampleJsonSyncClass.java index 9413b11..3fe60a6 100644 --- a/parameter_tools/src/test/java/ExampleJsonSyncClass.java +++ b/parameter_tools/src/test/java/ExampleJsonSyncClass.java @@ -2,14 +2,13 @@ import coppercore.parameter_tools.JSONSync; - public class ExampleJsonSyncClass { - - public static JSONSync synced = new JSONSync( - new ExampleJsonSyncClass(), - "filePath", - new JSONSync.JSONSyncConfigBuilder().build() - ); + + public static JSONSync synced = + new JSONSync( + new ExampleJsonSyncClass(), + "filePath", + new JSONSync.JSONSyncConfigBuilder().build()); public final String testText = ""; public final Double testDouble = 0.0; @@ -21,13 +20,24 @@ public class BasicMotorDataHolder { public final Double minVoltage = 0.0; public Double currentVoltage = 0.0; - public String toString(){ - return "minVoltage: " + minVoltage + "\nmaxVoltage: "+maxVoltage+"\ncurrentVoltage: "+currentVoltage; + public String toString() { + return "minVoltage: " + + minVoltage + + "\nmaxVoltage: " + + maxVoltage + + "\ncurrentVoltage: " + + currentVoltage; } } - public String toString(){ - return "testText: " + testText + "\ntestInt: "+testInt+"\ntestDouble: "+ - testDouble+"\nmotorData: "+motorData; + public String toString() { + return "testText: " + + testText + + "\ntestInt: " + + testInt + + "\ntestDouble: " + + testDouble + + "\nmotorData: " + + motorData; } -} \ No newline at end of file +} diff --git a/parameter_tools/src/test/java/JSONSyncTests.java b/parameter_tools/src/test/java/JSONSyncTests.java index 4200c76..2965e3b 100644 --- a/parameter_tools/src/test/java/JSONSyncTests.java +++ b/parameter_tools/src/test/java/JSONSyncTests.java @@ -1,30 +1,25 @@ package coppercore.paremeter_tools.test; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.DisplayName; +import coppercore.parameter_tools.JSONSync; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; -import coppercore.parameter_tools.JSONSync; -import coppercore.paremeter_tools.test.ExampleJsonSyncClass; - public class JSONSyncTests { - - public void main(String[] args){ + public void main(String[] args) { JsonSyncLoadDataTest(); } @BeforeEach - public void TestPrep(){ - ExampleJsonSyncClass.synced = new JSONSync( - new ExampleJsonSyncClass(), - "filePath", - new JSONSync.JSONSyncConfigBuilder().build() - ); + public void TestPrep() { + ExampleJsonSyncClass.synced = + new JSONSync( + new ExampleJsonSyncClass(), + "filePath", + new JSONSync.JSONSyncConfigBuilder().build()); } - //@Test + // @Test public void JsonSyncLoadDataTest() { ExampleJsonSyncClass.synced.loadData(); ExampleJsonSyncClass instance = ExampleJsonSyncClass.synced.getObject(); @@ -34,9 +29,9 @@ public void JsonSyncLoadDataTest() { Assertions.assertNull(instance.motorData); } - //@Test + // @Test public void JsonSyncSetFileTest() { - + ExampleJsonSyncClass.synced.setFile("filePath"); ExampleJsonSyncClass.synced.loadData(); ExampleJsonSyncClass instance = ExampleJsonSyncClass.synced.getObject(); @@ -48,5 +43,4 @@ public void JsonSyncSetFileTest() { Assertions.assertEquals(16.4, instance.motorData.maxVoltage); Assertions.assertEquals(0.0, instance.motorData.currentVoltage); } - }