From bc1ccff075a6fd100afac5184b162400d22085a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20D=C3=B6rscheln?= Date: Thu, 28 Jul 2022 11:57:30 +0200 Subject: [PATCH 1/3] Added ability to apply patch to existing list inplace --- .../java/com/github/difflib/patch/Patch.java | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) 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..18473c9a 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.verifyAntApplyTo(target); if (valid != VerifyChunk.OK) { - conflictOutput.processConflict(valid, delta, result); + conflictOutput.processConflict(valid, delta, target); } } - return result; } private static class PatchApplyingContext { From ec149bd860917252e09a322368cb978df236f75c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20D=C3=B6rscheln?= Date: Thu, 28 Jul 2022 12:18:42 +0200 Subject: [PATCH 2/3] Corrected typo 'verifyAntApplyTo' --- .../src/main/java/com/github/difflib/patch/AbstractDelta.java | 2 +- .../src/main/java/com/github/difflib/patch/Patch.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 18473c9a..e4483de3 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 @@ -72,7 +72,7 @@ public void applyToExisting(List target) throws PatchFailedException { ListIterator> it = getDeltas().listIterator(deltas.size()); while (it.hasPrevious()) { AbstractDelta delta = it.previous(); - VerifyChunk valid = delta.verifyAntApplyTo(target); + VerifyChunk valid = delta.verifyAndApplyTo(target); if (valid != VerifyChunk.OK) { conflictOutput.processConflict(valid, delta, target); } From c9abf484ec69f2cc4bbba2355f55f334c71981bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20D=C3=B6rscheln?= Date: Thu, 28 Jul 2022 12:19:25 +0200 Subject: [PATCH 3/3] Added restoreToExisting method as opposite to applyToExisting --- .../java/com/github/difflib/patch/Patch.java | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) 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 e4483de3..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 @@ -233,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; } /**