List<Object> additions() {
        List<Object> result = Lists.newArrayList();

        for (ListDiffEntry next : diff.getDifferences()) {
          if (next.isAddition()) {
            result.add(next.getElement());
          }
        }

        return result;
      }
 @Override
 public void handleListChange(ListChangeEvent<E> event) {
   for (ListDiffEntry<? extends E> entry : event.diff.getDifferencesAsList()) {
     E element = entry.getElement();
     if (entry.isAddition()) {
       added(element);
     } else {
       removed(element);
     }
   }
 }
 public void handleListChange(ListChangeEvent event) {
   ListDiffEntry[] differences = event.diff.getDifferences();
   for (int i = 0; i < differences.length; i++) {
     ListDiffEntry entry = differences[i];
     if (entry.isAddition()) {
       listenerSupport.hookListener(entry.getElement());
     } else {
       listenerSupport.unhookListener(entry.getElement());
     }
   }
 }
 public void handleListChange(ListChangeEvent event) {
   ListDiff diff = event.diff;
   ListDiffEntry[] differences = diff.getDifferences();
   for (int i = 0; i < differences.length; i++) {
     ListDiffEntry listDiffEntry = differences[i];
     Binding binding = (Binding) listDiffEntry.getElement();
     if (listDiffEntry.isAddition()) {
       binding.getTarget().addChangeListener(uiChangeListener);
     } else {
       binding.getTarget().removeChangeListener(uiChangeListener);
     }
   }
 }
 public void handleListChange(ListChangeEvent event) {
   if (viewer.getControl().isDisposed()) {
     return;
   }
   ListDiffEntry[] differences = event.diff.getDifferences();
   for (int i = 0; i < differences.length; i++) {
     ListDiffEntry entry = differences[i];
     if (entry.isAddition()) {
       knownElements.add(entry.getElement());
       if (viewer instanceof AbstractListViewer) {
         ((AbstractListViewer) viewer).add(entry.getElement());
       } else {
         ((TableViewer) viewer).insert(entry.getElement(), entry.getPosition());
       }
     } else {
       if (viewer instanceof AbstractListViewer) {
         ((AbstractListViewer) viewer).remove(entry.getElement());
       } else {
         ((TableViewer) viewer).remove(entry.getElement());
       }
       knownElements.remove(entry.getElement());
     }
   }
 }
  @Test
  public void test() {
    WritableSet set = new WritableSet(TestRealm.getDefault());
    SetToListAdapter adapter = new SetToListAdapter(set, null);

    ListChangeListener listener = new ListChangeListener();
    adapter.addListChangeListener(listener);

    set.add("Huhu");
    ListDiffEntry e1 = listener.event.diff.getDifferences()[0];
    assertTrue(e1.isAddition());
    assertEquals("Huhu", e1.getElement());
    assertEquals(1, adapter.size());

    set.add("Haha");
    e1 = listener.event.diff.getDifferences()[0];
    assertTrue(e1.isAddition());
    assertEquals("Haha", e1.getElement());
    assertEquals(1, e1.getPosition());
    assertEquals(2, adapter.size());

    set.add("Blabla");
    e1 = listener.event.diff.getDifferences()[0];
    assertTrue(e1.isAddition());
    assertEquals("Blabla", e1.getElement());
    assertEquals(2, e1.getPosition());
    assertEquals(3, adapter.size());

    List<String> added = Arrays.asList("Blublu", "Foo", "Bar");
    set.addAll(added);
    e1 = listener.event.diff.getDifferences()[0];
    ListDiffEntry e2 = listener.event.diff.getDifferences()[1];
    ListDiffEntry e3 = listener.event.diff.getDifferences()[2];
    assertEquals(3, listener.event.diff.getDifferences().length);
    assertEquals(6, adapter.size());
    assertTrue(e1.isAddition());
    assertTrue(e2.isAddition());
    assertTrue(e3.isAddition());
    assertTrue(added.contains(e1.getElement()));
    assertTrue(added.contains(e2.getElement()));
    assertTrue(added.contains(e3.getElement()));
    assertTrue(e1.getPosition() >= 3 && e1.getPosition() <= 5);
    assertTrue(e2.getPosition() >= 3 && e1.getPosition() <= 5);
    assertTrue(e3.getPosition() >= 3 && e1.getPosition() <= 5);

    set.remove("Haha");
    e1 = listener.event.diff.getDifferences()[0];
    assertTrue(!e1.isAddition());
    assertEquals("Haha", e1.getElement());
    assertEquals(1, e1.getPosition());
    assertEquals(5, adapter.size());

    set.removeAll(added);
    e1 = listener.event.diff.getDifferences()[0];
    e2 = listener.event.diff.getDifferences()[1];
    e3 = listener.event.diff.getDifferences()[2];
    assertEquals(3, listener.event.diff.getDifferences().length);
    assertEquals(2, adapter.size());
    assertTrue(!e1.isAddition());
    assertTrue(!e2.isAddition());
    assertTrue(!e3.isAddition());
    assertTrue(added.contains(e1.getElement()));
    assertTrue(added.contains(e2.getElement()));
    assertTrue(added.contains(e3.getElement()));
    assertTrue(e1.getPosition() >= 2 && e1.getPosition() <= 4);
    assertTrue(e2.getPosition() >= 2 && e1.getPosition() <= 4);
    assertTrue(e3.getPosition() >= 2 && e1.getPosition() <= 4);
  }