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

Commit 413c161

Browse files
author
dm.naumenko@gmail.com
committed
Repo init
git-svn-id: http://java-diff-utils.googlecode.com/svn/trunk@2 d8d7d024-a22d-11de-b755-fd640f38fa9d
1 parent fd7d842 commit 413c161

18 files changed

+1448
-0
lines changed

src/difflib/ChangeDelta.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package difflib;
2+
3+
import java.util.List;
4+
5+
/**
6+
* Describes the change-delta between original and revised texts.
7+
*
8+
* @author <a href="dm.naumenko@gmail.com">Dmitry Naumenko</a>
9+
*/
10+
public class ChangeDelta extends Delta {
11+
12+
/**
13+
* {@inheritDoc}
14+
*/
15+
public ChangeDelta(Chunk original, Chunk revised) {
16+
super(original, revised);
17+
}
18+
19+
/**
20+
* {@inheritDoc}
21+
*/
22+
@Override
23+
public void applyTo(List<Object> target) {
24+
int position = getOriginal().getPosition();
25+
int originalSize = getOriginal().getSize();
26+
for (int i = 0; i < originalSize; i++) {
27+
target.remove(position);
28+
}
29+
int i = 0;
30+
for (Object line: getRevised().getLines()) {
31+
target.add(position + i, line);
32+
i++;
33+
}
34+
}
35+
36+
}

src/difflib/Chunk.java

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package difflib;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Holds the information about the part of text involved in the diff process
7+
*
8+
* <p>Text is represented as <code>Object[]</code> because
9+
* the diff engine is capable of handling more than plain ascci. In fact,
10+
* arrays or lists of any type that implements
11+
* {@link java.lang.Object#hashCode hashCode()} and
12+
* {@link java.lang.Object#equals equals()}
13+
* correctly can be subject to differencing using this
14+
* library.</p>
15+
*
16+
* @author <a href="dm.naumenko@gmail.com>Dmitry Naumenko</a>
17+
*/
18+
public class Chunk {
19+
private int position;
20+
private int size;
21+
private List<?> lines;
22+
23+
/**
24+
* Creates a chunk and saves a copy of affected lines
25+
*
26+
* @param position the start position
27+
* @param size the size of a Chunk
28+
* @param lines the affected lines
29+
*/
30+
public Chunk(int position, int size, List<?> lines) {
31+
this.position = position;
32+
this.size = size;
33+
this.lines = lines;
34+
}
35+
36+
/**
37+
* Creates a chunk and saves a copy of affected lines
38+
*
39+
* @param position the start position
40+
* @param size the size of a Chunk
41+
* @param lines the affected lines
42+
*/
43+
public Chunk(int position, int size, Object[] lines) {
44+
this.position = position;
45+
this.size = size;
46+
this.lines = Arrays.asList(lines);
47+
}
48+
49+
/**
50+
* @return the start position of chunk in the text
51+
*/
52+
public int getPosition() {
53+
return position;
54+
}
55+
/**
56+
* @param position the start position to set
57+
*/
58+
public void setPosition(int position) {
59+
this.position = position;
60+
}
61+
62+
/**
63+
* @return the size of Chunk (size of affected lines)
64+
*/
65+
public int getSize() {
66+
return size;
67+
}
68+
/**
69+
* @param size the size of affected lines to set
70+
*/
71+
public void setSize(int size) {
72+
this.size = size;
73+
}
74+
75+
/**
76+
* @return the affected lines
77+
*/
78+
public List<?> getLines() {
79+
return lines;
80+
}
81+
/**
82+
* @param lines the affected lines to set
83+
*/
84+
public void setLines(List<?> lines) {
85+
this.lines = lines;
86+
}
87+
88+
/* (non-Javadoc)
89+
* @see java.lang.Object#hashCode()
90+
*/
91+
@Override
92+
public int hashCode() {
93+
final int prime = 31;
94+
int result = 1;
95+
result = prime * result + ((lines == null) ? 0 : lines.hashCode());
96+
result = prime * result + position;
97+
result = prime * result + size;
98+
return result;
99+
}
100+
101+
/* (non-Javadoc)
102+
* @see java.lang.Object#equals(java.lang.Object)
103+
*/
104+
@Override
105+
public boolean equals(Object obj) {
106+
if (this == obj)
107+
return true;
108+
if (obj == null)
109+
return false;
110+
if (getClass() != obj.getClass())
111+
return false;
112+
Chunk other = (Chunk) obj;
113+
if (lines == null) {
114+
if (other.lines != null)
115+
return false;
116+
} else if (!lines.equals(other.lines))
117+
return false;
118+
if (position != other.position)
119+
return false;
120+
if (size != other.size)
121+
return false;
122+
return true;
123+
}
124+
125+
@Override
126+
public String toString() {
127+
return "[position: " + position + ", size: " + size + ", lines: " + lines + "]";
128+
}
129+
130+
}

src/difflib/DeleteDelta.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package difflib;
2+
3+
import java.util.List;
4+
5+
/**
6+
* Describes the delete-delta between original and revised texts.
7+
*
8+
* @author <a href="dm.naumenko@gmail.com">Dmitry Naumenko</a>
9+
*/
10+
public class DeleteDelta extends Delta {
11+
12+
/**
13+
* {@inheritDoc}
14+
*/
15+
public DeleteDelta(Chunk original, Chunk revised) {
16+
super(original, revised);
17+
}
18+
19+
/**
20+
* {@inheritDoc}
21+
*/
22+
@Override
23+
public void applyTo(List<Object> target) {
24+
int position = getOriginal().getPosition();
25+
int size = getOriginal().getSize();
26+
for (int i = 0; i < size; i++) {
27+
target.remove(position);
28+
}
29+
}
30+
31+
}

src/difflib/Delta.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package difflib;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Describes the delta between original and revised texts.
7+
*
8+
* @author <a href="dm.naumenko@gmail.com">Dmitry Naumenko</a>
9+
*/
10+
public abstract class Delta {
11+
private Chunk original;
12+
private Chunk revised;
13+
14+
/**
15+
* Construct the delta for original and revised chunks
16+
*
17+
* @param original chunk describes the original text
18+
* @param revised chunk describes the revised text
19+
*/
20+
public Delta(Chunk original, Chunk revised) {
21+
this.original = original;
22+
this.revised = revised;
23+
}
24+
25+
/**
26+
* Applies this delta as the patch for a given target
27+
*
28+
* @param target the given target
29+
*/
30+
public abstract void applyTo(List<Object> target);
31+
32+
/**
33+
* @return the Chunk describing the original text
34+
*/
35+
public Chunk getOriginal() {
36+
return original;
37+
}
38+
/**
39+
* @param original the Chunk describing the original text to set
40+
*/
41+
public void setOriginal(Chunk original) {
42+
this.original = original;
43+
}
44+
45+
/**
46+
* @return the Chunk describing the revised text
47+
*/
48+
public Chunk getRevised() {
49+
return revised;
50+
}
51+
/**
52+
* @param revised the Chunk describing the revised text to set
53+
*/
54+
public void setRevised(Chunk revised) {
55+
this.revised = revised;
56+
}
57+
}

src/difflib/DiffAlgorithm.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package difflib;
2+
3+
import java.util.*;
4+
5+
/**
6+
* The general interface for computing diffs between two texts
7+
*
8+
* @author <a href="dm.naumenko@gmail.com">Dmitry Naumenko</a>
9+
*/
10+
public interface DiffAlgorithm {
11+
12+
/**
13+
* Computes the difference between the original
14+
* sequence and the revised sequence and returns it
15+
* as a {@link difflib.Patch Patch} object.
16+
*
17+
* @param original the original text
18+
* @param revised the revised text
19+
* @return the patch
20+
*/
21+
public Patch diff(Object[] original, Object[] revised);
22+
23+
/**
24+
* Computes the difference between the original
25+
* sequence and the revised sequence and returns it
26+
* as a {@link difflib.Patch Patch} object.
27+
*
28+
* @param original the original text
29+
* @param revised the revised text
30+
* @return the patch
31+
*/
32+
public Patch diff(List<?> original, List<?> revised);
33+
}

src/difflib/DiffRow.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package difflib;
2+
3+
/**
4+
* Describes the diff row in form [tag, oldLine, newLine) for showing the difference between two
5+
* texts
6+
*
7+
* @author <a href="dm.naumenko@gmail.com">Dmitry Naumenko</a>
8+
*/
9+
public class DiffRow {
10+
private Tag tag;
11+
private String oldLine;
12+
private String newLine;
13+
14+
public enum Tag {
15+
INSERT, DELETE, CHANGE
16+
}
17+
18+
/**
19+
* @return the tag
20+
*/
21+
public Tag getTag() {
22+
return tag;
23+
}
24+
/**
25+
* @param tag the tag to set
26+
*/
27+
public void setTag(Tag tag) {
28+
this.tag = tag;
29+
}
30+
31+
/**
32+
* @return the oldLine
33+
*/
34+
public String getOldLine() {
35+
return oldLine;
36+
}
37+
/**
38+
* @param oldLine the oldLine to set
39+
*/
40+
public void setOldLine(String oldLine) {
41+
this.oldLine = oldLine;
42+
}
43+
44+
/**
45+
* @return the newLine
46+
*/
47+
public String getNewLine() {
48+
return newLine;
49+
}
50+
/**
51+
* @param newLine the newLine to set
52+
*/
53+
public void setNewLine(String newLine) {
54+
this.newLine = newLine;
55+
}
56+
}

0 commit comments

Comments
 (0)