Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork201
Examples
All examples are part of the tests sourcecode atdiffutils/examples or are simply tests. The examples are reduced to present the interesting parts of it.
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);}
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);
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);
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 testsenctencefor diffutils.
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 | This is a testfor diffutils. |
This is the second line. | This is the second line. |
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 (Deltadelta :patch.getDeltas()) {System.out.println(delta);}
output is:
[DeleteDelta, position: 0, lines: [1]][ChangeDelta, position: 4, lines: [5] to [6]]