mirror of
https://github.com/java-diff-utils/java-diff-utils.git
synced 2026-03-13 10:11:17 +08:00
replaced equalizer by BiPredicate
This commit is contained in:
@@ -23,7 +23,6 @@ import difflib.algorithm.DiffAlgorithm;
|
||||
import difflib.algorithm.DiffException;
|
||||
import difflib.algorithm.myers.MyersDiff;
|
||||
import difflib.patch.Delta;
|
||||
import difflib.patch.Equalizer;
|
||||
import difflib.patch.Patch;
|
||||
import difflib.patch.PatchFailedException;
|
||||
import java.util.Arrays;
|
||||
@@ -31,6 +30,7 @@ import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.BiPredicate;
|
||||
import static java.util.stream.Collectors.joining;
|
||||
|
||||
/**
|
||||
@@ -74,7 +74,7 @@ public final class DiffUtils {
|
||||
* {@code null}.
|
||||
*/
|
||||
public static <T> Patch<T> diff(List<T> original, List<T> revised,
|
||||
Equalizer<T> equalizer) throws DiffException {
|
||||
BiPredicate<T,T> equalizer) throws DiffException {
|
||||
if (equalizer != null) {
|
||||
return DiffUtils.diff(original, revised,
|
||||
new MyersDiff<>(equalizer));
|
||||
|
||||
@@ -24,11 +24,11 @@ import difflib.algorithm.DifferentiationFailedException;
|
||||
import difflib.algorithm.DiffAlgorithm;
|
||||
import difflib.algorithm.DiffException;
|
||||
import difflib.patch.DeltaType;
|
||||
import difflib.patch.Equalizer;
|
||||
import difflib.patch.Patch;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.BiPredicate;
|
||||
|
||||
/**
|
||||
* A clean-room implementation of Eugene Myers greedy differencing algorithm.
|
||||
@@ -38,12 +38,12 @@ public final class MyersDiff<T> implements DiffAlgorithm<T> {
|
||||
/**
|
||||
* Default equalizer.
|
||||
*/
|
||||
private final Equalizer<T> DEFAULT_EQUALIZER = Object::equals;
|
||||
private final BiPredicate<T,T> DEFAULT_EQUALIZER = Object::equals;
|
||||
|
||||
/**
|
||||
* The equalizer.
|
||||
*/
|
||||
private final Equalizer<T> equalizer;
|
||||
private final BiPredicate<T,T> equalizer;
|
||||
|
||||
/**
|
||||
* Constructs an instance of the Myers differencing algorithm.
|
||||
@@ -57,7 +57,7 @@ public final class MyersDiff<T> implements DiffAlgorithm<T> {
|
||||
*
|
||||
* @param equalizer Must not be {@code null}.
|
||||
*/
|
||||
public MyersDiff(final Equalizer<T> equalizer) {
|
||||
public MyersDiff(final BiPredicate<T,T> equalizer) {
|
||||
Objects.requireNonNull(equalizer, "equalizer must not be null");
|
||||
this.equalizer = equalizer;
|
||||
}
|
||||
@@ -123,7 +123,7 @@ public final class MyersDiff<T> implements DiffAlgorithm<T> {
|
||||
|
||||
PathNode node = new PathNode(i, j, false, false, prev);
|
||||
|
||||
while (i < N && j < M && equalizer.equals(orig.get(i), rev.get(j))) {
|
||||
while (i < N && j < M && equalizer.test(orig.get(i), rev.get(j))) {
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
/*-
|
||||
* #%L
|
||||
* java-diff-utils
|
||||
* %%
|
||||
* Copyright (C) 2009 - 2017 java-diff-utils
|
||||
* %%
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
* #L%
|
||||
*/
|
||||
package difflib.patch;
|
||||
|
||||
/**
|
||||
* Specifies when two compared elements are equal.
|
||||
*
|
||||
* @param T The type of the compared elements in the 'lines'.
|
||||
*/
|
||||
public interface Equalizer<T> {
|
||||
|
||||
/**
|
||||
* Indicates if two elements are equal according to the diff mechanism.
|
||||
*
|
||||
* @param original The original element. Must not be {@code null}.
|
||||
* @param revised The revised element. Must not be {@code null}.
|
||||
* @return Returns true if the elements are equal.
|
||||
*/
|
||||
boolean equals(T original, T revised);
|
||||
}
|
||||
@@ -25,11 +25,11 @@ import difflib.patch.ChangeDelta;
|
||||
import difflib.patch.Chunk;
|
||||
import difflib.patch.DeleteDelta;
|
||||
import difflib.patch.Delta;
|
||||
import difflib.patch.Equalizer;
|
||||
import difflib.patch.InsertDelta;
|
||||
import difflib.patch.Patch;
|
||||
import difflib.text.DiffRow.Tag;
|
||||
import java.util.*;
|
||||
import java.util.function.BiPredicate;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -56,7 +56,7 @@ public class DiffRowGenerator {
|
||||
private final Function<Boolean, String> newTag;
|
||||
private final boolean inlineDiffByWord;
|
||||
private final int columnWidth;
|
||||
private final Equalizer<String> equalizer;
|
||||
private final BiPredicate<String, String> equalizer;
|
||||
private final boolean mergeOriginalRevised;
|
||||
|
||||
/**
|
||||
@@ -172,9 +172,9 @@ public class DiffRowGenerator {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public static final Equalizer<String> IGNORE_WHITESPACE_EQUALIZER = (original, revised)
|
||||
public static final BiPredicate<String,String> IGNORE_WHITESPACE_EQUALIZER = (original, revised)
|
||||
-> original.trim().replaceAll("\\s+", " ").equals(revised.trim().replaceAll("\\s+", " "));
|
||||
public static final Equalizer<String> DEFAULT_EQUALIZER = Object::equals;
|
||||
public static final BiPredicate<String,String> DEFAULT_EQUALIZER = Object::equals;
|
||||
|
||||
private DiffRowGenerator(Builder builder) {
|
||||
showInlineDiffs = builder.showInlineDiffs;
|
||||
|
||||
Reference in New Issue
Block a user