added a first test for dynamic route update

This commit is contained in:
Axel Uhl
2011-06-11 16:36:58 +02:00
parent 3607a459e7
commit be3d1f2df7
7 changed files with 170 additions and 20 deletions
@@ -29,7 +29,7 @@ public class Patch<T> {
private List<Delta<T>> deltas = new LinkedList<Delta<T>>();
/**
* Apply this patch to the given target
* Apply this patch to the given target, producing a new list as result
* @return the patched text
* @throws PatchFailedException if can't apply patch
*/
@@ -43,6 +43,21 @@ public class Patch<T> {
return result;
}
/**
* Apply this patch to the given target in-place, updating the <code>target</code> list
* @return the patched text
* @throws PatchFailedException if can't apply patch
*/
public List<T> applyToInPlace(List<T> target) throws PatchFailedException {
List<T> result = target;
ListIterator<Delta<T>> it = getDeltas().listIterator(deltas.size());
while (it.hasPrevious()) {
Delta<T> delta = it.previous();
delta.applyTo(result);
}
return result;
}
/**
* Restore the text to original. Opposite to applyTo() method.
* @param target the given target
@@ -74,4 +89,20 @@ public class Patch<T> {
Collections.sort(deltas, new DeltaComparator<T>());
return deltas;
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
result.append('[');
boolean first = true;
for (Delta<T> delta : getDeltas()) {
if (first) {
first = false;
} else {
result.append(", ");
}
result.append(delta);
}
return result.toString();
}
}