Skip to content

Commit

Permalink
Implement better file api
Browse files Browse the repository at this point in the history
  • Loading branch information
HttpMarco committed Feb 25, 2024
1 parent 63cf269 commit 0ad4f0c
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 87 deletions.
40 changes: 40 additions & 0 deletions osgan-files/src/main/java/dev/httpmarco/osgon/files/Document.java
Original file line number Diff line number Diff line change
@@ -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<T> {

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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T> extends Document<T> {

public JsonDocument(T defaultValue, Path path) {
super(defaultValue, path);
}

@SneakyThrows
@Override
public void updateDocument() {
Files.writeString(path(), JsonUtils.toPrettyJson(value()));
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
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;

@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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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> T readObject(String key, Class<T> clazz) {
return JsonUtils.fromPrettyJson(readString(key), clazz);
return JsonUtils.fromJson(readString(key), clazz);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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> T fromJson(String string, Class<T> tClass) {
return JSON.fromJson(string, tClass);
}

public static <T> T fromJson(FileReader fileReader, Class<T> tClass) {
return JSON.fromJson(fileReader, tClass);
}
}

0 comments on commit 0ad4f0c

Please sign in to comment.