Skip to content

Commit 64f242c

Browse files
authored
Merge pull request #171 from VariantSync/refactorings
Fix the children order in line graph and various refactorings
2 parents 2762ab9 + f2e36e2 commit 64f242c

206 files changed

Lines changed: 3224 additions & 2218 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/main/java/org/variantsync/diffdetective/diff/git/GitDiffer.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,12 @@ public static CommitDiffResult createCommitDiff(
111111
final CanonicalTreeParser currentTreeParser = new CanonicalTreeParser();
112112
final CanonicalTreeParser prevTreeParser = new CanonicalTreeParser();
113113
try (ObjectReader reader = repository.getGitRepo().getRepository().newObjectReader()) {
114-
try {
115-
currentTreeParser.reset(reader, childCommit.getTree());
116-
if (parentCommit != null) {
117-
prevTreeParser.reset(reader, parentCommit.getTree());
118-
}
119-
} catch (IOException e) {
120-
return CommitDiffResult.Failure(DiffError.JGIT_ERROR, e.toString());
114+
currentTreeParser.reset(reader, childCommit.getTree());
115+
if (parentCommit != null) {
116+
prevTreeParser.reset(reader, parentCommit.getTree());
121117
}
118+
} catch (IOException e) {
119+
return CommitDiffResult.Failure(DiffError.JGIT_ERROR, e.toString());
122120
}
123121

124122
final AbstractTreeIterator parentTreeIterator;

src/main/java/org/variantsync/diffdetective/gumtree/VariationDiffAdapter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public VariationDiffAdapter(Projection<L> node) {
2222
super(node);
2323
}
2424

25+
@Override
2526
protected VariationTreeAdapter<L> newInstance(VariationNode<?, L> node) {
2627
return new VariationDiffAdapter<>(Cast.unchecked(node));
2728
}

src/main/java/org/variantsync/diffdetective/gumtree/VariationTreeAdapter.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import java.util.ArrayList;
44
import java.util.Iterator;
5+
import java.util.LinkedHashMap;
56
import java.util.Map.Entry;
67
import java.util.stream.Collectors;
8+
import java.util.stream.Stream;
79

810
import org.variantsync.diffdetective.variation.Label;
911
import org.variantsync.diffdetective.variation.tree.VariationNode;
@@ -24,15 +26,17 @@
2426
public class VariationTreeAdapter<L extends Label> extends AbstractTree {
2527
private String cachedLabel;
2628
private VariationNode<?, L> backingNode;
29+
private LinkedHashMap<String, Object> metadata;
2730

2831
public VariationTreeAdapter(VariationNode<?, L> node) {
2932
this.backingNode = node;
33+
this.metadata = new LinkedHashMap<>();
3034

31-
if (backingNode.isConditionalAnnotation()) {
32-
cachedLabel = backingNode.getFormula().toString();
33-
} else {
34-
cachedLabel = backingNode.getLabel().getLines().stream().collect(Collectors.joining("\n"));
35-
}
35+
cachedLabel =
36+
Stream.concat(
37+
backingNode.getLabel().getLines().stream(),
38+
backingNode.getLabel().getTrailingLines().stream()
39+
).collect(Collectors.joining("\n"));
3640

3741
var children = new ArrayList<Tree>(node.getChildren().size());
3842
for (var child : node.getChildren()) {
@@ -41,7 +45,7 @@ public VariationTreeAdapter(VariationNode<?, L> node) {
4145
setChildren(children);
4246
}
4347

44-
protected VariationTreeAdapter newInstance(VariationNode<?, L> node) {
48+
protected VariationTreeAdapter<L> newInstance(VariationNode<?, L> node) {
4549
return new VariationTreeAdapter<>(node);
4650
}
4751

@@ -65,12 +69,12 @@ public int getLength() {
6569

6670
@Override
6771
public Iterator<Entry<String, Object>> getMetadata() {
68-
throw new UnsupportedOperationException();
72+
return metadata.entrySet().iterator();
6973
}
7074

7175
@Override
72-
public Object getMetadata(String arg0) {
73-
throw new UnsupportedOperationException();
76+
public Object getMetadata(String key) {
77+
return metadata.get(key);
7478
}
7579

7680
/**
@@ -97,8 +101,8 @@ public void setLength(int length) {
97101
}
98102

99103
@Override
100-
public Object setMetadata(String name, Object value) {
101-
throw new UnsupportedOperationException();
104+
public Object setMetadata(String key, Object value) {
105+
return metadata.put(key, value);
102106
}
103107

104108
@Override

src/main/java/org/variantsync/diffdetective/util/Assert.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public static void assertFalse(boolean condition, String errorMessage) {
8686
}
8787

8888
/** Throws {@link AssertionError} with {@code errorMessage} as error message. */
89-
public static void fail(String errorMessage) {
89+
public static <T> T fail(String errorMessage) {
9090
throw new AssertionError(errorMessage);
9191
}
9292

@@ -105,12 +105,18 @@ public static void assertNull(Object o) {
105105
}
106106

107107
public static <T> void assertEquals(T expected, T actual) {
108+
assertEquals(expected, actual, null);
109+
}
110+
111+
public static <T> void assertEquals(T expected, T actual, String message) {
112+
String prefix = message == null ? "" : message + ": ";
113+
108114
if (expected == null) {
109115
if (actual != null) {
110-
fail("expected is null but actual is not!");
116+
fail(prefix + "expected is null but actually it is " + actual);
111117
}
112118
} else {
113-
assertTrue(expected.equals(actual), expected + " != " + actual);
119+
assertTrue(expected.equals(actual), prefix + expected + " != " + actual);
114120
}
115121
}
116122
}

src/main/java/org/variantsync/diffdetective/variation/DiffLinesLabel.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
import java.util.ArrayList;
44
import java.util.Arrays;
5+
import java.util.Iterator;
56
import java.util.List;
67
import java.util.stream.Collectors;
78

89
import org.variantsync.diffdetective.diff.text.DiffLineNumber;
910
import org.variantsync.diffdetective.util.Assert;
1011
import org.variantsync.diffdetective.util.StringUtils;
12+
import org.variantsync.diffdetective.variation.diff.Time;
1113
import org.variantsync.diffdetective.variation.diff.VariationDiff; // For Javadoc
1214

1315
/**
@@ -82,6 +84,66 @@ public List<String> getTrailingLines() {
8284
return getDiffTrailingLines().stream().map(Line::content).toList();
8385
}
8486

87+
/**
88+
* Returns a deep copy where the line numbers at {@code time} are set to
89+
* {@link DiffLineNumber#InvalidLineNumber}.
90+
*/
91+
@Override
92+
public DiffLinesLabel withoutTimeDependentState(Time time) {
93+
return new DiffLinesLabel(
94+
mapWithoutTimeDependentState(getDiffLines(), time),
95+
mapWithoutTimeDependentState(getDiffTrailingLines(), time)
96+
);
97+
}
98+
99+
private List<Line> mapWithoutTimeDependentState(List<Line> lines, Time time) {
100+
return lines
101+
.stream()
102+
.map(line -> new Line(
103+
line.content(),
104+
line.lineNumber().withLineNumberAtTime(DiffLineNumber.InvalidLineNumber, time)
105+
))
106+
.toList();
107+
}
108+
109+
/**
110+
* Returns a deep copy where the line numbers at {@code time} are copied from
111+
* {@code otherLabel}. The number of lines and their content must be identical in {@code this}
112+
* and {@code otherLabel}.
113+
*
114+
* @see withoutTimeDependentState
115+
*/
116+
@Override
117+
public DiffLinesLabel withTimeDependentStateFrom(Label otherLabel, Time time) {
118+
DiffLinesLabel other = (DiffLinesLabel) otherLabel;
119+
120+
return new DiffLinesLabel(
121+
zipWithTimeDependentStateFrom(this.getDiffLines(), other.getDiffLines(), time),
122+
zipWithTimeDependentStateFrom(this.getDiffTrailingLines(), other.getDiffTrailingLines(), time)
123+
);
124+
}
125+
126+
private static List<Line> zipWithTimeDependentStateFrom(List<Line> linesA, List<Line> linesB, Time time) {
127+
List<Line> result = new ArrayList<>(linesA.size());
128+
129+
Iterator<Line> itA = linesA.iterator();
130+
Iterator<Line> itB = linesA.iterator();
131+
while (itA.hasNext() && itB.hasNext()) {
132+
Line lineA = itA.next();
133+
Line lineB = itB.next();
134+
135+
Assert.assertEquals(lineA.content(), lineB.content(), "Mismatching line contents in a call to `withTimeDependentStateFrom` detected");
136+
result.add(new Line(
137+
lineA.content(),
138+
lineB.lineNumber().withLineNumberAtTime(lineB.lineNumber().atTime(time), time)
139+
));
140+
}
141+
142+
Assert.assertFalse(itA.hasNext() || itB.hasNext(), "Mismatching line counts in a call to `withTimeDependentStateFrom` detected");
143+
144+
return result;
145+
}
146+
85147
@Override
86148
public String toString() {
87149
return lines

src/main/java/org/variantsync/diffdetective/variation/Label.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.List;
44

5+
import org.variantsync.diffdetective.util.Assert;
6+
import org.variantsync.diffdetective.variation.diff.Time;
57
import org.variantsync.diffdetective.variation.diff.VariationDiff; // For Javadoc
68
import org.variantsync.diffdetective.variation.tree.VariationTree; // For Javadoc
79

@@ -19,6 +21,29 @@ public interface Label {
1921
* For example, {@code #endif}s are stored as trailing lines.
2022
*/
2123
List<String> getTrailingLines();
24+
25+
/**
26+
* Returns a deep copy where the state that is only valid at {@code time} is set to its default
27+
* value. Note that not all implementations need to have time dependent state.
28+
*
29+
* @see withTimeDependentStateFrom
30+
*/
31+
default Label withoutTimeDependentState(Time time) {
32+
return this.clone();
33+
}
34+
35+
/**
36+
* Returns a deep copy where the state that is only valid at {@code time} is copied from
37+
* {@code other}. All time independent state must be equal in {@code this} and {@code other}.
38+
* Note that not all implementations need to have time dependent state.
39+
*
40+
* @see withoutTimeDependentState
41+
*/
42+
default Label withTimeDependentStateFrom(Label other, Time time) {
43+
Assert.assertEquals(this, other);
44+
return this.clone();
45+
}
46+
2247
/**
2348
* Creates a deep copy of this label.
2449
*/

0 commit comments

Comments
 (0)