Skip to content

Commit

Permalink
Simplify control flow
Browse files Browse the repository at this point in the history
  • Loading branch information
marcphilipp committed Sep 13, 2024
1 parent 20fc333 commit 68b3845
Showing 1 changed file with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

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

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static java.util.Comparator.comparing;
import static java.util.Comparator.naturalOrder;
import static java.util.stream.Collectors.groupingBy;
Expand Down Expand Up @@ -46,14 +48,7 @@ private static Comparator<String> globalKeyFirst() {
private final Map<String, ReadWriteLock> locksByKey = new ConcurrentHashMap<>();

ResourceLock getLockForResources(Collection<ExclusiveResource> resources) {
if (resources.isEmpty()) {
return NopLock.INSTANCE;
}
if (resources.size() == 1) {
return getLockForResource(getOnlyElement(resources));
}
List<Lock> locks = getDistinctSortedLocks(resources);
return new CompositeLock(locks);
return toResourceLock(getDistinctSortedLocks(resources));
}

ResourceLock getLockForResource(ExclusiveResource resource) {
Expand All @@ -68,6 +63,12 @@ else if (GLOBAL_READ_WRITE.equals(resource)) {
}

private List<Lock> getDistinctSortedLocks(Collection<ExclusiveResource> resources) {
if (resources.isEmpty()) {
return emptyList();
}
if (resources.size() == 1) {
return singletonList(toLock(getOnlyElement(resources)));
}
// @formatter:off
Map<String, List<ExclusiveResource>> resourcesByKey = resources.stream()
.sorted(COMPARATOR)
Expand All @@ -86,4 +87,14 @@ private Lock toLock(ExclusiveResource resource) {
return resource.getLockMode() == READ ? lock.readLock() : lock.writeLock();
}

private ResourceLock toResourceLock(List<Lock> locks) {
switch (locks.size()) {
case 0:
return NopLock.INSTANCE;
case 1:
return new SingleLock(locks.get(0));
default:
return new CompositeLock(locks);
}
}
}

0 comments on commit 68b3845

Please sign in to comment.