add TodoItemStorage class

This commit is contained in:
hlq07 2018-05-12 01:05:41 +08:00
parent a97b0565bc
commit 2052812189
3 changed files with 111 additions and 7 deletions

View File

@ -1,13 +1,11 @@
package org.cutem.cutecalendar.model; package org.cutem.cutecalendar.model;
import com.google.gson.GsonBuilder;
import org.cutem.cutecalendar.util.CalendarUtil; import org.cutem.cutecalendar.util.CalendarUtil;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.*; import java.util.*;
public class TodoItemManager { public class TodoItemManager {
private static final String TODO_LIST_SAVE_NAME = "todo-list.json";
private static TodoItemManager ourInstance; private static TodoItemManager ourInstance;
@ -51,6 +49,10 @@ public class TodoItemManager {
addDateCache(item); addDateCache(item);
} }
public void add(@NotNull Collection<TodoItem> items) {
items.forEach(this::add);
}
public void remove(@NotNull TodoItem item) { public void remove(@NotNull TodoItem item) {
remove(item.getId()); remove(item.getId());
} }
@ -129,11 +131,32 @@ public class TodoItemManager {
String toJson() { String toJson() {
GsonBuilder gson = new GsonBuilder(); return TodoItemsStorage.toJson(items);
gson.registerTypeAdapter(TodoItem.class, new TodoItemSerializer()); }
return gson.create().toJson(items);
boolean save(String filename) {
return TodoItemsStorage.save(filename, items);
}
public boolean save() {
return save(TodoItemsStorage.TODO_LIST_SAVE_NAME);
}
void load(String filename) {
add(TodoItemsStorage.load(filename));
}
public void load() {
load(TodoItemsStorage.TODO_LIST_SAVE_NAME);
} }
public TodoItem get(long id) {
return idCache.getOrDefault(id, null);
}
} }

View File

@ -0,0 +1,63 @@
package org.cutem.cutecalendar.model;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import org.jetbrains.annotations.NotNull;
import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class TodoItemsStorage {
public static final String TODO_LIST_SAVE_NAME = "todo-list.json";
public static String toJson(Collection<TodoItem> items) {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(TodoItem.class, new TodoItemSerializer());
return gsonBuilder.create().toJson(items);
}
public static boolean save(@NotNull String filename, Collection<TodoItem> items) {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(TodoItem.class, new TodoItemSerializer());
Gson gson = gsonBuilder.create();
try (
OutputStream out = new BufferedOutputStream(new FileOutputStream(filename))
) {
OutputStreamWriter writer = new OutputStreamWriter(out);
gson.toJson(items, writer);
writer.flush();
} catch (IOException ex) {
return false;
}
return true;
}
public static List<TodoItem> load(@NotNull String filename) {
ArrayList<TodoItem> items = new ArrayList<>();
try (
InputStream in = new BufferedInputStream(new FileInputStream(filename))
) {
JsonParser parser = new JsonParser();
JsonArray ele = parser.parse(new InputStreamReader(in)).getAsJsonArray();
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(TodoItem.class, new TodoItemDeserializer());
Gson gson = gsonBuilder.create();
ele.forEach(o -> items.add(gson.fromJson(o, TodoItem.class)));
} catch (IOException ex) {
// do nothing
}
return items;
}
}

View File

@ -6,16 +6,19 @@ import org.junit.Test;
import java.util.Calendar; import java.util.Calendar;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotEquals;
public class TodoItemManagerTest { public class TodoItemManagerTest {
@Before @Before
public void setUp() { public void setUp() {
System.out.println("##### TodoItemManagerTest *bgn* #####");
} }
@After @After
public void tearDown() { public void tearDown() {
System.out.println("##### TodoItemManagerTest *end* #####");
} }
@Test @Test
@ -45,17 +48,32 @@ public class TodoItemManagerTest {
assertNotEquals(item1, item2); assertNotEquals(item1, item2);
String oldJson = manager.toJson();
System.out.println( System.out.println(
manager.toJson() oldJson
); );
manager.remove(item1); manager.remove(item1);
System.out.println( System.out.println(
manager.toJson() manager.toJson()
); );
String filename = "build/" + TodoItemsStorage.TODO_LIST_SAVE_NAME;
manager.remove(item1);
manager.remove(item2);
manager.add(item1);
manager.add(item2);
System.out.println(manager.toJson());
System.out.println(manager.save(filename));
manager.remove(item1);
manager.remove(item2);
manager.load(filename);
String newJson = manager.toJson();
System.out.println(
newJson
);
assertEquals(oldJson, newJson);
} }