* 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();
@@ -63,123 +62,17 @@ public class DiffRowGenerator {
private final boolean reportLinesUnchanged;
/**
- * This class used for building the DiffRowGenerator.
- *
- * @author dmitry
+ * Wrap the elements in the sequence with the given tag
*
+ * @param sequence
+ * @param startPosition the position from which tag should start. The counting start from a zero.
+ * @param endPosition the position before which tag should should be closed.
+ * @param generator
*/
- public static class Builder {
-
- private boolean showInlineDiffs = false;
- private boolean ignoreWhiteSpaces = false;
-
- private Function oldTag = f -> f ? "" : "";
- private Function newTag = f -> f ? "" : "";
-
- private int columnWidth = 0;
- private boolean mergeOriginalRevised = false;
- private boolean inlineDiffByWord = false;
- private boolean reportLinesUnchanged = false;
-
- private Builder() {
- }
-
- /**
- * Show inline diffs in generating diff rows or not.
- *
- * @param val the value to set. Default: false.
- * @return builder with configured showInlineDiff parameter
- */
- public Builder showInlineDiffs(boolean val) {
- showInlineDiffs = val;
- return this;
- }
-
- /**
- * Ignore white spaces in generating diff rows or not.
- *
- * @param val the value to set. Default: true.
- * @return builder with configured ignoreWhiteSpaces parameter
- */
- public Builder ignoreWhiteSpaces(boolean val) {
- ignoreWhiteSpaces = val;
- return this;
- }
-
- /**
- * Give the originial old and new text lines to Diffrow without any additional processing.
- *
- * @param val the value to set. Default: false.
- * @return builder with configured reportLinesUnWrapped parameter
- */
- public Builder reportLinesUnchanged(final boolean val) {
- reportLinesUnchanged = val;
- return this;
- }
-
- /**
- * Generator for Old-Text-Tags.
- *
- * @param tag the tag to set. Without angle brackets. Default: span.
- * @return builder with configured ignoreBlankLines parameter
- */
- public Builder oldTag(Function generator) {
- this.oldTag = generator;
- return this;
- }
-
- /**
- * Generator for New-Text-Tags.
- *
- * @param generator
- * @return
- */
- public Builder newTag(Function generator) {
- this.newTag = generator;
- return this;
- }
-
- /**
- * Set the column with of generated lines of original and revised texts.
- *
- * @param width the width to set. Making it < 0 doesn't have any sense. Default 80. @return builder with config
- * ured ignoreBlankLines parameter
- */
- public Builder columnWidth(int width) {
- if (width >= 0) {
- columnWidth = width;
- }
- return this;
- }
-
- /**
- * Build the DiffRowGenerator. If some parameters is not set, the default values are used.
- *
- * @return the customized DiffRowGenerator
- */
- public DiffRowGenerator build() {
- return new DiffRowGenerator(this);
- }
-
- /**
- * Merge the complete result within the original text. This makes sense for one line display.
- *
- * @param mergeOriginalRevised
- * @return
- */
- public Builder mergeOriginalRevised(boolean mergeOriginalRevised) {
- this.mergeOriginalRevised = mergeOriginalRevised;
- return this;
- }
-
- /**
- * Per default each character is separatly processed. This variant introduces processing by word, which should
- * deliver no in word changes.
- */
- public Builder inlineDiffByWord(boolean inlineDiffByWord) {
- this.inlineDiffByWord = inlineDiffByWord;
- return this;
- }
+ public static void wrapInTag(List sequence, int startPosition,
+ int endPosition, Function generator) {
+ sequence.add(startPosition, generator.apply(true));
+ sequence.add(endPosition, generator.apply(false));
}
public static Builder create() {
@@ -203,11 +96,11 @@ private DiffRowGenerator(Builder builder) {
* displaying side-by-side diff.
*
* @param original the original text
- * @param revised the revised text
+ * @param revised the revised text
* @return the DiffRows between original and revised texts
*/
public List generateDiffRows(List original, List revised) throws DiffException {
- return generateDiffRows(original, DiffUtils.diff(original, revised, equalizer));
+ return generateDiffRows(original, DiffUtils.diff(original, revised, equalizer, 1));
}
private String preprocessLine(String line) {
@@ -251,8 +144,7 @@ private DiffRow buildDiffRowWithoutNormalizing(Tag type, String orgline, String
* for displaying side-by-side diff.
*
* @param original the original text
- * @param revised the revised text
- * @param patch the given patch
+ * @param patch the given patch
* @return the DiffRows between original and revised texts
*/
public List generateDiffRows(final List original, Patch patch) throws DiffException {
@@ -332,7 +224,7 @@ private List generateInlineDiffs(Delta delta) throws DiffExcept
}
}
- List> inlineDeltas = DiffUtils.diff(origList, revList).getDeltas();
+ List> inlineDeltas = DiffUtils.diff(origList, revList, 1).getDeltas();
Collections.reverse(inlineDeltas);
for (Delta inlineDelta : inlineDeltas) {
@@ -391,17 +283,122 @@ private List generateInlineDiffs(Delta delta) throws DiffExcept
}
/**
- * Wrap the elements in the sequence with the given tag
+ * This class used for building the DiffRowGenerator.
*
- * @param startPosition the position from which tag should start. The counting start from a zero.
- * @param endPosition the position before which tag should should be closed.
- * @param tag the tag name without angle brackets, just a word
- * @param cssClass the optional css class
+ * @author dmitry
*/
- public static void wrapInTag(List sequence, int startPosition,
- int endPosition, Function generator) {
- sequence.add(startPosition, generator.apply(true));
- sequence.add(endPosition, generator.apply(false));
+ public static class Builder {
+
+ private boolean showInlineDiffs = false;
+ private boolean ignoreWhiteSpaces = false;
+
+ private Function oldTag = f -> f ? "" : "";
+ private Function newTag = f -> f ? "" : "";
+
+ private int columnWidth = 0;
+ private boolean mergeOriginalRevised = false;
+ private boolean inlineDiffByWord = false;
+ private boolean reportLinesUnchanged = false;
+
+ private Builder() {
+ }
+
+ /**
+ * Show inline diffs in generating diff rows or not.
+ *
+ * @param val the value to set. Default: false.
+ * @return builder with configured showInlineDiff parameter
+ */
+ public Builder showInlineDiffs(boolean val) {
+ showInlineDiffs = val;
+ return this;
+ }
+
+ /**
+ * Ignore white spaces in generating diff rows or not.
+ *
+ * @param val the value to set. Default: true.
+ * @return builder with configured ignoreWhiteSpaces parameter
+ */
+ public Builder ignoreWhiteSpaces(boolean val) {
+ ignoreWhiteSpaces = val;
+ return this;
+ }
+
+ /**
+ * Give the originial old and new text lines to Diffrow without any additional processing.
+ *
+ * @param val the value to set. Default: false.
+ * @return builder with configured reportLinesUnWrapped parameter
+ */
+ public Builder reportLinesUnchanged(final boolean val) {
+ reportLinesUnchanged = val;
+ return this;
+ }
+
+ /**
+ * Generator for Old-Text-Tags.
+ *
+ * @param generator the tag to set. Without angle brackets. Default: span.
+ * @return builder with configured ignoreBlankLines parameter
+ */
+ public Builder oldTag(Function generator) {
+ this.oldTag = generator;
+ return this;
+ }
+
+ /**
+ * Generator for New-Text-Tags.
+ *
+ * @param generator
+ * @return
+ */
+ public Builder newTag(Function generator) {
+ this.newTag = generator;
+ return this;
+ }
+
+ /**
+ * Set the column with of generated lines of original and revised texts.
+ *
+ * @param width the width to set. Making it < 0 doesn't have any sense. Default 80. @return builder with config
+ * ured ignoreBlankLines parameter
+ */
+ public Builder columnWidth(int width) {
+ if (width >= 0) {
+ columnWidth = width;
+ }
+ return this;
+ }
+
+ /**
+ * Build the DiffRowGenerator. If some parameters is not set, the default values are used.
+ *
+ * @return the customized DiffRowGenerator
+ */
+ public DiffRowGenerator build() {
+ return new DiffRowGenerator(this);
+ }
+
+ /**
+ * Merge the complete result within the original text. This makes sense for one line display.
+ *
+ * @param mergeOriginalRevised
+ * @return
+ */
+ public Builder mergeOriginalRevised(boolean mergeOriginalRevised) {
+ this.mergeOriginalRevised = mergeOriginalRevised;
+ return this;
+ }
+
+ /**
+ * Per default each character is separatly processed. This variant introduces processing by word, which should
+ * deliver no in word changes.
+ */
+ public Builder inlineDiffByWord(boolean inlineDiffByWord) {
+ this.inlineDiffByWord = inlineDiffByWord;
+ return this;
+ }
}
protected final static List splitStringPreserveDelimiter(String str) {
diff --git a/src/test/java/com/github/difflib/DiffUtilsTest.java b/src/test/java/com/github/difflib/DiffUtilsTest.java
index d815401b..0ab8ac9b 100644
--- a/src/test/java/com/github/difflib/DiffUtilsTest.java
+++ b/src/test/java/com/github/difflib/DiffUtilsTest.java
@@ -1,12 +1,10 @@
package com.github.difflib;
import com.github.difflib.algorithm.DiffException;
-import com.github.difflib.patch.ChangeDelta;
-import com.github.difflib.patch.Chunk;
-import com.github.difflib.patch.DeleteDelta;
-import com.github.difflib.patch.Delta;
-import com.github.difflib.patch.InsertDelta;
-import com.github.difflib.patch.Patch;
+import com.github.difflib.patch.*;
+import org.junit.Ignore;
+import org.junit.Test;
+
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
@@ -17,38 +15,93 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
-import static java.util.stream.Collectors.toList;
import java.util.zip.ZipFile;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-import org.junit.Ignore;
-import org.junit.Test;
+
+import static java.util.stream.Collectors.toList;
+import static org.junit.Assert.*;
public class DiffUtilsTest {
@Test
public void testDiff_Insert() throws DiffException {
final Patch patch = DiffUtils.diff(Arrays.asList("hhh"), Arrays.
- asList("hhh", "jjj", "kkk"));
+ asList("hhh", "jjj", "kkk"), 1);
assertNotNull(patch);
assertEquals(1, patch.getDeltas().size());
final Delta delta = patch.getDeltas().get(0);
assertTrue(delta instanceof InsertDelta);
- assertEquals(new Chunk<>(1, Collections.emptyList()), delta.getOriginal());
- assertEquals(new Chunk<>(1, Arrays.asList("jjj", "kkk")), delta.getRevised());
+
+ Chunk originalChunk = new Chunk<>(1, Collections.emptyList());
+ originalChunk.setBefore(Arrays.asList("hhh"));
+ Chunk revisedChunk = new Chunk<>(1, Arrays.asList("jjj", "kkk"));
+ revisedChunk.setBefore(Arrays.asList("hhh"));
+
+ assertEquals(originalChunk, delta.getOriginal());
+ assertEquals(revisedChunk, delta.getRevised());
+ }
+
+ @Test
+ public void testDiff_Insert_Before_After() throws DiffException {
+ final List test_from = Arrays.asList("hhh", "ttt", "xxx");
+ final List test_to = Arrays.asList("hhh", "jjj", "kkk", "ttt", "xxx");
+
+ final Patch patch = DiffUtils.diff(test_from, test_to, 1);
+ assertNotNull(patch);
+ assertEquals(1, patch.getDeltas().size());
+
+ final Delta delta = patch.getDeltas().get(0);
+ System.out.println("INSERT: Original Chunk: " + delta.getOriginal() + " Before: " + delta.getOriginal().getBefore() + " After: " + delta.getOriginal().getAfter());
+ assertTrue(delta.getOriginal().getBefore().size() == 1);
+ assertTrue(delta.getOriginal().getAfter().size() == 1);
+ assertEquals(Arrays.asList("hhh"), delta.getOriginal().getBefore());
+ assertEquals(Arrays.asList("ttt"), delta.getOriginal().getAfter());
+
+ System.out.println("INSERT: Revised Chunk: " + delta.getRevised() + " Before: " + delta.getRevised().getBefore() + " After: " + delta.getRevised().getAfter());
+ assertTrue(delta.getRevised().getBefore().size() == 1);
+ assertTrue(delta.getRevised().getAfter().size() == 1);
+ assertEquals(Arrays.asList("hhh"), delta.getRevised().getBefore());
+ assertEquals(Arrays.asList("ttt"), delta.getRevised().getAfter());
}
@Test
public void testDiff_Delete() throws DiffException {
final Patch patch = DiffUtils.diff(Arrays.asList("ddd", "fff", "ggg"), Arrays.
- asList("ggg"));
+ asList("ggg"), 1);
assertNotNull(patch);
assertEquals(1, patch.getDeltas().size());
final Delta delta = patch.getDeltas().get(0);
assertTrue(delta instanceof DeleteDelta);
- assertEquals(new Chunk<>(0, Arrays.asList("ddd", "fff")), delta.getOriginal());
- assertEquals(new Chunk<>(0, Collections.emptyList()), delta.getRevised());
+
+ Chunk originalChunk = new Chunk<>(0, Arrays.asList("ddd", "fff"));
+ originalChunk.setAfter(Arrays.asList("ggg"));
+ Chunk revisedChunk = new Chunk<>(0, Collections.emptyList());
+ revisedChunk.setAfter(Arrays.asList("ggg"));
+
+ assertEquals(originalChunk, delta.getOriginal());
+ assertEquals(revisedChunk, delta.getRevised());
+ }
+
+ @Test
+ public void testDiff_Delete_Before_After() throws DiffException {
+ final List test_from = Arrays.asList("fff", "ggg");
+ final List test_to = Arrays.asList("ggg");
+
+ final Patch patch = DiffUtils.diff(test_from, test_to, 1);
+ assertNotNull(patch);
+ assertEquals(1, patch.getDeltas().size());
+
+ final Delta delta = patch.getDeltas().get(0);
+ System.out.println("DELETE: Original Chunk: " + delta.getOriginal() + " Before: " + delta.getOriginal().getBefore() + " After: " + delta.getOriginal().getAfter());
+ assertTrue(delta.getOriginal().getBefore().size() == 0);
+ assertTrue(delta.getOriginal().getAfter().size() == 1);
+ assertEquals(Arrays.asList(), delta.getOriginal().getBefore());
+ assertEquals(Arrays.asList("ggg"), delta.getOriginal().getAfter());
+
+ System.out.println("DELETE: Revised Chunk: " + delta.getRevised() + " Before: " + delta.getRevised().getBefore() + " After: " + delta.getRevised().getAfter());
+ assertTrue(delta.getRevised().getBefore().size() == 0);
+ assertTrue(delta.getRevised().getAfter().size() == 1);
+ assertEquals(Arrays.asList(), delta.getRevised().getBefore());
+ assertEquals(Arrays.asList("ggg"), delta.getRevised().getAfter());
}
@Test
@@ -56,25 +109,55 @@ public void testDiff_Change() throws DiffException {
final List changeTest_from = Arrays.asList("aaa", "bbb", "ccc");
final List changeTest_to = Arrays.asList("aaa", "zzz", "ccc");
- final Patch patch = DiffUtils.diff(changeTest_from, changeTest_to);
+ final Patch patch = DiffUtils.diff(changeTest_from, changeTest_to, 1);
assertNotNull(patch);
assertEquals(1, patch.getDeltas().size());
final Delta delta = patch.getDeltas().get(0);
assertTrue(delta instanceof ChangeDelta);
- assertEquals(new Chunk<>(1, Arrays.asList("bbb")), delta.getOriginal());
- assertEquals(new Chunk<>(1, Arrays.asList("zzz")), delta.getRevised());
+
+ Chunk originalChunk = new Chunk<>(1, Arrays.asList("bbb"));
+ originalChunk.setBefore(Arrays.asList("aaa"));
+ originalChunk.setAfter(Arrays.asList("ccc"));
+ Chunk revisedChunk = new Chunk<>(1, Arrays.asList("zzz"));
+ revisedChunk.setBefore(Arrays.asList("aaa"));
+ revisedChunk.setAfter(Arrays.asList("ccc"));
+ assertEquals(originalChunk, delta.getOriginal());
+ assertEquals(revisedChunk, delta.getRevised());
+ }
+
+ @Test
+ public void testDiff_Change_Before_After() throws DiffException {
+ final List test_from = Arrays.asList("aaa", "bbb", "ccc");
+ final List test_to = Arrays.asList("aaa", "zzz", "ccc");
+
+ final Patch patch = DiffUtils.diff(test_from, test_to, 1);
+ assertNotNull(patch);
+ assertEquals(1, patch.getDeltas().size());
+
+ final Delta delta = patch.getDeltas().get(0);
+ System.out.println("CHANGE: Original Chunk: " + delta.getOriginal() + " Before: " + delta.getOriginal().getBefore() + " After: " + delta.getOriginal().getAfter());
+ assertTrue(delta.getOriginal().getBefore().size() == 1);
+ assertTrue(delta.getOriginal().getAfter().size() == 1);
+ assertEquals(Arrays.asList("aaa"), delta.getOriginal().getBefore());
+ assertEquals(Arrays.asList("ccc"), delta.getOriginal().getAfter());
+
+ System.out.println("CHANGE: Revised Chunk: " + delta.getRevised() + " Before: " + delta.getRevised().getBefore() + " After: " + delta.getRevised().getAfter());
+ assertTrue(delta.getRevised().getBefore().size() == 1);
+ assertTrue(delta.getRevised().getAfter().size() == 1);
+ assertEquals(Arrays.asList("aaa"), delta.getRevised().getBefore());
+ assertEquals(Arrays.asList("ccc"), delta.getRevised().getAfter());
}
@Test
public void testDiff_EmptyList() throws DiffException {
- final Patch patch = DiffUtils.diff(new ArrayList<>(), new ArrayList<>());
+ final Patch patch = DiffUtils.diff(new ArrayList<>(), new ArrayList<>(), 1);
assertNotNull(patch);
assertEquals(0, patch.getDeltas().size());
}
@Test
public void testDiff_EmptyListWithNonEmpty() throws DiffException {
- final Patch patch = DiffUtils.diff(new ArrayList<>(), Arrays.asList("aaa"));
+ final Patch patch = DiffUtils.diff(new ArrayList<>(), Arrays.asList("aaa"), 1);
assertNotNull(patch);
assertEquals(1, patch.getDeltas().size());
final Delta delta = patch.getDeltas().get(0);
@@ -83,7 +166,7 @@ public void testDiff_EmptyListWithNonEmpty() throws DiffException {
@Test
public void testDiffInline() throws DiffException {
- final Patch patch = DiffUtils.diffInline("", "test");
+ final Patch patch = DiffUtils.diffInline("", "test", 1);
assertEquals(1, patch.getDeltas().size());
assertTrue(patch.getDeltas().get(0) instanceof InsertDelta);
assertEquals(0, patch.getDeltas().get(0).getOriginal().getPosition());
@@ -93,7 +176,7 @@ public void testDiffInline() throws DiffException {
@Test
public void testDiffInline2() throws DiffException {
- final Patch patch = DiffUtils.diffInline("es", "fest");
+ final Patch patch = DiffUtils.diffInline("es", "fest", 1);
assertEquals(2, patch.getDeltas().size());
assertTrue(patch.getDeltas().get(0) instanceof InsertDelta);
assertEquals(0, patch.getDeltas().get(0).getOriginal().getPosition());
@@ -109,7 +192,7 @@ public void testDiffIntegerList() throws DiffException {
List original = Arrays.asList(1, 2, 3, 4, 5);
List revised = Arrays.asList(2, 3, 4, 6);
- final Patch patch = DiffUtils.diff(original, revised);
+ final Patch patch = DiffUtils.diff(original, revised, 1);
for (Delta delta : patch.getDeltas()) {
System.out.println(delta);
@@ -125,7 +208,7 @@ public void testDiffMissesChangeForkDnaumenkoIssue31() throws DiffException {
List original = Arrays.asList("line1", "line2", "line3");
List revised = Arrays.asList("line1", "line2-2", "line4");
- Patch patch = DiffUtils.diff(original, revised);
+ Patch patch = DiffUtils.diff(original, revised, 1);
assertEquals(1, patch.getDeltas().size());
assertEquals("[ChangeDelta, position: 1, lines: [line2, line3] to [line2-2, line4]]", patch.getDeltas().get(0).toString());
}
@@ -137,10 +220,10 @@ public void testDiffMissesChangeForkDnaumenkoIssue31() throws DiffException {
@Ignore
public void testPossibleDiffHangOnLargeDatasetDnaumenkoIssue26() throws IOException, DiffException {
ZipFile zip = new ZipFile(TestConstants.MOCK_FOLDER + "/large_dataset1.zip");
-
+
Patch patch = DiffUtils.diff(
readStringListFromInputStream(zip.getInputStream(zip.getEntry("ta"))),
- readStringListFromInputStream(zip.getInputStream(zip.getEntry("tb"))));
+ readStringListFromInputStream(zip.getInputStream(zip.getEntry("tb"))), 1);
assertEquals(1, patch.getDeltas().size());
}
@@ -155,7 +238,7 @@ public static List readStringListFromInputStream(InputStream is) throws
@Test
public void testDiffMyersExample1() throws DiffException {
- final Patch patch = DiffUtils.diff(Arrays.asList("A", "B", "C", "A", "B", "B", "A"), Arrays.asList("C", "B", "A", "B", "A", "C"));
+ final Patch patch = DiffUtils.diff(Arrays.asList("A", "B", "C", "A", "B", "B", "A"), Arrays.asList("C", "B", "A", "B", "A", "C"), 1);
assertNotNull(patch);
assertEquals(4, patch.getDeltas().size());
assertEquals("Patch{deltas=[[DeleteDelta, position: 0, lines: [A, B]], [InsertDelta, position: 3, lines: [B]], [DeleteDelta, position: 5, lines: [B]], [InsertDelta, position: 7, lines: [C]]]}", patch.toString());
diff --git a/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java b/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java
index 89ab32f6..966b525c 100644
--- a/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java
+++ b/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java
@@ -3,6 +3,8 @@
import com.github.difflib.algorithm.DiffException;
import com.github.difflib.patch.Patch;
import com.github.difflib.patch.PatchFailedException;
+import org.junit.Test;
+
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
@@ -10,9 +12,9 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
-import org.junit.Test;
public class GenerateUnifiedDiffTest {
@@ -46,7 +48,7 @@ public void testGenerateUnifiedWithOneDelta() throws DiffException, IOException
@Test
public void testGenerateUnifiedDiffWithoutAnyDeltas() throws DiffException {
List test = Arrays.asList("abc");
- Patch patch = DiffUtils.diff(test, test);
+ Patch patch = DiffUtils.diff(test, test, 1);
UnifiedDiffUtils.generateUnifiedDiff("abc", "abc", test, patch, 0);
}
@@ -98,7 +100,7 @@ public void testDiffWithHeaderLineInText() throws DiffException {
revised.add("test line 4");
revised.add("test line 5");
- Patch patch = DiffUtils.diff(original, revised);
+ Patch patch = DiffUtils.diff(original, revised, 1);
List udiff = UnifiedDiffUtils.generateUnifiedDiff("original", "revised",
original, patch, 10);
UnifiedDiffUtils.parseUnifiedDiff(udiff);
@@ -106,7 +108,7 @@ public void testDiffWithHeaderLineInText() throws DiffException {
private void verify(List origLines, List revLines,
String originalFile, String revisedFile) throws DiffException {
- Patch patch = DiffUtils.diff(origLines, revLines);
+ Patch patch = DiffUtils.diff(origLines, revLines, 1);
List unifiedDiff = UnifiedDiffUtils.generateUnifiedDiff(originalFile, revisedFile,
origLines, patch, 10);
diff --git a/src/test/java/com/github/difflib/algorithm/jgit/HistogramDiffTest.java b/src/test/java/com/github/difflib/algorithm/jgit/HistogramDiffTest.java
index ec44adea..36a29d88 100644
--- a/src/test/java/com/github/difflib/algorithm/jgit/HistogramDiffTest.java
+++ b/src/test/java/com/github/difflib/algorithm/jgit/HistogramDiffTest.java
@@ -18,14 +18,13 @@
import com.github.difflib.algorithm.DiffException;
import com.github.difflib.patch.Patch;
import com.github.difflib.patch.PatchFailedException;
+import org.junit.*;
+
import java.util.Arrays;
import java.util.List;
-import org.junit.After;
-import org.junit.AfterClass;
-import static org.junit.Assert.*;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
/**
*
@@ -59,7 +58,7 @@ public void tearDown() {
public void testDiff() throws DiffException, PatchFailedException {
List orgList = Arrays.asList("A", "B", "C", "A", "B", "B", "A");
List revList = Arrays.asList("C", "B", "A", "B", "A", "C");
- final Patch patch = Patch.generate(orgList, revList, new HistogramDiff().diff(orgList, revList));
+ final Patch patch = Patch.generate(orgList, revList, new HistogramDiff().diff(orgList, revList), 1);
System.out.println(patch);
assertNotNull(patch);
assertEquals(3, patch.getDeltas().size());
diff --git a/src/test/java/com/github/difflib/algorithm/jgit/LRHistogramDiffTest.java b/src/test/java/com/github/difflib/algorithm/jgit/LRHistogramDiffTest.java
index 93e2950e..30366c30 100644
--- a/src/test/java/com/github/difflib/algorithm/jgit/LRHistogramDiffTest.java
+++ b/src/test/java/com/github/difflib/algorithm/jgit/LRHistogramDiffTest.java
@@ -15,20 +15,19 @@
*/
package com.github.difflib.algorithm.jgit;
-import static com.github.difflib.DiffUtilsTest.readStringListFromInputStream;
import com.github.difflib.TestConstants;
import com.github.difflib.algorithm.DiffException;
import com.github.difflib.patch.Patch;
import com.github.difflib.patch.PatchFailedException;
+import org.junit.*;
+
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipFile;
-import org.junit.After;
-import org.junit.AfterClass;
-import static org.junit.Assert.*;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
+
+import static com.github.difflib.DiffUtilsTest.readStringListFromInputStream;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
/**
*
@@ -61,8 +60,8 @@ public void testPossibleDiffHangOnLargeDatasetDnaumenkoIssue26() throws IOExcept
List original = readStringListFromInputStream(zip.getInputStream(zip.getEntry("ta")));
List revised = readStringListFromInputStream(zip.getInputStream(zip.getEntry("tb")));
- Patch patch = Patch.generate(original, revised, new HistogramDiff().diff(original, revised));
-
+ Patch patch = Patch.generate(original, revised, new HistogramDiff().diff(original, revised), 1);
+
assertEquals(34, patch.getDeltas().size());
List created = patch.applyTo(original);
diff --git a/src/test/java/com/github/difflib/algorithm/myers/MyersDiffTest.java b/src/test/java/com/github/difflib/algorithm/myers/MyersDiffTest.java
index 9b911ff1..c564581b 100644
--- a/src/test/java/com/github/difflib/algorithm/myers/MyersDiffTest.java
+++ b/src/test/java/com/github/difflib/algorithm/myers/MyersDiffTest.java
@@ -17,10 +17,13 @@
import com.github.difflib.algorithm.DiffException;
import com.github.difflib.patch.Patch;
+import org.junit.Test;
+
import java.util.Arrays;
import java.util.List;
-import static org.junit.Assert.*;
-import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
/**
*
@@ -32,7 +35,7 @@ public class MyersDiffTest {
public void testDiffMyersExample1Forward() throws DiffException {
List original = Arrays.asList("A", "B", "C", "A", "B", "B", "A");
List revised = Arrays.asList("C", "B", "A", "B", "A", "C");
- final Patch patch = Patch.generate(original, revised, new MyersDiff().diff(original, revised));
+ final Patch patch = Patch.generate(original, revised, new MyersDiff().diff(original, revised), 1);
assertNotNull(patch);
assertEquals(4, patch.getDeltas().size());
assertEquals("Patch{deltas=[[DeleteDelta, position: 0, lines: [A, B]], [InsertDelta, position: 3, lines: [B]], [DeleteDelta, position: 5, lines: [B]], [InsertDelta, position: 7, lines: [C]]]}", patch.toString());
diff --git a/src/test/java/com/github/difflib/examples/ComputeDifference.java b/src/test/java/com/github/difflib/examples/ComputeDifference.java
index 82ff0190..92665e7e 100644
--- a/src/test/java/com/github/difflib/examples/ComputeDifference.java
+++ b/src/test/java/com/github/difflib/examples/ComputeDifference.java
@@ -5,6 +5,7 @@
import com.github.difflib.algorithm.DiffException;
import com.github.difflib.patch.Delta;
import com.github.difflib.patch.Patch;
+
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
@@ -20,7 +21,7 @@ public static void main(String[] args) throws DiffException, IOException {
List revised = Files.readAllLines(new File(RIVISED).toPath());
// Compute diff. Get the Patch object. Patch is the container for computed deltas.
- Patch patch = DiffUtils.diff(original, revised);
+ Patch patch = DiffUtils.diff(original, revised, 1);
for (Delta delta : patch.getDeltas()) {
System.out.println(delta);
diff --git a/src/test/java/com/github/difflib/patch/PatchTest.java b/src/test/java/com/github/difflib/patch/PatchTest.java
index 792785eb..15111117 100644
--- a/src/test/java/com/github/difflib/patch/PatchTest.java
+++ b/src/test/java/com/github/difflib/patch/PatchTest.java
@@ -2,11 +2,13 @@
import com.github.difflib.DiffUtils;
import com.github.difflib.algorithm.DiffException;
+import org.junit.Test;
+
import java.util.Arrays;
import java.util.List;
+
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
-import org.junit.Test;
public class PatchTest {
@@ -15,7 +17,7 @@ public void testPatch_Insert() throws DiffException {
final List insertTest_from = Arrays.asList("hhh");
final List insertTest_to = Arrays.asList("hhh", "jjj", "kkk", "lll");
- final Patch patch = DiffUtils.diff(insertTest_from, insertTest_to);
+ final Patch patch = DiffUtils.diff(insertTest_from, insertTest_to, 1);
try {
assertEquals(insertTest_to, DiffUtils.patch(insertTest_from, patch));
} catch (PatchFailedException e) {
@@ -28,7 +30,7 @@ public void testPatch_Delete() throws DiffException {
final List deleteTest_from = Arrays.asList("ddd", "fff", "ggg", "hhh");
final List deleteTest_to = Arrays.asList("ggg");
- final Patch patch = DiffUtils.diff(deleteTest_from, deleteTest_to);
+ final Patch patch = DiffUtils.diff(deleteTest_from, deleteTest_to, 1);
try {
assertEquals(deleteTest_to, DiffUtils.patch(deleteTest_from, patch));
} catch (PatchFailedException e) {
@@ -41,7 +43,7 @@ public void testPatch_Change() throws DiffException {
final List changeTest_from = Arrays.asList("aaa", "bbb", "ccc", "ddd");
final List changeTest_to = Arrays.asList("aaa", "bxb", "cxc", "ddd");
- final Patch patch = DiffUtils.diff(changeTest_from, changeTest_to);
+ final Patch patch = DiffUtils.diff(changeTest_from, changeTest_to, 1);
try {
assertEquals(changeTest_to, DiffUtils.patch(changeTest_from, patch));
} catch (PatchFailedException e) {
From 3463ff0f44ab55d37cdcf49762b41a9afde80361 Mon Sep 17 00:00:00 2001
From: Kogoro
Date: Tue, 31 Oct 2017 17:54:17 +0100
Subject: [PATCH 4/6] Added Chunk verification for other chunks and verify now
includes surroundings check
---
README.md | 3 ++
.../java/com/github/difflib/patch/Chunk.java | 36 ++++++++++++-----
.../java/com/github/difflib/patch/Delta.java | 24 ++++++-----
.../com/github/difflib/patch/ChunkTest.java | 40 +++++++++++++++++++
.../com/github/difflib/patch/PatchTest.java | 26 +++++++++++-
5 files changed, 109 insertions(+), 20 deletions(-)
create mode 100644 src/test/java/com/github/difflib/patch/ChunkTest.java
diff --git a/README.md b/README.md
index 2a965de2..8b44d7a9 100644
--- a/README.md
+++ b/README.md
@@ -52,6 +52,9 @@ But it can easily replaced by any other which is better for handing your texts.
### Changelog ###
* Version 2.2-SNAPSHOT
* Added chunk to support surroundings of differences
+ * Verify also looks at surroundings
+ * Deltas now can proof if another delta change the same chunk
+ * Chunks now can proof if another chunk change the same positions
* Version 2.1-SNAPSHOT
* included checkstyle source code conventions
* Version 2.0
diff --git a/src/main/java/com/github/difflib/patch/Chunk.java b/src/main/java/com/github/difflib/patch/Chunk.java
index ca0775e7..b078afe1 100644
--- a/src/main/java/com/github/difflib/patch/Chunk.java
+++ b/src/main/java/com/github/difflib/patch/Chunk.java
@@ -25,7 +25,7 @@
/**
* Holds the information about the part of text involved in the diff process
- *
+ *
*
* Text is represented as Object[]
because the diff engine is capable of handling more than plain ascci. In
* fact, arrays or lists of any type that implements {@link java.lang.Object#hashCode hashCode()} and
@@ -47,7 +47,7 @@ public final class Chunk {
* Creates a chunk and saves a copy of affected lines
*
* @param position the start position
- * @param lines the affected lines
+ * @param lines the affected lines
*/
public Chunk(int position, List lines) {
this.position = position;
@@ -58,7 +58,7 @@ public Chunk(int position, List lines) {
* Creates a chunk and saves a copy of affected lines
*
* @param position the start position
- * @param lines the affected lines
+ * @param lines the affected lines
*/
public Chunk(int position, T[] lines) {
this.position = position;
@@ -80,6 +80,26 @@ public void verify(List target) throws PatchFailedException {
"Incorrect Chunk: the chunk content doesn't match the target");
}
}
+ for (int i = 0; i < before.size(); i++) {
+ if (!before.get(i).equals(target.get(position - before.size() + i)))
+ throw new PatchFailedException("Incorrect Chunk: the chunk's surroundings (before) doesn't match the target");
+ }
+ for (int i = 0; i < after.size(); i++) {
+ if (!after.get(i).equals(target.get(last() + after.size() - i)))
+ throw new PatchFailedException("Incorrect Chunk: the chunk's surroundings (after) doesn't match the target");
+ }
+ }
+
+ /**
+ * Verifies that this chunk does not affect the same positions as the given chunk.
+ *
+ * @param other the chunk to be proof against this chunk.
+ * @throws PatchFailedException if both deltas affect the same chunk.
+ */
+ public void verify(Chunk other) throws PatchFailedException {
+ if (Math.max(last(), other.last()) - Math.min(getPosition(), other.getPosition()) < (size() + other.size())) {
+ throw new PatchFailedException("Multiple chunks would override the same target position");
+ }
}
/**
@@ -135,7 +155,7 @@ public void setAfter(List after) {
/*
* (non-Javadoc)
- *
+ *
* @see java.lang.Object#hashCode()
*/
@Override
@@ -150,7 +170,7 @@ public int hashCode() {
/*
* (non-Javadoc)
- *
+ *
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
@@ -172,14 +192,12 @@ public boolean equals(Object obj) {
} else if (!lines.equals(other.lines)) {
return false;
}
- if (!before.equals(other.before)) return false;
- if (!after.equals(other.after)) return false;
- return position == other.position;
+ return before.equals(other.before) && after.equals(other.after) && position == other.position;
}
@Override
public String toString() {
- return "[position: " + position + ", size: " + size() + ", lines: " + lines + "]";
+ return "[position: " + position + ", size: " + size() + ", lines: " + lines + ", before: " + before + ", after: " + after + "]";
}
}
diff --git a/src/main/java/com/github/difflib/patch/Delta.java b/src/main/java/com/github/difflib/patch/Delta.java
index 657a8f4f..769e7fa6 100644
--- a/src/main/java/com/github/difflib/patch/Delta.java
+++ b/src/main/java/com/github/difflib/patch/Delta.java
@@ -19,7 +19,8 @@
*/
package com.github.difflib.patch;
-import java.util.*;
+import java.util.List;
+import java.util.Objects;
/**
* Describes the delta between original and revised texts.
@@ -59,11 +60,21 @@ public void verify(List target) throws PatchFailedException {
getOriginal().verify(target);
}
+ /**
+ * Verifies that this delta does not affect the same chunk as the given delta.
+ *
+ * @param delta the delta to be proof against this delta.
+ * @throws PatchFailedException if both deltas affect the same chunk.
+ */
+ public void verify(Delta delta) throws PatchFailedException {
+ getOriginal().verify(delta.getOriginal());
+ }
+
/**
* Applies this delta as the patch for a given target
*
* @param target the given target
- * @throws PatchFailedException
+ * @throws PatchFailedException if a error occured
*/
public abstract void applyTo(List target) throws PatchFailedException;
@@ -121,13 +132,8 @@ public boolean equals(Object obj) {
return false;
}
if (revised == null) {
- if (other.revised != null) {
- return false;
- }
- } else if (!revised.equals(other.revised)) {
- return false;
- }
- return true;
+ return other.revised == null;
+ } else return revised.equals(other.revised);
}
}
diff --git a/src/test/java/com/github/difflib/patch/ChunkTest.java b/src/test/java/com/github/difflib/patch/ChunkTest.java
new file mode 100644
index 00000000..90140ccc
--- /dev/null
+++ b/src/test/java/com/github/difflib/patch/ChunkTest.java
@@ -0,0 +1,40 @@
+package com.github.difflib.patch;
+
+import com.github.difflib.DiffUtils;
+import com.github.difflib.algorithm.DiffException;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.assertTrue;
+
+public class ChunkTest {
+
+ @Test
+ public void testChunk_verify() throws DiffException {
+ final List insertTest_from = Arrays.asList("hhh", "lll");
+ final List insertTest_to = Arrays.asList("hhh", "jjj", "kkk", "lll");
+
+ final Patch patch = DiffUtils.diff(insertTest_from, insertTest_to, 1);
+ assertTrue(patch.getDeltas().size() > 0);
+ Delta delta = patch.getDeltas().get(0);
+
+ PatchFailedException exception = null;
+ try {
+ delta.verify(insertTest_from);
+ } catch (PatchFailedException e) {
+ exception = e;
+ }
+ assertTrue(exception == null);
+
+ exception = null;
+ try {
+ delta.verify(insertTest_to);
+ } catch (PatchFailedException e) {
+ exception = e;
+ }
+ assertTrue(exception != null);
+ }
+
+}
diff --git a/src/test/java/com/github/difflib/patch/PatchTest.java b/src/test/java/com/github/difflib/patch/PatchTest.java
index 15111117..ec1ab7fc 100644
--- a/src/test/java/com/github/difflib/patch/PatchTest.java
+++ b/src/test/java/com/github/difflib/patch/PatchTest.java
@@ -7,8 +7,7 @@
import java.util.Arrays;
import java.util.List;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.*;
public class PatchTest {
@@ -50,4 +49,27 @@ public void testPatch_Change() throws DiffException {
fail(e.getMessage());
}
}
+
+ @Test
+ public void testPatch_ChunkVerify() throws DiffException {
+ final List changeTest_from = Arrays.asList("aaa", "ddd");
+ final List changeTest_to = Arrays.asList("aaa", "bbb", "ccc", "ddd");
+
+ final Patch patchOne = DiffUtils.diff(changeTest_from, changeTest_to, 1);
+ assertTrue(patchOne.getDeltas().size() > 0);
+ final Patch patchTwo = DiffUtils.diff(changeTest_from, changeTest_to, 1);
+ assertTrue(patchTwo.getDeltas().size() > 0);
+
+ for (Delta deltaOne : patchOne.getDeltas()) {
+ for (Delta deltaTwo : patchTwo.getDeltas()) {
+ PatchFailedException exception = null;
+ try {
+ deltaOne.verify(deltaTwo);
+ } catch (PatchFailedException e) {
+ exception = e;
+ }
+ assertTrue(exception != null);
+ }
+ }
+ }
}
From b0ca17de10b8071ff05800bf77bb7b9c9b9b6eab Mon Sep 17 00:00:00 2001
From: Kogoro
Date: Tue, 31 Oct 2017 18:56:35 +0100
Subject: [PATCH 5/6] Applying style checks
---
nb-configuration.xml | 38 ++--
pom.xml | 202 +++++++++---------
.../java/com/github/difflib/DiffUtils.java | 33 ++-
.../com/github/difflib/UnifiedDiffUtils.java | 22 +-
.../com/github/difflib/algorithm/Change.java | 1 -
.../difflib/algorithm/DiffAlgorithm.java | 15 +-
.../difflib/algorithm/jgit/HistogramDiff.java | 7 +-
.../difflib/algorithm/myers/MyersDiff.java | 12 +-
.../difflib/algorithm/myers/PathNode.java | 12 +-
.../com/github/difflib/patch/ChangeDelta.java | 5 +-
.../java/com/github/difflib/patch/Chunk.java | 6 +-
.../com/github/difflib/patch/DeleteDelta.java | 5 +-
.../java/com/github/difflib/patch/Delta.java | 9 +-
.../com/github/difflib/patch/DeltaType.java | 1 -
.../com/github/difflib/patch/InsertDelta.java | 5 +-
.../java/com/github/difflib/patch/Patch.java | 6 +-
.../java/com/github/difflib/text/DiffRow.java | 8 +-
.../com/github/difflib/text/StringUtils.java | 3 +-
.../difflib/GenerateUnifiedDiffTest.java | 9 +-
.../com/github/difflib/TestConstants.java | 3 +-
.../algorithm/jgit/HistogramDiffTest.java | 1 -
.../algorithm/jgit/LRHistogramDiffTest.java | 3 +-
.../algorithm/myers/MyersDiffTest.java | 1 -
.../github/difflib/examples/ApplyPatch.java | 1 +
.../difflib/text/DiffRowGeneratorTest.java | 4 +-
.../github/difflib/text/StringUtilsTest.java | 7 +-
26 files changed, 212 insertions(+), 207 deletions(-)
diff --git a/nb-configuration.xml b/nb-configuration.xml
index f5e3165a..e8402d7c 100644
--- a/nb-configuration.xml
+++ b/nb-configuration.xml
@@ -1,22 +1,22 @@
-
-
-
- none
- false
- true
- LF
- false
-
+
+
+
+ none
+ false
+ true
+ LF
+ false
+
diff --git a/pom.xml b/pom.xml
index c5a90936..0a72d9dd 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1,4 +1,5 @@
-
+
4.0.0
com.github.java-diff-utils
diffutils
@@ -6,33 +7,34 @@
2.2-SNAPSHOT
java-diff-utils
- The DiffUtils library for computing diffs, applying patches, generationg side-by-side view in Java.
+ The DiffUtils library for computing diffs, applying patches, generationg side-by-side view in Java.
+
https://github.com/wumpz/java-diff-utils
2009
-
+ -->
scm:git:https://github.com/wumpz/java-diff-utils.git
scm:git:ssh://git@github.com:wumpz/java-diff-utils.git
https://github.com/wumpz/java-diff-utils.git
diffutils-2.2
-
-
+
+
GitHub Issues
https://github.com/wumpz/java-diff-utils/issues
-
+
java-diff-utils
-
+
Christopher Sontag
@@ -51,7 +53,7 @@
juanco@suigeneris.org
-
+
The Apache Software License, Version 2.0
@@ -108,7 +110,7 @@
-
+
org.apache.maven.plugins
maven-compiler-plugin
@@ -119,7 +121,7 @@
UTF-8
-
+
-
-
-
-
-
-
-
-
-
-
-
+ install
+
+
+
+ org.apache.maven.plugins
+ maven-checkstyle-plugin
+ 2.17
+
+
+ verify-style
+ process-classes
+
+ check
+
+
+
+
+ true
+ true
+ ${project.build.sourceDirectory}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
-
-
-
+
+
+
+
-
-
-
-
-
-
-
- com.puppycrawl.tools
- checkstyle
- 6.19
-
-
-
-
-
-
-
- doclint-java8-disable
-
- [1.8,)
-
-
- -Xdoclint:none
-
-
-
- long-running-tests
-
-
-
- org.apache.maven.plugins
- maven-surefire-plugin
-
-
- xxx
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+ com.puppycrawl.tools
+ checkstyle
+ 6.19
+
+
+
+
+
+
+
+ doclint-java8-disable
+
+ [1.8,)
+
+
+ -Xdoclint:none
+
+
+
+ long-running-tests
+
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+
+
+ xxx
+
+
+
+
+
+
+
diff --git a/src/main/java/com/github/difflib/DiffUtils.java b/src/main/java/com/github/difflib/DiffUtils.java
index 41df8a70..2652b3a1 100644
--- a/src/main/java/com/github/difflib/DiffUtils.java
+++ b/src/main/java/com/github/difflib/DiffUtils.java
@@ -43,8 +43,8 @@ public final class DiffUtils {
/**
* Computes the difference between the original and revised list of elements with default diff algorithm
*
- * @param original The original text. Must not be {@code null}.
- * @param revised The revised text. Must not be {@code null}.
+ * @param original The original text. Must not be {@code null}.
+ * @param revised The revised text. Must not be {@code null}.
* @param linesBeforeAfter - Amount of lines for before and after chunk content
* @return The patch describing the difference between the original and revised sequences. Never {@code null}.
*/
@@ -62,16 +62,15 @@ public static Patch diff(String originalText, String revisedText, int li
/**
* Computes the difference between the original and revised list of elements with default diff algorithm
*
- * @param original The original text. Must not be {@code null}.
- * @param revised The revised text. Must not be {@code null}.
- *
- * @param equalizer the equalizer object to replace the default compare algorithm (Object.equals). If {@code null}
- * the default equalizer of the default algorithm is used..
+ * @param original The original text. Must not be {@code null}.
+ * @param revised The revised text. Must not be {@code null}.
+ * @param equalizer the equalizer object to replace the default compare algorithm (Object.equals). If {@code null}
+ * the default equalizer of the default algorithm is used..
* @param linesBeforeAfter - Amount of lines for before and after chunk content
* @return The patch describing the difference between the original and revised sequences. Never {@code null}.
*/
public static Patch diff(List original, List revised,
- BiPredicate equalizer, int linesBeforeAfter) throws DiffException {
+ BiPredicate equalizer, int linesBeforeAfter) throws DiffException {
if (equalizer != null) {
return DiffUtils.diff(original, revised,
new MyersDiff<>(equalizer), linesBeforeAfter);
@@ -82,17 +81,17 @@ public static Patch diff(List original, List revised,
/**
* Computes the difference between the original and revised list of elements with default diff algorithm
*
- * @param original The original text. Must not be {@code null}.
- * @param revised The revised text. Must not be {@code null}.
- * @param algorithm The diff algorithm. Must not be {@code null}.
+ * @param original The original text. Must not be {@code null}.
+ * @param revised The revised text. Must not be {@code null}.
+ * @param algorithm The diff algorithm. Must not be {@code null}.
* @param linesBeforeAfter - Amount of lines for before and after chunk content
* @return The patch describing the difference between the original and revised sequences. Never {@code null}.
*/
public static Patch diff(List original, List revised,
- DiffAlgorithm algorithm, int linesBeforeAfter) throws DiffException {
- Objects.requireNonNull(original,"original must not be null");
- Objects.requireNonNull(revised,"revised must not be null");
- Objects.requireNonNull(algorithm,"algorithm must not be null");
+ DiffAlgorithm algorithm, int linesBeforeAfter) throws DiffException {
+ 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 Patch.generate(original, revised, algorithm.diff(original, revised), linesBeforeAfter);
}
@@ -134,7 +133,7 @@ private static List compressLines(List lines, String delimiter)
* Patch the original text with given patch
*
* @param original the original text
- * @param patch the given patch
+ * @param patch the given patch
* @return the revised text
* @throws PatchFailedException if can't apply patch
*/
@@ -147,7 +146,7 @@ public static List patch(List original, Patch patch)
* Unpatch the revised text for a given patch
*
* @param revised the revised text
- * @param patch the given patch
+ * @param patch the given patch
* @return the original text
*/
public static List unpatch(List revised, Patch patch) {
diff --git a/src/main/java/com/github/difflib/UnifiedDiffUtils.java b/src/main/java/com/github/difflib/UnifiedDiffUtils.java
index 1395c795..742584ee 100644
--- a/src/main/java/com/github/difflib/UnifiedDiffUtils.java
+++ b/src/main/java/com/github/difflib/UnifiedDiffUtils.java
@@ -19,13 +19,13 @@
import com.github.difflib.patch.Chunk;
import com.github.difflib.patch.Delta;
import com.github.difflib.patch.Patch;
+
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
- *
* @author toben
*/
public final class UnifiedDiffUtils {
@@ -130,17 +130,17 @@ public static Patch parseUnifiedDiff(List diff) {
* generateUnifiedDiff takes a Patch and some other arguments, returning the Unified Diff format text representing
* the Patch.
*
- * @param original - Filename of the original (unrevised file)
- * @param revised - Filename of the revised file
+ * @param original - Filename of the original (unrevised file)
+ * @param revised - Filename of the revised file
* @param originalLines - Lines of the original file
- * @param patch - Patch created by the diff() function
- * @param contextSize - number of lines of context output around each difference in the file.
+ * @param patch - Patch created by the diff() function
+ * @param contextSize - number of lines of context output around each difference in the file.
* @return List of strings representing the Unified Diff representation of the Patch argument.
* @author Bill James (tankerbay@gmail.com)
*/
public static List generateUnifiedDiff(String original,
- String revised, List originalLines, Patch patch,
- int contextSize) {
+ String revised, List originalLines, Patch patch,
+ int contextSize) {
if (!patch.getDeltas().isEmpty()) {
List ret = new ArrayList<>();
ret.add("--- " + original);
@@ -200,14 +200,14 @@ public static List generateUnifiedDiff(String original,
/**
* processDeltas takes a list of Deltas and outputs them together in a single block of Unified-Diff-format text.
*
- * @param origLines - the lines of the original file
- * @param deltas - the Deltas to be output as a single block
+ * @param origLines - the lines of the original file
+ * @param deltas - the Deltas to be output as a single block
* @param contextSize - the number of lines of context to place around block
* @return
* @author Bill James (tankerbay@gmail.com)
*/
private static List processDeltas(List origLines,
- List> deltas, int contextSize) {
+ List> deltas, int contextSize) {
List buffer = new ArrayList<>();
int origTotal = 0; // counter for total lines output from Original
int revTotal = 0; // counter for total lines output from Original
@@ -275,7 +275,7 @@ private static List processDeltas(List origLines,
// Create and insert the block header, conforming to the Unified Diff
// standard
- StringBuffer header = new StringBuffer();
+ StringBuilder header = new StringBuilder();
header.append("@@ -");
header.append(origStart);
header.append(",");
diff --git a/src/main/java/com/github/difflib/algorithm/Change.java b/src/main/java/com/github/difflib/algorithm/Change.java
index b90e2022..f57b4f8d 100644
--- a/src/main/java/com/github/difflib/algorithm/Change.java
+++ b/src/main/java/com/github/difflib/algorithm/Change.java
@@ -18,7 +18,6 @@
import com.github.difflib.patch.DeltaType;
/**
- *
* @author toben
*/
public class Change {
diff --git a/src/main/java/com/github/difflib/algorithm/DiffAlgorithm.java b/src/main/java/com/github/difflib/algorithm/DiffAlgorithm.java
index b97a15a4..928c0985 100644
--- a/src/main/java/com/github/difflib/algorithm/DiffAlgorithm.java
+++ b/src/main/java/com/github/difflib/algorithm/DiffAlgorithm.java
@@ -20,13 +20,16 @@
package com.github.difflib.algorithm;
import com.github.difflib.patch.Patch;
-import java.util.*;
+
+import java.util.Arrays;
+import java.util.List;
/**
* The general interface for computing diffs between two lists of elements of type T.
+ *
+ * T The type of the compared elements in the 'lines'.
*
* @author Dmitry Naumenko
- * @param T The type of the compared elements in the 'lines'.
*/
public interface DiffAlgorithm {
@@ -35,10 +38,10 @@ public interface DiffAlgorithm {
* object.
*
* @param original The original sequence. Must not be {@code null}.
- * @param revised The revised sequence. Must not be {@code null}.
+ * @param revised The revised sequence. Must not be {@code null}.
* @return The patch representing the diff of the given sequences. Never {@code null}.
*/
- public default List diff(T[] original, T[] revised) throws DiffException {
+ default List diff(T[] original, T[] revised) throws DiffException {
return diff(Arrays.asList(original), Arrays.asList(revised));
}
@@ -47,8 +50,8 @@ public default List diff(T[] original, T[] revised) throws DiffException
* object.
*
* @param original The original sequence. Must not be {@code null}.
- * @param revised The revised sequence. Must not be {@code null}.
+ * @param revised The revised sequence. Must not be {@code null}.
* @return The patch representing the diff of the given sequences. Never {@code null}.
*/
- public List diff(List original, List revised) throws DiffException;
+ List diff(List original, List revised) throws DiffException;
}
diff --git a/src/main/java/com/github/difflib/algorithm/jgit/HistogramDiff.java b/src/main/java/com/github/difflib/algorithm/jgit/HistogramDiff.java
index 6ca80103..2fa38203 100644
--- a/src/main/java/com/github/difflib/algorithm/jgit/HistogramDiff.java
+++ b/src/main/java/com/github/difflib/algorithm/jgit/HistogramDiff.java
@@ -19,14 +19,15 @@
import com.github.difflib.algorithm.DiffAlgorithm;
import com.github.difflib.algorithm.DiffException;
import com.github.difflib.patch.DeltaType;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Objects;
import org.eclipse.jgit.diff.Edit;
import org.eclipse.jgit.diff.EditList;
import org.eclipse.jgit.diff.Sequence;
import org.eclipse.jgit.diff.SequenceComparator;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+
/**
* HistorgramDiff using JGit - Library. This one is much more performant than the orginal Myers implementation.
*
diff --git a/src/main/java/com/github/difflib/algorithm/myers/MyersDiff.java b/src/main/java/com/github/difflib/algorithm/myers/MyersDiff.java
index ec76ec3f..7b3b2998 100644
--- a/src/main/java/com/github/difflib/algorithm/myers/MyersDiff.java
+++ b/src/main/java/com/github/difflib/algorithm/myers/MyersDiff.java
@@ -25,6 +25,7 @@
import com.github.difflib.algorithm.DifferentiationFailedException;
import com.github.difflib.patch.DeltaType;
import com.github.difflib.patch.Patch;
+
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@@ -49,7 +50,7 @@ public MyersDiff(final BiPredicate equalizer) {
/**
* {@inheritDoc}
- *
+ *
* Return empty diff if get the error while procession the difference.
*/
@Override
@@ -66,7 +67,7 @@ public List diff(final List original, final List revised) throws D
* to Gene Myers differencing algorithm.
*
* @param orig The original sequence.
- * @param rev The revised sequence.
+ * @param rev The revised sequence.
* @return A minimum {@link PathNode Path} accross the differences graph.
* @throws DifferentiationFailedException if a diff path could not be found.
*/
@@ -131,11 +132,10 @@ private PathNode buildPath(final List orig, final List rev)
/**
* Constructs a {@link Patch} from a difference path.
*
- * @param path The path.
- * @param orig The original sequence.
- * @param rev The revised sequence.
+ * @param actualPath The path.
+ * @param orig The original sequence.
+ * @param rev The revised sequence.
* @return A {@link Patch} script corresponding to the path.
- * @throws DifferentiationFailedException if a {@link Patch} could not be built from the given path.
*/
private List buildRevision(PathNode actualPath, List orig, List rev) {
Objects.requireNonNull(actualPath, "path is null");
diff --git a/src/main/java/com/github/difflib/algorithm/myers/PathNode.java b/src/main/java/com/github/difflib/algorithm/myers/PathNode.java
index 01d669f7..856485cf 100644
--- a/src/main/java/com/github/difflib/algorithm/myers/PathNode.java
+++ b/src/main/java/com/github/difflib/algorithm/myers/PathNode.java
@@ -23,10 +23,6 @@
* A node in a diffpath.
*
* @author Juanco Anez
- *
- * @see DiffNode
- * @see Snake
- *
*/
public final class PathNode {
@@ -50,8 +46,8 @@ public final class PathNode {
/**
* Concatenates a new path node with an existing diffpath.
*
- * @param i The position in the original sequence for the new node.
- * @param j The position in the revised sequence for the new node.
+ * @param i The position in the original sequence for the new node.
+ * @param j The position in the revised sequence for the new node.
* @param prev The previous node in the path.
*/
public PathNode(int i, int j, boolean snake, boolean bootstrap, PathNode prev) {
@@ -82,10 +78,10 @@ public boolean isBootstrap() {
}
/**
- * Skips sequences of {@link DiffNode DiffNodes} until a {@link Snake} or bootstrap node is found, or the end of the
+ * Skips sequences of {@link PathNode PathNode} until a snake or bootstrap node is found, or the end of the
* path is reached.
*
- * @return The next first {@link Snake} or bootstrap node in the path, or null
if none found.
+ * @return The next first snake or bootstrap node in the path, or null
if none found.
*/
public final PathNode previousSnake() {
if (isBootstrap()) {
diff --git a/src/main/java/com/github/difflib/patch/ChangeDelta.java b/src/main/java/com/github/difflib/patch/ChangeDelta.java
index 34a8bd6b..9a90b9cb 100644
--- a/src/main/java/com/github/difflib/patch/ChangeDelta.java
+++ b/src/main/java/com/github/difflib/patch/ChangeDelta.java
@@ -23,9 +23,10 @@
/**
* Describes the change-delta between original and revised texts.
+ *
+ * T The type of the compared elements in the 'lines'.
*
* @author Dmitry Naumenko
- * @param T The type of the compared elements in the 'lines'.
*/
public final class ChangeDelta extends Delta {
@@ -33,7 +34,7 @@ public final class ChangeDelta extends Delta {
* Creates a change delta with the two given chunks.
*
* @param original The original chunk. Must not be {@code null}.
- * @param revised The original chunk. Must not be {@code null}.
+ * @param revised The original chunk. Must not be {@code null}.
*/
public ChangeDelta(Chunk original, Chunk revised) {
super(DeltaType.CHANGE, original, revised);
diff --git a/src/main/java/com/github/difflib/patch/Chunk.java b/src/main/java/com/github/difflib/patch/Chunk.java
index 69413511..23be93bd 100644
--- a/src/main/java/com/github/difflib/patch/Chunk.java
+++ b/src/main/java/com/github/difflib/patch/Chunk.java
@@ -81,12 +81,14 @@ public void verify(List target) throws PatchFailedException {
}
}
for (int i = 0; i < before.size(); i++) {
- if (!before.get(i).equals(target.get(position - before.size() + i)))
+ if (!before.get(i).equals(target.get(position - before.size() + i))) {
throw new PatchFailedException("Incorrect Chunk: the chunk's surroundings (before) doesn't match the target");
+ }
}
for (int i = 0; i < after.size(); i++) {
- if (!after.get(i).equals(target.get(last() + after.size() - i)))
+ if (!after.get(i).equals(target.get(last() + after.size() - i))) {
throw new PatchFailedException("Incorrect Chunk: the chunk's surroundings (after) doesn't match the target");
+ }
}
}
diff --git a/src/main/java/com/github/difflib/patch/DeleteDelta.java b/src/main/java/com/github/difflib/patch/DeleteDelta.java
index 0ef14e91..fd0bb155 100644
--- a/src/main/java/com/github/difflib/patch/DeleteDelta.java
+++ b/src/main/java/com/github/difflib/patch/DeleteDelta.java
@@ -23,9 +23,10 @@
/**
* Describes the delete-delta between original and revised texts.
+ *
+ * T The type of the compared elements in the 'lines'.
*
* @author Dmitry Naumenko
- * @param T The type of the compared elements in the 'lines'.
*/
public final class DeleteDelta extends Delta {
@@ -33,7 +34,7 @@ public final class DeleteDelta extends Delta {
* Creates a change delta with the two given chunks.
*
* @param original The original chunk. Must not be {@code null}.
- * @param revised The original chunk. Must not be {@code null}.
+ * @param revised The original chunk. Must not be {@code null}.
*/
public DeleteDelta(Chunk original, Chunk revised) {
super(DeltaType.DELETE, original, revised);
diff --git a/src/main/java/com/github/difflib/patch/Delta.java b/src/main/java/com/github/difflib/patch/Delta.java
index 769e7fa6..4af3d3a4 100644
--- a/src/main/java/com/github/difflib/patch/Delta.java
+++ b/src/main/java/com/github/difflib/patch/Delta.java
@@ -24,9 +24,10 @@
/**
* Describes the delta between original and revised texts.
+ *
+ * T The type of the compared elements in the 'lines'.
*
* @author Dmitry Naumenko
- * @param T The type of the compared elements in the 'lines'.
*/
public abstract class Delta {
@@ -38,7 +39,7 @@ public abstract class Delta {
* Construct the delta for original and revised chunks
*
* @param original Chunk describing the original text. Must not be {@code null}.
- * @param revised Chunk describing the revised text. Must not be {@code null}.
+ * @param revised Chunk describing the revised text. Must not be {@code null}.
*/
public Delta(DeltaType deltaType, Chunk