1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.finalist.util;
19
20 import com.lowagie.text.html.HtmlEncoder;
21
22 /***
23 * This class represents a line of text from a source file that conflicted during the diff process.
24 *
25 * @author Michael O'Connor - Finalist IT Group.
26 */
27 public class DiffConflictLine {
28
29 private String line;
30 private int number;
31 private boolean firstFile;
32
33 /*** A special case of DiffConflictLine, used to represent the last line in a file. */
34 public static final DiffConflictLine EOF = new DiffConflictLine();
35
36
37 /***
38 * Constructs a DiffConflictLine.
39 * @param firstFile <code>true</code> if this line comes from the 'first' file (a diff involves 2 files).
40 * @param number the line number within the original file.
41 * @param line the text of the line.
42 */
43 public DiffConflictLine(boolean firstFile, int number, String line) {
44 this.number = number;
45 this.firstFile = firstFile;
46 this.line = line;
47 }
48
49 private DiffConflictLine() {
50 }
51
52
53 /***
54 * Checks if the given line has the same text as this one (ignoring whitespace).
55 * @param line2 the other line.
56 * @return <code>true</code> if equal.
57 */
58 public boolean lineEquals(DiffConflictLine line2) {
59 return line.trim().equals(line2.getLine().trim());
60 }
61
62 public String getLine() {
63 return line;
64 }
65
66 public boolean isEof() {
67 return (this == EOF);
68 }
69
70 public int getLineNumber() {
71 return number;
72 }
73
74 public boolean isFirstFile() {
75 return firstFile;
76 }
77 /***
78 * By default this renders a HTML result.
79 * @return
80 */
81 public String toString() {
82 return "<font class='file" + (firstFile ? "1" : "2") + "-code'>" +
83 (firstFile ? "<" : ">") + HtmlEncoder.encode(getLine()) + "</font><br>";
84 }
85
86 /***
87 * Checks if a given conflict line precedes this one.
88 *
89 * @param next
90 * @return
91 */
92 public boolean precedes(DiffConflictLine next) {
93 return next != null &&
94 (firstFile == next.firstFile) &&
95 next.getLineNumber() == number + 1;
96 }
97
98 }