-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDiffLinesLabel.java
More file actions
168 lines (139 loc) · 5.53 KB
/
DiffLinesLabel.java
File metadata and controls
168 lines (139 loc) · 5.53 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package org.variantsync.diffdetective.variation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import org.variantsync.diffdetective.diff.text.DiffLineNumber;
import org.variantsync.diffdetective.util.Assert;
import org.variantsync.diffdetective.util.StringUtils;
import org.variantsync.diffdetective.variation.diff.Time;
import org.variantsync.diffdetective.variation.diff.VariationDiff; // For Javadoc
/**
* A label consisting of lines with {@link DiffLineNumber line number} information for each line.
*
* Using this label, a {@link VariationDiff} encodes all information (except the original line
* ending encoding, i.e. {@code LF} vs {@code CRLF}) necessary to reconstruct the original files
* and, when used during construction, the line diff between these files.
*/
public class DiffLinesLabel implements Label {
private final List<Line> lines;
private List<Line> trailingLines;
public record Line(String content, DiffLineNumber lineNumber) {
public static Line withInvalidLineNumber(String content) {
return new Line(content, DiffLineNumber.Invalid());
}
}
public DiffLinesLabel() {
this(new ArrayList<>());
}
public DiffLinesLabel(List<Line> lines) {
this(lines, new ArrayList<>());
}
public DiffLinesLabel(List<Line> lines, List<Line> trailingLines) {
Assert.assertNotNull(lines);
Assert.assertNotNull(trailingLines);
this.lines = lines;
this.trailingLines = trailingLines;
}
public static DiffLinesLabel withInvalidLineNumbers(List<String> lines) {
return new DiffLinesLabel(lines.stream().map(Line::withInvalidLineNumber).toList());
}
public static DiffLinesLabel ofCodeBlock(String codeBlock) {
return withInvalidLineNumbers(Arrays.asList(StringUtils.LINEBREAK_REGEX.split(codeBlock, -1)));
}
public void addDiffLine(Line newLine) {
lines.add(newLine);
}
public void addDiffLines(List<Line> newLines) {
lines.addAll(newLines);
}
public List<Line> getDiffLines() {
return lines;
}
public void setDiffTrailingLines(List<Line> newLines) {
Assert.assertNotNull(newLines);
trailingLines = newLines;
}
public List<Line> getDiffTrailingLines() {
return trailingLines;
}
@Override
public List<String> getLines() {
return getDiffLines().stream().map(Line::content).toList();
}
@Override
public List<String> getTrailingLines() {
return getDiffTrailingLines().stream().map(Line::content).toList();
}
/**
* Returns a deep copy where the line numbers at {@code time} are set to
* {@link DiffLineNumber#InvalidLineNumber}.
*/
@Override
public DiffLinesLabel withoutTimeDependentState(Time time) {
return new DiffLinesLabel(
mapWithoutTimeDependentState(getDiffLines(), time),
mapWithoutTimeDependentState(getDiffTrailingLines(), time)
);
}
private List<Line> mapWithoutTimeDependentState(List<Line> lines, Time time) {
return lines
.stream()
.map(line -> new Line(
line.content(),
line.lineNumber().withLineNumberAtTime(DiffLineNumber.InvalidLineNumber, time)
))
.toList();
}
/**
* Returns a deep copy where the line numbers at {@code time} are copied from
* {@code otherLabel}. The number of lines and their content must be identical in {@code this}
* and {@code otherLabel}.
*
* @see withoutTimeDependentState
*/
@Override
public DiffLinesLabel withTimeDependentStateFrom(Label otherLabel, Time time) {
DiffLinesLabel other = (DiffLinesLabel) otherLabel;
return new DiffLinesLabel(
zipWithTimeDependentStateFrom(this.getDiffLines(), other.getDiffLines(), time),
zipWithTimeDependentStateFrom(this.getDiffTrailingLines(), other.getDiffTrailingLines(), time)
);
}
private static List<Line> zipWithTimeDependentStateFrom(List<Line> linesA, List<Line> linesB, Time time) {
List<Line> result = new ArrayList<>(linesA.size());
Iterator<Line> itA = linesA.iterator();
Iterator<Line> itB = linesA.iterator();
while (itA.hasNext() && itB.hasNext()) {
Line lineA = itA.next();
Line lineB = itB.next();
Assert.assertEquals(lineA.content(), lineB.content(), "Mismatching line contents in a call to `withTimeDependentStateFrom` detected");
result.add(new Line(
lineA.content(),
lineB.lineNumber().withLineNumberAtTime(lineB.lineNumber().atTime(time), time)
));
}
Assert.assertFalse(itA.hasNext() || itB.hasNext(), "Mismatching line counts in a call to `withTimeDependentStateFrom` detected");
return result;
}
@Override
public String toString() {
return lines
.stream()
.map(Line::content)
.collect(Collectors.joining(StringUtils.LINEBREAK));
}
@Override
public DiffLinesLabel clone() {
return new DiffLinesLabel(new ArrayList<>(lines), new ArrayList<>(trailingLines));
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DiffLinesLabel that = (DiffLinesLabel) o;
return lines.equals(that.lines) &&
trailingLines.equals(that.trailingLines);
}
}