refactor: refactor PersistentDataUtilities (credit: ChatGPT)

This commit is contained in:
Chayapak 2024-11-19 17:59:38 +07:00
parent d3845f7c8a
commit e4970cc56b
Signed by: ChomeNS
SSH key fingerprint: SHA256:0YoxhdyXsgbc0nfeB2N6FYE60mxMU7DS4uCUMaw2mvA

View file

@ -4,7 +4,6 @@ import com.google.gson.Gson;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive; import com.google.gson.JsonPrimitive;
import me.chayapak1.chomens_bot.Main;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
@ -12,77 +11,87 @@ import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardOpenOption; import java.nio.file.StandardOpenOption;
import java.util.concurrent.locks.ReentrantLock;
public class PersistentDataUtilities { public class PersistentDataUtilities {
public static final Path path = Path.of("persistent.json"); private static final Path path = Path.of("persistent.json");
private static BufferedWriter writer; private static final Gson gson = new Gson();
public static JsonObject jsonObject = new JsonObject(); public static JsonObject jsonObject = new JsonObject();
private static boolean stopping = false; private static final ReentrantLock lock = new ReentrantLock();
private static volatile boolean stopping = false;
public static void init () { public static void init () {
lock.lock();
try { try {
if (!Files.exists(path)) Files.createFile(path); if (Files.exists(path)) {
try (BufferedReader reader = Files.newBufferedReader(path)) {
// loads the persistent data from the last session jsonObject = gson.fromJson(reader, JsonObject.class);
else { }
final BufferedReader reader = Files.newBufferedReader(path); } else {
Files.createFile(path);
final Gson gson = new Gson();
jsonObject = gson.fromJson(reader, JsonObject.class);
} }
writer = Files.newBufferedWriter(path, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
lock.unlock();
} }
} }
private static void write (String string) { private static void writeToFile() {
if (stopping) return; // is this necessary? if (stopping) return;
Main.executorService.submit(() -> { lock.lock();
try {
writer.close();
writer = Files.newBufferedWriter(path, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); try (BufferedWriter writer = Files.newBufferedWriter(path, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
writer.write(gson.toJson(jsonObject));
writer.write(string); } catch (IOException e) {
writer.flush(); e.printStackTrace();
} catch (IOException ignored) {} } finally {
}); lock.unlock();
}
} }
public static void stop () { public static void stop () {
stopping = true; stopping = true;
write(jsonObject.toString());
lock.lock();
try {
writeToFile();
} finally {
lock.unlock();
}
} }
public static void put (String property, JsonElement value) { public static void put (String property, JsonElement value) {
jsonObject.add(property, value); lock.lock();
write(jsonObject.toString());
try {
jsonObject.add(property, value);
writeToFile();
} finally {
lock.unlock();
}
} }
public static void put (String property, String value) { public static void put (String property, String value) {
jsonObject.add(property, new JsonPrimitive(value)); put(property, new JsonPrimitive(value));
write(jsonObject.toString());
} }
public static void put (String property, boolean value) { public static void put (String property, boolean value) {
jsonObject.add(property, new JsonPrimitive(value)); put(property, new JsonPrimitive(value));
write(jsonObject.toString());
} }
public static void put (String property, int value) { public static void put (String property, int value) {
jsonObject.add(property, new JsonPrimitive(value)); put(property, new JsonPrimitive(value));
write(jsonObject.toString());
} }
public static void put (String property, char value) { public static void put (String property, char value) {
jsonObject.add(property, new JsonPrimitive(value)); put(property, new JsonPrimitive(value));
write(jsonObject.toString());
} }
} }