-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiffr.java
More file actions
71 lines (56 loc) · 2.05 KB
/
Diffr.java
File metadata and controls
71 lines (56 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package diffr.diff;
import com.google.common.collect.Lists;
import diffr.suffixtree.SuffixTree;
import diffr.suffixtree.SuffixTree.Matcher;
import diffr.suffixtree.SuffixTrees;
import diffr.util.instruction.CopyInstruction;
import diffr.util.instruction.InsertInstruction;
import diffr.util.instruction.Instruction;
import java.util.List;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Generates a list of {@link Instruction}s to transform original file into a new file.
*
* @author Sarina Gurung
* @author Jakub D Kozlowski
* @since 0.3
*/
public final class Diffr {
private final List<String> originalFile;
private final List<String> newFile;
/**
* Default constructor.
*
* @param originalFile original file to be transform.
* @param newFile new file to transform {@code originalFile} to.
*
* @throws NullPointerException if any parameter is null.
*/
public Diffr(final List<String> originalFile, final List<String> newFile) {
this.originalFile = checkNotNull(originalFile);
this.newFile = checkNotNull(newFile);
}
/**
* Gets the list of {@link Instruction}s to transform {@code originalFile} to {@code newFile}.
*
* @return list of {@link Instruction}s.
*/
public List<Instruction> diff() {
final List<Instruction> instructions = Lists.newArrayList();
final SuffixTree<String> suffixTree = SuffixTrees.newSuffixTree(this.originalFile);
Matcher<String> matcher = suffixTree.matcher();
for (final String newFileLine : newFile) {
if (!matcher.matchNext(newFileLine).isMatched()) {
if (!matcher.isRoot()) {
instructions.add(new CopyInstruction(matcher.range()));
}
instructions.add(new InsertInstruction(newFileLine));
matcher = suffixTree.matcher();
}
}
if (!matcher.isRoot()) {
instructions.add(new CopyInstruction(matcher.range()));
}
return instructions;
}
}