mirror of
https://github.com/zeroflag/fcl.git
synced 2025-01-11 20:01:10 +01:00
persisting mementos
This commit is contained in:
parent
1b94ee5305
commit
e52108be5f
1 changed files with 14 additions and 1 deletions
|
@ -2,6 +2,8 @@ package com.vectron.fcl;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
import com.google.gson.stream.JsonReader;
|
||||||
import com.vectron.fcl.types.Obj;
|
import com.vectron.fcl.types.Obj;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
@ -13,6 +15,8 @@ import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static java.util.Arrays.asList;
|
||||||
|
|
||||||
public class FclStack {
|
public class FclStack {
|
||||||
private static final Gson gson;
|
private static final Gson gson;
|
||||||
private final LStack<Obj> stack = new LStack<>();
|
private final LStack<Obj> stack = new LStack<>();
|
||||||
|
@ -81,7 +85,7 @@ public class FclStack {
|
||||||
stream = fileStore.open(fileName(id));
|
stream = fileStore.open(fileName(id));
|
||||||
Obj[] loaded = gson.fromJson(new BufferedReader(new InputStreamReader(stream)), Obj[].class);
|
Obj[] loaded = gson.fromJson(new BufferedReader(new InputStreamReader(stream)), Obj[].class);
|
||||||
stack.clear();
|
stack.clear();
|
||||||
stack.addAll(Arrays.asList(loaded));
|
stack.addAll(asList(loaded));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
} finally {
|
} 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) {
|
public void save(FileStore fileStore, String id) {
|
||||||
fileStore.save(gson.toJson(stack.toArray(new Obj[0])).getBytes(), fileName(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) {
|
private String fileName(String id) {
|
||||||
return String.format("stack%s.json", id);
|
return String.format("stack%s.json", id);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue