From 0ad4f0cfc3f2984e87b9a28630f25b60f591752d Mon Sep 17 00:00:00 2001 From: HttpMarco Date: Sun, 25 Feb 2024 23:35:20 +0100 Subject: [PATCH] Implement better file api --- .../dev/httpmarco/osgon/files/Document.java | 40 +++++++++++ .../files/{OsganFiles.java => Files.java} | 7 +- .../ConfigExclude.java | 2 +- .../files/configuration/gson/JsonUtils.java | 66 ------------------- .../gson => }/haste/HasteAPI.java | 4 +- .../JsonByteExclusionStrategy.java} | 6 +- .../osgon/files/json/JsonDocument.java | 20 ++++++ .../JsonObjectSerializer.java} | 21 +++--- .../httpmarco/osgon/files/json/JsonUtils.java | 54 +++++++++++++++ 9 files changed, 133 insertions(+), 87 deletions(-) create mode 100644 osgan-files/src/main/java/dev/httpmarco/osgon/files/Document.java rename osgan-files/src/main/java/dev/httpmarco/osgon/files/{OsganFiles.java => Files.java} (87%) rename osgan-files/src/main/java/dev/httpmarco/osgon/files/{configuration => annotations}/ConfigExclude.java (76%) delete mode 100644 osgan-files/src/main/java/dev/httpmarco/osgon/files/configuration/gson/JsonUtils.java rename osgan-files/src/main/java/dev/httpmarco/osgon/files/{configuration/gson => }/haste/HasteAPI.java (94%) rename osgan-files/src/main/java/dev/httpmarco/osgon/files/{configuration/gson/exclusion/ByteExclusionStrategy.java => json/JsonByteExclusionStrategy.java} (65%) create mode 100644 osgan-files/src/main/java/dev/httpmarco/osgon/files/json/JsonDocument.java rename osgan-files/src/main/java/dev/httpmarco/osgon/files/{configuration/gson/JsonDocument.java => json/JsonObjectSerializer.java} (69%) create mode 100644 osgan-files/src/main/java/dev/httpmarco/osgon/files/json/JsonUtils.java diff --git a/osgan-files/src/main/java/dev/httpmarco/osgon/files/Document.java b/osgan-files/src/main/java/dev/httpmarco/osgon/files/Document.java new file mode 100644 index 0000000..6799162 --- /dev/null +++ b/osgan-files/src/main/java/dev/httpmarco/osgon/files/Document.java @@ -0,0 +1,40 @@ +package dev.httpmarco.osgon.files; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.SneakyThrows; +import lombok.experimental.Accessors; + +import java.nio.file.Files; +import java.nio.file.Path; + +@Getter +@Accessors(fluent = true) +public abstract class Document { + + private final Path path; + private T value; + + public Document(T defaultValue, Path path) { + this.value = defaultValue; + this.path = path; + + this.updateDocument(); + } + + public void value(T value) { + this.value = value; + } + + public void updateWithValue(T value) { + this.value = value; + this.updateDocument(); + } + + public abstract void updateDocument(); + + @SneakyThrows + public void delete() { + Files.delete(this.path); + } +} diff --git a/osgan-files/src/main/java/dev/httpmarco/osgon/files/OsganFiles.java b/osgan-files/src/main/java/dev/httpmarco/osgon/files/Files.java similarity index 87% rename from osgan-files/src/main/java/dev/httpmarco/osgon/files/OsganFiles.java rename to osgan-files/src/main/java/dev/httpmarco/osgon/files/Files.java index 41b2eef..3646e44 100644 --- a/osgan-files/src/main/java/dev/httpmarco/osgon/files/OsganFiles.java +++ b/osgan-files/src/main/java/dev/httpmarco/osgon/files/Files.java @@ -6,10 +6,9 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.nio.file.Files; import java.nio.file.Path; -public class OsganFiles { +public class Files { public static void copyInputStreamToFile(InputStream inputStream, File file) throws IOException { try (var outputStream = new FileOutputStream(file, false)) { @@ -23,7 +22,7 @@ public static void copyInputStreamToFile(InputStream inputStream, File file) thr @SneakyThrows public static void writeString(Path path, String content) { - Files.writeString(path, content); + java.nio.file.Files.writeString(path, content); } public static void writeString(String path, String content) { @@ -32,7 +31,7 @@ public static void writeString(String path, String content) { @SneakyThrows public static String readString(Path path) { - return Files.readString(path); + return java.nio.file.Files.readString(path); } public static String readString(String path) { diff --git a/osgan-files/src/main/java/dev/httpmarco/osgon/files/configuration/ConfigExclude.java b/osgan-files/src/main/java/dev/httpmarco/osgon/files/annotations/ConfigExclude.java similarity index 76% rename from osgan-files/src/main/java/dev/httpmarco/osgon/files/configuration/ConfigExclude.java rename to osgan-files/src/main/java/dev/httpmarco/osgon/files/annotations/ConfigExclude.java index e3ee383..fe0ad37 100644 --- a/osgan-files/src/main/java/dev/httpmarco/osgon/files/configuration/ConfigExclude.java +++ b/osgan-files/src/main/java/dev/httpmarco/osgon/files/annotations/ConfigExclude.java @@ -1,4 +1,4 @@ -package dev.httpmarco.osgon.files.configuration; +package dev.httpmarco.osgon.files.annotations; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; diff --git a/osgan-files/src/main/java/dev/httpmarco/osgon/files/configuration/gson/JsonUtils.java b/osgan-files/src/main/java/dev/httpmarco/osgon/files/configuration/gson/JsonUtils.java deleted file mode 100644 index cd6c30e..0000000 --- a/osgan-files/src/main/java/dev/httpmarco/osgon/files/configuration/gson/JsonUtils.java +++ /dev/null @@ -1,66 +0,0 @@ -package dev.httpmarco.osgon.files.configuration.gson; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import dev.httpmarco.osgon.files.configuration.gson.exclusion.ByteExclusionStrategy; - -import java.io.FileReader; -import java.io.FileWriter; - -public class JsonUtils { - - private static final Gson GSON = new GsonBuilder() - .disableHtmlEscaping() - .setExclusionStrategies(new ByteExclusionStrategy()) - .create(); - private static final Gson PRETTY_GSON = new GsonBuilder() - .setPrettyPrinting() - .disableHtmlEscaping() - .setExclusionStrategies(new ByteExclusionStrategy()) - .create(); - - - public static Gson getGson() { - return GSON; - } - - public static Gson getPrettyGson() { - return PRETTY_GSON; - } - - public static String toJson(Object object) { - return GSON.toJson(object); - } - - public static String toPrettyJson(Object object) { - return PRETTY_GSON.toJson(object); - } - - public static void writeJson(Object object, FileWriter fileWriter) { - GSON.toJson(object, fileWriter); - } - - public static void writePrettyJson(Object object, FileWriter fileWriter) { - PRETTY_GSON.toJson(object, fileWriter); - } - - - public static T fromJson(String string, Class tClass) { - return GSON.fromJson(string, tClass); - } - - public static T fromPrettyJson(String string, Class tClass) { - return PRETTY_GSON.fromJson(string, tClass); - } - - public static T fromJson(FileReader fileReader, Class tClass) { - return GSON.fromJson(fileReader, tClass); - } - - public static T fromPrettyJson(FileReader fileReader, Class tClass) { - return PRETTY_GSON.fromJson(fileReader, tClass); - } - - -} - diff --git a/osgan-files/src/main/java/dev/httpmarco/osgon/files/configuration/gson/haste/HasteAPI.java b/osgan-files/src/main/java/dev/httpmarco/osgon/files/haste/HasteAPI.java similarity index 94% rename from osgan-files/src/main/java/dev/httpmarco/osgon/files/configuration/gson/haste/HasteAPI.java rename to osgan-files/src/main/java/dev/httpmarco/osgon/files/haste/HasteAPI.java index d845892..00704b1 100644 --- a/osgan-files/src/main/java/dev/httpmarco/osgon/files/configuration/gson/haste/HasteAPI.java +++ b/osgan-files/src/main/java/dev/httpmarco/osgon/files/haste/HasteAPI.java @@ -1,7 +1,7 @@ -package dev.httpmarco.osgon.files.configuration.gson.haste; +package dev.httpmarco.osgon.files.haste; import com.google.gson.JsonObject; -import dev.httpmarco.osgon.files.configuration.gson.JsonUtils; +import dev.httpmarco.osgon.files.json.JsonUtils; import lombok.SneakyThrows; import javax.net.ssl.HttpsURLConnection; diff --git a/osgan-files/src/main/java/dev/httpmarco/osgon/files/configuration/gson/exclusion/ByteExclusionStrategy.java b/osgan-files/src/main/java/dev/httpmarco/osgon/files/json/JsonByteExclusionStrategy.java similarity index 65% rename from osgan-files/src/main/java/dev/httpmarco/osgon/files/configuration/gson/exclusion/ByteExclusionStrategy.java rename to osgan-files/src/main/java/dev/httpmarco/osgon/files/json/JsonByteExclusionStrategy.java index dbc9c6a..4730338 100644 --- a/osgan-files/src/main/java/dev/httpmarco/osgon/files/configuration/gson/exclusion/ByteExclusionStrategy.java +++ b/osgan-files/src/main/java/dev/httpmarco/osgon/files/json/JsonByteExclusionStrategy.java @@ -1,10 +1,10 @@ -package dev.httpmarco.osgon.files.configuration.gson.exclusion; +package dev.httpmarco.osgon.files.json; import com.google.gson.ExclusionStrategy; import com.google.gson.FieldAttributes; -import dev.httpmarco.osgon.files.configuration.ConfigExclude; +import dev.httpmarco.osgon.files.annotations.ConfigExclude; -public class ByteExclusionStrategy implements ExclusionStrategy { +public final class JsonByteExclusionStrategy implements ExclusionStrategy { @Override public boolean shouldSkipField(FieldAttributes f) { return f.getAnnotation(ConfigExclude.class) != null; diff --git a/osgan-files/src/main/java/dev/httpmarco/osgon/files/json/JsonDocument.java b/osgan-files/src/main/java/dev/httpmarco/osgon/files/json/JsonDocument.java new file mode 100644 index 0000000..8dbebe4 --- /dev/null +++ b/osgan-files/src/main/java/dev/httpmarco/osgon/files/json/JsonDocument.java @@ -0,0 +1,20 @@ +package dev.httpmarco.osgon.files.json; + +import dev.httpmarco.osgon.files.Document; +import lombok.SneakyThrows; + +import java.nio.file.Files; +import java.nio.file.Path; + +public class JsonDocument extends Document { + + public JsonDocument(T defaultValue, Path path) { + super(defaultValue, path); + } + + @SneakyThrows + @Override + public void updateDocument() { + Files.writeString(path(), JsonUtils.toPrettyJson(value())); + } +} diff --git a/osgan-files/src/main/java/dev/httpmarco/osgon/files/configuration/gson/JsonDocument.java b/osgan-files/src/main/java/dev/httpmarco/osgon/files/json/JsonObjectSerializer.java similarity index 69% rename from osgan-files/src/main/java/dev/httpmarco/osgon/files/configuration/gson/JsonDocument.java rename to osgan-files/src/main/java/dev/httpmarco/osgon/files/json/JsonObjectSerializer.java index 72bcfb2..26fd4f4 100644 --- a/osgan-files/src/main/java/dev/httpmarco/osgon/files/configuration/gson/JsonDocument.java +++ b/osgan-files/src/main/java/dev/httpmarco/osgon/files/json/JsonObjectSerializer.java @@ -1,7 +1,6 @@ -package dev.httpmarco.osgon.files.configuration.gson; +package dev.httpmarco.osgon.files.json; import com.google.gson.JsonObject; -import dev.httpmarco.osgon.files.configuration.gson.JsonUtils; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; @@ -9,15 +8,15 @@ @Getter @AllArgsConstructor @NoArgsConstructor -public class JsonDocument { +public class JsonObjectSerializer { private JsonObject jsonObject = new JsonObject(); - public JsonDocument(String gsonObject) { + public JsonObjectSerializer(String gsonObject) { this.jsonObject = JsonUtils.getGson().fromJson(gsonObject, JsonObject.class); } - public JsonDocument append(String key, String value) { + public JsonObjectSerializer append(String key, String value) { jsonObject.addProperty(key, value); return this; } @@ -26,7 +25,7 @@ public String readString(String key) { return jsonObject.get(key).getAsString(); } - public JsonDocument append(String key, int value) { + public JsonObjectSerializer append(String key, int value) { jsonObject.addProperty(key, value); return this; } @@ -35,7 +34,7 @@ public int readInt(String key) { return jsonObject.get(key).getAsInt(); } - public JsonDocument append(String key, double value) { + public JsonObjectSerializer append(String key, double value) { jsonObject.addProperty(key, value); return this; } @@ -44,7 +43,7 @@ public double readDouble(String key) { return jsonObject.get(key).getAsDouble(); } - public JsonDocument append(String key, long value) { + public JsonObjectSerializer append(String key, long value) { jsonObject.addProperty(key, value); return this; } @@ -53,7 +52,7 @@ public long readLong(String key) { return jsonObject.get(key).getAsLong(); } - public JsonDocument append(String key, boolean value) { + public JsonObjectSerializer append(String key, boolean value) { jsonObject.addProperty(key, value); return this; } @@ -62,13 +61,13 @@ public boolean readBoolean(String key) { return jsonObject.get(key).getAsBoolean(); } - public JsonDocument append(String key, Object value) { + public JsonObjectSerializer append(String key, Object value) { append(key, JsonUtils.toJson(value)); return this; } public T readObject(String key, Class clazz) { - return JsonUtils.fromPrettyJson(readString(key), clazz); + return JsonUtils.fromJson(readString(key), clazz); } @Override diff --git a/osgan-files/src/main/java/dev/httpmarco/osgon/files/json/JsonUtils.java b/osgan-files/src/main/java/dev/httpmarco/osgon/files/json/JsonUtils.java new file mode 100644 index 0000000..1f62fd4 --- /dev/null +++ b/osgan-files/src/main/java/dev/httpmarco/osgon/files/json/JsonUtils.java @@ -0,0 +1,54 @@ +package dev.httpmarco.osgon.files.json; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import java.io.FileReader; +import java.io.FileWriter; + +public class JsonUtils { + + private static final Gson JSON = new GsonBuilder() + .disableHtmlEscaping() + .setExclusionStrategies(new JsonByteExclusionStrategy()) + .create(); + private static final Gson PRETTY_JSON = new GsonBuilder() + .setPrettyPrinting() + .disableHtmlEscaping() + .setExclusionStrategies(new JsonByteExclusionStrategy()) + .create(); + + + public static Gson getGson() { + return JSON; + } + + public static Gson getPrettyGson() { + return PRETTY_JSON; + } + + public static String toJson(Object object) { + return JSON.toJson(object); + } + + public static String toPrettyJson(Object object) { + return PRETTY_JSON.toJson(object); + } + + public static void writeJson(Object object, FileWriter fileWriter) { + JSON.toJson(object, fileWriter); + } + + public static void writePrettyJson(Object object, FileWriter fileWriter) { + PRETTY_JSON.toJson(object, fileWriter); + } + + public static T fromJson(String string, Class tClass) { + return JSON.fromJson(string, tClass); + } + + public static T fromJson(FileReader fileReader, Class tClass) { + return JSON.fromJson(fileReader, tClass); + } +} +