@@ -48,22 +48,35 @@ public Patch(int estimatedPatchSize) { |
48 | 48 | } |
49 | 49 |
|
50 | 50 | /** |
51 | | - * Apply this patch to the given target |
| 51 | + * Creates a new list, the patch is being applied to. |
52 | 52 | * |
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 |
55 | 56 | */ |
56 | 57 | public List<T> applyTo(List<T> target) throws PatchFailedException { |
57 | 58 | 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 { |
58 | 72 | ListIterator<AbstractDelta<T>> it = getDeltas().listIterator(deltas.size()); |
59 | 73 | while (it.hasPrevious()) { |
60 | 74 | AbstractDelta<T> delta = it.previous(); |
61 | | - VerifyChunk valid = delta.verifyAntApplyTo(result); |
| 75 | + VerifyChunk valid = delta.verifyAndApplyTo(target); |
62 | 76 | if (valid != VerifyChunk.OK) { |
63 | | - conflictOutput.processConflict(valid, delta, result); |
| 77 | + conflictOutput.processConflict(valid, delta, target); |
64 | 78 | } |
65 | 79 | } |
66 | | - return result; |
67 | 80 | } |
68 | 81 |
|
69 | 82 | private static class PatchApplyingContext<T> { |
@@ -220,19 +233,33 @@ public Patch withConflictOutput(ConflictOutput<T> conflictOutput) { |
220 | 233 | } |
221 | 234 |
|
222 | 235 | /** |
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. |
224 | 238 | * |
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. |
227 | 241 | */ |
228 | 242 | public List<T> restore(List<T> target) { |
229 | 243 | 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) { |
230 | 258 | ListIterator<AbstractDelta<T>> it = getDeltas().listIterator(deltas.size()); |
231 | 259 | while (it.hasPrevious()) { |
232 | 260 | AbstractDelta<T> delta = it.previous(); |
233 | | - delta.restore(result); |
| 261 | + delta.restore(target); |
234 | 262 | } |
235 | | - return result; |
236 | 263 | } |
237 | 264 |
|
238 | 265 | /** |
|