Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7d0fd54

Browse files
andre161292André Dörscheln
and
André Dörscheln
authored
Added ability to apply patch to existing list inplace (java-diff-utils#152)
* Added ability to apply patch to existing list inplace* Corrected typo 'verifyAntApplyTo'* Added restoreToExisting method as opposite to applyToExistingCo-authored-by: André Dörscheln <ad@itesign.de>
1 parent15934d5 commit7d0fd54

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

‎java-diff-utils/src/main/java/com/github/difflib/patch/AbstractDelta.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected VerifyChunk verifyChunkToFitTarget(List<T> target) throws PatchFailedE
5858
returngetSource().verifyChunk(target);
5959
}
6060

61-
protectedVerifyChunkverifyAntApplyTo(List<T>target)throwsPatchFailedException {
61+
protectedVerifyChunkverifyAndApplyTo(List<T>target)throwsPatchFailedException {
6262
finalVerifyChunkverify =verifyChunkToFitTarget(target);
6363
if (verify ==VerifyChunk.OK) {
6464
applyTo(target);

‎java-diff-utils/src/main/java/com/github/difflib/patch/Patch.java‎

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,35 @@ public Patch(int estimatedPatchSize) {
4848
}
4949

5050
/**
51-
*Apply this patch to thegiven target
51+
*Creates a new list, thepatch 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
publicList<T>applyTo(List<T>target)throwsPatchFailedException {
5758
List<T>result =newArrayList<>(target);
59+
applyToExisting(result);
60+
returnresult;
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+
publicvoidapplyToExisting(List<T>target)throwsPatchFailedException {
5872
ListIterator<AbstractDelta<T>>it =getDeltas().listIterator(deltas.size());
5973
while (it.hasPrevious()) {
6074
AbstractDelta<T>delta =it.previous();
61-
VerifyChunkvalid =delta.verifyAntApplyTo(result);
75+
VerifyChunkvalid =delta.verifyAndApplyTo(target);
6276
if (valid !=VerifyChunk.OK) {
63-
conflictOutput.processConflict(valid,delta,result);
77+
conflictOutput.processConflict(valid,delta,target);
6478
}
6579
}
66-
returnresult;
6780
}
6881

6982
privatestaticclassPatchApplyingContext<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 targetthe given target
226-
* @return the restoredtext
239+
* @param targetThe list to copy and apply changes to.
240+
* @returnA new list, containingthe restoredstate.
227241
*/
228242
publicList<T>restore(List<T>target) {
229243
List<T>result =newArrayList<>(target);
244+
restoreToExisting(result);
245+
returnresult;
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+
publicvoidrestoreToExisting(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-
returnresult;
236263
}
237264

238265
/**

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp