public boolean containsDeleted(final K key) { if (deleted != null) { return search(deleted, key) >= 0; } return false; }
/** * @return null if the element is not found; * otherwise, return the element in the deleted list. */ public E getDeleted(final K key) { if (deleted != null) { final int c = search(deleted, key); if (c >= 0) { return deleted.get(c); } } return null; }
public boolean removeDeleted(final E element) { if (deleted != null) { final int i = search(deleted, element.getKey()); if (i >= 0 && deleted.get(i) == element) { deleted.remove(i); return true; } } return false; }
private static <K, E extends Diff.Element<K>> Container<E> accessPrevious( final K name, final List<E> clist, final List<E> dlist) { final int d = search(dlist, name); if (d >= 0) { // the element was in previous and was once deleted in current. return new Container<E>(dlist.get(d)); } else { final int c = search(clist, name); // When c >= 0, the element in current is a newly created element. return c < 0? null: new Container<E>(null); } }
/** * Create an element in current state. * @return the c-list insertion point for undo. */ public int create(final E element) { final int c = search(created, element.getKey()); addCreated(element, c); return c; }
/** * Delete an element from current state. * @return the undo information. */ public UndoInfo<E> delete(final E element) { final int c = search(created, element.getKey()); E previous = null; Integer d = null; if (c >= 0) { // remove a newly created element previous = created.remove(c); } else { // not in c-list, it must be in previous d = search(deleted, element.getKey()); addDeleted(element, d); } return new UndoInfo<E>(c, previous, d); }
/** * Modify an element in current state. * @return the undo information. */ public UndoInfo<E> modify(final E oldElement, final E newElement) { Preconditions.checkArgument(oldElement != newElement, "They are the same object: oldElement == newElement = %s", newElement); Preconditions.checkArgument(oldElement.compareTo(newElement.getKey()) == 0, "The names do not match: oldElement=%s, newElement=%s", oldElement, newElement); final int c = search(created, newElement.getKey()); E previous = null; Integer d = null; if (c >= 0) { // Case 1.1.3 and 2.3.3: element is already in c-list, previous = created.set(c, newElement); // For previous != oldElement, set it to oldElement previous = oldElement; } else { d = search(deleted, oldElement.getKey()); if (d < 0) { // Case 2.3: neither in c-list nor d-list addCreated(newElement, c); addDeleted(oldElement, d); } } return new UndoInfo<E>(c, previous, d); }
private static <K, E extends Diff.Element<K>> Container<E> accessPrevious( final K name, final List<E> clist, final List<E> dlist) { final int d = search(dlist, name); if (d >= 0) { // the element was in previous and was once deleted in current. return new Container<E>(dlist.get(d)); } else { final int c = search(clist, name); // When c >= 0, the element in current is a newly created element. return c < 0? null: new Container<E>(null); } }
private static <K, E extends Diff.Element<K>> Container<E> accessPrevious( final K name, final List<E> clist, final List<E> dlist) { final int d = search(dlist, name); if (d >= 0) { // the element was in previous and was once deleted in current. return new Container<E>(dlist.get(d)); } else { final int c = search(clist, name); // When c >= 0, the element in current is a newly created element. return c < 0? null: new Container<E>(null); } }
/** * @return null if the element is not found; * otherwise, return the element in the created/deleted list. */ public E search(final ListType type, final K name) { final List<E> list = getList(type); final int c = search(list, name); return c < 0 ? null : list.get(c); }
/** * @return null if the element is not found; * otherwise, return the element in the created/deleted list. */ public E search(final ListType type, final K name) { final List<E> list = getList(type); final int c = search(list, name); return c < 0 ? null : list.get(c); }
public int searchIndex(final ListType type, final K name) { return search(getList(type), name); }
public int searchIndex(final ListType type, final K name) { return search(getList(type), name); }
/** * Create an element in current state. * @return the c-list insertion point for undo. */ public int create(final E element) { final int c = search(created, element.getKey()); insert(ListType.CREATED, element, c); return c; }
/** * Create an element in current state. * @return the c-list insertion point for undo. */ public int create(final E element) { final int c = search(created, element.getKey()); insert(ListType.CREATED, element, c); return c; }
/** * Delete an element from current state. * @return the undo information. */ public UndoInfo<E> delete(final E element) { final int c = search(created, element.getKey()); E previous = null; Integer d = null; if (c >= 0) { // remove a newly created element previous = created.remove(c); } else { // not in c-list, it must be in previous d = search(deleted, element.getKey()); insert(ListType.DELETED, element, d); } return new UndoInfo<E>(c, previous, d); }
/** * Delete an element from current state. * @return the undo information. */ public UndoInfo<E> delete(final E element) { final int c = search(created, element.getKey()); E previous = null; Integer d = null; if (c >= 0) { // remove a newly created element previous = created.remove(c); } else { // not in c-list, it must be in previous d = search(deleted, element.getKey()); insert(ListType.DELETED, element, d); } return new UndoInfo<E>(c, previous, d); }
/** * Modify an element in current state. * @return the undo information. */ public UndoInfo<E> modify(final E oldElement, final E newElement) { Preconditions.checkArgument(oldElement != newElement, "They are the same object: oldElement == newElement = %s", newElement); Preconditions.checkArgument(oldElement.compareTo(newElement.getKey()) == 0, "The names do not match: oldElement=%s, newElement=%s", oldElement, newElement); final int c = search(created, newElement.getKey()); E previous = null; Integer d = null; if (c >= 0) { // Case 1.1.3 and 2.3.3: element is already in c-list, previous = created.set(c, newElement); // For previous != oldElement, set it to oldElement previous = oldElement; } else { d = search(deleted, oldElement.getKey()); if (d < 0) { // Case 2.3: neither in c-list nor d-list insert(ListType.CREATED, newElement, c); insert(ListType.DELETED, oldElement, d); } } return new UndoInfo<E>(c, previous, d); }
static void delete(INode inode, final List<INode> current, Diff<byte[], INode> diff) { final int i = Diff.search(current, inode.getKey()); current.remove(i); if (diff != null) { //test undo with 1/UNDO_TEST_P probability final boolean testUndo = RANDOM.nextInt(UNDO_TEST_P) == 0; String before = null; if (testUndo) { before = diff.toString(); } final UndoInfo<INode> undoInfo = diff.delete(inode); if (testUndo) { final String after = diff.toString(); //undo diff.undoDelete(inode, undoInfo); assertDiff(before, diff); //re-do diff.delete(inode); assertDiff(after, diff); } } }
static void create(INode inode, final List<INode> current, Diff<byte[], INode> diff) { final int i = Diff.search(current, inode.getKey()); Assert.assertTrue(i < 0); current.add(-i - 1, inode); if (diff != null) { //test undo with 1/UNDO_TEST_P probability final boolean testUndo = RANDOM.nextInt(UNDO_TEST_P) == 0; String before = null; if (testUndo) { before = diff.toString(); } final int undoInfo = diff.create(inode); if (testUndo) { final String after = diff.toString(); //undo diff.undoCreate(inode, undoInfo); assertDiff(before, diff); //re-do diff.create(inode); assertDiff(after, diff); } } }