passing through exceptions

This commit is contained in:
wumpz
2017-04-03 15:12:16 +02:00
parent eeae5dd675
commit 37556b7dba
4 changed files with 18 additions and 21 deletions

View File

@@ -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 <T> Patch<T> diff(List<T> original, List<T> revised) {
public static <T> Patch<T> diff(List<T> original, List<T> revised) throws DiffException {
return DiffUtils.diff(original, revised, new MyersDiff<>());
}
@@ -72,7 +73,7 @@ public final class DiffUtils {
* {@code null}.
*/
public static <T> Patch<T> diff(List<T> original, List<T> revised,
Equalizer<T> equalizer) {
Equalizer<T> 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 <T> Patch<T> diff(List<T> original, List<T> revised,
DiffAlgorithm<T> algorithm) {
DiffAlgorithm<T> 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<String> diffInline(String original, String revised) {
public static Patch<String> diffInline(String original, String revised) throws DiffException {
LinkedList<String> origList = new LinkedList<>();
LinkedList<String> 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()) {

View File

@@ -38,7 +38,7 @@ public interface DiffAlgorithm<T> {
* @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<T> diff(T[] original, T[] revised);
public Patch<T> 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<T> {
* @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<T> diff(List<T> original, List<T> revised);
public Patch<T> diff(List<T> original, List<T> revised) throws DiffException;
}

View File

@@ -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<T> implements DiffAlgorithm<T> {
* @return Returns an empty diff if get the error while procession the difference.
*/
@Override
public Patch<T> diff(final T[] original, final T[] revised) {
public Patch<T> diff(final T[] original, final T[] revised) throws DiffException {
return diff(Arrays.asList(original), Arrays.asList(revised));
}
@@ -96,21 +97,15 @@ public class MyersDiff<T> implements DiffAlgorithm<T> {
* Return empty diff if get the error while procession the difference.
*/
@Override
public Patch<T> diff(final List<T> original, final List<T> revised) {
public Patch<T> diff(final List<T> original, final List<T> revised) throws DiffException {
if (original == null) {
throw new IllegalArgumentException("original list must not be null");
}
if (revised == null) {
throw new IllegalArgumentException("revised list must not be null");
}
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);
}
/**

View File

@@ -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<DiffRow> generateDiffRows(List<String> original, List<String> revised) {
public List<DiffRow> generateDiffRows(List<String> original, List<String> 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<DiffRow> generateDiffRows(List<String> original, List<String> revised, Patch<String> patch) {
public List<DiffRow> generateDiffRows(List<String> original, List<String> revised, Patch<String> 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<String> delta) {
private void addInlineDiffs(Delta<String> delta) throws DiffException {
List<String> orig = (List<String>) delta.getOriginal().getLines();
List<String> rev = (List<String>) delta.getRevised().getLines();
LinkedList<String> origList = new LinkedList<>();