ListDiff { public static <T> ListDiff<T> of(Collection<T> original, Collection<T> updated) { List<T> added = new ArrayList<T>(); List<T> removed = new ArrayList<T>(); if (original == null && updated == null) { } else if (original != null && updated == null) { removed.addAll(original); } else if (original == null && updated != null) { added.addAll(updated); } else { for (T t : original) { if (!updated.contains(t)) { removed.add(t); } } for (T t : updated) { if (!original.contains(t)) { added.add(t); } } } return new ListDiff<T>(added, removed); } private  ListDiff(List<T> added, List<T> removed); static ListDiff<T> of(Collection<T> original, Collection<T> updated); final List<T> added; final List<T> removed; }
@Test public void removed() { List<Integer> original = asList(1, 2, 3); List<Integer> updated = asList(1, 2); assertThat(ListDiff.of(original, updated).removed, is(asList(3))); }
@Test public void added() { List<Integer> original = asList(1, 2, 3); List<Integer> updated = asList(1, 2, 3, 4); assertThat(ListDiff.of(original, updated).added, is(asList(4))); }
@Test public void same() { List<Integer> original = asList(1, 2, 3); List<Integer> updated = asList(1, 2, 3); assertThat(ListDiff.of(original, updated).added.size(), is(0)); assertThat(ListDiff.of(original, updated).removed.size(), is(0)); }
@Test public void nullUpdated() { List<Integer> original = asList(1, 2, 3); List<Integer> updated = null; assertThat(ListDiff.of(original, updated).added.size(), is(0)); assertThat(ListDiff.of(original, updated).removed.size(), is(3)); }
@Test public void nullOriginal() { List<Integer> original = null; List<Integer> updated = asList(1, 2, 3); assertThat(ListDiff.of(original, updated).added.size(), is(3)); assertThat(ListDiff.of(original, updated).removed.size(), is(0)); }
@Test public void nullBoth() { List<Integer> original = null; List<Integer> updated = null; assertThat(ListDiff.of(original, updated).added.size(), is(0)); assertThat(ListDiff.of(original, updated).removed.size(), is(0)); }