|
1 | 1 | package org.variantsync.diffdetective.variation; |
2 | 2 |
|
3 | 3 | import java.io.IOException; |
4 | | -import java.util.List; |
5 | | -import java.util.Stack; |
6 | | -import java.util.function.Function; |
7 | 4 | import java.util.stream.Collectors; |
8 | 5 |
|
9 | 6 | import org.eclipse.jgit.diff.DiffAlgorithm.SupportedAlgorithm; |
|
12 | 9 | import org.variantsync.diffdetective.variation.diff.VariationDiff; |
13 | 10 | import org.variantsync.diffdetective.variation.diff.construction.JGitDiff; |
14 | 11 | import org.variantsync.diffdetective.variation.tree.VariationTree; |
15 | | -import org.variantsync.diffdetective.variation.tree.VariationTreeNode; |
16 | 12 |
|
17 | 13 | public class VariationUnparser { |
18 | 14 | /** |
19 | 15 | * Unparse {@link VariationTree}s into a {@link String}. |
20 | 16 | * |
21 | 17 | * @param tree that is unparsed |
22 | | - * @param linesToLabel a function that converts lists of lines into labels |
23 | 18 | * @return the unparsed variation tree |
24 | 19 | * @param <L> the type of labels of the tree |
25 | 20 | */ |
26 | | - public static <L extends Label> String unparseTree(VariationTree<L> tree, Function<List<String>, L> linesToLabel) { |
27 | | - if (!tree.root().getChildren().isEmpty()) { |
28 | | - StringBuilder result = new StringBuilder(); |
29 | | - Stack<VariationTreeNode<L>> stack = new Stack<>(); |
30 | | - for (int i = tree.root().getChildren().size() - 1; i >= 0; i--) { |
31 | | - stack.push(tree.root().getChildren().get(i)); |
32 | | - } |
33 | | - while (!stack.empty()) { |
34 | | - VariationTreeNode<L> node = stack.pop(); |
35 | | - if (node.isIf()) { |
36 | | - stack.push(new VariationTreeNode<>(NodeType.ARTIFACT, null, null, |
37 | | - linesToLabel.apply(node.getEndIf()))); |
38 | | - } |
39 | | - for (String line : node.getLabel().getLines()) { |
40 | | - result.append(line); |
41 | | - result.append("\n"); |
42 | | - } |
43 | | - for (int i = node.getChildren().size() - 1; i >= 0; i--) { |
44 | | - stack.push(node.getChildren().get(i)); |
45 | | - } |
46 | | - } |
47 | | - return result.substring(0, result.length() - 1); |
48 | | - } else { |
49 | | - return ""; |
50 | | - } |
51 | | - } |
52 | | - |
53 | | - /** |
54 | | - * Unparse {@link VariationTree}s into a {@link String}. |
55 | | - * |
56 | | - * @param tree that is unparsed |
57 | | - * @param linesToLabel a function that converts lists of lines into labels |
58 | | - * @return the unparsed variation tree |
59 | | - */ |
60 | | - public static String unparseTree(VariationTree<DiffLinesLabel> tree) { |
61 | | - return unparseTree(tree, DiffLinesLabel::withInvalidLineNumbers); |
| 21 | + public static <L extends Label> String unparseTree(VariationTree<L> tree) { |
| 22 | + return tree.unparse(); |
62 | 23 | } |
63 | 24 |
|
64 | 25 | /** |
65 | 26 | * Unparse {@link VariationDiff}s into a {@link String}. |
66 | 27 | * |
67 | 28 | * @param diff that is unparsed |
68 | | - * @param linesToLabel a function that converts lists of lines into labels |
69 | 29 | * @return the unparsed variation diff |
70 | 30 | * @param <L> the type of labels of the tree |
71 | 31 | * @throws IOException |
72 | 32 | */ |
73 | | - public static <L extends Label> String unparseDiff(VariationDiff<L> diff, Function<List<String>, L> linesToLabel) throws IOException { |
74 | | - String tree1 = unparseTree(diff.project(Time.BEFORE), linesToLabel); |
75 | | - String tree2 = unparseTree(diff.project(Time.AFTER), linesToLabel); |
| 33 | + public static <L extends Label> String unparseDiff(VariationDiff<L> diff) throws IOException { |
| 34 | + String tree1 = unparseTree(diff.project(Time.BEFORE)); |
| 35 | + String tree2 = unparseTree(diff.project(Time.AFTER)); |
76 | 36 | return JGitDiff.textDiff(tree1, tree2, SupportedAlgorithm.MYERS); |
77 | 37 | } |
78 | 38 |
|
79 | | - /** |
80 | | - * Unparse {@link VariationDiff}s into a {@link String}. |
81 | | - * |
82 | | - * @param diff that is unparsed |
83 | | - * @return the unparsed variation diff |
84 | | - * @throws IOException |
85 | | - */ |
86 | | - public static String unparseDiff(VariationDiff<DiffLinesLabel> diff) throws IOException { |
87 | | - return unparseDiff(diff, DiffLinesLabel::withInvalidLineNumbers); |
88 | | - } |
89 | | - |
90 | 39 | /** |
91 | 40 | * Extract the state of the diffed text before or after {@code diff}. |
92 | 41 | * |
|
0 commit comments