From 7cc8b4d41b878c0faec99d8a4977c11327c1bb16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Pflieger?= Date: Mon, 30 Sep 2019 11:20:59 +0200 Subject: [PATCH] Customize text/DiffRowGenerator with algorithm and equalizer Based on the possibilities of the DiffUtils itself, the DiffRowGenerator has been extended to provide the customizing of the underlying DiffUtils. Moreover the tagCreator (supplied by oldTag/newTag) is now a BiFunction, consuming new the type of the processed tag. --- .../github/difflib/text/DiffRowGenerator.java | 44 +++++++++++++------ .../difflib/text/DiffRowGeneratorTest.java | 32 +++++++------- 2 files changed, 47 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/github/difflib/text/DiffRowGenerator.java b/src/main/java/com/github/difflib/text/DiffRowGenerator.java index 4f963cd7..0a69aa1d 100644 --- a/src/main/java/com/github/difflib/text/DiffRowGenerator.java +++ b/src/main/java/com/github/difflib/text/DiffRowGenerator.java @@ -25,6 +25,7 @@ import com.github.difflib.patch.Patch; import com.github.difflib.text.DiffRow.Tag; import java.util.*; +import java.util.function.BiFunction; import java.util.function.BiPredicate; import java.util.function.Function; import java.util.regex.Matcher; @@ -106,7 +107,7 @@ protected final static List splitStringPreserveDelimiter(String str, Pat * @param tagGenerator the tag generator */ static void wrapInTag(List sequence, int startPosition, - int endPosition, Function tagGenerator) { + int endPosition, BiFunction tagGenerator) { int endPos = endPosition; while (endPos >= startPosition) { @@ -123,7 +124,7 @@ static void wrapInTag(List sequence, int startPosition, break; } - sequence.add(endPos, tagGenerator.apply(false)); + sequence.add(endPos, tagGenerator.apply(null, false)); endPos--; //search position for end tag @@ -134,7 +135,7 @@ static void wrapInTag(List sequence, int startPosition, endPos--; } - sequence.add(endPos, tagGenerator.apply(true)); + sequence.add(endPos, tagGenerator.apply(null, true)); endPos--; } @@ -146,8 +147,8 @@ static void wrapInTag(List sequence, int startPosition, private final boolean ignoreWhiteSpaces; private final Function> inlineDiffSplitter; private final boolean mergeOriginalRevised; - private final Function newTag; - private final Function oldTag; + private final BiFunction newTag; + private final BiFunction oldTag; private final boolean reportLinesUnchanged; private final Function lineNormalizer; @@ -161,7 +162,11 @@ private DiffRowGenerator(Builder builder) { columnWidth = builder.columnWidth; mergeOriginalRevised = builder.mergeOriginalRevised; inlineDiffSplitter = builder.inlineDiffSplitter; - equalizer = ignoreWhiteSpaces ? IGNORE_WHITESPACE_EQUALIZER : DEFAULT_EQUALIZER; + if (Objects.nonNull(builder.equalizer)) { + equalizer = builder.equalizer; + } else { + equalizer = ignoreWhiteSpaces ? IGNORE_WHITESPACE_EQUALIZER : DEFAULT_EQUALIZER; + } reportLinesUnchanged = builder.reportLinesUnchanged; lineNormalizer = builder.lineNormalizer; @@ -245,15 +250,15 @@ private DiffRow buildDiffRow(Tag type, String orgline, String newline) { String wrapOrg = preprocessLine(orgline); if (Tag.DELETE == type) { if (mergeOriginalRevised || showInlineDiffs) { - wrapOrg = oldTag.apply(true) + wrapOrg + oldTag.apply(false); + wrapOrg = oldTag.apply(type, true) + wrapOrg + oldTag.apply(type, false); } } String wrapNew = preprocessLine(newline); if (Tag.INSERT == type) { if (mergeOriginalRevised) { - wrapOrg = newTag.apply(true) + wrapNew + newTag.apply(false); + wrapOrg = newTag.apply(type, true) + wrapNew + newTag.apply(type, false); } else if (showInlineDiffs) { - wrapNew = newTag.apply(true) + wrapNew + newTag.apply(false); + wrapNew = newTag.apply(type, true) + wrapNew + newTag.apply(type, false); } } return new DiffRow(type, wrapOrg, wrapNew); @@ -367,8 +372,8 @@ public static class Builder { private boolean showInlineDiffs = false; private boolean ignoreWhiteSpaces = false; - private Function oldTag = f -> f ? "" : ""; - private Function newTag = f -> f ? "" : ""; + private BiFunction oldTag = (tag, isStart) -> isStart ? "" : ""; + private BiFunction newTag = (tag, isStart) -> isStart ? "" : ""; private int columnWidth = 0; private boolean mergeOriginalRevised = false; @@ -376,6 +381,8 @@ public static class Builder { private Function> inlineDiffSplitter = SPLITTER_BY_CHARACTER; private Function lineNormalizer = LINE_NORMALIZER_FOR_HTML; + private BiPredicate equalizer = null; + private Builder() { } @@ -419,7 +426,7 @@ public Builder reportLinesUnchanged(final boolean val) { * @param generator the tag generator * @return builder with configured ignoreBlankLines parameter */ - public Builder oldTag(Function generator) { + public Builder oldTag(BiFunction generator) { this.oldTag = generator; return this; } @@ -430,7 +437,7 @@ public Builder oldTag(Function generator) { * @param generator * @return */ - public Builder newTag(Function generator) { + public Builder newTag(BiFunction generator) { this.newTag = generator; return this; } @@ -505,5 +512,16 @@ public Builder lineNormalizer(Function lineNormalizer) { this.lineNormalizer = lineNormalizer; return this; } + + /** + * Provide an equalizer for diff processing. + * + * @param val equalizer for diff processing. + * @return builder with configured equalizer parameter + */ + public Builder equalizer(BiPredicate val) { + equalizer = val; + return this; + } } } diff --git a/src/test/java/com/github/difflib/text/DiffRowGeneratorTest.java b/src/test/java/com/github/difflib/text/DiffRowGeneratorTest.java index c667b251..ba6d0eae 100644 --- a/src/test/java/com/github/difflib/text/DiffRowGeneratorTest.java +++ b/src/test/java/com/github/difflib/text/DiffRowGeneratorTest.java @@ -225,8 +225,8 @@ public void testGeneratorExample1() throws DiffException { .showInlineDiffs(true) .mergeOriginalRevised(true) .inlineDiffByWord(true) - .oldTag(f -> "~") - .newTag(f -> "**") + .oldTag((tag, isStart) -> "~") + .newTag((tag, isStart) -> "**") .build(); List rows = generator.generateDiffRows( Arrays.asList("This is a test senctence."), @@ -243,8 +243,8 @@ public void testGeneratorExample2() throws DiffException { DiffRowGenerator generator = DiffRowGenerator.create() .showInlineDiffs(true) .inlineDiffByWord(true) - .oldTag(f -> "~") - .newTag(f -> "**") + .oldTag((tag, isStart) -> "~") + .newTag((tag, isStart) -> "**") .build(); List rows = generator.generateDiffRows( Arrays.asList("This is a test senctence.", "This is the second line.", "And here is the finish."), @@ -285,8 +285,8 @@ public void testGeneratorIssue14() throws DiffException { .showInlineDiffs(true) .mergeOriginalRevised(true) .inlineDiffBySplitter(line -> DiffRowGenerator.splitStringPreserveDelimiter(line, Pattern.compile(","))) - .oldTag(f -> "~") - .newTag(f -> "**") + .oldTag((tag, isStart) -> "~") + .newTag((tag, isStart) -> "**") .build(); List rows = generator.generateDiffRows( Arrays.asList("J. G. Feldstein, Chair"), @@ -304,8 +304,8 @@ public void testGeneratorIssue15() throws DiffException, IOException { .showInlineDiffs(true) //show the ~ ~ and ** ** symbols on each difference .inlineDiffByWord(true) //show the ~ ~ and ** ** around each different word instead of each letter //.reportLinesUnchanged(true) //experiment - .oldTag(f -> "~") - .newTag(f -> "**") + .oldTag((tag, isStart) -> "~") + .newTag((tag, isStart) -> "**") .build(); List listOne = Files.lines(new File("target/test-classes/mocks/issue15_1.txt").toPath()) @@ -332,8 +332,8 @@ public void testGeneratorIssue22() throws DiffException { DiffRowGenerator generator = DiffRowGenerator.create() .showInlineDiffs(true) .inlineDiffByWord(true) - .oldTag(f -> "~") - .newTag(f -> "**") + .oldTag((tag, isStart) -> "~") + .newTag((tag, isStart) -> "**") .build(); String aa = "This is a test senctence."; String bb = "This is a test for diffutils.\nThis is the second line."; @@ -356,8 +356,8 @@ public void testGeneratorIssue22_2() throws DiffException { DiffRowGenerator generator = DiffRowGenerator.create() .showInlineDiffs(true) .inlineDiffByWord(true) - .oldTag(f -> "~") - .newTag(f -> "**") + .oldTag((tag, isStart) -> "~") + .newTag((tag, isStart) -> "**") .build(); String aa = "This is a test for diffutils.\nThis is the second line."; String bb = "This is a test senctence."; @@ -374,8 +374,8 @@ public void testGeneratorIssue22_3() throws DiffException { DiffRowGenerator generator = DiffRowGenerator.create() .showInlineDiffs(true) .inlineDiffByWord(true) - .oldTag(f -> "~") - .newTag(f -> "**") + .oldTag((tag, isStart) -> "~") + .newTag((tag, isStart) -> "**") .build(); String aa = "This is a test senctence."; String bb = "This is a test for diffutils.\nThis is the second line.\nAnd one more."; @@ -411,8 +411,8 @@ public void testGenerationIssue44reportLinesUnchangedProblem() throws DiffExcept DiffRowGenerator generator = DiffRowGenerator.create() .showInlineDiffs(true) .reportLinesUnchanged(true) - .oldTag(f -> "~~") - .newTag(f -> "**") + .oldTag((tag, isStart) -> "~~") + .newTag((tag, isStart) -> "**") .build(); List rows = generator.generateDiffRows(Arrays.asList("
To do
"), Arrays.asList("
Done
")); assertEquals("[[CHANGE,
~~T~~o~~ do~~
,
**D**o**ne**
]]", rows.toString());