From b063ca561a58e4a4e6b15b5949f7634747911aae Mon Sep 17 00:00:00 2001 From: Christopher So Date: Tue, 24 Oct 2017 23:01:42 +0200 Subject: [PATCH 1/6] Update README.md --- README.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/README.md b/README.md index 0cf16e4e..856e25ba 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,4 @@ -# java-diff-utils - -## Status ## -[![Build Status](https://travis-ci.org/wumpz/java-diff-utils.svg?branch=master)](https://travis-ci.org/wumpz/java-diff-utils) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/7eba77f10bed4c2a8d08ac8dc8da4a86)](https://www.codacy.com/app/wumpz/java-diff-utils?utm_source=github.com&utm_medium=referral&utm_content=wumpz/java-diff-utils&utm_campaign=Badge_Grade) +# java-diff-utils [![Build Status](https://api.travis-ci.org/Kogoro/java-diff-utils.svg?branch=master)](https://travis-ci.org/Kogoro/java-diff-utils) ## Intro ## Diff Utils library is an OpenSource library for performing the comparison operations between texts: computing diffs, applying patches, generating unified diffs or parsing them, generating diff output for easy future displaying (like side-by-side view) and so on. From e6fa5938a9a3b3f55adc0cec45e7183c4802412e Mon Sep 17 00:00:00 2001 From: wumpz Date: Wed, 25 Oct 2017 08:49:20 +0200 Subject: [PATCH 2/6] included checkstyle source code conventions --- README.md | 22 + java-diff-utils.iml | 58 +-- nb-configuration.xml | 38 +- pom.xml | 451 ++++++++++-------- .../java/com/github/difflib/DiffUtils.java | 39 +- .../com/github/difflib/UnifiedDiffUtils.java | 16 +- .../com/github/difflib/algorithm/Change.java | 1 + .../difflib/algorithm/DiffAlgorithm.java | 8 +- .../DifferentiationFailedException.java | 7 +- .../difflib/algorithm/jgit/HistogramDiff.java | 15 +- .../difflib/algorithm/myers/MyersDiff.java | 18 +- .../difflib/algorithm/myers/PathNode.java | 11 +- .../java/com/github/difflib/patch/Chunk.java | 7 +- .../com/github/difflib/patch/DeleteDelta.java | 2 +- .../java/com/github/difflib/patch/Delta.java | 2 +- .../com/github/difflib/patch/DeltaType.java | 3 +- .../com/github/difflib/patch/InsertDelta.java | 2 +- .../java/com/github/difflib/patch/Patch.java | 10 +- .../java/com/github/difflib/text/DiffRow.java | 3 +- .../github/difflib/text/DiffRowGenerator.java | 2 +- .../com/github/difflib/DiffUtilsTest.java | 15 +- .../difflib/GenerateUnifiedDiffTest.java | 2 - .../com/github/difflib/TestConstants.java | 1 + .../algorithm/jgit/HistogramDiffTest.java | 23 +- .../algorithm/jgit/LRHistogramDiffTest.java | 23 +- .../algorithm/myers/MyersDiffTest.java | 8 +- .../com/github/difflib/patch/PatchTest.java | 2 - .../difflib/text/DiffRowGeneratorTest.java | 49 +- .../github/difflib/text/StringUtilsTest.java | 10 +- 29 files changed, 445 insertions(+), 403 deletions(-) diff --git a/README.md b/README.md index 9b55899f..ca9f2979 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,8 @@ This is a test ~senctence~**for diffutils**. But it can easily replaced by any other which is better for handing your texts. I have plan to add implementation of some in future. ### Changelog ### + * Version 2.1-SNAPSHOT + * included checkstyle source code conventions * Version 2.0 * switch to maven and removed other artifacts * changed groupid to **com.github.java-diff-utils** due to different forks at github @@ -66,6 +68,26 @@ But it can easily replaced by any other which is better for handing your texts. * Ant build script * Generate output in unified diff format (thanks for Bill James) +## Source Code conventions + +Recently a checkstyle process was integrated into the build process. JSqlParser follows the sun java format convention. There are no TABs allowed. Use spaces. + +```java +public static Patch diff(List original, List revised, + BiPredicate equalizer) throws DiffException { + if (equalizer != null) { + return DiffUtils.diff(original, revised, + new MyersDiff<>(equalizer)); + } + return DiffUtils.diff(original, revised, new MyersDiff<>()); +} +``` + +This is a valid piece of source code: +* blocks without braces are not allowed +* after control statements (if, while, for) a whitespace is expected +* the opening brace should be in the same line as the control statement + ### To Install ### **This jar is not yet to get at maven central.** diff --git a/java-diff-utils.iml b/java-diff-utils.iml index 5f6b53a8..1dc1f4d5 100644 --- a/java-diff-utils.iml +++ b/java-diff-utils.iml @@ -1,29 +1,29 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/nb-configuration.xml b/nb-configuration.xml index f1692631..f5e3165a 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 2c86f6a7..c15a9a96 100644 --- a/pom.xml +++ b/pom.xml @@ -1,213 +1,272 @@ - 4.0.0 - com.github.java-diff-utils - diffutils - jar - 2.1-SNAPSHOT + 4.0.0 + com.github.java-diff-utils + diffutils + jar + 2.1-SNAPSHOT - java-diff-utils - The DiffUtils library for computing diffs, applying patches, generationg side-by-side view in Java. - https://github.com/wumpz/java-diff-utils - 2009 + java-diff-utils + 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.0 - + + 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.0 + - - GitHub Issues - https://github.com/wumpz/java-diff-utils/issues - + + GitHub Issues + https://github.com/wumpz/java-diff-utils/issues + - - java-diff-utils - + + java-diff-utils + - - - Tobias Warneke - t.warneke@gmx.net - - - Dmitry Naumenko - dm.naumenko@gmail.com - - - Juanco Anez - juanco@suigeneris.org - - + + + Tobias Warneke + t.warneke@gmx.net + + + Dmitry Naumenko + dm.naumenko@gmail.com + + + Juanco Anez + juanco@suigeneris.org + + - - - The Apache Software License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.txt - repo - A business-friendly OSS license - - + + + The Apache Software License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + A business-friendly OSS license + + - - UTF-8 - + + UTF-8 + - - - junit - junit - 4.12 - jar - test - - - org.eclipse.jgit - org.eclipse.jgit - 4.4.1.201607150455-r - - - com.googlecode.javaewah - JavaEWAH - - - commons-codec - commons-codec - - - commons-logging - commons-logging - - - org.apache.httpcomponents - httpclient - - - com.jcraft - jsch - - - org.slf4j - slf4j-api - - - - + + + junit + junit + 4.12 + jar + test + + + org.eclipse.jgit + org.eclipse.jgit + 4.4.1.201607150455-r + + + com.googlecode.javaewah + JavaEWAH + + + commons-codec + commons-codec + + + commons-logging + commons-logging + + + org.apache.httpcomponents + httpclient + + + com.jcraft + jsch + + + org.slf4j + slf4j-api + + + + - - + + - - org.apache.maven.plugins - maven-compiler-plugin - 3.6.1 - - 1.8 - 1.8 - UTF-8 - - + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.1 + + 1.8 + 1.8 + UTF-8 + + - - - - maven-jar-plugin - 3.0.2 - - - ${project.build.outputDirectory}/META-INF/MANIFEST.MF - - - - - org.apache.felix - maven-bundle-plugin - 3.3.0 - - - bundle-manifest - process-classes - - manifest - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 2.10.4 - - ${javadoc.opts} - - - - attach-javadocs - - jar - - - - - - org.apache.maven.plugins - maven-surefire-plugin - 2.19.1 - - - **/LR*.java - - - - - org.apache.maven.plugins - maven-release-plugin - 2.5.3 - - true - false - forked-path + + + + maven-jar-plugin + 3.0.2 + + + ${project.build.outputDirectory}/META-INF/MANIFEST.MF + + + + + org.apache.felix + maven-bundle-plugin + 3.3.0 + + + bundle-manifest + process-classes + + manifest + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.10.4 + + ${javadoc.opts} + + + + attach-javadocs + + jar + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.19.1 + + + **/LR*.java + + + + + org.apache.maven.plugins + maven-release-plugin + 2.5.3 + + true + false + forked-path install - - - - - - - doclint-java8-disable - - [1.8,) - - - -Xdoclint:none - - - - long-running-tests - - - - org.apache.maven.plugins - maven-surefire-plugin - - - xxx - - - - - - - + + + + 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 + + + + + + + diff --git a/src/main/java/com/github/difflib/DiffUtils.java b/src/main/java/com/github/difflib/DiffUtils.java index c0a7a74c..807d7b4a 100644 --- a/src/main/java/com/github/difflib/DiffUtils.java +++ b/src/main/java/com/github/difflib/DiffUtils.java @@ -42,18 +42,16 @@ public final class DiffUtils { /** - * Computes the difference between the original and revised list of elements with default diff - * algorithm + * 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}. - * @return The patch describing the difference between the original and revised sequences. Never - * {@code null}. + * @return The patch describing the difference between the original and revised sequences. Never {@code null}. */ public static Patch diff(List original, List revised) throws DiffException { return DiffUtils.diff(original, revised, new MyersDiff<>()); } - + /** * Computes the difference between the original and revised text. */ @@ -62,19 +60,17 @@ public static Patch diff(String originalText, String revisedText) throws } /** - * Computes the difference between the original and revised list of elements with default diff - * algorithm + * 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.. - * @return The patch describing the difference between the original and revised sequences. Never - * {@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.. + * @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) throws DiffException { + BiPredicate equalizer) throws DiffException { if (equalizer != null) { return DiffUtils.diff(original, revised, new MyersDiff<>(equalizer)); @@ -83,28 +79,25 @@ public static Patch diff(List original, List revised, } /** - * Computes the difference between the original and revised list of elements with default diff - * algorithm + * 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}. - * @return The patch describing the difference between the original and revised sequences. Never - * {@code null}. + * @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) 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"); - + 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)); } /** - * Computes the difference between the given texts inline. This one uses the "trick" to make out - * of texts lists of characters, like DiffRowGenerator does and merges those changes at the end - * together again. + * Computes the difference between the given texts inline. This one uses the "trick" to make out of texts lists of + * characters, like DiffRowGenerator does and merges those changes at the end together again. * * @param original * @param revised diff --git a/src/main/java/com/github/difflib/UnifiedDiffUtils.java b/src/main/java/com/github/difflib/UnifiedDiffUtils.java index c48b050f..1395c795 100644 --- a/src/main/java/com/github/difflib/UnifiedDiffUtils.java +++ b/src/main/java/com/github/difflib/UnifiedDiffUtils.java @@ -29,6 +29,7 @@ * @author toben */ public final class UnifiedDiffUtils { + private static final Pattern UNIFIED_DIFF_CHUNK_REGEXP = Pattern .compile("^@@\\s+-(?:(\\d+)(?:,(\\d+))?)\\s+\\+(?:(\\d+)(?:,(\\d+))?)\\s+@@$"); @@ -124,10 +125,10 @@ public static Patch parseUnifiedDiff(List diff) { return patch; } - + /** - * generateUnifiedDiff takes a Patch and some other arguments, returning the Unified Diff format - * text representing the Patch. + * 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 @@ -195,10 +196,9 @@ public static List generateUnifiedDiff(String original, } return new ArrayList<>(); } - + /** - * processDeltas takes a list of Deltas and outputs them together in a single block of - * Unified-Diff-format text. + * 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 @@ -289,7 +289,7 @@ private static List processDeltas(List origLines, return buffer; } - + /** * getDeltaText returns the lines to be added to the Unified Diff text from the Delta parameter * @@ -307,7 +307,7 @@ private static List getDeltaText(Delta delta) { } return buffer; } - + private UnifiedDiffUtils() { } } diff --git a/src/main/java/com/github/difflib/algorithm/Change.java b/src/main/java/com/github/difflib/algorithm/Change.java index eb21d3fe..b90e2022 100644 --- a/src/main/java/com/github/difflib/algorithm/Change.java +++ b/src/main/java/com/github/difflib/algorithm/Change.java @@ -22,6 +22,7 @@ * @author toben */ public class Change { + public final DeltaType deltaType; public final int startOriginal; public final int endOriginal; diff --git a/src/main/java/com/github/difflib/algorithm/DiffAlgorithm.java b/src/main/java/com/github/difflib/algorithm/DiffAlgorithm.java index e6a20151..b97a15a4 100644 --- a/src/main/java/com/github/difflib/algorithm/DiffAlgorithm.java +++ b/src/main/java/com/github/difflib/algorithm/DiffAlgorithm.java @@ -31,8 +31,8 @@ public interface DiffAlgorithm { /** - * Computes the difference between the original sequence and the revised sequence and returns it - * as a {@link Patch} object. + * Computes the difference between the original sequence and the revised sequence and returns it as a {@link Patch} + * object. * * @param original The original sequence. Must not be {@code null}. * @param revised The revised sequence. Must not be {@code null}. @@ -43,8 +43,8 @@ public default List diff(T[] original, T[] revised) throws DiffException } /** - * Computes the difference between the original sequence and the revised sequence and returns it - * as a {@link Patch} object. + * Computes the difference between the original sequence and the revised sequence and returns it as a {@link Patch} + * object. * * @param original The original sequence. Must not be {@code null}. * @param revised The revised sequence. Must not be {@code null}. diff --git a/src/main/java/com/github/difflib/algorithm/DifferentiationFailedException.java b/src/main/java/com/github/difflib/algorithm/DifferentiationFailedException.java index 132288f5..80c881e5 100644 --- a/src/main/java/com/github/difflib/algorithm/DifferentiationFailedException.java +++ b/src/main/java/com/github/difflib/algorithm/DifferentiationFailedException.java @@ -19,12 +19,9 @@ */ package com.github.difflib.algorithm; -import com.github.difflib.algorithm.DiffException; - /** - * Thrown whenever the differencing engine cannot produce the differences between two revisions of - * ta text. - + * Thrown whenever the differencing engine cannot produce the differences between two revisions of ta text. + * * @see MyersDiff * @see difflib.DiffAlgorithm */ 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 978f56e9..6ca80103 100644 --- a/src/main/java/com/github/difflib/algorithm/jgit/HistogramDiff.java +++ b/src/main/java/com/github/difflib/algorithm/jgit/HistogramDiff.java @@ -28,9 +28,8 @@ import org.eclipse.jgit.diff.SequenceComparator; /** - * HistorgramDiff using JGit - Library. This one is much more performant than the - * orginal Myers implementation. - * + * HistorgramDiff using JGit - Library. This one is much more performant than the orginal Myers implementation. + * * @author toben */ public class HistogramDiff implements DiffAlgorithm { @@ -43,19 +42,19 @@ public List diff(List original, List revised) throws DiffException diffList.addAll(new org.eclipse.jgit.diff.HistogramDiff().diff(new DataListComparator<>(), new DataList<>(original), new DataList<>(revised))); List patch = new ArrayList<>(); for (Edit edit : diffList) { - DeltaType type = DeltaType.EQUAL; + DeltaType type = DeltaType.EQUAL; switch (edit.getType()) { case DELETE: - type = DeltaType.DELETE; + type = DeltaType.DELETE; break; case INSERT: - type = DeltaType.INSERT; + type = DeltaType.INSERT; break; case REPLACE: - type = DeltaType.CHANGE; + type = DeltaType.CHANGE; break; } - patch.add(new Change(type,edit.getBeginA(), edit.getEndA(), edit.getBeginB(), edit.getEndB())); + patch.add(new Change(type, edit.getBeginA(), edit.getEndA(), edit.getBeginB(), edit.getEndB())); } return patch; } 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 6907f681..ec76ec3f 100644 --- a/src/main/java/com/github/difflib/algorithm/myers/MyersDiff.java +++ b/src/main/java/com/github/difflib/algorithm/myers/MyersDiff.java @@ -20,9 +20,9 @@ package com.github.difflib.algorithm.myers; import com.github.difflib.algorithm.Change; -import com.github.difflib.algorithm.DifferentiationFailedException; import com.github.difflib.algorithm.DiffAlgorithm; import com.github.difflib.algorithm.DiffException; +import com.github.difflib.algorithm.DifferentiationFailedException; import com.github.difflib.patch.DeltaType; import com.github.difflib.patch.Patch; import java.util.ArrayList; @@ -35,16 +35,14 @@ */ public final class MyersDiff implements DiffAlgorithm { - private final BiPredicate DEFAULT_EQUALIZER = Object::equals; - private final BiPredicate equalizer; - + private final BiPredicate DEFAULT_EQUALIZER = Object::equals; + private final BiPredicate equalizer; public MyersDiff() { equalizer = DEFAULT_EQUALIZER; } - - public MyersDiff(final BiPredicate equalizer) { + public MyersDiff(final BiPredicate equalizer) { Objects.requireNonNull(equalizer, "equalizer must not be null"); this.equalizer = equalizer; } @@ -64,9 +62,8 @@ public List diff(final List original, final List revised) throws D } /** - * Computes the minimum diffpath that expresses de differences between the - * original and revised sequences, according to Gene Myers differencing - * algorithm. + * Computes the minimum diffpath that expresses de differences between the original and revised sequences, according + * to Gene Myers differencing algorithm. * * @param orig The original sequence. * @param rev The revised sequence. @@ -138,8 +135,7 @@ private PathNode buildPath(final List orig, final List rev) * @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. + * @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 c60b4829..01d669f7 100644 --- a/src/main/java/com/github/difflib/algorithm/myers/PathNode.java +++ b/src/main/java/com/github/difflib/algorithm/myers/PathNode.java @@ -44,7 +44,7 @@ public final class PathNode { public final PathNode prev; public final boolean snake; - + public final boolean bootstrap; /** @@ -61,7 +61,7 @@ public PathNode(int i, int j, boolean snake, boolean bootstrap, PathNode prev) { if (snake) { this.prev = prev; } else { - this.prev = (prev == null ? null : prev.previousSnake()); + this.prev = prev == null ? null : prev.previousSnake(); } this.snake = snake; } @@ -82,11 +82,10 @@ public boolean isBootstrap() { } /** - * Skips sequences of {@link DiffNode DiffNodes} until a {@link Snake} or bootstrap node is - * found, or the end of the path is reached. + * Skips sequences of {@link DiffNode DiffNodes} until a {@link 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 {@link 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/Chunk.java b/src/main/java/com/github/difflib/patch/Chunk.java index 8b21e7b8..5855c3f1 100644 --- a/src/main/java/com/github/difflib/patch/Chunk.java +++ b/src/main/java/com/github/difflib/patch/Chunk.java @@ -26,10 +26,9 @@ * 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 {@link java.lang.Object#equals equals()} - * correctly can be subject to differencing using this library. + * 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 + * {@link java.lang.Object#equals equals()} correctly can be subject to differencing using this library. *

* * @author target) throws PatchFailedException { target.add(position + i, lines.get(i)); } } - + @Override public void restore(List target) { int position = getRevised().getPosition(); diff --git a/src/main/java/com/github/difflib/patch/Patch.java b/src/main/java/com/github/difflib/patch/Patch.java index 4d2f2e4d..ec7c357b 100644 --- a/src/main/java/com/github/difflib/patch/Patch.java +++ b/src/main/java/com/github/difflib/patch/Patch.java @@ -41,11 +41,11 @@ public final class Patch { public Patch() { this(10); } - + public Patch(int estimatedPatchSize) { - deltas = new ArrayList<>(estimatedPatchSize); + deltas = new ArrayList<>(estimatedPatchSize); } - + /** * Apply this patch to the given target * @@ -87,7 +87,7 @@ public void addDelta(Delta delta) { deltas.add(delta); } - /** + /** * Get the list of computed deltas * * @return the deltas @@ -101,7 +101,7 @@ public List> getDeltas() { public String toString() { return "Patch{" + "deltas=" + deltas + '}'; } - + public static Patch generate(List original, List revised, List changes) { Patch patch = new Patch<>(changes.size()); for (Change change : changes) { diff --git a/src/main/java/com/github/difflib/text/DiffRow.java b/src/main/java/com/github/difflib/text/DiffRow.java index f6ab5f5b..dd8cd9d6 100644 --- a/src/main/java/com/github/difflib/text/DiffRow.java +++ b/src/main/java/com/github/difflib/text/DiffRow.java @@ -22,8 +22,7 @@ import java.io.Serializable; /** - * Describes the diff row in form [tag, oldLine, newLine) for showing the difference between two - * texts + * Describes the diff row in form [tag, oldLine, newLine) for showing the difference between two texts * * @author Dmitry Naumenko */ diff --git a/src/main/java/com/github/difflib/text/DiffRowGenerator.java b/src/main/java/com/github/difflib/text/DiffRowGenerator.java index e25e9286..ad13542b 100644 --- a/src/main/java/com/github/difflib/text/DiffRowGenerator.java +++ b/src/main/java/com/github/difflib/text/DiffRowGenerator.java @@ -43,7 +43,7 @@ * * 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(); + * ignoreWhiteSpaces(true).columnWidth(100).build(); * */ public class DiffRowGenerator { diff --git a/src/test/java/com/github/difflib/DiffUtilsTest.java b/src/test/java/com/github/difflib/DiffUtilsTest.java index d8aa3cf1..d815401b 100644 --- a/src/test/java/com/github/difflib/DiffUtilsTest.java +++ b/src/test/java/com/github/difflib/DiffUtilsTest.java @@ -1,6 +1,5 @@ package com.github.difflib; -import com.github.difflib.DiffUtils; import com.github.difflib.algorithm.DiffException; import com.github.difflib.patch.ChangeDelta; import com.github.difflib.patch.Chunk; @@ -138,25 +137,25 @@ 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("ta"))), readStringListFromInputStream(zip.getInputStream(zip.getEntry("tb")))); - + assertEquals(1, patch.getDeltas().size()); } - + public static List readStringListFromInputStream(InputStream is) throws IOException { try (BufferedReader reader = new BufferedReader( new InputStreamReader(is, Charset.forName(StandardCharsets.UTF_8.name())))) { - + return reader.lines().collect(toList()); } } - + @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")); 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 79ceec3e..89ab32f6 100644 --- a/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java +++ b/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java @@ -1,7 +1,5 @@ package com.github.difflib; -import com.github.difflib.DiffUtils; -import com.github.difflib.UnifiedDiffUtils; import com.github.difflib.algorithm.DiffException; import com.github.difflib.patch.Patch; import com.github.difflib.patch.PatchFailedException; diff --git a/src/test/java/com/github/difflib/TestConstants.java b/src/test/java/com/github/difflib/TestConstants.java index 7abb9a4d..56f5bf66 100644 --- a/src/test/java/com/github/difflib/TestConstants.java +++ b/src/test/java/com/github/difflib/TestConstants.java @@ -7,6 +7,7 @@ * */ public final class TestConstants { + public static final String BASE_FOLDER_RESOURCES = "target/test-classes/"; /** * The base folder containing the test files. Ends with {@link #FS}. 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 1de65885..ec44adea 100644 --- a/src/test/java/com/github/difflib/algorithm/jgit/HistogramDiffTest.java +++ b/src/test/java/com/github/difflib/algorithm/jgit/HistogramDiffTest.java @@ -15,44 +15,39 @@ */ package com.github.difflib.algorithm.jgit; -import com.github.difflib.algorithm.jgit.HistogramDiff; -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 java.io.IOException; import java.util.Arrays; 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 org.junit.Assert.*; /** * * @author toben */ public class HistogramDiffTest { - + public HistogramDiffTest() { } - + @BeforeClass public static void setUpClass() { } - + @AfterClass public static void tearDownClass() { } - + @Before public void setUp() { } - + @After public void tearDown() { } @@ -62,14 +57,14 @@ public void tearDown() { */ @Test 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"); + 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)); System.out.println(patch); assertNotNull(patch); assertEquals(3, patch.getDeltas().size()); assertEquals("Patch{deltas=[[DeleteDelta, position: 0, lines: [A, B]], [DeleteDelta, position: 3, lines: [A, B]], [InsertDelta, position: 7, lines: [B, A, C]]]}", patch.toString()); - + List patched = patch.applyTo(orgList); assertEquals(revList, patched); } 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 6ddb26de..93e2950e 100644 --- a/src/test/java/com/github/difflib/algorithm/jgit/LRHistogramDiffTest.java +++ b/src/test/java/com/github/difflib/algorithm/jgit/LRHistogramDiffTest.java @@ -15,61 +15,58 @@ */ package com.github.difflib.algorithm.jgit; -import com.github.difflib.algorithm.jgit.HistogramDiff; 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 java.io.IOException; -import java.util.Arrays; 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 org.junit.Assert.*; /** * * @author toben */ public class LRHistogramDiffTest { - + public LRHistogramDiffTest() { } - + @BeforeClass public static void setUpClass() { } - + @AfterClass public static void tearDownClass() { } - + @Before public void setUp() { } - + @After public void tearDown() { } - @Test public void testPossibleDiffHangOnLargeDatasetDnaumenkoIssue26() throws IOException, DiffException, PatchFailedException { ZipFile zip = new ZipFile(TestConstants.MOCK_FOLDER + "/large_dataset1.zip"); 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)); - + assertEquals(34, patch.getDeltas().size()); - + List created = patch.applyTo(original); assertArrayEquals(revised.toArray(), created.toArray()); } - + } 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 6ef04e69..9b911ff1 100644 --- a/src/test/java/com/github/difflib/algorithm/myers/MyersDiffTest.java +++ b/src/test/java/com/github/difflib/algorithm/myers/MyersDiffTest.java @@ -15,18 +15,12 @@ */ package com.github.difflib.algorithm.myers; -import com.github.difflib.algorithm.myers.MyersDiff; -import com.github.difflib.DiffUtils; import com.github.difflib.algorithm.DiffException; import com.github.difflib.patch.Patch; import java.util.Arrays; import java.util.List; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; import static org.junit.Assert.*; +import org.junit.Test; /** * diff --git a/src/test/java/com/github/difflib/patch/PatchTest.java b/src/test/java/com/github/difflib/patch/PatchTest.java index cfda69ae..792785eb 100644 --- a/src/test/java/com/github/difflib/patch/PatchTest.java +++ b/src/test/java/com/github/difflib/patch/PatchTest.java @@ -1,7 +1,5 @@ package com.github.difflib.patch; -import com.github.difflib.patch.Patch; -import com.github.difflib.patch.PatchFailedException; import com.github.difflib.DiffUtils; import com.github.difflib.algorithm.DiffException; import java.util.Arrays; diff --git a/src/test/java/com/github/difflib/text/DiffRowGeneratorTest.java b/src/test/java/com/github/difflib/text/DiffRowGeneratorTest.java index 616c189e..3326fe97 100644 --- a/src/test/java/com/github/difflib/text/DiffRowGeneratorTest.java +++ b/src/test/java/com/github/difflib/text/DiffRowGeneratorTest.java @@ -1,7 +1,5 @@ package com.github.difflib.text; -import com.github.difflib.text.DiffRow; -import com.github.difflib.text.DiffRowGenerator; import com.github.difflib.algorithm.DiffException; import java.util.Arrays; import java.util.List; @@ -24,7 +22,7 @@ public void testGenerator_Default() throws DiffException { assertEquals(3, rows.size()); } - + @Test public void testGenerator_Default2() throws DiffException { String first = "anything \n \nother"; @@ -83,14 +81,14 @@ private void print(List diffRows) { System.out.println(row); } } - + @Test public void testGeneratorWithWordWrap() throws DiffException { String first = "anything \n \nother"; String second = "anything\n\nother"; DiffRowGenerator generator = DiffRowGenerator.create() - .columnWidth(5) + .columnWidth(5) .build(); List rows = generator.generateDiffRows(split(first), split(second)); print(rows); @@ -100,7 +98,7 @@ public void testGeneratorWithWordWrap() throws DiffException { assertEquals("[CHANGE, ,]", rows.get(1).toString()); assertEquals("[EQUAL,other,other]", rows.get(2).toString()); } - + @Test public void testGeneratorWithMerge() throws DiffException { String first = "anything \n \nother"; @@ -118,20 +116,20 @@ public void testGeneratorWithMerge() throws DiffException { assertEquals("[CHANGE, ,]", rows.get(1).toString()); assertEquals("[EQUAL,other,other]", rows.get(2).toString()); } - + @Test public void testGeneratorWithMerge2() throws DiffException { DiffRowGenerator generator = DiffRowGenerator.create() .showInlineDiffs(true) .mergeOriginalRevised(true) .build(); - List rows = generator.generateDiffRows(Arrays.asList("Test"),Arrays.asList("ester")); + List rows = generator.generateDiffRows(Arrays.asList("Test"), Arrays.asList("ester")); print(rows); assertEquals(1, rows.size()); assertEquals("[CHANGE,Tester,ester]", rows.get(0).toString()); } - + @Test public void testGeneratorWithMerge3() throws DiffException { String first = "test\nanything \n \nother"; @@ -152,7 +150,7 @@ public void testGeneratorWithMerge3() throws DiffException { assertEquals("[INSERT,test,test]", rows.get(4).toString()); assertEquals("[INSERT,test2,test2]", rows.get(5).toString()); } - + @Test public void testGeneratorWithMergeByWord4() throws DiffException { DiffRowGenerator generator = DiffRowGenerator.create() @@ -160,13 +158,13 @@ public void testGeneratorWithMergeByWord4() throws DiffException { .mergeOriginalRevised(true) .inlineDiffByWord(true) .build(); - List rows = generator.generateDiffRows(Arrays.asList("Test"),Arrays.asList("ester")); + List rows = generator.generateDiffRows(Arrays.asList("Test"), Arrays.asList("ester")); print(rows); assertEquals(1, rows.size()); assertEquals("[CHANGE,Testester,ester]", rows.get(0).toString()); } - + @Test public void testGeneratorWithMergeByWord5() throws DiffException { DiffRowGenerator generator = DiffRowGenerator.create() @@ -175,20 +173,20 @@ public void testGeneratorWithMergeByWord5() throws DiffException { .inlineDiffByWord(true) .columnWidth(80) .build(); - List rows = generator.generateDiffRows(Arrays.asList("Test feature"),Arrays.asList("ester feature best")); + List rows = generator.generateDiffRows(Arrays.asList("Test feature"), Arrays.asList("ester feature best")); print(rows); assertEquals(1, rows.size()); assertEquals("[CHANGE,Testester
feature best,ester feature best]", rows.get(0).toString()); } - + @Test public void testSplitString() { List list = DiffRowGenerator.splitStringPreserveDelimiter("test,test2"); assertEquals(3, list.size()); assertEquals("[test, ,, test2]", list.toString()); } - + @Test public void testSplitString2() { List list = DiffRowGenerator.splitStringPreserveDelimiter("test , test2"); @@ -196,7 +194,7 @@ public void testSplitString2() { assertEquals(5, list.size()); assertEquals("[test, , ,, , test2]", list.toString()); } - + @Test public void testSplitString3() { List list = DiffRowGenerator.splitStringPreserveDelimiter("test,test2,"); @@ -204,8 +202,7 @@ public void testSplitString3() { assertEquals(4, list.size()); assertEquals("[test, ,, test2, ,]", list.toString()); } - - + @Test public void testGeneratorExample1() throws DiffException { DiffRowGenerator generator = DiffRowGenerator.create() @@ -218,13 +215,13 @@ public void testGeneratorExample1() throws DiffException { List 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()); - + + System.out.println(rows.get(0).getOldLine()); + assertEquals(1, rows.size()); assertEquals("This is a test ~senctence~**for diffutils**.", rows.get(0).getOldLine()); } - + @Test public void testGeneratorExample2() throws DiffException { DiffRowGenerator generator = DiffRowGenerator.create() @@ -236,25 +233,25 @@ public void testGeneratorExample2() throws DiffException { List rows = generator.generateDiffRows( Arrays.asList("This is a test senctence.", "This is the second line.", "And here is the finish."), Arrays.asList("This is a test for diffutils.", "This is the second line.")); - + System.out.println("|original|new|"); System.out.println("|--------|---|"); for (DiffRow row : rows) { System.out.println("|" + row.getOldLine() + "|" + row.getNewLine() + "|"); } - + assertEquals(3, rows.size()); assertEquals("This is a test ~senctence~.", rows.get(0).getOldLine()); assertEquals("This is a test **for diffutils**.", rows.get(0).getNewLine()); } - + @Test public void testGeneratorUnchanged() throws DiffException { String first = "anything \n \nother"; String second = "anything\n\nother"; DiffRowGenerator generator = DiffRowGenerator.create() - .columnWidth(5) + .columnWidth(5) .reportLinesUnchanged(true) .build(); List rows = generator.generateDiffRows(split(first), split(second)); diff --git a/src/test/java/com/github/difflib/text/StringUtilsTest.java b/src/test/java/com/github/difflib/text/StringUtilsTest.java index ad8f1db2..2b120d09 100644 --- a/src/test/java/com/github/difflib/text/StringUtilsTest.java +++ b/src/test/java/com/github/difflib/text/StringUtilsTest.java @@ -16,8 +16,8 @@ package com.github.difflib.text; import java.util.Collections; -import org.junit.Test; import static org.junit.Assert.*; +import org.junit.Test; /** * @@ -38,7 +38,7 @@ public void testHtmlEntites() { */ @Test public void testNormalize_String() { - assertEquals(" test",StringUtils.normalize("\ttest")); + assertEquals(" test", StringUtils.normalize("\ttest")); } /** @@ -46,7 +46,7 @@ public void testNormalize_String() { */ @Test public void testNormalize_List() { - assertEquals(Collections.singletonList(" test"),StringUtils.normalize(Collections.singletonList("\ttest"))); + assertEquals(Collections.singletonList(" test"), StringUtils.normalize(Collections.singletonList("\ttest"))); } /** @@ -58,10 +58,10 @@ public void testWrapText_String_int() { assertEquals("tes
t", StringUtils.wrapText("test", 3)); assertEquals("test", StringUtils.wrapText("test", 10)); } - + @Test(expected = IllegalArgumentException.class) public void testWrapText_String_int_zero() { assertEquals("test", StringUtils.wrapText("test", -1)); } - + } From a7ba8e03212010e423a6b20183600f747a761b8d Mon Sep 17 00:00:00 2001 From: Kogoro Date: Tue, 24 Oct 2017 22:36:42 +0200 Subject: [PATCH 3/6] Added Chunk surroundings --- README.md | 2 + pom.xml | 340 +++++++++--------- .../java/com/github/difflib/DiffUtils.java | 43 +-- .../java/com/github/difflib/patch/Chunk.java | 32 +- .../java/com/github/difflib/patch/Patch.java | 35 +- .../github/difflib/text/DiffRowGenerator.java | 273 +++++++------- .../com/github/difflib/DiffUtilsTest.java | 143 ++++++-- .../difflib/GenerateUnifiedDiffTest.java | 10 +- .../algorithm/jgit/HistogramDiffTest.java | 13 +- .../algorithm/jgit/LRHistogramDiffTest.java | 17 +- .../algorithm/myers/MyersDiffTest.java | 9 +- .../difflib/examples/ComputeDifference.java | 3 +- .../com/github/difflib/patch/PatchTest.java | 10 +- 13 files changed, 539 insertions(+), 391 deletions(-) diff --git a/README.md b/README.md index ca9f2979..2a965de2 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,8 @@ This is a test ~senctence~**for diffutils**. But it can easily replaced by any other which is better for handing your texts. I have plan to add implementation of some in future. ### Changelog ### + * Version 2.2-SNAPSHOT + * Added chunk to support surroundings of differences * Version 2.1-SNAPSHOT * included checkstyle source code conventions * Version 2.0 diff --git a/pom.xml b/pom.xml index c15a9a96..c5a90936 100644 --- a/pom.xml +++ b/pom.xml @@ -1,182 +1,186 @@ - 4.0.0 - com.github.java-diff-utils - diffutils - jar - 2.1-SNAPSHOT + 4.0.0 + com.github.java-diff-utils + diffutils + jar + 2.2-SNAPSHOT - java-diff-utils - The DiffUtils library for computing diffs, applying patches, generationg side-by-side view in Java. - https://github.com/wumpz/java-diff-utils - 2009 + java-diff-utils + 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.0 - + + 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 - + + GitHub Issues + https://github.com/wumpz/java-diff-utils/issues + - - java-diff-utils - + + java-diff-utils + - - - Tobias Warneke - t.warneke@gmx.net - - - Dmitry Naumenko - dm.naumenko@gmail.com - - - Juanco Anez - juanco@suigeneris.org - - + + + Christopher Sontag + ch.sontag@gmail.com + + + Tobias Warneke + t.warneke@gmx.net + + + Dmitry Naumenko + dm.naumenko@gmail.com + + + Juanco Anez + juanco@suigeneris.org + + - - - The Apache Software License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.txt - repo - A business-friendly OSS license - - + + + The Apache Software License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + A business-friendly OSS license + + - - UTF-8 - + + UTF-8 + - - - junit - junit - 4.12 - jar - test - - - org.eclipse.jgit - org.eclipse.jgit - 4.4.1.201607150455-r - - - com.googlecode.javaewah - JavaEWAH - - - commons-codec - commons-codec - - - commons-logging - commons-logging - - - org.apache.httpcomponents - httpclient - - - com.jcraft - jsch - - - org.slf4j - slf4j-api - - - - + + + junit + junit + 4.12 + jar + test + + + org.eclipse.jgit + org.eclipse.jgit + 4.4.1.201607150455-r + + + com.googlecode.javaewah + JavaEWAH + + + commons-codec + commons-codec + + + commons-logging + commons-logging + + + org.apache.httpcomponents + httpclient + + + com.jcraft + jsch + + + org.slf4j + slf4j-api + + + + - - + + - - org.apache.maven.plugins - maven-compiler-plugin - 3.6.1 - - 1.8 - 1.8 - UTF-8 - - + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.1 + + 1.8 + 1.8 + UTF-8 + + - - - - maven-jar-plugin - 3.0.2 - - - ${project.build.outputDirectory}/META-INF/MANIFEST.MF - - - - - org.apache.felix - maven-bundle-plugin - 3.3.0 - - - bundle-manifest - process-classes - - manifest - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 2.10.4 - - ${javadoc.opts} - - - - attach-javadocs - - jar - - - - - - org.apache.maven.plugins - maven-surefire-plugin - 2.19.1 - - - **/LR*.java - - - - - org.apache.maven.plugins - maven-release-plugin - 2.5.3 - - true - false - forked-path + + + + maven-jar-plugin + 3.0.2 + + + ${project.build.outputDirectory}/META-INF/MANIFEST.MF + + + + + org.apache.felix + maven-bundle-plugin + 3.3.0 + + + bundle-manifest + process-classes + + manifest + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.10.4 + + ${javadoc.opts} + + + + attach-javadocs + + jar + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.19.1 + + + **/LR*.java + + + + + org.apache.maven.plugins + maven-release-plugin + 2.5.3 + + true + false + forked-path install @@ -218,14 +222,14 @@ - + - + - + diff --git a/src/main/java/com/github/difflib/DiffUtils.java b/src/main/java/com/github/difflib/DiffUtils.java index 807d7b4a..41df8a70 100644 --- a/src/main/java/com/github/difflib/DiffUtils.java +++ b/src/main/java/com/github/difflib/DiffUtils.java @@ -25,19 +25,18 @@ import com.github.difflib.patch.Delta; import com.github.difflib.patch.Patch; import com.github.difflib.patch.PatchFailedException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Objects; + +import java.util.*; import java.util.function.BiPredicate; + import static java.util.stream.Collectors.joining; /** * Implements the difference and patching engine * * @author Dmitry Naumenko - * @version 0.4.1 + * @author Christopher Sontag + * @version 0.4.2 */ public final class DiffUtils { @@ -46,17 +45,18 @@ public final class DiffUtils { * * @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}. */ - public static Patch diff(List original, List revised) throws DiffException { - return DiffUtils.diff(original, revised, new MyersDiff<>()); + public static Patch diff(List original, List revised, int linesBeforeAfter) throws DiffException { + return DiffUtils.diff(original, revised, new MyersDiff<>(), linesBeforeAfter); } /** * Computes the difference between the original and revised text. */ - public static Patch diff(String originalText, String revisedText) throws DiffException { - return DiffUtils.diff(Arrays.asList(originalText.split("\n")), Arrays.asList(revisedText.split("\n"))); + public static Patch diff(String originalText, String revisedText, int linesBeforeAfter) throws DiffException { + return DiffUtils.diff(Arrays.asList(originalText.split("\n")), Arrays.asList(revisedText.split("\n")), linesBeforeAfter); } /** @@ -67,15 +67,16 @@ public static Patch diff(String originalText, String revisedText) throws * * @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) throws DiffException { + BiPredicate equalizer, int linesBeforeAfter) throws DiffException { if (equalizer != null) { return DiffUtils.diff(original, revised, - new MyersDiff<>(equalizer)); + new MyersDiff<>(equalizer), linesBeforeAfter); } - return DiffUtils.diff(original, revised, new MyersDiff<>()); + return DiffUtils.diff(original, revised, new MyersDiff<>(), linesBeforeAfter); } /** @@ -84,15 +85,16 @@ public static Patch diff(List original, List revised, * @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) 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)); + return Patch.generate(original, revised, algorithm.diff(original, revised), linesBeforeAfter); } /** @@ -101,9 +103,10 @@ public static Patch diff(List original, List revised, * * @param original * @param revised + * @param linesBeforeAfter - Amount of lines for before and after chunk content * @return */ - public static Patch diffInline(String original, String revised) throws DiffException { + public static Patch diffInline(String original, String revised, int linesBeforeAfter) throws DiffException { List origList = new ArrayList<>(); List revList = new ArrayList<>(); for (Character character : original.toCharArray()) { @@ -112,7 +115,7 @@ public static Patch diffInline(String original, String revised) throws D for (Character character : revised.toCharArray()) { revList.add(character.toString()); } - Patch patch = DiffUtils.diff(origList, revList); + Patch patch = DiffUtils.diff(origList, revList, linesBeforeAfter); for (Delta delta : patch.getDeltas()) { delta.getOriginal().setLines(compressLines(delta.getOriginal().getLines(), "")); delta.getRevised().setLines(compressLines(delta.getRevised().getLines(), "")); diff --git a/src/main/java/com/github/difflib/patch/Chunk.java b/src/main/java/com/github/difflib/patch/Chunk.java index 5855c3f1..ca0775e7 100644 --- a/src/main/java/com/github/difflib/patch/Chunk.java +++ b/src/main/java/com/github/difflib/patch/Chunk.java @@ -19,6 +19,7 @@ */ package com.github.difflib.patch; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -30,14 +31,17 @@ * fact, arrays or lists of any type that implements {@link java.lang.Object#hashCode hashCode()} and * {@link java.lang.Object#equals equals()} correctly can be subject to differencing using this library. *

+ * T The type of the compared elements in the 'lines'. * - * @author Dmitry Naumenko + * @author Christopher Sontag */ public final class Chunk { private final int position; private List lines; + private List before = new ArrayList<>(); + private List after = new ArrayList<>(); /** * Creates a chunk and saves a copy of affected lines @@ -107,6 +111,28 @@ public int last() { return getPosition() + size() - 1; } + /** + * @return the lines before the affected lines + */ + public List getBefore() { + return before; + } + + public void setBefore(List before) { + this.before = before; + } + + /** + * @return the lines after the affected lines + */ + public List getAfter() { + return after; + } + + public void setAfter(List after) { + this.after = after; + } + /* * (non-Javadoc) * @@ -146,6 +172,8 @@ 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; } diff --git a/src/main/java/com/github/difflib/patch/Patch.java b/src/main/java/com/github/difflib/patch/Patch.java index ec7c357b..8092cb91 100644 --- a/src/main/java/com/github/difflib/patch/Patch.java +++ b/src/main/java/com/github/difflib/patch/Patch.java @@ -20,19 +20,20 @@ package com.github.difflib.patch; import com.github.difflib.algorithm.Change; -import static com.github.difflib.patch.DeltaType.DELETE; -import static com.github.difflib.patch.DeltaType.INSERT; + import java.util.ArrayList; import java.util.Collections; -import static java.util.Comparator.comparing; import java.util.List; import java.util.ListIterator; +import static java.util.Comparator.comparing; + /** * Describes the patch holding all deltas between the 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'. + * @author Christopher Sontag */ public final class Patch { @@ -102,11 +103,34 @@ public String toString() { return "Patch{" + "deltas=" + deltas + '}'; } - public static Patch generate(List original, List revised, List changes) { + public static Patch generate(List original, List revised, List changes, int surroundingLines) { Patch patch = new Patch<>(changes.size()); for (Change change : changes) { + Chunk orgChunk = new Chunk<>(change.startOriginal, new ArrayList<>(original.subList(change.startOriginal, change.endOriginal))); + if (change.startOriginal - surroundingLines >= 0) { + orgChunk.setBefore(new ArrayList<>(original.subList(change.startOriginal - surroundingLines, change.startOriginal))); + } else { + orgChunk.setBefore(new ArrayList<>(original.subList(0, change.startOriginal))); + } + if (change.endOriginal + surroundingLines <= original.size()) { + orgChunk.setAfter(new ArrayList<>(original.subList(change.endOriginal, change.endOriginal + surroundingLines))); + } else { + orgChunk.setAfter(new ArrayList<>(original.subList(change.endOriginal, original.size()))); + } + Chunk revChunk = new Chunk<>(change.startRevised, new ArrayList<>(revised.subList(change.startRevised, change.endRevised))); + if (change.startRevised - surroundingLines >= 0) { + revChunk.setBefore(new ArrayList<>(revised.subList(change.startRevised - surroundingLines, change.startRevised))); + } else { + revChunk.setBefore(new ArrayList<>(revised.subList(0, change.startRevised))); + } + if (change.endRevised + surroundingLines <= revised.size()) { + revChunk.setAfter(new ArrayList<>(revised.subList(change.endRevised, change.endRevised + surroundingLines))); + } else { + revChunk.setAfter(new ArrayList<>(revised.subList(change.endRevised, revised.size()))); + } + switch (change.deltaType) { case DELETE: patch.addDelta(new DeleteDelta<>(orgChunk, revChunk)); @@ -121,4 +145,5 @@ public static Patch generate(List original, List revised, List * 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 original, Chunk revised) { Objects.requireNonNull(deltaType, "deltaType must not be null"); @@ -133,7 +134,9 @@ public boolean equals(Object obj) { } if (revised == null) { return other.revised == null; - } else return revised.equals(other.revised); + } else { + return revised.equals(other.revised); + } } } diff --git a/src/main/java/com/github/difflib/patch/DeltaType.java b/src/main/java/com/github/difflib/patch/DeltaType.java index 3c121964..8ea775fd 100644 --- a/src/main/java/com/github/difflib/patch/DeltaType.java +++ b/src/main/java/com/github/difflib/patch/DeltaType.java @@ -17,7 +17,6 @@ /** * Specifies the type of the delta. - * */ public enum DeltaType { /** diff --git a/src/main/java/com/github/difflib/patch/InsertDelta.java b/src/main/java/com/github/difflib/patch/InsertDelta.java index d6656e02..2f8a62d3 100644 --- a/src/main/java/com/github/difflib/patch/InsertDelta.java +++ b/src/main/java/com/github/difflib/patch/InsertDelta.java @@ -23,9 +23,10 @@ /** * Describes the add-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 InsertDelta extends Delta { @@ -33,7 +34,7 @@ public final class InsertDelta extends Delta { * Creates an insert 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 InsertDelta(Chunk original, Chunk revised) { super(DeltaType.INSERT, original, revised); diff --git a/src/main/java/com/github/difflib/patch/Patch.java b/src/main/java/com/github/difflib/patch/Patch.java index 8092cb91..2ec6d42c 100644 --- a/src/main/java/com/github/difflib/patch/Patch.java +++ b/src/main/java/com/github/difflib/patch/Patch.java @@ -22,7 +22,6 @@ import com.github.difflib.algorithm.Change; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.ListIterator; @@ -30,8 +29,9 @@ /** * Describes the patch holding all deltas between the original and revised texts. - * + *

* T The type of the compared elements in the 'lines'. + * * @author Dmitry Naumenko * @author Christopher Sontag */ @@ -94,7 +94,7 @@ public void addDelta(Delta delta) { * @return the deltas */ public List> getDeltas() { - Collections.sort(deltas, comparing(d -> d.getOriginal().getPosition())); + deltas.sort(comparing(d -> d.getOriginal().getPosition())); return deltas; } diff --git a/src/main/java/com/github/difflib/text/DiffRow.java b/src/main/java/com/github/difflib/text/DiffRow.java index dd8cd9d6..2f76ebf5 100644 --- a/src/main/java/com/github/difflib/text/DiffRow.java +++ b/src/main/java/com/github/difflib/text/DiffRow.java @@ -72,7 +72,7 @@ public String getNewLine() { /* * (non-Javadoc) - * + * * @see java.lang.Object#hashCode() */ @Override @@ -87,7 +87,7 @@ public int hashCode() { /* * (non-Javadoc) - * + * * @see java.lang.Object#equals(java.lang.Object) */ @Override @@ -117,9 +117,7 @@ public boolean equals(Object obj) { return false; } if (tag == null) { - if (other.tag != null) { - return false; - } + return other.tag == null; } else if (!tag.equals(other.tag)) { return false; } diff --git a/src/main/java/com/github/difflib/text/StringUtils.java b/src/main/java/com/github/difflib/text/StringUtils.java index 4166b311..36344502 100644 --- a/src/main/java/com/github/difflib/text/StringUtils.java +++ b/src/main/java/com/github/difflib/text/StringUtils.java @@ -20,6 +20,7 @@ package com.github.difflib.text; import java.util.List; + import static java.util.stream.Collectors.toList; final class StringUtils { @@ -53,7 +54,7 @@ public static List wrapText(List list, int columnWidth) { /** * Wrap the text with the given column width * - * @param line the text + * @param line the text * @param columnWidth the given column * @return the wrapped text */ diff --git a/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java b/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java index 966b525c..f553b9ef 100644 --- a/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java +++ b/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java @@ -6,7 +6,6 @@ import org.junit.Test; import java.io.BufferedReader; -import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; @@ -18,9 +17,9 @@ public class GenerateUnifiedDiffTest { - private static List fileToLines(String filename) throws FileNotFoundException, IOException { + private static List fileToLines(String filename) throws IOException { List lines = new ArrayList<>(); - String line = ""; + String line; try (BufferedReader in = new BufferedReader(new FileReader(filename))) { while ((line = in.readLine()) != null) { lines.add(line); @@ -107,7 +106,7 @@ public void testDiffWithHeaderLineInText() throws DiffException { } private void verify(List origLines, List revLines, - String originalFile, String revisedFile) throws DiffException { + String originalFile, String revisedFile) throws DiffException { Patch patch = DiffUtils.diff(origLines, revLines, 1); List unifiedDiff = UnifiedDiffUtils.generateUnifiedDiff(originalFile, revisedFile, origLines, patch, 10); @@ -115,7 +114,7 @@ private void verify(List origLines, List revLines, Patch fromUnifiedPatch = UnifiedDiffUtils.parseUnifiedDiff(unifiedDiff); List patchedLines; try { - patchedLines = (List) fromUnifiedPatch.applyTo(origLines); + patchedLines = fromUnifiedPatch.applyTo(origLines); assertTrue(revLines.size() == patchedLines.size()); for (int i = 0; i < revLines.size(); i++) { String l1 = revLines.get(i); diff --git a/src/test/java/com/github/difflib/TestConstants.java b/src/test/java/com/github/difflib/TestConstants.java index 56f5bf66..bd675d3c 100644 --- a/src/test/java/com/github/difflib/TestConstants.java +++ b/src/test/java/com/github/difflib/TestConstants.java @@ -4,13 +4,12 @@ * Test constants * * @author simon.mittermueller@gmail.com - * */ public final class TestConstants { public static final String BASE_FOLDER_RESOURCES = "target/test-classes/"; /** - * The base folder containing the test files. Ends with {@link #FS}. + * The base folder containing the test files. Ends with '/'. */ public static final String MOCK_FOLDER = BASE_FOLDER_RESOURCES + "/mocks/"; 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 36a29d88..dadcf151 100644 --- a/src/test/java/com/github/difflib/algorithm/jgit/HistogramDiffTest.java +++ b/src/test/java/com/github/difflib/algorithm/jgit/HistogramDiffTest.java @@ -27,7 +27,6 @@ import static org.junit.Assert.assertNotNull; /** - * * @author toben */ public class HistogramDiffTest { 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 30366c30..1cc36191 100644 --- a/src/test/java/com/github/difflib/algorithm/jgit/LRHistogramDiffTest.java +++ b/src/test/java/com/github/difflib/algorithm/jgit/LRHistogramDiffTest.java @@ -30,7 +30,6 @@ import static org.junit.Assert.assertEquals; /** - * * @author toben */ public class LRHistogramDiffTest { @@ -61,7 +60,7 @@ public void testPossibleDiffHangOnLargeDatasetDnaumenkoIssue26() throws IOExcept List revised = readStringListFromInputStream(zip.getInputStream(zip.getEntry("tb"))); 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 c564581b..2f87236a 100644 --- a/src/test/java/com/github/difflib/algorithm/myers/MyersDiffTest.java +++ b/src/test/java/com/github/difflib/algorithm/myers/MyersDiffTest.java @@ -26,7 +26,6 @@ import static org.junit.Assert.assertNotNull; /** - * * @author tw */ public class MyersDiffTest { diff --git a/src/test/java/com/github/difflib/examples/ApplyPatch.java b/src/test/java/com/github/difflib/examples/ApplyPatch.java index 4eca7d36..226234e9 100644 --- a/src/test/java/com/github/difflib/examples/ApplyPatch.java +++ b/src/test/java/com/github/difflib/examples/ApplyPatch.java @@ -5,6 +5,7 @@ import com.github.difflib.UnifiedDiffUtils; import com.github.difflib.patch.Patch; import com.github.difflib.patch.PatchFailedException; + import java.io.File; import java.io.IOException; import java.nio.file.Files; diff --git a/src/test/java/com/github/difflib/text/DiffRowGeneratorTest.java b/src/test/java/com/github/difflib/text/DiffRowGeneratorTest.java index 3326fe97..bfa3d150 100644 --- a/src/test/java/com/github/difflib/text/DiffRowGeneratorTest.java +++ b/src/test/java/com/github/difflib/text/DiffRowGeneratorTest.java @@ -1,11 +1,13 @@ package com.github.difflib.text; 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.assertTrue; -import org.junit.Test; public class DiffRowGeneratorTest { diff --git a/src/test/java/com/github/difflib/text/StringUtilsTest.java b/src/test/java/com/github/difflib/text/StringUtilsTest.java index 2b120d09..82bd0645 100644 --- a/src/test/java/com/github/difflib/text/StringUtilsTest.java +++ b/src/test/java/com/github/difflib/text/StringUtilsTest.java @@ -15,12 +15,13 @@ */ package com.github.difflib.text; -import java.util.Collections; -import static org.junit.Assert.*; import org.junit.Test; +import java.util.Collections; + +import static org.junit.Assert.assertEquals; + /** - * * @author tw */ public class StringUtilsTest { From 1757a420addfb0425e5fe9d39ddfb66b44414b35 Mon Sep 17 00:00:00 2001 From: Kogoro Date: Wed, 1 Nov 2017 03:30:25 +0100 Subject: [PATCH 6/6] Added source jar to pom, Bugfixes --- pom.xml | 15 ++++++++++++ .../com/github/difflib/patch/ChangeDelta.java | 7 ++---- .../java/com/github/difflib/patch/Chunk.java | 23 +++++++------------ 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/pom.xml b/pom.xml index 0a72d9dd..43843f14 100644 --- a/pom.xml +++ b/pom.xml @@ -159,6 +159,21 @@ attach-javadocs + verify + + jar + + + + + + org.apache.maven.plugins + maven-source-plugin + 3.0.1 + + + attach-sources + verify jar diff --git a/src/main/java/com/github/difflib/patch/ChangeDelta.java b/src/main/java/com/github/difflib/patch/ChangeDelta.java index 9a90b9cb..970d5736 100644 --- a/src/main/java/com/github/difflib/patch/ChangeDelta.java +++ b/src/main/java/com/github/difflib/patch/ChangeDelta.java @@ -22,9 +22,7 @@ import java.util.List; /** - * Describes the change-delta between original and revised texts. - *

- * T The type of the compared elements in the 'lines'. + * Describes the change-delta between original and revised texts.

T The type of the compared elements in the 'lines'. * * @author Dmitry Naumenko */ @@ -71,7 +69,6 @@ public void restore(List target) { @Override public String toString() { - return "[ChangeDelta, position: " + getOriginal().getPosition() + ", lines: " - + getOriginal().getLines() + " to " + getRevised().getLines() + "]"; + return "[ChangeDelta, position: " + getOriginal().getPosition() + ", lines: " + getOriginal().getLines() + " to " + getRevised().getLines() + "]"; } } diff --git a/src/main/java/com/github/difflib/patch/Chunk.java b/src/main/java/com/github/difflib/patch/Chunk.java index 23be93bd..068beed3 100644 --- a/src/main/java/com/github/difflib/patch/Chunk.java +++ b/src/main/java/com/github/difflib/patch/Chunk.java @@ -24,14 +24,10 @@ import java.util.List; /** - * 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 {@link java.lang.Object#equals equals()} - * correctly can be subject to differencing using this library. - *

- * T The type of the compared elements in the 'lines'. + * 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 + * {@link java.lang.Object#equals equals()} correctly can be subject to differencing using this library.

T The type of the compared elements in the + * 'lines'. * * @author Dmitry Naumenko * @author Christopher Sontag @@ -76,17 +72,16 @@ public void verify(List target) throws PatchFailedException { } for (int i = 0; i < size(); i++) { if (!target.get(position + i).equals(lines.get(i))) { - throw new PatchFailedException( - "Incorrect Chunk: the chunk content doesn't match the target"); + throw new 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))) { + for (int i = before.size(); i > 0; i--) { + if (!before.get(before.size() - i).equals(target.get(position - 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() + i + 1))) { throw new PatchFailedException("Incorrect Chunk: the chunk's surroundings (after) doesn't match the target"); } } @@ -157,7 +152,6 @@ public void setAfter(List after) { /* * (non-Javadoc) - * * @see java.lang.Object#hashCode() */ @Override @@ -172,7 +166,6 @@ public int hashCode() { /* * (non-Javadoc) - * * @see java.lang.Object#equals(java.lang.Object) */ @Override