Skip to content

Commit e64418e

Browse files
committed
refactor: move joinNode into DiffNode
This makes it more discoverable and consistent with `DiffNode.split`.
1 parent f9e3f57 commit e64418e

3 files changed

Lines changed: 26 additions & 24 deletions

File tree

src/main/java/org/variantsync/diffdetective/util/Assert.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public static void assertFalse(boolean condition, String errorMessage) {
8686
}
8787

8888
/** Throws {@link AssertionError} with {@code errorMessage} as error message. */
89-
public static void fail(String errorMessage) {
89+
public static <T> T fail(String errorMessage) {
9090
throw new AssertionError(errorMessage);
9191
}
9292

src/main/java/org/variantsync/diffdetective/variation/diff/DiffNode.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,30 @@ public DiffNode<L> split(Time time) {
408408
return other;
409409
}
410410

411+
/**
412+
* Merges {@code other} into this node.
413+
* {@code other} is removed from the graph and this node inherits all of its edges. This
414+
* node and {@code other} need to be compatible (exist at different times and have the
415+
* same {@link getNodeType node type} and {@link getLabel label}).
416+
*
417+
* @param other the node which is removed from the graph
418+
*/
419+
public void join(DiffNode<L> other) {
420+
Time time = switch (diffType) {
421+
case ADD -> AFTER;
422+
case REM -> BEFORE;
423+
case NON -> Assert.fail("Attempt to join a node that already exists at both times.");
424+
};
425+
Assert.assertEquals(other.diffType, DiffType.thatExistsOnlyAt(time.other()));
426+
Assert.assertEquals(getNodeType(), other.getNodeType());
427+
Assert.assertEquals(getLabel(), other.getLabel());
428+
429+
diffType = DiffType.NON;
430+
431+
this.stealChildrenOf(other);
432+
other.getParent(time).replaceChild(other, this, time);
433+
}
434+
411435
/**
412436
* Replaces a child of this node with another node.
413437
* <p>

src/main/java/org/variantsync/diffdetective/variation/diff/construction/GumTreeDiff.java

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.Map;
2323

2424
import static org.variantsync.diffdetective.variation.diff.DiffType.ADD;
25-
import static org.variantsync.diffdetective.variation.diff.DiffType.NON;
2625
import static org.variantsync.diffdetective.variation.diff.DiffType.REM;
2726
import static org.variantsync.diffdetective.variation.diff.Time.AFTER;
2827
import static org.variantsync.diffdetective.variation.diff.Time.BEFORE;
@@ -204,7 +203,7 @@ public static <L extends Label> DiffNode<L> improveMatching(DiffNode<L> tree, Ma
204203
afterNode.split(BEFORE);
205204
}
206205

207-
joinNode(beforeNode, afterNode);
206+
beforeNode.join(afterNode);
208207
}
209208

210209
Assert.assertTrue(beforeNode.isNon());
@@ -215,27 +214,6 @@ public static <L extends Label> DiffNode<L> improveMatching(DiffNode<L> tree, Ma
215214
return tree;
216215
}
217216

218-
/**
219-
* Merges {@code afterNode} into {@code beforeNode} such that {@code beforeNode.isNon() ==
220-
* true}. Essentially, an implicit matching is inserted between {@code beforeNode} and {@code
221-
* afterNode}.
222-
*
223-
* This method doesn't change the {@code BEFORE} and {@code AFTER} projection of {@code
224-
* beforeNode}.
225-
*
226-
* @param beforeNode the node which is will exist {@code BEFORE} and {@code AFTER} the edit
227-
* @param afterNode the node which is discarded
228-
*/
229-
private static <L extends Label> void joinNode(DiffNode<L> beforeNode, DiffNode<L> afterNode) {
230-
Assert.assertTrue(beforeNode.isRem());
231-
Assert.assertTrue(afterNode.isAdd());
232-
233-
beforeNode.diffType = NON;
234-
235-
beforeNode.stealChildrenOf(afterNode);
236-
afterNode.getParent(AFTER).replaceChild(afterNode, beforeNode, AFTER);
237-
}
238-
239217
/**
240218
* Makes the implicit matching of a {@code VariationDiff} explicit.
241219
*

0 commit comments

Comments
 (0)