24
24
import com .github .difflib .patch .Patch ;
25
25
import com .github .difflib .text .DiffRow .Tag ;
26
26
import java .util .*;
27
+ import java .util .function .BiFunction ;
27
28
import java .util .function .BiPredicate ;
28
29
import java .util .function .Function ;
29
30
import java .util .regex .Matcher ;
@@ -106,7 +107,7 @@ protected final static List<String> splitStringPreserveDelimiter(String str, Pat
106
107
* @param tagGenerator the tag generator
107
108
*/
108
109
static void wrapInTag (List <String > sequence , int startPosition ,
109
- int endPosition , Function < Boolean , String > tagGenerator ,
110
+ int endPosition , Tag tag , BiFunction < Tag , Boolean , String > tagGenerator ,
110
111
Function <String , String > processDiffs ) {
111
112
int endPos = endPosition ;
112
113
@@ -124,7 +125,7 @@ static void wrapInTag(List<String> sequence, int startPosition,
124
125
break ;
125
126
}
126
127
127
- sequence .add (endPos , tagGenerator .apply (false ));
128
+ sequence .add (endPos , tagGenerator .apply (tag , false ));
128
129
if (processDiffs != null ) {
129
130
sequence .set (endPos - 1 ,
130
131
processDiffs .apply (sequence .get (endPos - 1 )));
@@ -143,7 +144,7 @@ static void wrapInTag(List<String> sequence, int startPosition,
143
144
endPos --;
144
145
}
145
146
146
- sequence .add (endPos , tagGenerator .apply (true ));
147
+ sequence .add (endPos , tagGenerator .apply (tag , true ));
147
148
endPos --;
148
149
}
149
150
}
@@ -153,8 +154,8 @@ static void wrapInTag(List<String> sequence, int startPosition,
153
154
private final boolean ignoreWhiteSpaces ;
154
155
private final Function <String , List <String >> inlineDiffSplitter ;
155
156
private final boolean mergeOriginalRevised ;
156
- private final Function < Boolean , String > newTag ;
157
- private final Function < Boolean , String > oldTag ;
157
+ private final BiFunction < Tag , Boolean , String > newTag ;
158
+ private final BiFunction < Tag , Boolean , String > oldTag ;
158
159
private final boolean reportLinesUnchanged ;
159
160
private final Function <String , String > lineNormalizer ;
160
161
private final Function <String , String > processDiffs ;
@@ -169,7 +170,13 @@ private DiffRowGenerator(Builder builder) {
169
170
columnWidth = builder .columnWidth ;
170
171
mergeOriginalRevised = builder .mergeOriginalRevised ;
171
172
inlineDiffSplitter = builder .inlineDiffSplitter ;
172
- equalizer = ignoreWhiteSpaces ? IGNORE_WHITESPACE_EQUALIZER : DEFAULT_EQUALIZER ;
173
+
174
+ if (builder .equalizer != null ) {
175
+ equalizer = builder .equalizer ;
176
+ } else {
177
+ equalizer = ignoreWhiteSpaces ? IGNORE_WHITESPACE_EQUALIZER : DEFAULT_EQUALIZER ;
178
+ }
179
+
173
180
reportLinesUnchanged = builder .reportLinesUnchanged ;
174
181
lineNormalizer = builder .lineNormalizer ;
175
182
processDiffs = builder .processDiffs ;
@@ -254,15 +261,15 @@ private DiffRow buildDiffRow(Tag type, String orgline, String newline) {
254
261
String wrapOrg = preprocessLine (orgline );
255
262
if (Tag .DELETE == type ) {
256
263
if (mergeOriginalRevised || showInlineDiffs ) {
257
- wrapOrg = oldTag .apply (true ) + wrapOrg + oldTag .apply (false );
264
+ wrapOrg = oldTag .apply (type , true ) + wrapOrg + oldTag .apply (type , false );
258
265
}
259
266
}
260
267
String wrapNew = preprocessLine (newline );
261
268
if (Tag .INSERT == type ) {
262
269
if (mergeOriginalRevised ) {
263
- wrapOrg = newTag .apply (true ) + wrapNew + newTag .apply (false );
270
+ wrapOrg = newTag .apply (type , true ) + wrapNew + newTag .apply (type , false );
264
271
} else if (showInlineDiffs ) {
265
- wrapNew = newTag .apply (true ) + wrapNew + newTag .apply (false );
272
+ wrapNew = newTag .apply (type , true ) + wrapNew + newTag .apply (type , false );
266
273
}
267
274
}
268
275
return new DiffRow (type , wrapOrg , wrapNew );
@@ -308,19 +315,19 @@ private List<DiffRow> generateInlineDiffs(AbstractDelta<String> delta) {
308
315
if (inlineDelta instanceof DeleteDelta ) {
309
316
wrapInTag (origList , inlineOrig .getPosition (), inlineOrig
310
317
.getPosition ()
311
- + inlineOrig .size (), oldTag , processDiffs );
318
+ + inlineOrig .size (), Tag . DELETE , oldTag , processDiffs );
312
319
} else if (inlineDelta instanceof InsertDelta ) {
313
320
if (mergeOriginalRevised ) {
314
321
origList .addAll (inlineOrig .getPosition (),
315
322
revList .subList (inlineRev .getPosition (),
316
323
inlineRev .getPosition () + inlineRev .size ()));
317
324
wrapInTag (origList , inlineOrig .getPosition (),
318
325
inlineOrig .getPosition () + inlineRev .size (),
319
- newTag , processDiffs );
326
+ Tag . INSERT , newTag , processDiffs );
320
327
} else {
321
328
wrapInTag (revList , inlineRev .getPosition (),
322
329
inlineRev .getPosition () + inlineRev .size (),
323
- newTag , processDiffs );
330
+ Tag . INSERT , newTag , processDiffs );
324
331
}
325
332
} else if (inlineDelta instanceof ChangeDelta ) {
326
333
if (mergeOriginalRevised ) {
@@ -329,15 +336,15 @@ private List<DiffRow> generateInlineDiffs(AbstractDelta<String> delta) {
329
336
inlineRev .getPosition () + inlineRev .size ()));
330
337
wrapInTag (origList , inlineOrig .getPosition () + inlineOrig .size (),
331
338
inlineOrig .getPosition () + inlineOrig .size () + inlineRev .size (),
332
- newTag , processDiffs );
339
+ Tag . CHANGE , newTag , processDiffs );
333
340
} else {
334
341
wrapInTag (revList , inlineRev .getPosition (),
335
342
inlineRev .getPosition () + inlineRev .size (),
336
- newTag , processDiffs );
343
+ Tag . CHANGE , newTag , processDiffs );
337
344
}
338
345
wrapInTag (origList , inlineOrig .getPosition (),
339
346
inlineOrig .getPosition () + inlineOrig .size (),
340
- oldTag , processDiffs );
347
+ Tag . CHANGE , oldTag , processDiffs );
341
348
}
342
349
}
343
350
StringBuilder origResult = new StringBuilder ();
@@ -380,15 +387,18 @@ public static class Builder {
380
387
private boolean showInlineDiffs = false ;
381
388
private boolean ignoreWhiteSpaces = false ;
382
389
383
- private Function <Boolean , String > oldTag = f -> f ? "<span class=\" editOldInline\" >" : "</span>" ;
384
- private Function <Boolean , String > newTag = f -> f ? "<span class=\" editNewInline\" >" : "</span>" ;
390
+ private BiFunction <Tag , Boolean , String > oldTag =
391
+ (tag , f ) -> f ? "<span class=\" editOldInline\" >" : "</span>" ;
392
+ private BiFunction <Tag , Boolean , String > newTag =
393
+ (tag , f ) -> f ? "<span class=\" editNewInline\" >" : "</span>" ;
385
394
386
395
private int columnWidth = 0 ;
387
396
private boolean mergeOriginalRevised = false ;
388
397
private boolean reportLinesUnchanged = false ;
389
398
private Function <String , List <String >> inlineDiffSplitter = SPLITTER_BY_CHARACTER ;
390
399
private Function <String , String > lineNormalizer = LINE_NORMALIZER_FOR_HTML ;
391
400
private Function <String , String > processDiffs = null ;
401
+ private BiPredicate <String , String > equalizer = null ;
392
402
393
403
private Builder () {
394
404
}
@@ -433,21 +443,43 @@ public Builder reportLinesUnchanged(final boolean val) {
433
443
* @param generator the tag generator
434
444
* @return builder with configured ignoreBlankLines parameter
435
445
*/
436
- public Builder oldTag (Function < Boolean , String > generator ) {
446
+ public Builder oldTag (BiFunction < Tag , Boolean , String > generator ) {
437
447
this .oldTag = generator ;
438
448
return this ;
439
449
}
450
+
451
+ /**
452
+ * Generator for Old-Text-Tags.
453
+ *
454
+ * @param generator the tag generator
455
+ * @return builder with configured ignoreBlankLines parameter
456
+ */
457
+ public Builder oldTag (Function <Boolean , String > generator ) {
458
+ this .oldTag = (tag , f ) -> generator .apply (f );
459
+ return this ;
460
+ }
440
461
441
462
/**
442
463
* Generator for New-Text-Tags.
443
464
*
444
465
* @param generator
445
466
* @return
446
467
*/
447
- public Builder newTag (Function < Boolean , String > generator ) {
468
+ public Builder newTag (BiFunction < Tag , Boolean , String > generator ) {
448
469
this .newTag = generator ;
449
470
return this ;
450
471
}
472
+
473
+ /**
474
+ * Generator for New-Text-Tags.
475
+ *
476
+ * @param generator
477
+ * @return
478
+ */
479
+ public Builder newTag (Function <Boolean , String > generator ) {
480
+ this .newTag = (tag , f ) -> generator .apply (f );
481
+ return this ;
482
+ }
451
483
452
484
/**
453
485
* Processor for diffed text parts. Here e.g. whitecharacters could be replaced by something
@@ -534,5 +566,16 @@ public Builder lineNormalizer(Function<String, String> lineNormalizer) {
534
566
this .lineNormalizer = lineNormalizer ;
535
567
return this ;
536
568
}
569
+
570
+ /**
571
+ * Provide an equalizer for diff processing.
572
+ *
573
+ * @param equalizer equalizer for diff processing.
574
+ * @return builder with configured equalizer parameter
575
+ */
576
+ public Builder equalizer (BiPredicate <String , String > equalizer ) {
577
+ this .equalizer = equalizer ;
578
+ return this ;
579
+ }
537
580
}
538
581
}
0 commit comments