57 lines
1.6 KiB
Java
57 lines
1.6 KiB
Java
package io.gitlab.jfronny.respackopts.util.concurrent;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import java.util.*;
|
|
import java.util.concurrent.*;
|
|
|
|
public record JoinedFuture<T>(Iterable<Future<T>> futures) implements Future<Collection<T>> {
|
|
public static JoinedFuture<?> of(Iterable<? extends Future<?>> futures) {
|
|
// map is needed to satisfy javac
|
|
return new JoinedFuture<>(new FutureIterable(futures));
|
|
}
|
|
|
|
@Override
|
|
public boolean cancel(boolean mayInterruptIfRunning) {
|
|
boolean cancelled = false;
|
|
for (Future<T> future : futures) {
|
|
cancelled |= future.cancel(mayInterruptIfRunning);
|
|
}
|
|
return cancelled;
|
|
}
|
|
|
|
@Override
|
|
public boolean isCancelled() {
|
|
for (Future<T> future : futures) {
|
|
if (future.isCancelled()) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean isDone() {
|
|
for (Future<T> future : futures) {
|
|
if (!future.isDone()) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public Collection<T> get() throws InterruptedException, ExecutionException {
|
|
List<T> res = new LinkedList<>();
|
|
for (Future<T> future : futures) {
|
|
res.add(future.get());
|
|
}
|
|
return res;
|
|
}
|
|
|
|
@Override
|
|
public Collection<T> get(long timeout, @NotNull TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
|
|
List<T> res = new LinkedList<>();
|
|
for (Future<T> future : futures) {
|
|
res.add(future.get(timeout, unit));
|
|
}
|
|
return res;
|
|
}
|
|
}
|