Skip to content

Adjust generic parameters for PECS principle. #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
public class HistogramDiff<T> implements DiffAlgorithmI<T> {

@Override
public List<Change> computeDiff(List<T> source, List<T> target, DiffAlgorithmListener progress) {
public List<Change> computeDiff(
List<? extends T> source, List<? extends T> target, DiffAlgorithmListener progress) {
Objects.requireNonNull(source, "source list must not be null");
Objects.requireNonNull(target, "target list must not be null");
if (progress != null) {
Expand Down Expand Up @@ -92,9 +93,9 @@ public int hash(DataList<T> s, int i) {

class DataList<T> extends Sequence {

final List<T> data;
final List<? extends T> data;

public DataList(List<T> data) {
public DataList(List<? extends T> data) {
this.data = data;
}

Expand Down
26 changes: 16 additions & 10 deletions java-diff-utils/src/main/java/com/github/difflib/DiffUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public static void withDefaultDiffAlgorithmFactory(DiffAlgorithmFactory factory)
* @param progress a {@link DiffAlgorithmListener} representing the progress listener. Can be {@code null}.
* @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, DiffAlgorithmListener progress) {
public static <T> Patch<T> diff(
List<? extends T> original, List<? extends T> revised, DiffAlgorithmListener progress) {
return DiffUtils.diff(original, revised, DEFAULT_DIFF.create(), progress);
}

Expand All @@ -69,7 +70,7 @@ public static <T> Patch<T> diff(List<T> original, List<T> revised, DiffAlgorithm
* @param revised a {@link List} representing the revised sequence of elements. Must not be {@code null}.
* @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<? extends T> original, List<? extends T> revised) {
return DiffUtils.diff(original, revised, DEFAULT_DIFF.create(), null);
}

Expand All @@ -82,7 +83,7 @@ public static <T> Patch<T> diff(List<T> original, List<T> revised) {
* @param includeEqualParts a {@link boolean} representing whether to include equal parts in the resulting patch.
* @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, boolean includeEqualParts) {
public static <T> Patch<T> diff(List<? extends T> original, List<? extends T> revised, boolean includeEqualParts) {
return DiffUtils.diff(original, revised, DEFAULT_DIFF.create(), null, includeEqualParts);
}

Expand Down Expand Up @@ -110,15 +111,19 @@ public static Patch<String> diff(String sourceText, String targetText, DiffAlgor
* @return The patch describing the difference between the original and
* revised sequences. Never {@code null}.
*/
public static <T> Patch<T> diff(List<T> source, List<T> target, BiPredicate<T, T> equalizer) {
public static <T> Patch<T> diff(
List<? extends T> source, List<? extends T> target, BiPredicate<? super T, ? super T> equalizer) {
if (equalizer != null) {
return DiffUtils.diff(source, target, DEFAULT_DIFF.create(equalizer));
}
return DiffUtils.diff(source, target, new MyersDiff<>());
}

public static <T> Patch<T> diff(
List<T> original, List<T> revised, DiffAlgorithmI<T> algorithm, DiffAlgorithmListener progress) {
List<? extends T> original,
List<? extends T> revised,
DiffAlgorithmI<T> algorithm,
DiffAlgorithmListener progress) {
return diff(original, revised, algorithm, progress, false);
}

Expand All @@ -135,8 +140,8 @@ public static <T> Patch<T> diff(
* revised sequences. Never {@code null}.
*/
public static <T> Patch<T> diff(
List<T> original,
List<T> revised,
List<? extends T> original,
List<? extends T> revised,
DiffAlgorithmI<T> algorithm,
DiffAlgorithmListener progress,
boolean includeEqualParts) {
Expand All @@ -157,7 +162,8 @@ public static <T> Patch<T> diff(
* @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, DiffAlgorithmI<T> algorithm) {
public static <T> Patch<T> diff(
List<? extends T> original, List<? extends T> revised, DiffAlgorithmI<T> algorithm) {
return diff(original, revised, algorithm, null);
}

Expand Down Expand Up @@ -196,7 +202,7 @@ public static Patch<String> diffInline(String original, String revised) {
* @return the revised list.
* @throws PatchFailedException if the patch cannot be applied.
*/
public static <T> List<T> patch(List<T> original, Patch<T> patch) throws PatchFailedException {
public static <T> List<T> patch(List<? extends T> original, Patch<T> patch) throws PatchFailedException {
return patch.applyTo(original);
}

Expand All @@ -208,7 +214,7 @@ public static <T> List<T> patch(List<T> original, Patch<T> patch) throws PatchFa
* @return the original list.
* @throws PatchFailedException if the patch cannot be applied.
*/
public static <T> List<T> unpatch(List<T> revised, Patch<T> patch) {
public static <T> List<T> unpatch(List<? extends T> revised, Patch<T> patch) {
return patch.restore(revised);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@
public interface DiffAlgorithmFactory {
<T> DiffAlgorithmI<T> create();

<T> DiffAlgorithmI<T> create(BiPredicate<T, T> equalizer);
<T> DiffAlgorithmI<T> create(BiPredicate<? super T, ? super T> equalizer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public interface DiffAlgorithmI<T> {
* @param progress progress listener
* @return
*/
List<Change> computeDiff(List<T> source, List<T> target, DiffAlgorithmListener progress);
List<Change> computeDiff(List<? extends T> source, List<? extends T> target, DiffAlgorithmListener progress);

/**
* Simple extension to compute a changeset using arrays.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
*/
public final class MyersDiff<T> implements DiffAlgorithmI<T> {

private final BiPredicate<T, T> equalizer;
private final BiPredicate<? super T, ? super T> equalizer;

public MyersDiff() {
equalizer = Object::equals;
}

public MyersDiff(final BiPredicate<T, T> equalizer) {
public MyersDiff(final BiPredicate<? super T, ? super T> equalizer) {
Objects.requireNonNull(equalizer, "equalizer must not be null");
this.equalizer = equalizer;
}
Expand All @@ -48,7 +48,8 @@ public MyersDiff(final BiPredicate<T, T> equalizer) {
* Return empty diff if get the error while procession the difference.
*/
@Override
public List<Change> computeDiff(final List<T> source, final List<T> target, DiffAlgorithmListener progress) {
public List<Change> computeDiff(
final List<? extends T> source, final List<? extends T> target, DiffAlgorithmListener progress) {
Objects.requireNonNull(source, "source list must not be null");
Objects.requireNonNull(target, "target list must not be null");

Expand All @@ -71,9 +72,10 @@ public List<Change> computeDiff(final List<T> source, final List<T> target, Diff
* @param orig The original 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.
* @throws IllegalStateException if a diff path could not be found.
*/
private PathNode buildPath(final List<T> orig, final List<T> rev, DiffAlgorithmListener progress) {
private PathNode buildPath(
final List<? extends T> orig, final List<? extends T> rev, DiffAlgorithmListener progress) {
Objects.requireNonNull(orig, "original sequence is null");
Objects.requireNonNull(rev, "revised sequence is null");

Expand Down Expand Up @@ -140,10 +142,10 @@ private PathNode buildPath(final List<T> orig, final List<T> rev, DiffAlgorithmL
* @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
* @throws IllegalStateException if a {@link Patch} could not be
* built from the given path.
*/
private List<Change> buildRevision(PathNode actualPath, List<T> orig, List<T> rev) {
private List<Change> buildRevision(PathNode actualPath, List<? extends T> orig, List<? extends T> rev) {
Objects.requireNonNull(actualPath, "path is null");
Objects.requireNonNull(orig, "original sequence is null");
Objects.requireNonNull(rev, "revised sequence is null");
Expand Down Expand Up @@ -190,7 +192,7 @@ public <T> DiffAlgorithmI<T> create() {
}

@Override
public <T> DiffAlgorithmI<T> create(BiPredicate<T, T> equalizer) {
public <T> DiffAlgorithmI<T> create(BiPredicate<? super T, ? super T> equalizer) {
return new MyersDiff<>(equalizer);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,20 @@
*/
public class MyersDiffWithLinearSpace<T> implements DiffAlgorithmI<T> {

private final BiPredicate<T, T> equalizer;
private final BiPredicate<? super T, ? super T> equalizer;

public MyersDiffWithLinearSpace() {
equalizer = Object::equals;
}

public MyersDiffWithLinearSpace(final BiPredicate<T, T> equalizer) {
public MyersDiffWithLinearSpace(final BiPredicate<? super T, ? super T> equalizer) {
Objects.requireNonNull(equalizer, "equalizer must not be null");
this.equalizer = equalizer;
}

@Override
public List<Change> computeDiff(List<T> source, List<T> target, DiffAlgorithmListener progress) {
public List<Change> computeDiff(
List<? extends T> source, List<? extends T> target, DiffAlgorithmListener progress) {
Objects.requireNonNull(source, "source list must not be null");
Objects.requireNonNull(target, "target list must not be null");

Expand Down Expand Up @@ -200,10 +201,10 @@ private class DiffData {
final int[] vDown;
final int[] vUp;
final List<Change> script;
final List<T> source;
final List<T> target;
final List<? extends T> source;
final List<? extends T> target;

public DiffData(List<T> source, List<T> target) {
public DiffData(List<? extends T> source, List<? extends T> target) {
this.source = source;
this.target = target;
size = source.size() + target.size() + 2;
Expand Down Expand Up @@ -237,7 +238,7 @@ public <T> DiffAlgorithmI<T> create() {
}

@Override
public <T> DiffAlgorithmI<T> create(BiPredicate<T, T> equalizer) {
public <T> DiffAlgorithmI<T> create(BiPredicate<? super T, ? super T> equalizer) {
return new MyersDiffWithLinearSpace<>(equalizer);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.util.List;

/**
* This delta contains equal lines of data. Therefore nothing is to do in applyTo and restore.
* This delta contains equal lines of data. Therefore, nothing is to do in applyTo and restore.
* @author tobens
*/
public class EqualDelta<T> extends AbstractDelta<T> {
Expand Down Expand Up @@ -49,6 +49,6 @@ public String toString() {

@Override
public AbstractDelta<T> withChunks(Chunk<T> original, Chunk<T> revised) {
return new EqualDelta<T>(original, revised);
return new EqualDelta<>(original, revised);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public Patch(int estimatedPatchSize) {
* @return A new list containing the applied patch.
* @throws PatchFailedException if the patch cannot be applied
*/
public List<T> applyTo(List<T> target) throws PatchFailedException {
public List<T> applyTo(List<? extends T> target) throws PatchFailedException {
List<T> result = new ArrayList<>(target);
applyToExisting(result);
return result;
Expand Down Expand Up @@ -244,7 +244,7 @@ public Patch withConflictOutput(ConflictOutput<T> conflictOutput) {
* @param target The list to copy and apply changes to.
* @return A new list, containing the restored state.
*/
public List<T> restore(List<T> target) {
public List<T> restore(List<? extends T> target) {
List<T> result = new ArrayList<>(target);
restoreToExisting(result);
return result;
Expand Down Expand Up @@ -294,12 +294,12 @@ public static <T> Patch<T> generate(List<T> original, List<T> revised, List<Chan
return generate(original, revised, changes, false);
}

private static <T> Chunk<T> buildChunk(int start, int end, List<T> data) {
private static <T> Chunk<T> buildChunk(int start, int end, List<? extends T> data) {
return new Chunk<>(start, new ArrayList<>(data.subList(start, end)));
}

public static <T> Patch<T> generate(
List<T> original, List<T> revised, List<Change> _changes, boolean includeEquals) {
List<? extends T> original, List<? extends T> revised, List<Change> _changes, boolean includeEquals) {
Patch<T> patch = new Patch<>(_changes.size());
int startOriginal = 0;
int startRevised = 0;
Expand Down
40 changes: 20 additions & 20 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,28 +132,28 @@
<sourceDirectories>${project.build.sourceDirectory}</sourceDirectories>
<checkstyleRules>
<module name="Checker">
<module name="SuppressWarningsFilter" />
<module name="SuppressWarningsFilter"/>
<module name="TreeWalker">
<module name="SuppressionCommentFilter" />
<module name="AvoidNestedBlocks" />
<module name="ConstantName" />
<module name="EmptyCatchBlock" />
<module name="EmptyStatement" />
<module name="MissingOverride" />
<module name="MultipleVariableDeclarations" />
<module name="ParameterAssignment" />
<module name="StringLiteralEquality" />
<module name="RedundantImport" />
<module name="UnusedImports" />
<module name="SuppressionCommentFilter"/>
<module name="AvoidNestedBlocks"/>
<module name="ConstantName"/>
<module name="EmptyCatchBlock"/>
<module name="EmptyStatement"/>
<module name="MissingOverride"/>
<module name="MultipleVariableDeclarations"/>
<module name="ParameterAssignment"/>
<module name="StringLiteralEquality"/>
<module name="RedundantImport"/>
<module name="UnusedImports"/>

<module name="WhitespaceAfter" />
<module name="WhitespaceAfter"/>

<module name="NeedBraces" />
<module name="UnnecessaryParentheses" />
<module name="LeftCurly" />
<module name="RightCurly" />
<module name="NeedBraces"/>
<module name="UnnecessaryParentheses"/>
<module name="LeftCurly"/>
<module name="RightCurly"/>

<module name="SuppressWarningsHolder" />
<module name="SuppressWarningsHolder"/>
</module>
</module>
</checkstyleRules>
Expand Down Expand Up @@ -188,10 +188,10 @@
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>2.30.0</version>
<version>2.46.0</version>
<configuration>
<java>
<palantirJavaFormat />
<palantirJavaFormat/>
<indent>
<tabs>true</tabs>
<spacesPerTab>2</spacesPerTab>
Expand Down