From 37556b7dba9a89d8282f33aa945db2bc31b15e4e Mon Sep 17 00:00:00 2001 From: wumpz Date: Mon, 3 Apr 2017 15:12:16 +0200 Subject: [PATCH] passing through exceptions --- src/main/java/difflib/DiffUtils.java | 13 +++++++------ .../java/difflib/algorithm/DiffAlgorithm.java | 4 ++-- .../java/difflib/algorithm/myers/MyersDiff.java | 15 +++++---------- src/main/java/difflib/text/DiffRowGenerator.java | 7 ++++--- 4 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/main/java/difflib/DiffUtils.java b/src/main/java/difflib/DiffUtils.java index 9736cf8..2726a0e 100644 --- a/src/main/java/difflib/DiffUtils.java +++ b/src/main/java/difflib/DiffUtils.java @@ -25,6 +25,7 @@ import difflib.patch.Chunk; import difflib.patch.Patch; import difflib.patch.ChangeDelta; import difflib.algorithm.DiffAlgorithm; +import difflib.algorithm.DiffException; import difflib.patch.Equalizer; import difflib.algorithm.myers.MyersDiff; import java.util.ArrayList; @@ -43,7 +44,7 @@ import java.util.regex.Pattern; */ public final class DiffUtils { - private static Pattern unifiedDiffChunkRe = Pattern + private static final Pattern UNIFIED_DIFF_CHUNK_REGEXP = Pattern .compile("^@@\\s+-(?:(\\d+)(?:,(\\d+))?)\\s+\\+(?:(\\d+)(?:,(\\d+))?)\\s+@@$"); /** @@ -55,7 +56,7 @@ public final class DiffUtils { * @return The patch describing the difference between the original and revised sequences. Never * {@code null}. */ - public static Patch diff(List original, List revised) { + public static Patch diff(List original, List revised) throws DiffException { return DiffUtils.diff(original, revised, new MyersDiff<>()); } @@ -72,7 +73,7 @@ public final class DiffUtils { * {@code null}. */ public static Patch diff(List original, List revised, - Equalizer equalizer) { + Equalizer equalizer) throws DiffException { if (equalizer != null) { return DiffUtils.diff(original, revised, new MyersDiff<>(equalizer)); @@ -91,7 +92,7 @@ public final class DiffUtils { * {@code null}. */ public static Patch diff(List original, List revised, - DiffAlgorithm algorithm) { + DiffAlgorithm algorithm) throws DiffException { if (original == null) { throw new IllegalArgumentException("original must not be null"); } @@ -113,7 +114,7 @@ public final class DiffUtils { * @param revised * @return */ - public static Patch diffInline(String original, String revised) { + public static Patch diffInline(String original, String revised) throws DiffException { LinkedList origList = new LinkedList<>(); LinkedList revList = new LinkedList<>(); for (Character character : original.toCharArray()) { @@ -190,7 +191,7 @@ public final class DiffUtils { } continue; } - Matcher m = unifiedDiffChunkRe.matcher(line); + Matcher m = UNIFIED_DIFF_CHUNK_REGEXP.matcher(line); if (m.find()) { // Process the lines in the previous chunk if (!rawChunk.isEmpty()) { diff --git a/src/main/java/difflib/algorithm/DiffAlgorithm.java b/src/main/java/difflib/algorithm/DiffAlgorithm.java index 732c87b..4f6059d 100644 --- a/src/main/java/difflib/algorithm/DiffAlgorithm.java +++ b/src/main/java/difflib/algorithm/DiffAlgorithm.java @@ -38,7 +38,7 @@ public interface DiffAlgorithm { * @param revised The revised sequence. Must not be {@code null}. * @return The patch representing the diff of the given sequences. Never {@code null}. */ - public Patch diff(T[] original, T[] revised); + public Patch diff(T[] original, T[] revised) throws DiffException; /** * Computes the difference between the original sequence and the revised sequence and returns it @@ -48,5 +48,5 @@ public interface DiffAlgorithm { * @param revised The revised sequence. Must not be {@code null}. * @return The patch representing the diff of the given sequences. Never {@code null}. */ - public Patch diff(List original, List revised); + public Patch diff(List original, List revised) throws DiffException; } diff --git a/src/main/java/difflib/algorithm/myers/MyersDiff.java b/src/main/java/difflib/algorithm/myers/MyersDiff.java index b5a9ee0..11ac201 100644 --- a/src/main/java/difflib/algorithm/myers/MyersDiff.java +++ b/src/main/java/difflib/algorithm/myers/MyersDiff.java @@ -28,6 +28,7 @@ import difflib.patch.DeleteDelta; import difflib.patch.ChangeDelta; import difflib.algorithm.DiffAlgorithm; import difflib.*; +import difflib.algorithm.DiffException; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; @@ -86,7 +87,7 @@ public class MyersDiff implements DiffAlgorithm { * @return Returns an empty diff if get the error while procession the difference. */ @Override - public Patch diff(final T[] original, final T[] revised) { + public Patch diff(final T[] original, final T[] revised) throws DiffException { return diff(Arrays.asList(original), Arrays.asList(revised)); } @@ -96,21 +97,15 @@ public class MyersDiff implements DiffAlgorithm { * Return empty diff if get the error while procession the difference. */ @Override - public Patch diff(final List original, final List revised) { + public Patch diff(final List original, final List revised) throws DiffException { if (original == null) { throw new IllegalArgumentException("original list must not be null"); } if (revised == null) { throw new IllegalArgumentException("revised list must not be null"); } - PathNode path; - try { - path = buildPath(original, revised); - return buildRevision(path, original, revised); - } catch (DifferentiationFailedException e) { - e.printStackTrace(); - } - return new Patch<>(); + PathNode path = buildPath(original, revised); + return buildRevision(path, original, revised); } /** diff --git a/src/main/java/difflib/text/DiffRowGenerator.java b/src/main/java/difflib/text/DiffRowGenerator.java index aaad211..cb95d60 100644 --- a/src/main/java/difflib/text/DiffRowGenerator.java +++ b/src/main/java/difflib/text/DiffRowGenerator.java @@ -25,6 +25,7 @@ import difflib.patch.DeleteDelta; import difflib.patch.Delta; import difflib.text.DiffRow.Tag; import difflib.DiffUtils; +import difflib.algorithm.DiffException; import difflib.patch.InsertDelta; import difflib.patch.Patch; import difflib.patch.Equalizer; @@ -193,7 +194,7 @@ public class DiffRowGenerator { * @param revised the revised text * @return the DiffRows between original and revised texts */ - public List generateDiffRows(List original, List revised) { + public List generateDiffRows(List original, List revised) throws DiffException { return generateDiffRows(original, revised, DiffUtils.diff(original, revised, equalizer)); } @@ -206,7 +207,7 @@ public class DiffRowGenerator { * @param patch the given patch * @return the DiffRows between original and revised texts */ - public List generateDiffRows(List original, List revised, Patch patch) { + public List generateDiffRows(List original, List revised, Patch patch) throws DiffException { // normalize the lines (expand tabs, escape html entities) original = StringUtils.normalize(original); revised = StringUtils.normalize(revised); @@ -288,7 +289,7 @@ public class DiffRowGenerator { * * @param delta the given delta */ - private void addInlineDiffs(Delta delta) { + private void addInlineDiffs(Delta delta) throws DiffException { List orig = (List) delta.getOriginal().getLines(); List rev = (List) delta.getRevised().getLines(); LinkedList origList = new LinkedList<>();