Skip to content

Commit 7e6f7f5

Browse files
committed
Use Comparator
1 parent 4b9db72 commit 7e6f7f5

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

junit-platform-engine/src/main/java/org/junit/platform/engine/support/hierarchical/WorkerThreadPoolHierarchicalTestExecutorService.java

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
package org.junit.platform.engine.support.hierarchical;
1212

13-
import static java.util.Comparator.naturalOrder;
14-
import static java.util.Comparator.reverseOrder;
13+
import static java.util.Comparator.comparing;
1514
import static java.util.Objects.requireNonNull;
1615
import static java.util.concurrent.CompletableFuture.completedFuture;
1716
import static java.util.concurrent.TimeUnit.SECONDS;
@@ -23,6 +22,7 @@
2322
import java.util.ArrayDeque;
2423
import java.util.ArrayList;
2524
import java.util.Collection;
25+
import java.util.Comparator;
2626
import java.util.Deque;
2727
import java.util.EnumMap;
2828
import java.util.Iterator;
@@ -361,13 +361,13 @@ else if (child.getExecutionMode() == SAME_THREAD) {
361361
if (!queueEntries.isEmpty()) {
362362
if (sameThreadTasks.isEmpty()) {
363363
// hold back one task for this thread
364-
var lastEntry = queueEntries.stream().max(naturalOrder()).orElseThrow();
364+
var lastEntry = queueEntries.stream().max(WorkQueue.Entry.COMPARATOR).orElseThrow();
365365
queueEntries.remove(lastEntry);
366366
sameThreadTasks.add(lastEntry.task);
367367
}
368368
forkAll(queueEntries);
369369
}
370-
queueEntries.sort(reverseOrder());
370+
queueEntries.sort(WorkQueue.Entry.COMPARATOR.reversed());
371371
return queueEntries;
372372
}
373373

@@ -653,7 +653,8 @@ private enum BlockingMode {
653653
}
654654

655655
private static class WorkQueue implements Iterable<WorkQueue.Entry> {
656-
private final Set<Entry> queue = new ConcurrentSkipListSet<>();
656+
657+
private final Set<Entry> queue = new ConcurrentSkipListSet<>(Entry.COMPARATOR);
657658

658659
Entry add(TestTask task, int index) {
659660
Entry entry = new Entry(task, index);
@@ -691,7 +692,15 @@ public Iterator<Entry> iterator() {
691692
return queue.iterator();
692693
}
693694

694-
private static final class Entry implements Comparable<Entry> {
695+
private static final class Entry {
696+
697+
private static final Comparator<Entry> SAME_LENGTH_UNIQUE_ID_COMPARATOR //
698+
= (e1, e2) -> compareBy(e1.uniqueId(), e2.uniqueId());
699+
700+
private static final Comparator<Entry> COMPARATOR = comparing(Entry::level).reversed() //
701+
.thenComparing(Entry::isContainer) // tests before containers
702+
.thenComparing(comparing(Entry::index).reversed()) //
703+
.thenComparing(SAME_LENGTH_UNIQUE_ID_COMPARATOR.reversed());
695704

696705
private final TestTask task;
697706
private final CompletableFuture<@Nullable Void> future;
@@ -712,24 +721,7 @@ private static final class Entry implements Comparable<Entry> {
712721
this.index = index;
713722
}
714723

715-
@Override
716-
public int compareTo(Entry that) {
717-
var result = Integer.compare(that.getLevel(), getLevel());
718-
if (result != 0) {
719-
return result;
720-
}
721-
result = Boolean.compare(this.isContainer(), that.isContainer());
722-
if (result != 0) {
723-
return result;
724-
}
725-
result = Integer.compare(that.index, index);
726-
if (result != 0) {
727-
return result;
728-
}
729-
return compareBy(that.uniqueId(), this.uniqueId());
730-
}
731-
732-
private int compareBy(UniqueId a, UniqueId b) {
724+
private static int compareBy(UniqueId a, UniqueId b) {
733725
var aIterator = a.getSegments().iterator();
734726
var bIterator = b.getSegments().iterator();
735727

@@ -745,15 +737,19 @@ private int compareBy(UniqueId a, UniqueId b) {
745737
return 0;
746738
}
747739

748-
private int compareBy(UniqueId.Segment a, UniqueId.Segment b) {
740+
private static int compareBy(UniqueId.Segment a, UniqueId.Segment b) {
749741
int result = a.getType().compareTo(b.getType());
750742
if (result != 0) {
751743
return result;
752744
}
753745
return a.getValue().compareTo(b.getValue());
754746
}
755747

756-
private int getLevel() {
748+
private int index() {
749+
return this.index;
750+
}
751+
752+
private int level() {
757753
return uniqueId().getSegments().size();
758754
}
759755

0 commit comments

Comments
 (0)