Top Description Inners Fields Constructors Methods
com.github.difflib.text

public final Class DiffRowGenerator

extends Object
Class Inheritance
Imports
com.github.difflib.DiffUtils, com.github.difflib.patch.AbstractDelta, .ChangeDelta, .Chunk, .DeleteDelta, .DeltaType, .InsertDelta, .Patch, com.github.difflib.text.DiffRow.Tag, java.util.*, java.util.function.BiFunction, .BiPredicate, .Function, java.util.regex.Matcher, .Pattern

This class for generating DiffRows for side-by-sidy view. You can customize the way of generating. For example, show inline diffs on not, ignoring white spaces or/and blank lines and so on. All parameters for generating are optional. If you do not specify them, the class will use the default values. These values are: showInlineDiffs = false; ignoreWhiteSpaces = true; ignoreBlankLines = true; ... For instantiating the DiffRowGenerator you should use the its builder. Like in example DiffRowGenerator generator = new DiffRowGenerator.Builder().showInlineDiffs(true). ignoreWhiteSpaces(true).columnWidth(100).build();

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
DiffRowGenerator.Builder

This class used for building the DiffRowGenerator.

Field Summary

Modifier and TypeField and Description
private final int
private final boolean
public static final BiPredicate<String, String>
private final BiPredicate<String, String>
public static final BiPredicate<String, String>
private final boolean
private final Function<String, List<String>>
public static final Function<String, String>
private final Function<String, String>
private final boolean
private final BiFunction<DiffRow.Tag, Boolean, String>
private final BiFunction<DiffRow.Tag, Boolean, String>
private final Function<String, String>
private final boolean
private final boolean
private final boolean
public static final Pattern
public static final Function<String, List<String>>
SPLITTER_BY_CHARACTER

Splitting lines by character to achieve char by char diff checking.

public static final Function<String, List<String>>
SPLITTER_BY_WORD

Splitting lines by word to achieve word by word diff checking.

public static final Pattern

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static String
private DiffRow
buildDiffRow(DiffRow.Tag type, String orgline, String newline)

private DiffRow
public static DiffRowGenerator.Builder
private List<AbstractDelta<String>>
decompressDeltas(AbstractDelta<String> delta)

Decompresses ChangeDeltas with different source and target size to a ChangeDelta with same size and a following InsertDelta or DeleteDelta.

public List<DiffRow>

Returns:

the DiffRows between original and revised texts
generateDiffRows
(List<String>
the original text
original
,
List<String>
the revised text
revised
)

Get the DiffRows describing the difference between original and revised texts using the given patch.

public List<DiffRow>

Returns:

the DiffRows between original and revised texts
generateDiffRows
(final List<String>
the original text
original
,
Patch<String>
the given patch
patch
)

Generates the DiffRows describing the difference between original and revised texts using the given patch.

private List<DiffRow>
generateInlineDiffs(AbstractDelta<String>
the given delta
delta
)

Add the inline diffs for given delta

pack-priv List<String>
private String
protected static final List<String>
private int
transformDeltaIntoDiffRow(final List<String> original, int endPos, List<DiffRow> diffRows, AbstractDelta<String> delta)

Transforms one patch delta into a DiffRow object.

pack-priv static void
wrapInTag(List<String> sequence, int
the position from which tag should start. The counting start from a zero.
startPosition
,
int
the position before which tag should should be closed.
endPosition
,
DiffRow.Tag tag, BiFunction<DiffRow.Tag, Boolean, String>
the tag generator
tagGenerator
,
Function<String, String> processDiffs, boolean replaceLinefeedWithSpace)

Wrap the elements in the sequence with the given tag

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

columnWidthback to summary
private final int columnWidth
decompressDeltasback to summary
private final boolean decompressDeltas
DEFAULT_EQUALIZERback to summary
public static final BiPredicate<String, String> DEFAULT_EQUALIZER
equalizerback to summary
private final BiPredicate<String, String> equalizer
IGNORE_WHITESPACE_EQUALIZERback to summary
public static final BiPredicate<String, String> IGNORE_WHITESPACE_EQUALIZER
ignoreWhiteSpacesback to summary
private final boolean ignoreWhiteSpaces
inlineDiffSplitterback to summary
private final Function<String, List<String>> inlineDiffSplitter
LINE_NORMALIZER_FOR_HTMLback to summary
public static final Function<String, String> LINE_NORMALIZER_FOR_HTML
lineNormalizerback to summary
private final Function<String, String> lineNormalizer
mergeOriginalRevisedback to summary
private final boolean mergeOriginalRevised
newTagback to summary
private final BiFunction<DiffRow.Tag, Boolean, String> newTag
oldTagback to summary
private final BiFunction<DiffRow.Tag, Boolean, String> oldTag
processDiffsback to summary
private final Function<String, String> processDiffs
replaceOriginalLinefeedInChangesWithSpacesback to summary
private final boolean replaceOriginalLinefeedInChangesWithSpaces
reportLinesUnchangedback to summary
private final boolean reportLinesUnchanged
showInlineDiffsback to summary
private final boolean showInlineDiffs
SPLIT_BY_WORD_PATTERNback to summary
public static final Pattern SPLIT_BY_WORD_PATTERN
SPLITTER_BY_CHARACTERback to summary
public static final Function<String, List<String>> SPLITTER_BY_CHARACTER

Splitting lines by character to achieve char by char diff checking.

SPLITTER_BY_WORDback to summary
public static final Function<String, List<String>> SPLITTER_BY_WORD

Splitting lines by word to achieve word by word diff checking.

WHITESPACE_PATTERNback to summary
public static final Pattern WHITESPACE_PATTERN

Constructor Detail

DiffRowGeneratorback to summary
private DiffRowGenerator(DiffRowGenerator.Builder builder)

Method Detail

adjustWhitespaceback to summary
private static String adjustWhitespace(String raw)
buildDiffRowback to summary
private DiffRow buildDiffRow(DiffRow.Tag type, String orgline, String newline)
buildDiffRowWithoutNormalizingback to summary
private DiffRow buildDiffRowWithoutNormalizing(DiffRow.Tag type, String orgline, String newline)
createback to summary
public static DiffRowGenerator.Builder create()
decompressDeltasback to summary
private List<AbstractDelta<String>> decompressDeltas(AbstractDelta<String> delta)

Decompresses ChangeDeltas with different source and target size to a ChangeDelta with same size and a following InsertDelta or DeleteDelta. With this problems of building DiffRows getting smaller.

generateDiffRowsback to summary
public List<DiffRow> generateDiffRows(List<String> original, List<String> revised)

Get the DiffRows describing the difference between original and revised texts using the given patch. Useful for displaying side-by-side diff.

Parameters
original:List<String>

the original text

revised:List<String>

the revised text

Returns:List<DiffRow>

the DiffRows between original and revised texts

generateDiffRowsback to summary
public List<DiffRow> generateDiffRows(final List<String> original, Patch<String> patch)

Generates the DiffRows describing the difference between original and revised texts using the given patch. Useful for displaying side-by-side diff.

Parameters
original:List<String>

the original text

patch:Patch<String>

the given patch

Returns:List<DiffRow>

the DiffRows between original and revised texts

generateInlineDiffsback to summary
private List<DiffRow> generateInlineDiffs(AbstractDelta<String> delta)

Add the inline diffs for given delta

Parameters
delta:AbstractDelta<String>

the given delta

normalizeLinesback to summary
pack-priv List<String> normalizeLines(List<String> list)
preprocessLineback to summary
private String preprocessLine(String line)
splitStringPreserveDelimiterback to summary
protected static final List<String> splitStringPreserveDelimiter(String str, Pattern SPLIT_PATTERN)
transformDeltaIntoDiffRowback to summary
private int transformDeltaIntoDiffRow(final List<String> original, int endPos, List<DiffRow> diffRows, AbstractDelta<String> delta)

Transforms one patch delta into a DiffRow object.

wrapInTagback to summary
pack-priv static void wrapInTag(List<String> sequence, int startPosition, int endPosition, DiffRow.Tag tag, BiFunction<DiffRow.Tag, Boolean, String> tagGenerator, Function<String, String> processDiffs, boolean replaceLinefeedWithSpace)

Wrap the elements in the sequence with the given tag

Parameters
startPosition:int

the position from which tag should start. The counting start from a zero.

endPosition:int

the position before which tag should should be closed.

tagGenerator:BiFunction<DiffRow.Tag, Boolean, String>

the tag generator

com.github.difflib.text back to summary

public Class DiffRowGenerator.Builder

extends Object
Class Inheritance

This class used for building the DiffRowGenerator.
Author
dmitry

Field Summary

Modifier and TypeField and Description
private int
private boolean
private BiPredicate<String, String>
private boolean
private Function<String, List<String>>
private Function<String, String>
private boolean
private BiFunction<DiffRow.Tag, Boolean, String>
private BiFunction<DiffRow.Tag, Boolean, String>
private Function<String, String>
private boolean
private boolean
private boolean

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public DiffRowGenerator

Returns:

the customized DiffRowGenerator
build
()

Build the DiffRowGenerator.

public DiffRowGenerator.Builder

Returns:

builder with config of column width
columnWidth
(int
the width to set. Making it < 0 doesn't make any sense. Default 80.
width
)

Set the column width of generated lines of original and revised texts.

public DiffRowGenerator.Builder
decompressDeltas(boolean decompressDeltas)

Deltas could be in a state, that would produce some unreasonable results within an inline diff.

public DiffRowGenerator.Builder

Returns:

builder with configured equalizer parameter
equalizer
(BiPredicate<String, String>
equalizer for diff processing.
equalizer
)

Provide an equalizer for diff processing.

public DiffRowGenerator.Builder

Returns:

builder with configured ignoreWhiteSpaces parameter
ignoreWhiteSpaces
(boolean
the value to set. Default: true.
val
)

Ignore white spaces in generating diff rows or not.

public DiffRowGenerator.Builder
inlineDiffBySplitter(Function<String, List<String>> inlineDiffSplitter)

To provide some customized splitting a splitter can be provided.

public DiffRowGenerator.Builder
inlineDiffByWord(boolean inlineDiffByWord)

Per default each character is separatly processed.

public DiffRowGenerator.Builder
lineNormalizer(Function<String, String> lineNormalizer)

By default DiffRowGenerator preprocesses lines for HTML output.

public DiffRowGenerator.Builder
mergeOriginalRevised(boolean mergeOriginalRevised)

Merge the complete result within the original text.

public DiffRowGenerator.Builder
newTag(BiFunction<DiffRow.Tag, Boolean, String> generator)

Generator for New-Text-Tags.

public DiffRowGenerator.Builder
newTag(Function<Boolean, String> generator)

Generator for New-Text-Tags.

public DiffRowGenerator.Builder

Returns:

builder with configured ignoreBlankLines parameter
oldTag
(BiFunction<DiffRow.Tag, Boolean, String>
the tag generator
generator
)

Generator for Old-Text-Tags.

public DiffRowGenerator.Builder

Returns:

builder with configured ignoreBlankLines parameter
oldTag
(Function<Boolean, String>
the tag generator
generator
)

Generator for Old-Text-Tags.

public DiffRowGenerator.Builder
processDiffs(Function<String, String> processDiffs)

Processor for diffed text parts.

public DiffRowGenerator.Builder
replaceOriginalLinefeedInChangesWithSpaces(boolean replace)

Sometimes it happens that a change contains multiple lines.

public DiffRowGenerator.Builder

Returns:

builder with configured reportLinesUnchanged parameter
reportLinesUnchanged
(final boolean
the value to set. Default: false.
val
)

Report all lines without markup on the old or new text.

public DiffRowGenerator.Builder

Returns:

builder with configured showInlineDiff parameter
showInlineDiffs
(boolean
the value to set. Default: false.
val
)

Show inline diffs in generating diff rows or not.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

columnWidthback to summary
private int columnWidth
decompressDeltasback to summary
private boolean decompressDeltas
equalizerback to summary
private BiPredicate<String, String> equalizer
ignoreWhiteSpacesback to summary
private boolean ignoreWhiteSpaces
inlineDiffSplitterback to summary
private Function<String, List<String>> inlineDiffSplitter
lineNormalizerback to summary
private Function<String, String> lineNormalizer
mergeOriginalRevisedback to summary
private boolean mergeOriginalRevised
newTagback to summary
private BiFunction<DiffRow.Tag, Boolean, String> newTag
oldTagback to summary
private BiFunction<DiffRow.Tag, Boolean, String> oldTag
processDiffsback to summary
private Function<String, String> processDiffs
replaceOriginalLinefeedInChangesWithSpacesback to summary
private boolean replaceOriginalLinefeedInChangesWithSpaces
reportLinesUnchangedback to summary
private boolean reportLinesUnchanged
showInlineDiffsback to summary
private boolean showInlineDiffs

Constructor Detail

Builderback to summary
private Builder()

Method Detail

buildback to summary
public DiffRowGenerator build()

Build the DiffRowGenerator. If some parameters is not set, the default values are used.

Returns:DiffRowGenerator

the customized DiffRowGenerator

columnWidthback to summary
public DiffRowGenerator.Builder columnWidth(int width)

Set the column width of generated lines of original and revised texts.

Parameters
width:int

the width to set. Making it < 0 doesn't make any sense. Default 80.

Returns:DiffRowGenerator.Builder

builder with config of column width

decompressDeltasback to summary
public DiffRowGenerator.Builder decompressDeltas(boolean decompressDeltas)

Deltas could be in a state, that would produce some unreasonable results within an inline diff. So the deltas are decompressed into smaller parts and rebuild. But this could result in more differences.

equalizerback to summary
public DiffRowGenerator.Builder equalizer(BiPredicate<String, String> equalizer)

Provide an equalizer for diff processing.

Parameters
equalizer:BiPredicate<String, String>

equalizer for diff processing.

Returns:DiffRowGenerator.Builder

builder with configured equalizer parameter

ignoreWhiteSpacesback to summary
public DiffRowGenerator.Builder ignoreWhiteSpaces(boolean val)

Ignore white spaces in generating diff rows or not.

Parameters
val:boolean

the value to set. Default: true.

Returns:DiffRowGenerator.Builder

builder with configured ignoreWhiteSpaces parameter

inlineDiffBySplitterback to summary
public DiffRowGenerator.Builder inlineDiffBySplitter(Function<String, List<String>> inlineDiffSplitter)

To provide some customized splitting a splitter can be provided. Here someone could think about sentence splitter, comma splitter or stuff like that.

inlineDiffByWordback to summary
public DiffRowGenerator.Builder inlineDiffByWord(boolean inlineDiffByWord)

Per default each character is separatly processed. This variant introduces processing by word, which does not deliver in word changes. Therefore the whole word will be tagged as changed:

false:    (aBa : aba) --  changed: a(B)a : a(b)a
true:     (aBa : aba) --  changed: (aBa) : (aba)
lineNormalizerback to summary
public DiffRowGenerator.Builder lineNormalizer(Function<String, String> lineNormalizer)

By default DiffRowGenerator preprocesses lines for HTML output. Tabs and special HTML characters like "<" are replaced with its encoded value. To change this you can provide a customized line normalizer here.

mergeOriginalRevisedback to summary
public DiffRowGenerator.Builder mergeOriginalRevised(boolean mergeOriginalRevised)

Merge the complete result within the original text. This makes sense for one line display.

newTagback to summary
public DiffRowGenerator.Builder newTag(BiFunction<DiffRow.Tag, Boolean, String> generator)

Generator for New-Text-Tags.

newTagback to summary
public DiffRowGenerator.Builder newTag(Function<Boolean, String> generator)

Generator for New-Text-Tags.

oldTagback to summary
public DiffRowGenerator.Builder oldTag(BiFunction<DiffRow.Tag, Boolean, String> generator)

Generator for Old-Text-Tags.

Parameters
generator:BiFunction<DiffRow.Tag, Boolean, String>

the tag generator

Returns:DiffRowGenerator.Builder

builder with configured ignoreBlankLines parameter

oldTagback to summary
public DiffRowGenerator.Builder oldTag(Function<Boolean, String> generator)

Generator for Old-Text-Tags.

Parameters
generator:Function<Boolean, String>

the tag generator

Returns:DiffRowGenerator.Builder

builder with configured ignoreBlankLines parameter

processDiffsback to summary
public DiffRowGenerator.Builder processDiffs(Function<String, String> processDiffs)

Processor for diffed text parts. Here e.g. whitecharacters could be replaced by something visible.

replaceOriginalLinefeedInChangesWithSpacesback to summary
public DiffRowGenerator.Builder replaceOriginalLinefeedInChangesWithSpaces(boolean replace)

Sometimes it happens that a change contains multiple lines. If there is no correspondence in old and new. To keep the merged line more readable the linefeeds could be replaced by spaces.

reportLinesUnchangedback to summary
public DiffRowGenerator.Builder reportLinesUnchanged(final boolean val)

Report all lines without markup on the old or new text.

Parameters
val:boolean

the value to set. Default: false.

Returns:DiffRowGenerator.Builder

builder with configured reportLinesUnchanged parameter

showInlineDiffsback to summary
public DiffRowGenerator.Builder showInlineDiffs(boolean val)

Show inline diffs in generating diff rows or not.

Parameters
val:boolean

the value to set. Default: false.

Returns:DiffRowGenerator.Builder

builder with configured showInlineDiff parameter