You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Tobias edited this page Mar 7, 2020
·
11 revisions
All examples are part of the tests sourcecode at diffutils/examples or are simply tests. The examples are reduced to present the interesting parts of it.
Compute the difference between two files and print its deltas
File: ComputeDifference.java
//build simple lists of the lines of the two testfilesList<String> original = Files.readAllLines(newFile(ORIGINAL).toPath());
List<String> revised = Files.readAllLines(newFile(RIVISED).toPath());
//compute the patch: this is the diffutils partPatch<String> patch = DiffUtils.diff(original, revised);
//simple output the computed patch to consolefor (AbstractDelta<String> delta : patch.getDeltas()) {
System.out.println(delta);
}
Get the file in unified format and apply it as the patch to given text
File: ApplyPatch.java
List<String> original = Files.readAllLines(newFile(ORIGINAL).toPath());
List<String> patched = Files.readAllLines(newFile(PATCH).toPath());
// At first, parse the unified diff file and get the patchPatch<String> patch = UnifiedDiffUtils.parseUnifiedDiff(patched);
// Then apply the computed patch to the given textList<String> result = DiffUtils.patch(original, patch);
//simple output to consoleSystem.out.println(result);
Generate a file in unified diff format import it and apply the patch
List<String> text1=Arrays.asList("this is a test","a test");
List<String> text2=Arrays.asList("this is a testfile","a test");
//generating diff information.Patch<String> diff = DiffUtils.diff(text1, text2);
//generating unified diff formatList<String> unifiedDiff = UnifiedDiffUtils.generateUnifiedDiff("original-file.txt", "new-file.txt", text1, diff, 0);
unifiedDiff.forEach(System.out::println);
//importing unified diff format from file or here from memory to a PatchPatch<String> importedPatch = UnifiedDiffUtils.parseUnifiedDiff(unifiedDiff);
//apply patch to original listList<String> patchedText = DiffUtils.patch(text1, importedPatch);
System.out.println(patchedText);
Compute the difference between two texts and print it in human-readable markup style
one liner
Test: DiffRowGeneratorTest.testGeneratorExample1
The DiffRowGenerator does the main part in producing readable texts. Its instantiated using a builder pattern.
//create a configured DiffRowGeneratorDiffRowGeneratorgenerator = DiffRowGenerator.create()
.showInlineDiffs(true)
.mergeOriginalRevised(true)
.inlineDiffByWord(true)
.oldTag(f -> "~") //introduce markdown style for strikethrough
.newTag(f -> "**") //introduce markdown style for bold
.build();
//compute the differences for two test texts.List<DiffRow> rows = generator.generateDiffRows(
Arrays.asList("This is a test senctence."),
Arrays.asList("This is a test for diffutils."));
System.out.println(rows.get(0).getOldLine());
output is:
This is a test senctencefor diffutils.
multi liner
Test: DiffRowGeneratorTest.testGeneratorExample2
The DiffRowGenerator does the main part in producing readable texts. Its instantiated using a builder pattern.
DiffRowGeneratorgenerator = DiffRowGenerator.create()
.showInlineDiffs(true)
.inlineDiffByWord(true)
.oldTag(f -> "~")
.newTag(f -> "**")
.build();
List<DiffRow> rows = generator.generateDiffRows(
Arrays.asList("This is a test senctence.", "This is the second line.", "And here is the finish."),
Arrays.asList("This is a test for diffutils.", "This is the second line."));
System.out.println("|original|new|");
System.out.println("|--------|---|");
for (DiffRowrow : rows) {
System.out.println("|" + row.getOldLine() + "|" + row.getNewLine() + "|");
}
output is:
original
new
This is a test senctence.
This is a test for diffutils.
This is the second line.
This is the second line.
And here is the finish.
Compute the difference between two non textual inputs
The difference computing part is generified. Therefore a List of objects could be used. Here is an example,
that demonstrates a patch for an Integer List.
Test: DiffUtilsTest.testDiffIntegerList