-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathVariationTreeAdapter.java
More file actions
122 lines (100 loc) · 3.29 KB
/
VariationTreeAdapter.java
File metadata and controls
122 lines (100 loc) · 3.29 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
package org.variantsync.diffdetective.gumtree;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.variantsync.diffdetective.variation.Label;
import org.variantsync.diffdetective.variation.tree.VariationNode;
import com.github.gumtreediff.tree.AbstractTree;
import com.github.gumtreediff.tree.Tree;
import com.github.gumtreediff.tree.Type;
import com.github.gumtreediff.tree.TypeSet;
/**
* Adapter for running Gumtree's matching algorithms on variation trees.
*
* This class is an <a href="https://en.wikipedia.org/wiki/Adapter_pattern">adapter</a> for a
* snapshot of a variation tree. This means that it doesn't reflect most changes to the underlying
* variation tree. Essentially it creates a copy of the tree structure and the labels of a variation
* tree by stores a reference to the variation node which it adapts.
*/
public class VariationTreeAdapter<L extends Label> extends AbstractTree {
private String cachedLabel;
private VariationNode<?, L> backingNode;
private LinkedHashMap<String, Object> metadata;
public VariationTreeAdapter(VariationNode<?, L> node) {
this.backingNode = node;
this.metadata = new LinkedHashMap<>();
cachedLabel =
Stream.concat(
backingNode.getLabel().getLines().stream(),
backingNode.getLabel().getTrailingLines().stream()
).collect(Collectors.joining("\n"));
var children = new ArrayList<Tree>(node.getChildren().size());
for (var child : node.getChildren()) {
children.add(newInstance(child));
}
setChildren(children);
}
protected VariationTreeAdapter<L> newInstance(VariationNode<?, L> node) {
return new VariationTreeAdapter<>(node);
}
public VariationNode<?, L> getVariationNode() {
return backingNode;
}
@Override
public String getLabel() {
return cachedLabel;
}
/**
* Uses line numbers instead of byte indexes.
*/
@Override
public int getLength() {
return 1;
}
@Override
public Iterator<Entry<String, Object>> getMetadata() {
return metadata.entrySet().iterator();
}
@Override
public Object getMetadata(String key) {
return metadata.get(key);
}
/**
* Uses line numbers instead of byte indexes.
*/
@Override
public int getPos() {
return backingNode.getLineRange().fromInclusive();
}
@Override
public Type getType() {
return TypeSet.type(backingNode.getNodeType().toString());
}
@Override
public void setLabel(String label) {
throw new UnsupportedOperationException();
}
@Override
public void setLength(int length) {
throw new UnsupportedOperationException();
}
@Override
public Object setMetadata(String key, Object value) {
return metadata.put(key, value);
}
@Override
public void setPos(int pos) {
throw new UnsupportedOperationException();
}
@Override
public void setType(Type type) {
throw new UnsupportedOperationException();
}
@Override
public Tree deepCopy() {
throw new UnsupportedOperationException();
}
}