Skip to content

Commit fe364c5

Browse files
committed
cleanup
1 parent 3f258e2 commit fe364c5

File tree

3 files changed

+34
-30
lines changed

3 files changed

+34
-30
lines changed

src/main/java/difflib/DiffUtils.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Collections;
3030
import java.util.LinkedList;
3131
import java.util.List;
32+
import java.util.Objects;
3233
import static java.util.stream.Collectors.joining;
3334

3435
/**
@@ -85,15 +86,10 @@ public static <T> Patch<T> diff(List<T> original, List<T> revised,
8586
*/
8687
public static <T> Patch<T> diff(List<T> original, List<T> revised,
8788
DiffAlgorithm<T> algorithm) throws DiffException {
88-
if (original == null) {
89-
throw new IllegalArgumentException("original must not be null");
90-
}
91-
if (revised == null) {
92-
throw new IllegalArgumentException("revised must not be null");
93-
}
94-
if (algorithm == null) {
95-
throw new IllegalArgumentException("algorithm must not be null");
96-
}
89+
Objects.requireNonNull(original,"original must not be null");
90+
Objects.requireNonNull(revised,"revised must not be null");
91+
Objects.requireNonNull(algorithm,"algorithm must not be null");
92+
9793
return algorithm.diff(original, revised);
9894
}
9995

src/main/java/difflib/algorithm/myers/MyersDiff.java

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.ArrayList;
3333
import java.util.Arrays;
3434
import java.util.List;
35+
import java.util.Objects;
3536

3637
/**
3738
* A clean-room implementation of <a href="http://www.cs.arizona.edu/people/gene/">
@@ -97,12 +98,9 @@ public Patch<T> diff(final T[] original, final T[] revised) throws DiffException
9798
*/
9899
@Override
99100
public Patch<T> diff(final List<T> original, final List<T> revised) throws DiffException {
100-
if (original == null) {
101-
throw new IllegalArgumentException("original list must not be null");
102-
}
103-
if (revised == null) {
104-
throw new IllegalArgumentException("revised list must not be null");
105-
}
101+
Objects.requireNonNull(original, "original list must not be null");
102+
Objects.requireNonNull(revised, "revised list must not be null");
103+
106104
PathNode path = buildPath(original, revised);
107105
return buildRevision(path, original, revised);
108106
}
@@ -118,12 +116,8 @@ public Patch<T> diff(final List<T> original, final List<T> revised) throws DiffE
118116
*/
119117
private PathNode buildPath(final List<T> orig, final List<T> rev)
120118
throws DifferentiationFailedException {
121-
if (orig == null) {
122-
throw new IllegalArgumentException("original sequence is null");
123-
}
124-
if (rev == null) {
125-
throw new IllegalArgumentException("revised sequence is null");
126-
}
119+
Objects.requireNonNull(orig, "original sequence is null");
120+
Objects.requireNonNull(rev, "revised sequence is null");
127121

128122
// these are local constants
129123
final int N = orig.size();
@@ -192,15 +186,9 @@ private PathNode buildPath(final List<T> orig, final List<T> rev)
192186
* path.
193187
*/
194188
private Patch<T> buildRevision(PathNode path, List<T> orig, List<T> rev) {
195-
if (path == null) {
196-
throw new IllegalArgumentException("path is null");
197-
}
198-
if (orig == null) {
199-
throw new IllegalArgumentException("original sequence is null");
200-
}
201-
if (rev == null) {
202-
throw new IllegalArgumentException("revised sequence is null");
203-
}
189+
Objects.requireNonNull(path, "path is null");
190+
Objects.requireNonNull(orig, "original sequence is null");
191+
Objects.requireNonNull(rev, "revised sequence is null");
204192

205193
Patch<T> patch = new Patch<>();
206194
if (path.isSnake()) {

src/test/java/difflib/text/DiffRowGeneratorTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,24 @@ public void testSplitString3() {
187187
assertEquals(4, list.size());
188188
assertEquals("[test, ,, test2, ,]", list.toString());
189189
}
190+
191+
192+
@Test
193+
public void testGeneratorExample1() throws DiffException {
194+
DiffRowGenerator generator = DiffRowGenerator.create()
195+
.showInlineDiffs(true)
196+
.mergeOriginalRevised(true)
197+
.inlineDiffByWord(true)
198+
.oldTag(f -> "~")
199+
.newTag(f -> "**")
200+
.build();
201+
List<DiffRow> rows = generator.generateDiffRows(
202+
Arrays.asList("This is a test senctence."),
203+
Arrays.asList("This is a test for diffutils."));
204+
205+
System.out.println(rows.get(0).getOldLine());
206+
207+
assertEquals(1, rows.size());
208+
assertEquals("This is a test ~senctence~**for diffutils**.", rows.get(0).getOldLine());
209+
}
190210
}

0 commit comments

Comments
 (0)