cleanup and fixes: comparator in Patch was broken which caused incorrect delta sorting, Chunk does not store lines as a separate field, in fact it is an attribute of the Chunk.lines collection.Minor cleanup in tests, still more to be done in geenerated udiff tests

This commit is contained in:
shotwave
2011-03-25 21:28:13 +00:00
parent 4f9928e4ef
commit 02ded9901f
12 changed files with 217 additions and 304 deletions

View File

@@ -15,7 +15,10 @@
*/
package difflib;
import java.util.*;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
/**
* Describes the patch holding all deltas between the original and revised texts.
@@ -24,11 +27,9 @@ import java.util.*;
*/
public class Patch {
private List<Delta> deltas = new LinkedList<Delta>();
private boolean isSorted = false;
/**
* Apply this patch to the given target
* @param target
* @return the patched text
* @throws PatchFailedException if can't apply patch
*/
@@ -63,45 +64,14 @@ public class Patch {
*/
public void addDelta(Delta delta) {
deltas.add(delta);
isSorted = false;
}
/**
* @param deltas the deltas to set
*/
public void setDeltas(List<Delta> deltas) {
this.deltas = deltas;
isSorted = false;
}
/**
* Get the list of computed deltas
* @return the deltas
*/
public List<Delta> getDeltas() {
if (!this.isSorted) {
Collections.sort(deltas, new Comparator<Delta>() {
public int compare(Delta d1, Delta d2) {
if (d1.getOriginal().getPosition() > d2.getOriginal().getPosition()) {
return 1;
} else if (d1.getOriginal().getPosition() > d2.getOriginal().getPosition()) {
return -1;
} else {
return 0;
}
}
});
isSorted = true;
}
Collections.sort(deltas, DeltaComparator.INSTANCE);
return deltas;
}
/**
* Get the specific delta from patch deltas
* @param index the index of delta
* @return the needed delta
*/
public Delta getDelta(int index) {
return deltas.get(index);
}
}