private static int compareBalancedStubs(StubElement<?> stub1, StubElement<?> stub2) {
    if (stub1 == stub2) return 0;
    StubElement<?> parent1 = stub1.getParentStub();
    StubElement<?> parent2 = stub2.getParentStub();
    int parentCmp = compareBalancedStubs(parent1, parent2);
    if (parentCmp != 0) return parentCmp;
    return Integer.compare(parent1.getChildrenStubs().indexOf(stub1), parent2.getChildrenStubs().indexOf(stub2));
  }
private static int compareBalancedStubs(StubElement<?> stub1, StubElement<?> stub2)