Skip to content

Commit

Permalink
Actual Added Spotless
Browse files Browse the repository at this point in the history
  • Loading branch information
avidraccoon committed Sep 9, 2024
1 parent 47c35c0 commit fa14b9a
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 73 deletions.
37 changes: 19 additions & 18 deletions parameter_tools/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -67,4 +68,4 @@ java {
}

// Automatically format code on build.
compileJava.dependsOn 'spotlessApply'
compileJava.dependsOn 'spotlessApply'
Original file line number Diff line number Diff line change
@@ -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<T> {

Expand All @@ -14,47 +14,60 @@ public class JSONSync<T> {

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<T>) 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;
Expand All @@ -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);
}
}
Expand Down
36 changes: 23 additions & 13 deletions parameter_tools/src/test/java/ExampleJsonSyncClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

import coppercore.parameter_tools.JSONSync;


public class ExampleJsonSyncClass {
public static JSONSync<ExampleJsonSyncClass> synced = new JSONSync<ExampleJsonSyncClass>(
new ExampleJsonSyncClass(),
"filePath",
new JSONSync.JSONSyncConfigBuilder().build()
);

public static JSONSync<ExampleJsonSyncClass> synced =
new JSONSync<ExampleJsonSyncClass>(
new ExampleJsonSyncClass(),
"filePath",
new JSONSync.JSONSyncConfigBuilder().build());

public final String testText = "";
public final Double testDouble = 0.0;
Expand All @@ -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;
}
}
}
28 changes: 11 additions & 17 deletions parameter_tools/src/test/java/JSONSyncTests.java
Original file line number Diff line number Diff line change
@@ -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<ExampleJsonSyncClass>(
new ExampleJsonSyncClass(),
"filePath",
new JSONSync.JSONSyncConfigBuilder().build()
);
public void TestPrep() {
ExampleJsonSyncClass.synced =
new JSONSync<ExampleJsonSyncClass>(
new ExampleJsonSyncClass(),
"filePath",
new JSONSync.JSONSyncConfigBuilder().build());
}

//@Test
// @Test
public void JsonSyncLoadDataTest() {
ExampleJsonSyncClass.synced.loadData();
ExampleJsonSyncClass instance = ExampleJsonSyncClass.synced.getObject();
Expand All @@ -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();
Expand All @@ -48,5 +43,4 @@ public void JsonSyncSetFileTest() {
Assertions.assertEquals(16.4, instance.motorData.maxVoltage);
Assertions.assertEquals(0.0, instance.motorData.currentVoltage);
}

}

0 comments on commit fa14b9a

Please sign in to comment.