From e52108be5f79fcf434a80768c321a5f62c00bf01 Mon Sep 17 00:00:00 2001 From: zeroflag Date: Sat, 14 Aug 2021 21:56:38 +0200 Subject: [PATCH] persisting mementos --- src/main/java/com/vectron/fcl/FclStack.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/vectron/fcl/FclStack.java b/src/main/java/com/vectron/fcl/FclStack.java index 7103300..49af113 100644 --- a/src/main/java/com/vectron/fcl/FclStack.java +++ b/src/main/java/com/vectron/fcl/FclStack.java @@ -2,6 +2,8 @@ package com.vectron.fcl; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; +import com.google.gson.stream.JsonReader; import com.vectron.fcl.types.Obj; import java.io.BufferedReader; @@ -13,6 +15,8 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import static java.util.Arrays.asList; + public class FclStack { private static final Gson gson; private final LStack stack = new LStack<>(); @@ -81,7 +85,7 @@ public class FclStack { stream = fileStore.open(fileName(id)); Obj[] loaded = gson.fromJson(new BufferedReader(new InputStreamReader(stream)), Obj[].class); stack.clear(); - stack.addAll(Arrays.asList(loaded)); + stack.addAll(asList(loaded)); } catch (IOException e) { throw new RuntimeException(e); } finally { @@ -95,10 +99,19 @@ public class FclStack { } } + public void load(JsonReader jsonReader) { + stack.clear(); + stack.addAll(asList(gson.fromJson(jsonReader, Obj[].class))); + } + public void save(FileStore fileStore, String id) { fileStore.save(gson.toJson(stack.toArray(new Obj[0])).getBytes(), fileName(id)); } + public JsonElement toJsonTree() { + return gson.toJsonTree(stack.toArray(new Obj[0])); + } + private String fileName(String id) { return String.format("stack%s.json", id); }