### Question:
VersionHistory implements Serializable { public TreeMap<Integer, HistoryNode> calculateNodes() { TreeMap<Integer, HistoryNode> result = new TreeMap<>(); HashSet<Integer> parents = new HashSet<>(); int snapshotNr = 0; for (VersionHistoryEntry entry : entries) { if (entry instanceof SnapshotVersionHistoryEntry) { int count = ((SnapshotVersionHistoryEntry) entry).count; for (int i = 0; i < count; i++) { HistoryNode node = new HistoryNode(); node.snapshotNr = snapshotNr++; if (!parents.isEmpty()) { node.parentNrs.addAll(parents); parents.clear(); } result.put(node.snapshotNr, node); parents.add(node.snapshotNr); } } else if (entry instanceof RestoreVersionHistoryEntry) { parents.add(((RestoreVersionHistoryEntry) entry).restoredSnapshotNr); } else { throw new RuntimeException("Should not happen"); } } for (HistoryNode node : result.values()) { for (Integer nr : node.parentNrs) { node.parents.add(result.get(nr)); } } return result; }  TreeMap<Integer, HistoryNode> calculateNodes(); int getVersionCount(); VersionHistoryEntry getLastEntry(); void addRestore(UUID streamId, int restoredSnapshotNr); void addVersion(UUID streamId); boolean isAncestorOf(VersionHistory child); @Override boolean equals(Object obj); @Override String toString();  }### Answer:
@Test public void testCalculateNodes() throws Exception { VersionHistory history = new VersionHistory(); UUID id = UUID.randomUUID(); history.addVersion(id); history.addRestore(id, 0); history.addVersion(id); history.addRestore(id, 0); history.addRestore(id, 1); history.addVersion(id); history.addVersion(id); TreeMap<Integer, HistoryNode> nodes = history.calculateNodes(); assertThat(nodes.entrySet(), hasSize(4)); assertThat(nodes.get(0).parents, is(empty())); assertThat(nodes.get(0).snapshotNr, is(0)); assertThat(nodes.get(1).parents, hasSize(1)); assertThat(nodes.get(1).parents, hasItem(nodes.get(0))); assertThat(nodes.get(2).parents, hasSize(2)); assertThat(nodes.get(2).parents, hasItem(nodes.get(0))); assertThat(nodes.get(2).parents, hasItem(nodes.get(1))); assertThat(nodes.get(3).parents, hasSize(1)); assertThat(nodes.get(3).parents, hasItem(nodes.get(2))); assertThat(nodes.get(3).snapshotNr, is(3)); }