|
| 1 | +package org.variantsync.diffdetective.gumtree; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Iterator; |
| 5 | +import java.util.Map.Entry; |
| 6 | +import java.util.stream.Collectors; |
| 7 | + |
| 8 | +import org.variantsync.diffdetective.variation.tree.VariationNode; |
| 9 | + |
| 10 | +import com.github.gumtreediff.tree.AbstractTree; |
| 11 | +import com.github.gumtreediff.tree.Tree; |
| 12 | +import com.github.gumtreediff.tree.Type; |
| 13 | +import com.github.gumtreediff.tree.TypeSet; |
| 14 | + |
| 15 | +/** |
| 16 | + * Adapter for running Gumtree's matching algorithms on variation trees. |
| 17 | + * |
| 18 | + * This class is an <a href="https://en.wikipedia.org/wiki/Adapter_pattern">adapter</a> for a |
| 19 | + * snapshot of a variation tree. This means that it doesn't reflect most changes to the underlying |
| 20 | + * variation tree. Essentially it creates a copy of the tree structure and the labels of a variation |
| 21 | + * tree by stores a reference to the variation node which it adapts. |
| 22 | + */ |
| 23 | +public class VariationTreeAdapter extends AbstractTree { |
| 24 | + private String cachedLabel; |
| 25 | + private VariationNode<?> backingNode; |
| 26 | + |
| 27 | + public VariationTreeAdapter(VariationNode<?> node) { |
| 28 | + this.backingNode = node; |
| 29 | + |
| 30 | + if (backingNode.isConditionalAnnotation()) { |
| 31 | + cachedLabel = backingNode.getFormula().toString(); |
| 32 | + } else { |
| 33 | + cachedLabel = backingNode.getLabelLines().stream().collect(Collectors.joining("\n")); |
| 34 | + } |
| 35 | + |
| 36 | + var children = new ArrayList<Tree>(node.getChildren().size()); |
| 37 | + for (var child : node.getChildren()) { |
| 38 | + children.add(newInstance(child)); |
| 39 | + } |
| 40 | + setChildren(children); |
| 41 | + } |
| 42 | + |
| 43 | + protected VariationTreeAdapter newInstance(VariationNode<?> node) { |
| 44 | + return new VariationTreeAdapter(node); |
| 45 | + } |
| 46 | + |
| 47 | + public VariationNode<?> getVariationNode() { |
| 48 | + return backingNode; |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + @Override |
| 53 | + public String getLabel() { |
| 54 | + return cachedLabel; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Uses line numbers instead of byte indexes. |
| 59 | + */ |
| 60 | + @Override |
| 61 | + public int getLength() { |
| 62 | + return 1; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public Iterator<Entry<String, Object>> getMetadata() { |
| 67 | + throw new UnsupportedOperationException(); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public Object getMetadata(String arg0) { |
| 72 | + throw new UnsupportedOperationException(); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Uses line numbers instead of byte indexes. |
| 77 | + */ |
| 78 | + @Override |
| 79 | + public int getPos() { |
| 80 | + return backingNode.getLineRange().fromInclusive(); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public Type getType() { |
| 85 | + return TypeSet.type(backingNode.getNodeType().toString()); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void setLabel(String label) { |
| 90 | + throw new UnsupportedOperationException(); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void setLength(int length) { |
| 95 | + throw new UnsupportedOperationException(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public Object setMetadata(String name, Object value) { |
| 100 | + throw new UnsupportedOperationException(); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void setPos(int pos) { |
| 105 | + throw new UnsupportedOperationException(); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public void setType(Type type) { |
| 110 | + throw new UnsupportedOperationException(); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public Tree deepCopy() { |
| 115 | + throw new UnsupportedOperationException(); |
| 116 | + } |
| 117 | +} |
0 commit comments