@@ -37,40 +37,62 @@ public class Patch<T>(private var conflictOutput: ConflictOutput<T> = ExceptionP
3737 }
3838
3939 /* *
40- * Apply this patch to the given target.
40+ * Apply this patch to the given target list, returning a new list .
4141 *
4242 * @return The patched text
4343 * @throws PatchFailedException If the patch cannot be applied
4444 */
4545 public fun applyTo (target : List <T >): List <T > {
4646 val result = target.toMutableList()
47+ applyToExisting(result)
48+ return result
49+ }
50+
51+ /* *
52+ * Apply this patch to the given target list, directly modifying it.
53+ *
54+ * @return The patched text
55+ * @throws PatchFailedException If the patch cannot be applied
56+ */
57+ @Suppress(" MemberVisibilityCanBePrivate" )
58+ public fun applyToExisting (target : MutableList <T >) {
4759 val it = deltas.listIterator(deltas.size)
4860
4961 while (it.hasPrevious()) {
5062 val delta = it.previous()
51- val verifyChunk = delta.verifyAndApplyTo(result )
52- conflictOutput.processConflict(verifyChunk, delta, result )
63+ val verifyChunk = delta.verifyAndApplyTo(target )
64+ conflictOutput.processConflict(verifyChunk, delta, target )
5365 }
54-
55- return result
5666 }
5767
5868 /* *
59- * Restore the text to its original form. Opposite of the [applyTo] method.
69+ * Creates a new list, containing the restored state of the given target list.
70+ * Opposite of the [applyTo] method.
6071 *
6172 * @param target The given target
6273 * @return The restored text
6374 */
6475 public fun restore (target : List <T >): List <T > {
6576 val result = target.toMutableList()
77+ restoreToExisting(result)
78+ return result
79+ }
80+
81+ /* *
82+ * Restores all changes within the given target list.
83+ * Opposite of the [applyToExisting] method.
84+ *
85+ * @param target The given target
86+ * @return The restored text
87+ */
88+ @Suppress(" MemberVisibilityCanBePrivate" )
89+ public fun restoreToExisting (target : MutableList <T >) {
6690 val it = deltas.listIterator(deltas.size)
6791
6892 while (it.hasPrevious()) {
6993 val delta = it.previous()
70- delta.restore(result )
94+ delta.restore(target )
7195 }
72-
73- return result
7496 }
7597
7698 /* *
@@ -102,7 +124,7 @@ public class Patch<T>(private var conflictOutput: ConflictOutput<T> = ExceptionP
102124 var startRevised = 0
103125
104126 val adjustedChanges = if (includeEquals) {
105- changes.sortedBy { it. startOriginal }
127+ changes.sortedBy( Change :: startOriginal)
106128 } else {
107129 changes
108130 }
0 commit comments