@Test public void fromDepthFirst() throws Exception { Debate d1 = debate(null); Debate d1_1 = debate(d1); Debate d1_1_1 = debate(d1_1); Debate d1_1_2 = debate(d1_1); Debate d1_2 = debate(d1); Debate d2 = debate(null); Debate d2_1 = debate(d2); Debate d2_1_1 = debate(d2_1); Debate d2_2 = debate(d2); Debate d3 = debate(null); Debate d3_1 = debate(d3); List<Debate> flatten = asList( d1, d1_1, d1_1_1, d1_1_2, d1_2, d2, d2_1, d2_1_1, d2_2, d3, d3_1); DebateTree debateTree = DebateTree.fromDepthFirst(flatten); assertEquals(flatten, debateTree.depthFirst().collect(toList())); }
public static DebateTree fromDepthFirst(List<Debate> flatten) { if (flatten.isEmpty()) { return new DebateTree(SortingNode.emptyRoot()); } SortingNode.Builder<Debate> builder = new SortingNode.Builder<>(); int currentLevel = flatten.get(0).getLevel() - 1; for (Debate debate : flatten) { if (debate.getLevel() == currentLevel) { builder = builder.siblingNode(debate); } else if (debate.getLevel() == currentLevel + 1) { builder = builder.childNode(debate); currentLevel++; } else if (debate.getLevel() < currentLevel) { while (debate.getLevel() < currentLevel) { builder = builder.parent(); currentLevel--; } builder = builder.siblingNode(debate); } else { throw new IllegalStateException("not well flatten depth first debate list"); } } return new DebateTree(builder.build()); }
DebateTree { public static DebateTree fromDepthFirst(List<Debate> flatten) { if (flatten.isEmpty()) { return new DebateTree(SortingNode.emptyRoot()); } SortingNode.Builder<Debate> builder = new SortingNode.Builder<>(); int currentLevel = flatten.get(0).getLevel() - 1; for (Debate debate : flatten) { if (debate.getLevel() == currentLevel) { builder = builder.siblingNode(debate); } else if (debate.getLevel() == currentLevel + 1) { builder = builder.childNode(debate); currentLevel++; } else if (debate.getLevel() < currentLevel) { while (debate.getLevel() < currentLevel) { builder = builder.parent(); currentLevel--; } builder = builder.siblingNode(debate); } else { throw new IllegalStateException("not well flatten depth first debate list"); } } return new DebateTree(builder.build()); } }
DebateTree { public static DebateTree fromDepthFirst(List<Debate> flatten) { if (flatten.isEmpty()) { return new DebateTree(SortingNode.emptyRoot()); } SortingNode.Builder<Debate> builder = new SortingNode.Builder<>(); int currentLevel = flatten.get(0).getLevel() - 1; for (Debate debate : flatten) { if (debate.getLevel() == currentLevel) { builder = builder.siblingNode(debate); } else if (debate.getLevel() == currentLevel + 1) { builder = builder.childNode(debate); currentLevel++; } else if (debate.getLevel() < currentLevel) { while (debate.getLevel() < currentLevel) { builder = builder.parent(); currentLevel--; } builder = builder.siblingNode(debate); } else { throw new IllegalStateException("not well flatten depth first debate list"); } } return new DebateTree(builder.build()); } DebateTree(SortingNode<Debate> node); }
DebateTree { public static DebateTree fromDepthFirst(List<Debate> flatten) { if (flatten.isEmpty()) { return new DebateTree(SortingNode.emptyRoot()); } SortingNode.Builder<Debate> builder = new SortingNode.Builder<>(); int currentLevel = flatten.get(0).getLevel() - 1; for (Debate debate : flatten) { if (debate.getLevel() == currentLevel) { builder = builder.siblingNode(debate); } else if (debate.getLevel() == currentLevel + 1) { builder = builder.childNode(debate); currentLevel++; } else if (debate.getLevel() < currentLevel) { while (debate.getLevel() < currentLevel) { builder = builder.parent(); currentLevel--; } builder = builder.siblingNode(debate); } else { throw new IllegalStateException("not well flatten depth first debate list"); } } return new DebateTree(builder.build()); } DebateTree(SortingNode<Debate> node); static DebateTree fromDepthFirst(List<Debate> flatten); List<SortingNode<Debate>> getChildren(); boolean isEmpty(); DebateTree sortByBestScore(ToDoubleFunction<Debate> scoreCalc); DebateTree sortByBestScore(); Stream<Debate> depthFirst(); V1DebateNodeDto toV1Dto(); }
DebateTree { public static DebateTree fromDepthFirst(List<Debate> flatten) { if (flatten.isEmpty()) { return new DebateTree(SortingNode.emptyRoot()); } SortingNode.Builder<Debate> builder = new SortingNode.Builder<>(); int currentLevel = flatten.get(0).getLevel() - 1; for (Debate debate : flatten) { if (debate.getLevel() == currentLevel) { builder = builder.siblingNode(debate); } else if (debate.getLevel() == currentLevel + 1) { builder = builder.childNode(debate); currentLevel++; } else if (debate.getLevel() < currentLevel) { while (debate.getLevel() < currentLevel) { builder = builder.parent(); currentLevel--; } builder = builder.siblingNode(debate); } else { throw new IllegalStateException("not well flatten depth first debate list"); } } return new DebateTree(builder.build()); } DebateTree(SortingNode<Debate> node); static DebateTree fromDepthFirst(List<Debate> flatten); List<SortingNode<Debate>> getChildren(); boolean isEmpty(); DebateTree sortByBestScore(ToDoubleFunction<Debate> scoreCalc); DebateTree sortByBestScore(); Stream<Debate> depthFirst(); V1DebateNodeDto toV1Dto(); static final ToDoubleFunction<Debate> DEBATE_TO_WILSON_SCORE; }