@Test public void nullNewAndOldValue() { ListDiff<String> d = ListDiff.of(null, null); assertThat(d.added.size(), is(0)); assertThat(d.removed.size(), is(0)); assertThat(d.moves.size(), is(0)); }
public static <T> ListDiff<T> of(List<T> oldValue, List<T> newValue) { List<Location<T>> added = new ArrayList<Location<T>>(); List<Location<T>> moves = new ArrayList<Location<T>>(); List<Location<T>> removed = new ArrayList<Location<T>>(); if (oldValue == null && newValue != null) { int i = 0; for (T t : newValue) { added.add(new Location<T>(t, i++, -1)); } } else if (oldValue != null && newValue == null) { for (T t : oldValue) { removed.add(new Location<T>(t, 0, -1)); } } else if (oldValue != null && newValue != null) { List<T> oldCopy = new ArrayList<T>(oldValue); List<T> newCopy = new ArrayList<T>(newValue); for (Iterator<T> i = oldCopy.iterator(); i.hasNext();) { T t = i.next(); if (!newCopy.remove(t)) { removed.add(new Location<T>(t, oldCopy.indexOf(t), -1)); i.remove(); } } List<T> withRemoves = new ArrayList<T>(oldCopy); for (T t : newValue) { if (!oldCopy.remove(t)) { int newIndex = newValue.indexOf(t); added.add(new Location<T>(t, newIndex, -1)); withRemoves.add(newIndex, t); } else { int oldIndex = withRemoves.indexOf(t); int newIndex = newValue.indexOf(t); if (oldIndex != newIndex) { moves.add(new Location<T>(t, newIndex, oldIndex)); withRemoves.remove(oldIndex); withRemoves.add(newIndex, t); } } } } return new ListDiff<T>(added, moves, removed); }
ListDiff { public static <T> ListDiff<T> of(List<T> oldValue, List<T> newValue) { List<Location<T>> added = new ArrayList<Location<T>>(); List<Location<T>> moves = new ArrayList<Location<T>>(); List<Location<T>> removed = new ArrayList<Location<T>>(); if (oldValue == null && newValue != null) { int i = 0; for (T t : newValue) { added.add(new Location<T>(t, i++, -1)); } } else if (oldValue != null && newValue == null) { for (T t : oldValue) { removed.add(new Location<T>(t, 0, -1)); } } else if (oldValue != null && newValue != null) { List<T> oldCopy = new ArrayList<T>(oldValue); List<T> newCopy = new ArrayList<T>(newValue); for (Iterator<T> i = oldCopy.iterator(); i.hasNext();) { T t = i.next(); if (!newCopy.remove(t)) { removed.add(new Location<T>(t, oldCopy.indexOf(t), -1)); i.remove(); } } List<T> withRemoves = new ArrayList<T>(oldCopy); for (T t : newValue) { if (!oldCopy.remove(t)) { int newIndex = newValue.indexOf(t); added.add(new Location<T>(t, newIndex, -1)); withRemoves.add(newIndex, t); } else { int oldIndex = withRemoves.indexOf(t); int newIndex = newValue.indexOf(t); if (oldIndex != newIndex) { moves.add(new Location<T>(t, newIndex, oldIndex)); withRemoves.remove(oldIndex); withRemoves.add(newIndex, t); } } } } return new ListDiff<T>(added, moves, removed); } }
ListDiff { public static <T> ListDiff<T> of(List<T> oldValue, List<T> newValue) { List<Location<T>> added = new ArrayList<Location<T>>(); List<Location<T>> moves = new ArrayList<Location<T>>(); List<Location<T>> removed = new ArrayList<Location<T>>(); if (oldValue == null && newValue != null) { int i = 0; for (T t : newValue) { added.add(new Location<T>(t, i++, -1)); } } else if (oldValue != null && newValue == null) { for (T t : oldValue) { removed.add(new Location<T>(t, 0, -1)); } } else if (oldValue != null && newValue != null) { List<T> oldCopy = new ArrayList<T>(oldValue); List<T> newCopy = new ArrayList<T>(newValue); for (Iterator<T> i = oldCopy.iterator(); i.hasNext();) { T t = i.next(); if (!newCopy.remove(t)) { removed.add(new Location<T>(t, oldCopy.indexOf(t), -1)); i.remove(); } } List<T> withRemoves = new ArrayList<T>(oldCopy); for (T t : newValue) { if (!oldCopy.remove(t)) { int newIndex = newValue.indexOf(t); added.add(new Location<T>(t, newIndex, -1)); withRemoves.add(newIndex, t); } else { int oldIndex = withRemoves.indexOf(t); int newIndex = newValue.indexOf(t); if (oldIndex != newIndex) { moves.add(new Location<T>(t, newIndex, oldIndex)); withRemoves.remove(oldIndex); withRemoves.add(newIndex, t); } } } } return new ListDiff<T>(added, moves, removed); } private  ListDiff(Collection<Location<T>> added, Collection<Location<T>> moves, Collection<Location<T>> removed); }
ListDiff { public static <T> ListDiff<T> of(List<T> oldValue, List<T> newValue) { List<Location<T>> added = new ArrayList<Location<T>>(); List<Location<T>> moves = new ArrayList<Location<T>>(); List<Location<T>> removed = new ArrayList<Location<T>>(); if (oldValue == null && newValue != null) { int i = 0; for (T t : newValue) { added.add(new Location<T>(t, i++, -1)); } } else if (oldValue != null && newValue == null) { for (T t : oldValue) { removed.add(new Location<T>(t, 0, -1)); } } else if (oldValue != null && newValue != null) { List<T> oldCopy = new ArrayList<T>(oldValue); List<T> newCopy = new ArrayList<T>(newValue); for (Iterator<T> i = oldCopy.iterator(); i.hasNext();) { T t = i.next(); if (!newCopy.remove(t)) { removed.add(new Location<T>(t, oldCopy.indexOf(t), -1)); i.remove(); } } List<T> withRemoves = new ArrayList<T>(oldCopy); for (T t : newValue) { if (!oldCopy.remove(t)) { int newIndex = newValue.indexOf(t); added.add(new Location<T>(t, newIndex, -1)); withRemoves.add(newIndex, t); } else { int oldIndex = withRemoves.indexOf(t); int newIndex = newValue.indexOf(t); if (oldIndex != newIndex) { moves.add(new Location<T>(t, newIndex, oldIndex)); withRemoves.remove(oldIndex); withRemoves.add(newIndex, t); } } } } return new ListDiff<T>(added, moves, removed); } private  ListDiff(Collection<Location<T>> added, Collection<Location<T>> moves, Collection<Location<T>> removed); static ListDiff<T> of(List<T> oldValue, List<T> newValue); void apply(List<T> copy); void apply(final List<U> copy, Mapper<T, U> mapper); void apply(ListLike<U> copy, Mapper<T, U> mapper); @Override String toString(); }
ListDiff { public static <T> ListDiff<T> of(List<T> oldValue, List<T> newValue) { List<Location<T>> added = new ArrayList<Location<T>>(); List<Location<T>> moves = new ArrayList<Location<T>>(); List<Location<T>> removed = new ArrayList<Location<T>>(); if (oldValue == null && newValue != null) { int i = 0; for (T t : newValue) { added.add(new Location<T>(t, i++, -1)); } } else if (oldValue != null && newValue == null) { for (T t : oldValue) { removed.add(new Location<T>(t, 0, -1)); } } else if (oldValue != null && newValue != null) { List<T> oldCopy = new ArrayList<T>(oldValue); List<T> newCopy = new ArrayList<T>(newValue); for (Iterator<T> i = oldCopy.iterator(); i.hasNext();) { T t = i.next(); if (!newCopy.remove(t)) { removed.add(new Location<T>(t, oldCopy.indexOf(t), -1)); i.remove(); } } List<T> withRemoves = new ArrayList<T>(oldCopy); for (T t : newValue) { if (!oldCopy.remove(t)) { int newIndex = newValue.indexOf(t); added.add(new Location<T>(t, newIndex, -1)); withRemoves.add(newIndex, t); } else { int oldIndex = withRemoves.indexOf(t); int newIndex = newValue.indexOf(t); if (oldIndex != newIndex) { moves.add(new Location<T>(t, newIndex, oldIndex)); withRemoves.remove(oldIndex); withRemoves.add(newIndex, t); } } } } return new ListDiff<T>(added, moves, removed); } private  ListDiff(Collection<Location<T>> added, Collection<Location<T>> moves, Collection<Location<T>> removed); static ListDiff<T> of(List<T> oldValue, List<T> newValue); void apply(List<T> copy); void apply(final List<U> copy, Mapper<T, U> mapper); void apply(ListLike<U> copy, Mapper<T, U> mapper); @Override String toString(); final Collection<Location<T>> added; final Collection<Location<T>> moves; final Collection<Location<T>> removed; }