fix some bugs

This commit is contained in:
hlq07 2018-05-27 01:28:53 +08:00
parent 5d1ed01c9c
commit 91a5d633cd
2 changed files with 15 additions and 49 deletions

View File

@ -98,15 +98,6 @@ public class TodoItem {
*/
public static final String IMPORTANCE = "importance";
/**
* Value of a name of possible string-type property index.
*
* The corresponding value of stringProperties is formatted: "parent", "child".
*
* If the instance have no {@code COMPOSITE}, then the corresponding value should be assumed as "parent".
*/
public static final String COMPOSITE = "composite";
/**
* Value of a name of possible string-type property index.
* <p>

View File

@ -139,9 +139,9 @@ public class TodoItemUtil {
public static boolean isIntersectedConflict(@NotNull TodoItem i1, @NotNull TodoItem i2) {
int t1 = i1.getType();
int t2 = i2.getType();
return isIntersected(i1, i2)
&& arrayContains(INTERSECTED_CONFLICT, t1)
&& arrayContains(INTERSECTED_CONFLICT, t2);
return arrayContains(INTERSECTED_CONFLICT, t1)
&& arrayContains(INTERSECTED_CONFLICT, t2)
&& isIntersected(i1, i2);
}
@ -232,33 +232,22 @@ public class TodoItemUtil {
}
public static boolean isParentChildConflict(@NotNull TodoItem parent, @NotNull TodoItem child) {
public static boolean canBind(@NotNull TodoItem parent, @NotNull TodoItem child) {
int pType = parent.getType();
int cType = child.getType();
return pType != cType && arrayContains(PARENT_CHILDREN_CONFLICT, pType)
&& arrayContains(PARENT_CHILDREN_CONFLICT, cType);
&& arrayContains(PARENT_CHILDREN_CONFLICT, cType)
&& isIn(parent, child);
}
public static boolean canBecomeSubTodoItem(@NotNull TodoItem parent, @NotNull TodoItem newItem) {
return isIn(parent, newItem) && !isParentChildConflict(parent, newItem);
}
public static boolean isParentTodoItem(@NotNull TodoItem item) {
return item.getStringProperty(CHILDREN_IDS) != null;
}
public static boolean isChildTodoItem(@NotNull TodoItem item) {
return item.getStringProperty(PARENT_ID) != null;
}
public static List<Long> getAllChildrenIds(@NotNull TodoItem item) {
String ids = item.getStringProperty(CHILDREN_IDS);
String[] idsSlice = ids.split("[;,]");
String[] idsSlice = ids == null ? new String[0] : ids.split("[;,]");
ArrayList<Long> result = new ArrayList<>(idsSlice.length);
for (int i = 0; i < ids.length(); i++) {
for (int i = 0; i < result.size(); i++) {
long id = Long.parseLong(idsSlice[i]);
result.set(i, id);
}
@ -267,7 +256,7 @@ public class TodoItemUtil {
public static long getParentId(@NotNull TodoItem item) {
String id = item.getStringProperty(PARENT_ID);
return Long.parseLong(id);
return id == null ? item.getId() : Long.parseLong(id);
}
public static String idsToString(@NotNull Collection<Long> ids) {
@ -277,21 +266,15 @@ public class TodoItemUtil {
for (long i : ids) {
if (!first) {
builder.append(';');
} else {
first = false;
}
builder.append(Long.toString(i));
first = false;
}
return builder.toString();
}
public static TodoItem[] bind(@NotNull TodoItem parent, @NotNull TodoItem child) {
if (!isParentTodoItem(parent)) {
throw new IllegalArgumentException("parent is not a parent todo-item");
}
if (!isChildTodoItem(child)) {
throw new IllegalArgumentException("child is not a child todo-item");
}
TodoItemFactory fParent = new TodoItemFactory().copy(parent);
TodoItemFactory fChild = new TodoItemFactory().copy(child);
@ -301,26 +284,18 @@ public class TodoItemUtil {
TodoItem newP = fParent
.stringProperties(CHILDREN_IDS, idsString)
.stringProperties(COMPOSITE, "parent")
.buildId(parent.getId());
String pIdString = Long.toString(parent.getId());
TodoItem newC = fChild
.stringProperties(PARENT_ID, pIdString)
.stringProperties(COMPOSITE, "child")
.buildId(child.getId());
return new TodoItem[]{newP, newC};
}
public static TodoItem[] unbind(@NotNull TodoItem parent, @NotNull TodoItem child) {
if (!isParentTodoItem(parent)) {
throw new IllegalArgumentException("parent is not a parent todo-item");
}
if (!isChildTodoItem(child)) {
throw new IllegalArgumentException("child is not a child todo-item");
}
TodoItemFactory fParent = new TodoItemFactory().copy(parent);
TodoItemFactory fChild = new TodoItemFactory().copy(child);
@ -331,18 +306,15 @@ public class TodoItemUtil {
TodoItem newP = fParent
.stringProperties(CHILDREN_IDS, idsString)
.stringProperties(COMPOSITE, "parent")
.buildId(parent.getId());
TodoItem newC = fChild
.stringProperties(PARENT_ID, null)
.stringProperties(COMPOSITE, "parent")
.buildId(child.getId());
return new TodoItem[]{newP, newC};
}
public static TodoItem finishSingleTodoItem(@NotNull TodoItem item) {
TodoItemFactory factory = new TodoItemFactory();
return factory.copy(item).stringProperties(FINISH, "finish").buildId(item.getId());
@ -350,7 +322,10 @@ public class TodoItemUtil {
public static TodoItem finishTimelessItem(@NotNull String title) {
TodoItemFactory factory = new TodoItemFactory();
return factory.wholeDay(Calendar.getInstance()).title(title).build();
return factory.wholeDay(Calendar.getInstance())
.title(title)
.stringProperties(FINISH, "finish")
.build();
}
}