mirror of
https://github.com/java-diff-utils/java-diff-utils.git
synced 2026-03-13 10:11:17 +08:00
cleanup
This commit is contained in:
@@ -29,6 +29,7 @@ import difflib.patch.PatchFailedException;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import static java.util.stream.Collectors.joining;
|
||||
|
||||
/**
|
||||
@@ -85,15 +86,10 @@ public final class DiffUtils {
|
||||
*/
|
||||
public static <T> Patch<T> diff(List<T> original, List<T> revised,
|
||||
DiffAlgorithm<T> algorithm) throws DiffException {
|
||||
if (original == null) {
|
||||
throw new IllegalArgumentException("original must not be null");
|
||||
}
|
||||
if (revised == null) {
|
||||
throw new IllegalArgumentException("revised must not be null");
|
||||
}
|
||||
if (algorithm == null) {
|
||||
throw new IllegalArgumentException("algorithm must not be null");
|
||||
}
|
||||
Objects.requireNonNull(original,"original must not be null");
|
||||
Objects.requireNonNull(revised,"revised must not be null");
|
||||
Objects.requireNonNull(algorithm,"algorithm must not be null");
|
||||
|
||||
return algorithm.diff(original, revised);
|
||||
}
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@ import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* A clean-room implementation of <a href="http://www.cs.arizona.edu/people/gene/">
|
||||
@@ -97,12 +98,9 @@ public final class MyersDiff<T> implements DiffAlgorithm<T> {
|
||||
*/
|
||||
@Override
|
||||
public Patch<T> diff(final List<T> original, final List<T> revised) throws DiffException {
|
||||
if (original == null) {
|
||||
throw new IllegalArgumentException("original list must not be null");
|
||||
}
|
||||
if (revised == null) {
|
||||
throw new IllegalArgumentException("revised list must not be null");
|
||||
}
|
||||
Objects.requireNonNull(original, "original list must not be null");
|
||||
Objects.requireNonNull(revised, "revised list must not be null");
|
||||
|
||||
PathNode path = buildPath(original, revised);
|
||||
return buildRevision(path, original, revised);
|
||||
}
|
||||
@@ -118,12 +116,8 @@ public final class MyersDiff<T> implements DiffAlgorithm<T> {
|
||||
*/
|
||||
private PathNode buildPath(final List<T> orig, final List<T> rev)
|
||||
throws DifferentiationFailedException {
|
||||
if (orig == null) {
|
||||
throw new IllegalArgumentException("original sequence is null");
|
||||
}
|
||||
if (rev == null) {
|
||||
throw new IllegalArgumentException("revised sequence is null");
|
||||
}
|
||||
Objects.requireNonNull(orig, "original sequence is null");
|
||||
Objects.requireNonNull(rev, "revised sequence is null");
|
||||
|
||||
// these are local constants
|
||||
final int N = orig.size();
|
||||
@@ -192,15 +186,9 @@ public final class MyersDiff<T> implements DiffAlgorithm<T> {
|
||||
* path.
|
||||
*/
|
||||
private Patch<T> buildRevision(PathNode path, List<T> orig, List<T> rev) {
|
||||
if (path == null) {
|
||||
throw new IllegalArgumentException("path is null");
|
||||
}
|
||||
if (orig == null) {
|
||||
throw new IllegalArgumentException("original sequence is null");
|
||||
}
|
||||
if (rev == null) {
|
||||
throw new IllegalArgumentException("revised sequence is null");
|
||||
}
|
||||
Objects.requireNonNull(path, "path is null");
|
||||
Objects.requireNonNull(orig, "original sequence is null");
|
||||
Objects.requireNonNull(rev, "revised sequence is null");
|
||||
|
||||
Patch<T> patch = new Patch<>();
|
||||
if (path.isSnake()) {
|
||||
|
||||
@@ -187,4 +187,24 @@ public class DiffRowGeneratorTest {
|
||||
assertEquals(4, list.size());
|
||||
assertEquals("[test, ,, test2, ,]", list.toString());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testGeneratorExample1() throws DiffException {
|
||||
DiffRowGenerator generator = DiffRowGenerator.create()
|
||||
.showInlineDiffs(true)
|
||||
.mergeOriginalRevised(true)
|
||||
.inlineDiffByWord(true)
|
||||
.oldTag(f -> "~")
|
||||
.newTag(f -> "**")
|
||||
.build();
|
||||
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());
|
||||
|
||||
assertEquals(1, rows.size());
|
||||
assertEquals("This is a test ~senctence~**for diffutils**.", rows.get(0).getOldLine());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user