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

Commit 32d6a50

Browse files
author
dm.naumenko@gmail.com
committed
New version which was lost after removing repo.
git-svn-id: http://java-diff-utils.googlecode.com/svn/trunk@4 d8d7d024-a22d-11de-b755-fd640f38fa9d
1 parent 413c161 commit 32d6a50

16 files changed

+1101
-98
lines changed

src/difflib/ChangeDelta.java

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/*
2+
Copyright 2009 Dmitry Naumenko (dm.naumenko@gmail.com)
3+
4+
This file is part of Java Diff Utills Library.
5+
6+
Java Diff Utills Library is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
Java Diff Utills Library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with Java Diff Utills Library. If not, see <http://www.gnu.org/licenses/>.
18+
*/
119
package difflib;
220

321
import java.util.List;
@@ -18,12 +36,14 @@ public ChangeDelta(Chunk original, Chunk revised) {
1836

1937
/**
2038
* {@inheritDoc}
39+
* @throws PatchFailedException
2140
*/
2241
@Override
23-
public void applyTo(List<Object> target) {
42+
public void applyTo(List<Object> target) throws PatchFailedException {
43+
verify(target);
2444
int position = getOriginal().getPosition();
25-
int originalSize = getOriginal().getSize();
26-
for (int i = 0; i < originalSize; i++) {
45+
int size = getOriginal().getSize();
46+
for (int i = 0; i < size; i++) {
2747
target.remove(position);
2848
}
2949
int i = 0;
@@ -33,4 +53,32 @@ public void applyTo(List<Object> target) {
3353
}
3454
}
3555

56+
/**
57+
* {@inheritDoc}
58+
*/
59+
@Override
60+
public void restore(List<Object> target) {
61+
int position = getRevised().getPosition();
62+
int size = getRevised().getSize();
63+
for (int i = 0; i < size; i++) {
64+
target.remove(position);
65+
}
66+
int i = 0;
67+
for (Object line: getOriginal().getLines()) {
68+
target.add(position + i, line);
69+
i++;
70+
}
71+
}
72+
73+
/**
74+
* {@inheritDoc}
75+
*/
76+
public void verify(List<?> target) throws PatchFailedException {
77+
getOriginal().verify(target);
78+
if (getOriginal().getPosition() > target.size()) {
79+
throw new PatchFailedException("Incorrect patch for delta: " +
80+
"delta original position > target size");
81+
}
82+
}
83+
3684
}

src/difflib/Chunk.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/*
2+
Copyright 2009 Dmitry Naumenko (dm.naumenko@gmail.com)
3+
4+
This file is part of Java Diff Utills Library.
5+
6+
Java Diff Utills Library is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
Java Diff Utills Library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with Java Diff Utills Library. If not, see <http://www.gnu.org/licenses/>.
18+
*/
119
package difflib;
220

321
import java.util.*;
@@ -46,6 +64,22 @@ public Chunk(int position, int size, Object[] lines) {
4664
this.lines = Arrays.asList(lines);
4765
}
4866

67+
/**
68+
* Verifies that this chunk's saved text matches the corresponding
69+
* text in the given sequence.
70+
* @param target the sequence to verify against.
71+
*/
72+
public void verify(List<?> target) throws PatchFailedException{
73+
if (last() > target.size()) {
74+
throw new PatchFailedException("Incorrect Chunk: the position of chunk > target size");
75+
}
76+
for (int i = 0; i < size; i++) {
77+
if (!target.get(position + i).equals(lines.get(i))) {
78+
throw new PatchFailedException("Incorrect Chunk: the chunk content doesn't match the target");
79+
}
80+
}
81+
}
82+
4983
/**
5084
* @return the start position of chunk in the text
5185
*/
@@ -84,6 +118,13 @@ public List<?> getLines() {
84118
public void setLines(List<?> lines) {
85119
this.lines = lines;
86120
}
121+
122+
/**
123+
* Returns the index of the last line of the chunk.
124+
*/
125+
public int last(){
126+
return getPosition() + getSize() - 1;
127+
}
87128

88129
/* (non-Javadoc)
89130
* @see java.lang.Object#hashCode()

src/difflib/DeleteDelta.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/*
2+
Copyright 2009 Dmitry Naumenko (dm.naumenko@gmail.com)
3+
4+
This file is part of Java Diff Utills Library.
5+
6+
Java Diff Utills Library is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
Java Diff Utills Library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with Java Diff Utills Library. If not, see <http://www.gnu.org/licenses/>.
18+
*/
119
package difflib;
220

321
import java.util.List;
@@ -18,14 +36,38 @@ public DeleteDelta(Chunk original, Chunk revised) {
1836

1937
/**
2038
* {@inheritDoc}
39+
* @throws PatchFailedException
2140
*/
2241
@Override
23-
public void applyTo(List<Object> target) {
42+
public void applyTo(List<Object> target) throws PatchFailedException {
43+
verify(target);
2444
int position = getOriginal().getPosition();
2545
int size = getOriginal().getSize();
2646
for (int i = 0; i < size; i++) {
2747
target.remove(position);
2848
}
2949
}
3050

51+
/**
52+
* {@inheritDoc}
53+
*/
54+
@Override
55+
public void restore(List<Object> target) {
56+
int position = this.getRevised().getPosition();
57+
List<?> lines = this.getOriginal().getLines();
58+
for (int i = 0; i < lines.size(); i++) {
59+
target.add(position + i, lines.get(i));
60+
}
61+
}
62+
63+
@Override
64+
public void verify(List<?> target) throws PatchFailedException {
65+
getOriginal().verify(target);
66+
}
67+
68+
@Override
69+
public String toString() {
70+
return "[DeleteDelta, position: " + getOriginal().getPosition() + ", lines: " +
71+
getOriginal().getLines() + "]";
72+
}
3173
}

src/difflib/Delta.java

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/*
2+
Copyright 2009 Dmitry Naumenko (dm.naumenko@gmail.com)
3+
4+
This file is part of Java Diff Utills Library.
5+
6+
Java Diff Utills Library is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
Java Diff Utills Library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with Java Diff Utills Library. If not, see <http://www.gnu.org/licenses/>.
18+
*/
119
package difflib;
220

321
import java.util.*;
@@ -22,12 +40,27 @@ public Delta(Chunk original, Chunk revised) {
2240
this.revised = revised;
2341
}
2442

43+
/**
44+
* Verifies that this delta can be used to patch the given text.
45+
* @param target the text to patch.
46+
* @throws PatchFailedException if the patch cannot be applied.
47+
*/
48+
public abstract void verify(List<?> target) throws PatchFailedException;
49+
2550
/**
2651
* Applies this delta as the patch for a given target
2752
*
28-
* @param target the given target
53+
* @param target the given target
54+
* @throws PatchFailedException
55+
*/
56+
public abstract void applyTo(List<Object> target) throws PatchFailedException;
57+
58+
/**
59+
* Cancel this delta for a given revised text. The action is opposite to patch.
60+
*
61+
* @param target the given revised text
2962
*/
30-
public abstract void applyTo(List<Object> target);
63+
public abstract void restore(List<Object> target);
3164

3265
/**
3366
* @return the Chunk describing the original text
@@ -54,4 +87,44 @@ public Chunk getRevised() {
5487
public void setRevised(Chunk revised) {
5588
this.revised = revised;
5689
}
90+
91+
/* (non-Javadoc)
92+
* @see java.lang.Object#hashCode()
93+
*/
94+
@Override
95+
public int hashCode() {
96+
final int prime = 31;
97+
int result = 1;
98+
result = prime * result
99+
+ ((original == null) ? 0 : original.hashCode());
100+
result = prime * result + ((revised == null) ? 0 : revised.hashCode());
101+
return result;
102+
}
103+
104+
/* (non-Javadoc)
105+
* @see java.lang.Object#equals(java.lang.Object)
106+
*/
107+
@Override
108+
public boolean equals(Object obj) {
109+
if (this == obj)
110+
return true;
111+
if (obj == null)
112+
return false;
113+
if (getClass() != obj.getClass())
114+
return false;
115+
Delta other = (Delta) obj;
116+
if (original == null) {
117+
if (other.original != null)
118+
return false;
119+
} else if (!original.equals(other.original))
120+
return false;
121+
if (revised == null) {
122+
if (other.revised != null)
123+
return false;
124+
} else if (!revised.equals(other.revised))
125+
return false;
126+
return true;
127+
}
128+
129+
57130
}

src/difflib/DiffAlgorithm.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/*
2+
Copyright 2009 Dmitry Naumenko (dm.naumenko@gmail.com)
3+
4+
This file is part of Java Diff Utills Library.
5+
6+
Java Diff Utills Library is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
Java Diff Utills Library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with Java Diff Utills Library. If not, see <http://www.gnu.org/licenses/>.
18+
*/
119
package difflib;
220

321
import java.util.*;

src/difflib/DiffException.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright 2009 Dmitry Naumenko (dm.naumenko@gmail.com)
3+
4+
This file is part of Java Diff Utills Library.
5+
6+
Java Diff Utills Library is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
Java Diff Utills Library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with Java Diff Utills Library. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
package difflib;
20+
21+
/**
22+
* Base class for all exceptions emanating from this package.
23+
*
24+
* @author <a href="mailto:juanco@suigeneris.org">Juanco Anez</a>
25+
*/
26+
public class DiffException extends Exception {
27+
28+
private static final long serialVersionUID = 1L;
29+
30+
public DiffException() {
31+
}
32+
33+
public DiffException(String msg) {
34+
super(msg);
35+
}
36+
}
37+

0 commit comments

Comments
 (0)