← Winnitude! Search  ·  Reader Mode (462 ms)
Links: Stay in Reader Retro Site Converter Retro Mode: OFF Original Page ↗
GITHUB · 2 LINKS

Examples

Repository navigation

Examples

Jump to bottom 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 testfiles
List<String> original = Files.readAllLines(new File(ORIGINAL).toPath());
List<String> revised = Files.readAllLines(new File(RIVISED).toPath());

//compute the patch: this is the diffutils part
Patch<String> patch = DiffUtils.diff(original, revised);

//simple output the computed patch to console
for (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(new File(ORIGINAL).toPath());
List<String> patched = Files.readAllLines(new File(PATCH).toPath());

// At first, parse the unified diff file and get the patch
Patch<String> patch = UnifiedDiffUtils.parseUnifiedDiff(patched);

// Then apply the computed patch to the given text
List<String> result = DiffUtils.patch(original, patch);

//simple output to console
System.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 format
List<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 Patch
Patch<String> importedPatch = UnifiedDiffUtils.parseUnifiedDiff(unifiedDiff);

//apply patch to original list
List<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 DiffRowGenerator
DiffRowGenerator generator = 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 senctence for diffutils .

multi liner

Test: DiffRowGeneratorTest.testGeneratorExample2

The DiffRowGenerator does the main part in producing readable texts. Its instantiated using a builder pattern.

DiffRowGenerator generator = 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 (DiffRow row : 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

List<Integer> original = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> revised = Arrays.asList(2, 3, 4, 6);

Patch<Integer> patch = DiffUtils.diff(original, revised);
        
for (Delta delta : patch.getDeltas()) {
    System.out.println(delta);
}

output is:

[DeleteDelta, position: 0, lines: [1]]
[ChangeDelta, position: 4, lines: [5] to [6]]

Clone this wiki locally