@Test public void sortByBestScore() throws Exception { Debate d1 = debate(null).withVote(100, 0); Debate d1_1 = debate(d1).withVote(80, 0); Debate d1_1_1 = debate(d1_1).withVote(60, 0); Debate d1_1_2 = debate(d1_1).withVote(120, 0); Debate d1_2 = debate(d1).withVote(150, 0); Debate d2 = debate(null).withVote(20, 0); Debate d2_1 = debate(d2).withVote(250, 0); Debate d2_1_1 = debate(d2_1).withVote(0, 0); Debate d2_2 = debate(d2).withVote(40, 0); Debate d3 = debate(null).withVote(10, 0); Debate d3_1 = debate(d3).withVote(3, 0); 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); ToDoubleFunction<Debate> upVoteCalc = (debate) -> debate == null ? 0 : debate.getUpVote(); DebateTree sorted = debateTree.sortByBestScore(upVoteCalc); List<Debate> bestScore = asList( d2, d2_1, d2_1_1, d2_2, d1, d1_2, d1_1, d1_1_2, d1_1_1, d3, d3_1); assertEquals(bestScore, sorted.depthFirst().collect(toList())); }
public DebateTree sortByBestScore(ToDoubleFunction<Debate> scoreCalc) { SortingNode<Debate> sorted = node.deepSort((child1, child2) -> { double score1 = maxScoreOfAllNodes(child1, scoreCalc); double score2 = maxScoreOfAllNodes(child2, scoreCalc); return Double.compare(score2, score1); }); return new DebateTree(sorted); }
DebateTree { public DebateTree sortByBestScore(ToDoubleFunction<Debate> scoreCalc) { SortingNode<Debate> sorted = node.deepSort((child1, child2) -> { double score1 = maxScoreOfAllNodes(child1, scoreCalc); double score2 = maxScoreOfAllNodes(child2, scoreCalc); return Double.compare(score2, score1); }); return new DebateTree(sorted); } }
DebateTree { public DebateTree sortByBestScore(ToDoubleFunction<Debate> scoreCalc) { SortingNode<Debate> sorted = node.deepSort((child1, child2) -> { double score1 = maxScoreOfAllNodes(child1, scoreCalc); double score2 = maxScoreOfAllNodes(child2, scoreCalc); return Double.compare(score2, score1); }); return new DebateTree(sorted); } DebateTree(SortingNode<Debate> node); }
DebateTree { public DebateTree sortByBestScore(ToDoubleFunction<Debate> scoreCalc) { SortingNode<Debate> sorted = node.deepSort((child1, child2) -> { double score1 = maxScoreOfAllNodes(child1, scoreCalc); double score2 = maxScoreOfAllNodes(child2, scoreCalc); return Double.compare(score2, score1); }); return new DebateTree(sorted); } 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 DebateTree sortByBestScore(ToDoubleFunction<Debate> scoreCalc) { SortingNode<Debate> sorted = node.deepSort((child1, child2) -> { double score1 = maxScoreOfAllNodes(child1, scoreCalc); double score2 = maxScoreOfAllNodes(child2, scoreCalc); return Double.compare(score2, score1); }); return new DebateTree(sorted); } 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; }