@Override public String toString() { return getClass().getSimpleName() + "{created=" + getCreatedUnmodifiable() + ", deleted=" + getDeletedUnmodifiable() + "}"; } }
/** * Apply the reverse of this diff to current state in order * to obtain the previous state. * @return the previous state of the list. */ public List<E> apply2Current(final List<E> current) { return apply2Previous(current, getDeletedUnmodifiable(), getCreatedUnmodifiable()); }
private static <K, E extends Diff.Element<K>> List<E> apply2Previous( final List<E> previous, final List<E> clist, final List<E> dlist) { // Assumptions: // (A1) All lists are sorted. // (A2) All elements in dlist must be in previous. // (A3) All elements in clist must be not in tmp = previous - dlist. final List<E> tmp = new ArrayList<E>(previous.size() - dlist.size()); { // tmp = previous - dlist final Iterator<E> i = previous.iterator(); for(E deleted : dlist) { E e = i.next(); //since dlist is non-empty, e must exist by (A2). int cmp = 0; for(; (cmp = e.compareTo(deleted.getKey())) < 0; e = i.next()) { tmp.add(e); } Preconditions.checkState(cmp == 0); // check (A2) } for(; i.hasNext(); ) { tmp.add(i.next()); } } final List<E> current = new ArrayList<E>(tmp.size() + clist.size()); { // current = tmp + clist final Iterator<E> tmpIterator = tmp.iterator(); final Iterator<E> cIterator = clist.iterator(); E t = tmpIterator.hasNext()? tmpIterator.next(): null;
= posterior.getCreatedUnmodifiable().iterator(); final Iterator<E> deletedIterator = posterior.getDeletedUnmodifiable().iterator();