Skip to content

Commit 90d19d0

Browse files
refactor: move initialization of NodeType based on AnnotationType to NodeType.java
1 parent e853ff7 commit 90d19d0

2 files changed

Lines changed: 35 additions & 12 deletions

File tree

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

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package org.variantsync.diffdetective.variation;
22

3-
import org.variantsync.diffdetective.variation.diff.DiffNode; // For Javadoc
4-
import org.variantsync.diffdetective.variation.tree.VariationNode; // For Javadoc
3+
import org.variantsync.diffdetective.feature.AnnotationType;
4+
import org.variantsync.diffdetective.variation.diff.DiffNode;
5+
import org.variantsync.diffdetective.variation.tree.VariationNode;
56

67
/**
78
* The type of nodes of a {@link DiffNode} and a {@link VariationNode}.
@@ -39,11 +40,11 @@ public boolean isAnnotation() {
3940

4041
/**
4142
* Creates a NodeType from its value names.
42-
*
43-
* @see Enum#name()
43+
*
4444
* @param name a string that equals the name of one value of this enum (ignoring
4545
* case)
4646
* @return The NodeType that has the given name
47+
* @see Enum#name()
4748
*/
4849
public static NodeType fromName(final String name) {
4950
for (NodeType candidate : values()) {
@@ -55,10 +56,37 @@ public static NodeType fromName(final String name) {
5556
throw new IllegalArgumentException("Given string \"" + name + "\" is not the name of a NodeType.");
5657
}
5758

58-
// TODO: fromAnnotationType constructor with switch case
59+
/**
60+
* Creates a NodeType from an AnnotationType.
61+
* <p>
62+
* All AnnotationType variants except for 'Endif' are supported.
63+
* There is no valid representation for 'Endif' annotations. Thus, the method throws an IllegalArgumentException
64+
* if it is given an 'Endif'.
65+
* </p>
66+
*
67+
* @param annotationType a variant of AnnotationType
68+
* @return The NodeType that fits the given AnnotationType
69+
*/
70+
public static NodeType fromAnnotationType(final AnnotationType annotationType) {
71+
switch (annotationType) {
72+
case If -> {
73+
return NodeType.IF;
74+
}
75+
case Elif -> {
76+
return NodeType.ELIF;
77+
}
78+
case Else -> {
79+
return NodeType.ELSE;
80+
}
81+
case None -> {
82+
return NodeType.ARTIFACT;
83+
}
84+
default -> throw new IllegalArgumentException(annotationType + "has no NodeType counterpart");
85+
}
86+
}
5987

6088
/**
61-
* Returns the number of bits required for storing {@link ordinal}.
89+
* Returns the number of bits required for storing.
6290
*/
6391
public static int getRequiredBitCount() {
6492
return 3;

src/main/java/org/variantsync/diffdetective/variation/diff/parse/VariationDiffParser.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,12 +325,7 @@ private void parseLine(
325325
lastArtifact.setToLine(toLine);
326326
} else {
327327
try {
328-
NodeType nodeType;
329-
if (annotationType == AnnotationType.None) {
330-
nodeType = NodeType.ARTIFACT;
331-
} else {
332-
nodeType = NodeType.fromName(annotationType.name);
333-
}
328+
NodeType nodeType = NodeType.fromAnnotationType(annotationType);
334329

335330
DiffNode<DiffLinesLabel> newNode = new DiffNode<DiffLinesLabel>(
336331
diffType,

0 commit comments

Comments
 (0)