Eidola home

org.eidola.util
Class ListDiff

java.lang.Object
  |
  +--org.eidola.util.CollectionDiff
        |
        +--org.eidola.util.ListDiff

public class ListDiff
extends CollectionDiff

Finds the difference between two ordered lists. Given a pair of lists, ListDiff provides a sequence of steps which will mutate the first list into the second. These steps implement ListDiff.Step, and include insert, move, and remove. You can ask for the steps using getSteps(), or call applySteps to have the ListDiff walk a ListMutator through the steps.

ListDiff is useful for situations where some structure (such as a user interface) is shadowing a changing list, and you don't want to rebuild the structure from scratch every time the list changes -- perhaps beacuse the cost of recreating the structure is high, or because you don't want to lose state information about existing elements whose position has changed in the list.

The algorithm ListDiff uses for generating the steps is quite simple. It does not guarantee the shortest possible sequence of steps, but usually comes quite close.

Version:
[Development version]
Author:
Paul Cantrell

Inner Class Summary
 class ListDiff.Insert
          A list transformation step: insert an object at a given position.
 class ListDiff.Move
          A list transformation step: move an object from one position to another.
 class ListDiff.Remove
          A list transformation step: remove an object at a given position.
 class ListDiff.Step
          A step in a list transformation.
 
Constructor Summary
ListDiff(List oldStuff, List newStuff)
          Prepares a comparison of two lists.
 
Method Summary
 void applySteps(ListMutator mutator)
          Walks a ListMutator through the sequence of steps which transform the old list into the new.
 boolean equals(Object other)
          Returns true if the other object is a list diff comparing equal lists.
 Iterator getSteps()
          Returns a sequence of steps which transform the old list into the new.
static void main(String[] args)
          For testing
 
Methods inherited from class org.eidola.util.CollectionDiff
getAdded, getRemoved, getSame, hashCode
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ListDiff

public ListDiff(List oldStuff,
                List newStuff)
Prepares a comparison of two lists.
Method Detail

main

public static void main(String[] args)
                 throws Exception
For testing

getSteps

public Iterator getSteps()
Returns a sequence of steps which transform the old list into the new. The Iterator returns ListDiff.Step objects.

applySteps

public void applySteps(ListMutator mutator)
Walks a ListMutator through the sequence of steps which transform the old list into the new.

equals

public boolean equals(Object other)
Returns true if the other object is a list diff comparing equal lists.
Overrides:
equals in class CollectionDiff

Eidola home