@@ -48,22 +48,35 @@ public Patch(int estimatedPatchSize) {
4848 }
4949
5050 /**
51- * Apply this patch to the given target
51+ * Creates a new list, the patch is being applied to.
5252 *
53- * @return the patched text
54- * @throws PatchFailedException if can't apply patch
53+ * @param target The list to apply the changes to.
54+ * @return A new list containing the applied patch.
55+ * @throws PatchFailedException if the patch cannot be applied
5556 */
5657 public List <T > applyTo (List <T > target ) throws PatchFailedException {
5758 List <T > result = new ArrayList <>(target );
59+ applyToExisting (result );
60+ return result ;
61+ }
62+
63+ /**
64+ * Applies the patch to the supplied list.
65+ *
66+ * @param target The list to apply the changes to. This list has to be modifiable,
67+ * otherwise exceptions may be thrown, depending on the used type of list.
68+ * @throws PatchFailedException if the patch cannot be applied
69+ * @throws RuntimeException (or similar) if the list is not modifiable.
70+ */
71+ public void applyToExisting (List <T > target ) throws PatchFailedException {
5872 ListIterator <AbstractDelta <T >> it = getDeltas ().listIterator (deltas .size ());
5973 while (it .hasPrevious ()) {
6074 AbstractDelta <T > delta = it .previous ();
61- VerifyChunk valid = delta .verifyAntApplyTo ( result );
75+ VerifyChunk valid = delta .verifyAndApplyTo ( target );
6276 if (valid != VerifyChunk .OK ) {
63- conflictOutput .processConflict (valid , delta , result );
77+ conflictOutput .processConflict (valid , delta , target );
6478 }
6579 }
66- return result ;
6780 }
6881
6982 private static class PatchApplyingContext <T > {
@@ -220,19 +233,33 @@ public Patch withConflictOutput(ConflictOutput<T> conflictOutput) {
220233 }
221234
222235 /**
223- * Restore the text to original. Opposite to applyTo() method.
236+ * Creates a new list, containing the restored state of the given list.
237+ * Opposite to {@link #applyTo(List)} method.
224238 *
225- * @param target the given target
226- * @return the restored text
239+ * @param target The list to copy and apply changes to.
240+ * @return A new list, containing the restored state.
227241 */
228242 public List <T > restore (List <T > target ) {
229243 List <T > result = new ArrayList <>(target );
244+ restoreToExisting (result );
245+ return result ;
246+ }
247+
248+
249+ /**
250+ * Restores all changes within the given list.
251+ * Opposite to {@link #applyToExisting(List)} method.
252+ *
253+ * @param target The list to restore changes in. This list has to be modifiable,
254+ * otherwise exceptions may be thrown, depending on the used type of list.
255+ * @throws RuntimeException (or similar) if the list is not modifiable.
256+ */
257+ public void restoreToExisting (List <T > target ) {
230258 ListIterator <AbstractDelta <T >> it = getDeltas ().listIterator (deltas .size ());
231259 while (it .hasPrevious ()) {
232260 AbstractDelta <T > delta = it .previous ();
233- delta .restore (result );
261+ delta .restore (target );
234262 }
235- return result ;
236263 }
237264
238265 /**
0 commit comments