Skip to content

Commit e791091

Browse files
committed
Add generateOriginalAndDiff method and test class.I split long code into short methods, changed ambiguous variable names to explicit variable names
1 parent cb2cc6f commit e791091

2 files changed

Lines changed: 25 additions & 19 deletions

File tree

java-diff-utils/src/main/java/com/github/difflib/UnifiedDiffUtils.java

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
import com.github.difflib.patch.AbstractDelta;
2121
import com.github.difflib.patch.Patch;
2222

23-
import java.util.*;
23+
import java.util.ArrayList;
24+
import java.util.HashMap;
25+
import java.util.List;
26+
import java.util.Map;
27+
import java.util.Optional;
2428
import java.util.regex.Matcher;
2529
import java.util.regex.Pattern;
2630
import java.util.stream.Collectors;
@@ -351,44 +355,50 @@ public static List<String> generateOriginalAndDiff(List<String> original, List<S
351355
revisedFileName = revisedFileName == null ? "revised" : revisedFileName;
352356
Patch<String> patch = com.github.difflib.DiffUtils.diff(original, revised);
353357
List<String> unifiedDiff = generateUnifiedDiff(originalFileName, revisedFileName, original, patch, 0);
354-
if (unifiedDiff.size() == 0) {
358+
if (unifiedDiff.isEmpty()) {
355359
unifiedDiff.add("--- " + originalFileName);
356360
unifiedDiff.add("+++ " + revisedFileName);
357361
unifiedDiff.add("@@ -0,0 +0,0 @@");
358362
} else if (unifiedDiff.size() >= 3 && !unifiedDiff.get(2).contains("@@ -1,")) {
359363
unifiedDiff.set(1, unifiedDiff.get(1));
360364
unifiedDiff.add(2, "@@ -0,0 +0,0 @@");
361365
}
362-
List<String> original1 = original.stream().map(v -> " " + v).collect(Collectors.toList());
363-
return insertOrig(original1, unifiedDiff);
366+
List<String> originalWithPrefix = original.stream().map(v -> " " + v).collect(Collectors.toList());
367+
return insertOrig(originalWithPrefix, unifiedDiff);
364368
}
365369

366370

367371
//Insert the diff format to the original file
368372
private static List<String> insertOrig(List<String> original, List<String> unifiedDiff) {
369373
List<String> result = new ArrayList<>();
370374
List<List<String>> diffList = new ArrayList<>();
371-
List<String> d = new ArrayList<>();
375+
List<String> diff = new ArrayList<>();
372376
for (int i = 0; i < unifiedDiff.size(); i++) {
373377
String u = unifiedDiff.get(i);
374378
if (u.startsWith("@@") && !"@@ -0,0 +0,0 @@".equals(u) && !u.contains("@@ -1,")) {
375379
List<String> twoList = new ArrayList<>();
376-
twoList.addAll(d);
380+
twoList.addAll(diff);
377381
diffList.add(twoList);
378-
d.clear();
379-
d.add(u);
382+
diff.clear();
383+
diff.add(u);
380384
continue;
381385
}
382386
if (i == unifiedDiff.size() - 1) {
383-
d.add(u);
387+
diff.add(u);
384388
List<String> twoList = new ArrayList<>();
385-
twoList.addAll(d);
389+
twoList.addAll(diff);
386390
diffList.add(twoList);
387-
d.clear();
391+
diff.clear();
388392
break;
389393
}
390-
d.add(u);
394+
diff.add(u);
391395
}
396+
insertOrig(diffList,result,original);
397+
return result;
398+
}
399+
400+
//Insert the diff format to the original file
401+
private static void insertOrig(List<List<String>> diffList, List<String> result,List<String> original) {
392402
for (int i = 0; i < diffList.size(); i++) {
393403
List<String> diff = diffList.get(i);
394404
List<String> nexDiff = i == diffList.size() - 1 ? null : diffList.get(i + 1);
@@ -405,7 +415,6 @@ private static List<String> insertOrig(List<String> original, List<String> unifi
405415
int end = nexMap.get("revRow") - 2;
406416
insert(result, getOrigList(original, start, end));
407417
}
408-
409418
if (simb.contains("@@ -1,") && null == nexSimb && map.get("orgDel") != original.size()) {
410419
insert(result, getOrigList(original, 0, original.size() - 1));
411420
} else if (null == nexSimb && (map.get("orgRow") + map.get("orgDel") - 1) < original.size()) {
@@ -414,7 +423,6 @@ private static List<String> insertOrig(List<String> original, List<String> unifi
414423
insert(result, getOrigList(original, start, original.size() - 1));
415424
}
416425
}
417-
return result;
418426
}
419427

420428
//Insert the unchanged content in the source file into result
@@ -441,11 +449,11 @@ private static Map<String, Integer> getRowMap(String str) {
441449
}
442450

443451
//Get the specified part of the line from the original file
444-
private static List<String> getOrigList(List<String> original1, int start, int end) {
452+
private static List<String> getOrigList(List<String> originalWithPrefix, int start, int end) {
445453
List<String> list = new ArrayList<>();
446-
if (original1.size() >= 1 && start <= end && end < original1.size()) {
454+
if (originalWithPrefix.size() >= 1 && start <= end && end < originalWithPrefix.size()) {
447455
for (; start <= end; start++) {
448-
list.add(original1.get(start));
456+
list.add(originalWithPrefix.get(start));
449457
}
450458
}
451459
return list;

java-diff-utils/src/test/java/com/github/difflib/examples/OriginalAndDiffTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,4 @@ public static List<String> fileToLines(String filename) throws FileNotFoundExcep
3434
}
3535
return lines;
3636
}
37-
38-
3937
}

0 commit comments

Comments
 (0)