Skip to content

Commit ac35daf

Browse files
committed
refactor: use a switch for GraphFormat
This version will emit an error if `GraphFormat` is changed and doesn't require an explicit exception for missing cases.
1 parent 531d811 commit ac35daf

1 file changed

Lines changed: 23 additions & 24 deletions

File tree

src/main/java/org/variantsync/diffdetective/variation/diff/serialize/LineGraphImport.java

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -133,34 +133,33 @@ private static VariationDiff<DiffLinesLabel> parseVariationDiff(final String lin
133133
}
134134

135135
// Handle trees and graphs differently
136-
if (options.graphFormat() == GraphFormat.DIFFGRAPH) {
137-
return DiffGraph.fromNodes(diffNodeList, variationDiffSource);
138-
} else if (options.graphFormat() == GraphFormat.VARIATION_DIFF) {
139-
// If you should interpret the input data as VariationDiffs, always expect a root to be present. Parse all nodes (v) to a list of nodes. Search for the root. Assert that there is exactly one root.
140-
DiffNode<DiffLinesLabel> root = null;
141-
for (final DiffNode<DiffLinesLabel> v : diffNodeList) {
142-
if (v.isRoot()) {
143-
// v is root candidate
144-
if (root != null) {
145-
throw new RuntimeException("Not a VariationDiff: Got more than one root! Got \"" + root + "\" and \"" + v + "\"!");
146-
}
147-
if (v.getNodeType() == NodeType.IF) {
148-
root = v;
149-
} else {
150-
throw new RuntimeException("Not a VariationDiff but a DiffGraph: The node \"" + v + "\" is not labeled as IF but has no parents!");
136+
return switch (options.graphFormat()) {
137+
case DIFFGRAPH:
138+
yield DiffGraph.fromNodes(diffNodeList, variationDiffSource);
139+
case VARIATION_DIFF:
140+
// If you should interpret the input data as VariationDiffs, always expect a root to be present. Parse all nodes (v) to a list of nodes. Search for the root. Assert that there is exactly one root.
141+
DiffNode<DiffLinesLabel> root = null;
142+
for (final DiffNode<DiffLinesLabel> v : diffNodeList) {
143+
if (v.isRoot()) {
144+
// v is root candidate
145+
if (root != null) {
146+
throw new RuntimeException("Not a VariationDiff: Got more than one root! Got \"" + root + "\" and \"" + v + "\"!");
147+
}
148+
if (v.getNodeType() == NodeType.IF) {
149+
root = v;
150+
} else {
151+
throw new RuntimeException("Not a VariationDiff but a DiffGraph: The node \"" + v + "\" is not labeled as IF but has no parents!");
152+
}
151153
}
152154
}
153-
}
154155

155-
if (root == null) {
156-
throw new RuntimeException("Not a VariationDiff but a DiffGraph: No root found!");
157-
}
156+
if (root == null) {
157+
throw new RuntimeException("Not a VariationDiff but a DiffGraph: No root found!");
158+
}
158159

159-
// countRootTypes.merge(root.getNodeType(), 1, Integer::sum);
160+
// countRootTypes.merge(root.getNodeType(), 1, Integer::sum);
160161

161-
return new VariationDiff<>(root, variationDiffSource);
162-
} else {
163-
throw new RuntimeException("Unsupported GraphFormat");
164-
}
162+
yield new VariationDiff<>(root, variationDiffSource);
163+
};
165164
}
166165
}

0 commit comments

Comments
 (0)