Skip to content
This repository was archived by the owner on Nov 12, 2019. It is now read-only.

Commit 8ceac1c

Browse files
author
simon.mittermueller@gmail.com
committed
Replace Object with generic type T (issue #22).
git-svn-id: http://java-diff-utils.googlecode.com/svn/trunk@52 d8d7d024-a22d-11de-b755-fd640f38fa9d
1 parent 2fc6bb2 commit 8ceac1c

16 files changed

+673
-518
lines changed

src/main/java/difflib/ChangeDelta.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@
2121
* Describes the change-delta between original and revised texts.
2222
*
2323
* @author <a href="dm.naumenko@gmail.com">Dmitry Naumenko</a>
24+
* @param T The type of the compared elements in the 'lines'.
2425
*/
25-
public class ChangeDelta extends Delta {
26+
public class ChangeDelta<T> extends Delta<T> {
2627

2728
/**
28-
* {@inheritDoc}
29+
* Creates a change delta with the two given chunks.
30+
* @param original The original chunk. Must not be {@code null}.
31+
* @param revised The original chunk. Must not be {@code null}.
2932
*/
30-
public ChangeDelta(Chunk original, Chunk revised) {
31-
super(original, revised);
33+
public ChangeDelta(Chunk<T> original, Chunk<T>revised) {
34+
super(original, revised);
3235
}
3336

3437
/**
@@ -37,15 +40,15 @@ public ChangeDelta(Chunk original, Chunk revised) {
3740
* @throws PatchFailedException
3841
*/
3942
@Override
40-
public void applyTo(List<Object> target) throws PatchFailedException {
43+
public void applyTo(List<T> target) throws PatchFailedException {
4144
verify(target);
4245
int position = getOriginal().getPosition();
4346
int size = getOriginal().size();
4447
for (int i = 0; i < size; i++) {
4548
target.remove(position);
4649
}
4750
int i = 0;
48-
for (Object line : getRevised().getLines()) {
51+
for (T line : getRevised().getLines()) {
4952
target.add(position + i, line);
5053
i++;
5154
}
@@ -55,14 +58,14 @@ public void applyTo(List<Object> target) throws PatchFailedException {
5558
* {@inheritDoc}
5659
*/
5760
@Override
58-
public void restore(List<Object> target) {
61+
public void restore(List<T> target) {
5962
int position = getRevised().getPosition();
6063
int size = getRevised().size();
6164
for (int i = 0; i < size; i++) {
6265
target.remove(position);
6366
}
6467
int i = 0;
65-
for (Object line : getOriginal().getLines()) {
68+
for (T line : getOriginal().getLines()) {
6669
target.add(position + i, line);
6770
i++;
6871
}
@@ -71,7 +74,7 @@ public void restore(List<Object> target) {
7174
/**
7275
* {@inheritDoc}
7376
*/
74-
public void verify(List<?> target) throws PatchFailedException {
77+
public void verify(List<T> target) throws PatchFailedException {
7578
getOriginal().verify(target);
7679
if (getOriginal().getPosition() > target.size()) {
7780
throw new PatchFailedException("Incorrect patch for delta: "

src/main/java/difflib/Chunk.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@
3030
* </p>
3131
*
3232
* @author <a href="dm.naumenko@gmail.com>Dmitry Naumenko</a>
33+
* @param T The type of the compared elements in the 'lines'.
3334
*/
34-
public class Chunk {
35+
public class Chunk<T> {
3536

3637
private final int position;
37-
private List<?> lines;
38+
private List<T> lines;
3839

3940
/**
4041
* Creates a chunk and saves a copy of affected lines
@@ -44,7 +45,7 @@ public class Chunk {
4445
* @param lines
4546
* the affected lines
4647
*/
47-
public Chunk(int position, List<?> lines) {
48+
public Chunk(int position, List<T> lines) {
4849
this.position = position;
4950
this.lines = lines;
5051
}
@@ -57,7 +58,7 @@ public Chunk(int position, List<?> lines) {
5758
* @param lines
5859
* the affected lines
5960
*/
60-
public Chunk(int position, Object[] lines) {
61+
public Chunk(int position, T[] lines) {
6162
this.position = position;
6263
this.lines = Arrays.asList(lines);
6364
}
@@ -69,7 +70,7 @@ public Chunk(int position, Object[] lines) {
6970
* @param target
7071
* the sequence to verify against.
7172
*/
72-
public void verify(List<?> target) throws PatchFailedException {
73+
public void verify(List<T> target) throws PatchFailedException {
7374
if (last() > target.size()) {
7475
throw new PatchFailedException("Incorrect Chunk: the position of chunk > target size");
7576
}
@@ -88,14 +89,14 @@ public int getPosition() {
8889
return position;
8990
}
9091

91-
public void setLines(List<?> lines) {
92+
public void setLines(List<T> lines) {
9293
this.lines = lines;
9394
}
9495

9596
/**
9697
* @return the affected lines
9798
*/
98-
public List<?> getLines() {
99+
public List<T> getLines() {
99100
return lines;
100101
}
101102

src/main/java/difflib/DeleteDelta.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,19 @@
2121
* Describes the delete-delta between original and revised texts.
2222
*
2323
* @author <a href="dm.naumenko@gmail.com">Dmitry Naumenko</a>
24+
* @param T The type of the compared elements in the 'lines'.
2425
*/
25-
public class DeleteDelta extends Delta {
26+
public class DeleteDelta<T> extends Delta<T> {
2627

27-
/**
28-
* {@inheritDoc}
29-
*/
30-
public DeleteDelta(Chunk original, Chunk revised) {
28+
/**
29+
* Creates a change delta with the two given chunks.
30+
*
31+
* @param original
32+
* The original chunk. Must not be {@code null}.
33+
* @param revised
34+
* The original chunk. Must not be {@code null}.
35+
*/
36+
public DeleteDelta(Chunk<T> original, Chunk<T> revised) {
3137
super(original, revised);
3238
}
3339

@@ -37,7 +43,7 @@ public DeleteDelta(Chunk original, Chunk revised) {
3743
* @throws PatchFailedException
3844
*/
3945
@Override
40-
public void applyTo(List<Object> target) throws PatchFailedException {
46+
public void applyTo(List<T> target) throws PatchFailedException {
4147
verify(target);
4248
int position = getOriginal().getPosition();
4349
int size = getOriginal().size();
@@ -50,9 +56,9 @@ public void applyTo(List<Object> target) throws PatchFailedException {
5056
* {@inheritDoc}
5157
*/
5258
@Override
53-
public void restore(List<Object> target) {
59+
public void restore(List<T> target) {
5460
int position = this.getRevised().getPosition();
55-
List<?> lines = this.getOriginal().getLines();
61+
List<T> lines = this.getOriginal().getLines();
5662
for (int i = 0; i < lines.size(); i++) {
5763
target.add(position + i, lines.get(i));
5864
}
@@ -64,7 +70,7 @@ public TYPE getType() {
6470
}
6571

6672
@Override
67-
public void verify(List<?> target) throws PatchFailedException {
73+
public void verify(List<T> target) throws PatchFailedException {
6874
getOriginal().verify(target);
6975
}
7076

src/main/java/difflib/Delta.java

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,42 @@
2121
* Describes the delta between original and revised texts.
2222
*
2323
* @author <a href="dm.naumenko@gmail.com">Dmitry Naumenko</a>
24+
* @param T The type of the compared elements in the 'lines'.
2425
*/
25-
public abstract class Delta {
26-
private Chunk original;
27-
private Chunk revised;
26+
public abstract class Delta<T> {
27+
28+
/** The original chunk. */
29+
private Chunk<T> original;
2830

31+
/** The revised chunk. */
32+
private Chunk<T> revised;
33+
34+
/**
35+
* Specifies the type of the delta.
36+
*
37+
*/
2938
public enum TYPE {
30-
CHANGE, DELETE, INSERT
39+
/** A change in the original. */
40+
CHANGE,
41+
/** A delete from the original. */
42+
DELETE,
43+
/** An insert into the original. */
44+
INSERT
3145
}
3246

3347
/**
3448
* Construct the delta for original and revised chunks
3549
*
36-
* @param original chunk describes the original text
37-
* @param revised chunk describes the revised text
50+
* @param original Chunk describing the original text. Must not be {@code null}.
51+
* @param revised Chunk describing the revised text. Must not be {@code null}.
3852
*/
39-
public Delta(Chunk original, Chunk revised) {
53+
public Delta(Chunk<T> original, Chunk<T> revised) {
54+
if (original == null) {
55+
throw new IllegalArgumentException("original must not be null");
56+
}
57+
if (revised == null) {
58+
throw new IllegalArgumentException("revised must not be null");
59+
}
4060
this.original = original;
4161
this.revised = revised;
4262
}
@@ -47,23 +67,23 @@ public Delta(Chunk original, Chunk revised) {
4767
* @param target the text to patch.
4868
* @throws PatchFailedException if the patch cannot be applied.
4969
*/
50-
public abstract void verify(List<?> target) throws PatchFailedException;
70+
public abstract void verify(List<T> target) throws PatchFailedException;
5171

5272
/**
5373
* Applies this delta as the patch for a given target
5474
*
5575
* @param target the given target
5676
* @throws PatchFailedException
5777
*/
58-
public abstract void applyTo(List<Object> target) throws PatchFailedException;
78+
public abstract void applyTo(List<T> target) throws PatchFailedException;
5979

6080
/**
6181
* Cancel this delta for a given revised text. The action is opposite to
6282
* patch.
6383
*
6484
* @param target the given revised text
6585
*/
66-
public abstract void restore(List<Object> target);
86+
public abstract void restore(List<T> target);
6787

6888
/**
6989
* Returns the type of delta
@@ -72,38 +92,33 @@ public Delta(Chunk original, Chunk revised) {
7292
public abstract TYPE getType();
7393

7494
/**
75-
* @return the Chunk describing the original text
95+
* @return The Chunk describing the original text.
7696
*/
77-
public Chunk getOriginal() {
97+
public Chunk<T> getOriginal() {
7898
return original;
7999
}
80100

81101
/**
82-
* @param original the Chunk describing the original text to set
102+
* @param original The Chunk describing the original text to set.
83103
*/
84-
public void setOriginal(Chunk original) {
104+
public void setOriginal(Chunk<T> original) {
85105
this.original = original;
86106
}
87107

88108
/**
89-
* @return the Chunk describing the revised text
109+
* @return The Chunk describing the revised text.
90110
*/
91-
public Chunk getRevised() {
111+
public Chunk<T> getRevised() {
92112
return revised;
93113
}
94114

95115
/**
96-
* @param revised the Chunk describing the revised text to set
116+
* @param revised The Chunk describing the revised text to set.
97117
*/
98-
public void setRevised(Chunk revised) {
118+
public void setRevised(Chunk<T> revised) {
99119
this.revised = revised;
100120
}
101121

102-
/*
103-
* (non-Javadoc)
104-
*
105-
* @see java.lang.Object#hashCode()
106-
*/
107122
@Override
108123
public int hashCode() {
109124
final int prime = 31;
@@ -113,11 +128,6 @@ public int hashCode() {
113128
return result;
114129
}
115130

116-
/*
117-
* (non-Javadoc)
118-
*
119-
* @see java.lang.Object#equals(java.lang.Object)
120-
*/
121131
@Override
122132
public boolean equals(Object obj) {
123133
if (this == obj)
@@ -126,7 +136,7 @@ public boolean equals(Object obj) {
126136
return false;
127137
if (getClass() != obj.getClass())
128138
return false;
129-
Delta other = (Delta) obj;
139+
Delta<T> other = (Delta) obj;
130140
if (original == null) {
131141
if (other.original != null)
132142
return false;

src/main/java/difflib/DeltaComparator.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55

66
/**
77
* @author mksenzov
8+
* @param T The type of the compared elements in the 'lines'.
89
*/
9-
public class DeltaComparator implements Comparator<Delta>, Serializable {
10+
public class DeltaComparator implements Comparator<Delta<?>>, Serializable {
1011
private static final long serialVersionUID = 1L;
11-
public static final Comparator<Delta> INSTANCE = new DeltaComparator();
12+
public static final Comparator<Delta<?>> INSTANCE = new DeltaComparator();
1213

1314
private DeltaComparator() {
1415
}
1516

16-
public int compare(final Delta a, final Delta b) {
17+
public int compare(final Delta<?> a, final Delta<?> b) {
1718
final int posA = a.getOriginal().getPosition();
1819
final int posB = b.getOriginal().getPosition();
1920
if (posA > posB) {

src/main/java/difflib/DiffAlgorithm.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,30 @@
1818
import java.util.*;
1919

2020
/**
21-
* The general interface for computing diffs between two texts
21+
* The general interface for computing diffs between two lists of elements of type T.
2222
*
2323
* @author <a href="dm.naumenko@gmail.com">Dmitry Naumenko</a>
24+
* @param T The type of the compared elements in the 'lines'.
2425
*/
25-
public interface DiffAlgorithm {
26+
public interface DiffAlgorithm<T> {
2627

2728
/**
2829
* Computes the difference between the original sequence and the revised
29-
* sequence and returns it as a {@link difflib.Patch Patch} object.
30+
* sequence and returns it as a {@link Patch} object.
3031
*
31-
* @param original the original text
32-
* @param revised the revised text
33-
* @return the patch
32+
* @param original The original sequence. Must not be {@code null}.
33+
* @param revised The revised sequence. Must not be {@code null}.
34+
* @return The patch representing the diff of the given sequences. Never {@code null}.
3435
*/
35-
public Patch diff(Object[] original, Object[] revised);
36+
public Patch<T> diff(T[] original, T[] revised);
3637

3738
/**
3839
* Computes the difference between the original sequence and the revised
39-
* sequence and returns it as a {@link difflib.Patch Patch} object.
40+
* sequence and returns it as a {@link Patch} object.
4041
*
41-
* @param original the original text
42-
* @param revised the revised text
43-
* @return the patch
42+
* @param original The original sequence. Must not be {@code null}.
43+
* @param revised The revised sequence. Must not be {@code null}.
44+
* @return The patch representing the diff of the given sequences. Never {@code null}.
4445
*/
45-
public Patch diff(List<?> original, List<?> revised);
46+
public Patch<T> diff(List<T> original, List<T> revised);
4647
}

0 commit comments

Comments
 (0)