refactor TodoItemManager

This commit is contained in:
hlq07 2018-05-26 14:47:59 +08:00
parent 6b13284c4a
commit edffa56488
1 changed files with 11 additions and 65 deletions

View File

@ -16,8 +16,6 @@ public class TodoItemManager {
private HashMap<Long, TodoItem> idCache = new HashMap<>();
private HashMap<String, List<TodoItem>> dateCache = new HashMap<>();
private TodoItemManager() {
}
@ -36,17 +34,12 @@ public class TodoItemManager {
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) {
if (idCache.containsKey(item.getId())) {
remove(item.getId());
}
idCache.put(item.getId(), item);
items.add(item);
addDateCache(item);
}
public void add(@NotNull Collection<TodoItem> items) {
@ -61,72 +54,31 @@ public class TodoItemManager {
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 static boolean isIntersectedDuring(@NotNull TodoItem item, @NotNull Calendar from, @NotNull Calendar to) {
return !from.after(item.getEnd()) && !to.before(item.getBgn());
}
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 TodoItem get(long id) {
return idCache.getOrDefault(id, null);
}
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);
for (TodoItem i : items) {
if (isIntersectedDuring(i, from, to)) {
result.add(i);
}
}
return result;
}
public boolean haveTodoItemOnSomeday(@NotNull Calendar someday) {
String date = CalendarUtil.getDatePartString(someday);
List<TodoItem> items = dateCache.getOrDefault(date, Collections.emptyList());
return !items.isEmpty();
Calendar[] whole = CalendarUtil.getWholeDayPeriod(someday);
List<TodoItem> l = queryTodoItemsDuring(whole[0], whole[1]);
return !l.isEmpty();
}
@ -153,10 +105,4 @@ public class TodoItemManager {
}
public TodoItem get(long id) {
return idCache.getOrDefault(id, null);
}
}