-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatchr.java
More file actions
59 lines (52 loc) · 1.95 KB
/
Patchr.java
File metadata and controls
59 lines (52 loc) · 1.95 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
package diffr.patch;
import com.google.common.collect.Range;
import diffr.util.instruction.*;
import java.util.ArrayList;
import java.util.List;
/**
* Generates a list of Strings by applying a patch to the original file.
*
* @author Amaury Couste
* @author William Martin
* @since 0.3
*/
public class Patchr {
private final List<String> originalFile;
private final List<Instruction> instructions;
/**
* Default constructor.
*
* @param originalFile the original file.
* @param patchFile the patch file.
* @throws IllegalPatchFileException if there was an error reading the patch file.
*/
public Patchr(final List<String> originalFile, final List<String> patchFile) throws IllegalPatchFileException {
this.originalFile = originalFile;
try {
this.instructions = Instructions.readInstructions(patchFile);
} catch (final IllegalPatchInstructionException ipe) {
throw new IllegalPatchFileException("Error. Illegal patch file: " + ipe.getMessage());
}
}
/**
* Applies the patch file to the original file.
*
* @return a list of strings representing the patched file.
*/
public List<String> patch() {
final List<String> patchedFile = new ArrayList<String>();
for (final Instruction instruction : instructions) {
switch (instruction.getType()) {
case Copy:
final CopyInstruction copyInstruction = (CopyInstruction) instruction;
final Range<Integer> range = copyInstruction.getRange();
patchedFile.addAll(originalFile.subList(range.lowerEndpoint(), 1 + range.upperEndpoint()));
break;
case Insert:
final InsertInstruction insertInstruction = (InsertInstruction) instruction;
patchedFile.add(insertInstruction.getText());
}
}
return patchedFile;
}
}