2525import java .util .ArrayList ;
2626import java .util .Collections ;
2727import static java .util .Comparator .comparing ;
28- import java .util .LinkedList ;
2928import java .util .List ;
3029import java .util .ListIterator ;
3130
3736 */
3837public final class Patch <T > {
3938
40- private final List <Delta <T >> deltas = new LinkedList <>() ;
39+ private final List <Delta <T >> deltas ;
4140
41+ public Patch () {
42+ this (10 );
43+ }
44+
45+ public Patch (int estimatedPatchSize ) {
46+ deltas = new ArrayList <>(estimatedPatchSize );
47+ }
48+
4249 /**
4350 * Apply this patch to the given target
4451 *
4552 * @return the patched text
4653 * @throws PatchFailedException if can't apply patch
4754 */
4855 public List <T > applyTo (List <T > target ) throws PatchFailedException {
49- List <T > result = new LinkedList <>(target );
56+ List <T > result = new ArrayList <>(target );
5057 ListIterator <Delta <T >> it = getDeltas ().listIterator (deltas .size ());
5158 while (it .hasPrevious ()) {
5259 Delta <T > delta = it .previous ();
@@ -62,7 +69,7 @@ public List<T> applyTo(List<T> target) throws PatchFailedException {
6269 * @return the restored text
6370 */
6471 public List <T > restore (List <T > target ) {
65- List <T > result = new LinkedList <>(target );
72+ List <T > result = new ArrayList <>(target );
6673 ListIterator <Delta <T >> it = getDeltas ().listIterator (deltas .size ());
6774 while (it .hasPrevious ()) {
6875 Delta <T > delta = it .previous ();
@@ -96,7 +103,7 @@ public String toString() {
96103 }
97104
98105 public static <T > Patch <T > generate (List <T > original , List <T > revised , List <Change > changes ) {
99- Patch <T > patch = new Patch <>();
106+ Patch <T > patch = new Patch <>(changes . size () );
100107 for (Change change : changes ) {
101108 Chunk <T > orgChunk = new Chunk <>(change .startOriginal , new ArrayList <>(original .subList (change .startOriginal , change .endOriginal )));
102109 Chunk <T > revChunk = new Chunk <>(change .startRevised , new ArrayList <>(revised .subList (change .startRevised , change .endRevised )));
0 commit comments