add some methods into TodoItemManager

This commit is contained in:
hlq07 2018-05-11 17:07:45 +08:00
parent 10facca1da
commit 4e3fd47539
1 changed files with 88 additions and 14 deletions

View File

@ -1,20 +1,25 @@
package org.cutem.cutecalendar.model; package org.cutem.cutecalendar.model;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import org.cutem.cutecalendar.util.CalendarUtil;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.HashMap; import java.util.*;
import java.util.HashSet;
public class TodoItemManager { public class TodoItemManager {
private static final String TODO_LIST_SAVE_NAME = "todo-list.json"; private static final String TODO_LIST_SAVE_NAME = "todo-list.json";
private static TodoItemManager ourInstance; private static TodoItemManager ourInstance;
private long idCount; private long idCount;
private HashSet<TodoItem> items = new HashSet<>(); private HashSet<TodoItem> items = new HashSet<>();
private HashMap<Long, TodoItem> idCache = new HashMap<>(); private HashMap<Long, TodoItem> idCache = new HashMap<>();
private HashMap<String, List<TodoItem>> dateCache = new HashMap<>();
private TodoItemManager() { private TodoItemManager() {
} }
@ -25,6 +30,7 @@ public class TodoItemManager {
return ourInstance; return ourInstance;
} }
public long getNextId() { public long getNextId() {
while (idCache.containsKey(idCount)) { while (idCache.containsKey(idCount)) {
++idCount; ++idCount;
@ -32,29 +38,96 @@ public class TodoItemManager {
return idCount++; return idCount++;
} }
private static boolean isIntersectedDuring(@NotNull TodoItem item, @NotNull Calendar from, @NotNull Calendar to) {
return !from.after(item.getEnd()) && !to.before(item.getBgn());
}
public void add(@NotNull TodoItem item) { public void add(@NotNull TodoItem item) {
if (idCache.containsKey(item.getId())) { if (idCache.containsKey(item.getId())) {
items.remove( remove(item.getId());
idCache.get(item.getId())
);
} }
items.add(item);
idCache.put(item.getId(), item); idCache.put(item.getId(), item);
} items.add(item);
addDateCache(item);
public void remove(long id) {
if (idCache.containsKey(id)) {
items.remove(idCache.get(id));
idCache.remove(id);
}
} }
public void remove(@NotNull TodoItem item) { public void remove(@NotNull TodoItem item) {
remove(item.getId()); remove(item.getId());
} }
public void remove(long id) {
if (idCache.containsKey(id)) {
TodoItem item = idCache.remove(id);
items.remove(item);
removeDateCache(item);
}
}
private void addDateCache(@NotNull TodoItem item) {
Calendar from = CalendarUtil.getWholeDayPeriod(item.getBgn())[0];
Calendar to = CalendarUtil.getWholeDayPeriod(item.getEnd())[1];
while (!from.after(to)) {
String date = CalendarUtil.getDatePartString(from);
if (!dateCache.containsKey(date)) {
dateCache.put(date, new ArrayList<>());
}
List<TodoItem> list = dateCache.get(date);
if (!list.contains(item)) {
list.add(item);
}
from.add(Calendar.DAY_OF_MONTH, 1);
}
}
private void removeDateCache(@NotNull TodoItem item) {
Calendar from = CalendarUtil.getWholeDayPeriod(item.getBgn())[0];
Calendar to = CalendarUtil.getWholeDayPeriod(item.getEnd())[1];
while (!from.after(to)) {
String date = CalendarUtil.getDatePartString(from);
List<TodoItem> list = dateCache.getOrDefault(date, Collections.emptyList());
list.remove(item);
from.add(Calendar.DAY_OF_MONTH, 1);
}
}
public List<TodoItem> queryTodoItemsDuring(@NotNull Calendar from, @NotNull Calendar to) {
ArrayList<TodoItem> result = new ArrayList<>();
Calendar c1 = CalendarUtil.getWholeDayPeriod(from)[0];
Calendar c2 = CalendarUtil.getWholeDayPeriod(to)[1];
while (!c1.after(c2)) {
String date = CalendarUtil.getDatePartString(c1);
List<TodoItem> list = dateCache.getOrDefault(date, Collections.emptyList());
list.forEach(item -> {
if (isIntersectedDuring(item, from, to)
&& !result.contains(item)) {
result.add(item);
}
});
c1.add(Calendar.DAY_OF_MONTH, 1);
}
return result;
}
public boolean haveTodoItemOnSomeday(@NotNull Calendar someday) {
String date = CalendarUtil.getDatePartString(someday);
List<TodoItem> items = dateCache.getOrDefault(date, Collections.emptyList());
return !items.isEmpty();
}
String toJson() { String toJson() {
GsonBuilder gson = new GsonBuilder(); GsonBuilder gson = new GsonBuilder();
@ -62,4 +135,5 @@ public class TodoItemManager {
return gson.create().toJson(items); return gson.create().toJson(items);
} }
} }