Skip to content

Commit bf6f5fb

Browse files
committed
fix: preserve the projection when splitting a DiffNode
1 parent de10f75 commit bf6f5fb

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,12 @@ public DiffNode<L> split(Time time) {
373373
other.addChildren(this.removeChildren(time), time);
374374
getParent(time).replaceChild(this, other, time);
375375

376-
// FIXME? If we allowed access to the backing node,
377-
// we could preserve the identity of this projection by moving it to `other`.
378-
this.projections[time.ordinal()] = null;
376+
// Preserve the projection by changing its `backingNode` to `other`.
377+
if (this.projections[time.ordinal()] != null) {
378+
other.projections[time.ordinal()] = this.projections[time.ordinal()];
379+
this.projections[time.ordinal()] = null;
380+
other.projections[time.ordinal()].backingNode = other;
381+
}
379382

380383
return other;
381384
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,16 @@
2121
* @see DiffNode#projection
2222
*/
2323
public class Projection<L extends Label> extends VariationNode<Projection<L>, L> {
24-
private DiffNode<L> backingNode;
24+
/**
25+
* The {@link DiffNode} which is projected at {@link time}.
26+
* Invariant: {@code backingNode.projection(time) == this}
27+
* <p>
28+
* Note that this field is package private because {@link DiffNode} needs to change it,
29+
* for example in {@link DiffNode#split},
30+
* to preserve the identity of this instance and prevent it from becoming invalid.
31+
* Only {@link DiffNode} is intended to have access to this field.
32+
*/
33+
DiffNode<L> backingNode;
2534
private Time time;
2635

2736
/**

0 commit comments

Comments
 (0)