diff --git a/java-diff-utils/src/main/java/com/github/difflib/patch/AbstractDelta.java b/java-diff-utils/src/main/java/com/github/difflib/patch/AbstractDelta.java index a315e010..f74f62ca 100644 --- a/java-diff-utils/src/main/java/com/github/difflib/patch/AbstractDelta.java +++ b/java-diff-utils/src/main/java/com/github/difflib/patch/AbstractDelta.java @@ -58,7 +58,7 @@ protected VerifyChunk verifyChunkToFitTarget(List target) throws PatchFailedE return getSource().verifyChunk(target); } - protected VerifyChunk verifyAntApplyTo(List target) throws PatchFailedException { + protected VerifyChunk verifyAndApplyTo(List target) throws PatchFailedException { final VerifyChunk verify = verifyChunkToFitTarget(target); if (verify == VerifyChunk.OK) { applyTo(target); diff --git a/java-diff-utils/src/main/java/com/github/difflib/patch/Patch.java b/java-diff-utils/src/main/java/com/github/difflib/patch/Patch.java index 5e3e51f8..aaff7d94 100644 --- a/java-diff-utils/src/main/java/com/github/difflib/patch/Patch.java +++ b/java-diff-utils/src/main/java/com/github/difflib/patch/Patch.java @@ -48,22 +48,35 @@ public Patch(int estimatedPatchSize) { } /** - * Apply this patch to the given target + * Creates a new list, the patch is being applied to. * - * @return the patched text - * @throws PatchFailedException if can't apply patch + * @param target The list to apply the changes to. + * @return A new list containing the applied patch. + * @throws PatchFailedException if the patch cannot be applied */ public List applyTo(List target) throws PatchFailedException { List result = new ArrayList<>(target); + applyToExisting(result); + return result; + } + + /** + * Applies the patch to the supplied list. + * + * @param target The list to apply the changes to. This list has to be modifiable, + * otherwise exceptions may be thrown, depending on the used type of list. + * @throws PatchFailedException if the patch cannot be applied + * @throws RuntimeException (or similar) if the list is not modifiable. + */ + public void applyToExisting(List target) throws PatchFailedException { ListIterator> it = getDeltas().listIterator(deltas.size()); while (it.hasPrevious()) { AbstractDelta delta = it.previous(); - VerifyChunk valid = delta.verifyAntApplyTo(result); + VerifyChunk valid = delta.verifyAndApplyTo(target); if (valid != VerifyChunk.OK) { - conflictOutput.processConflict(valid, delta, result); + conflictOutput.processConflict(valid, delta, target); } } - return result; } private static class PatchApplyingContext { @@ -220,19 +233,33 @@ public Patch withConflictOutput(ConflictOutput conflictOutput) { } /** - * Restore the text to original. Opposite to applyTo() method. + * Creates a new list, containing the restored state of the given list. + * Opposite to {@link #applyTo(List)} method. * - * @param target the given target - * @return the restored text + * @param target The list to copy and apply changes to. + * @return A new list, containing the restored state. */ public List restore(List target) { List result = new ArrayList<>(target); + restoreToExisting(result); + return result; + } + + + /** + * Restores all changes within the given list. + * Opposite to {@link #applyToExisting(List)} method. + * + * @param target The list to restore changes in. This list has to be modifiable, + * otherwise exceptions may be thrown, depending on the used type of list. + * @throws RuntimeException (or similar) if the list is not modifiable. + */ + public void restoreToExisting(List target) { ListIterator> it = getDeltas().listIterator(deltas.size()); while (it.hasPrevious()) { AbstractDelta delta = it.previous(); - delta.restore(result); + delta.restore(target); } - return result; } /**