mirror of
https://github.com/tommyskeff/futur4j.git
synced 2026-01-18 07:16:45 +00:00
add orDefault methods to promises & more factory overloads
This commit is contained in:
@@ -5,24 +5,19 @@ import dev.tommyjs.futur.promise.PromiseCompletion;
|
||||
import dev.tommyjs.futur.promise.PromiseFactory;
|
||||
import dev.tommyjs.futur.util.ConcurrentResultArray;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public class MappedResultJoiner<K, V> extends PromiseJoiner<Map.Entry<K, Promise<V>>, K, V, Map<K, V>> {
|
||||
|
||||
private final @Nullable BiConsumer<K, Throwable> exceptionHandler;
|
||||
private final @NotNull ConcurrentResultArray<Map.Entry<K, V>> results;
|
||||
|
||||
public MappedResultJoiner(
|
||||
@NotNull PromiseFactory factory,
|
||||
@NotNull Iterator<Map.Entry<K, Promise<V>>> promises,
|
||||
@Nullable BiConsumer<K, Throwable> exceptionHandler,
|
||||
int expectedSize, boolean link
|
||||
) {
|
||||
super(factory);
|
||||
this.exceptionHandler = exceptionHandler;
|
||||
this.results = new ConcurrentResultArray<>(expectedSize);
|
||||
join(promises, link);
|
||||
}
|
||||
@@ -38,17 +33,8 @@ public class MappedResultJoiner<K, V> extends PromiseJoiner<Map.Entry<K, Promise
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @Nullable Throwable onChildComplete(int index, K key, @NotNull PromiseCompletion<V> res) {
|
||||
if (res.isError()) {
|
||||
if (exceptionHandler == null) {
|
||||
return res.getException();
|
||||
}
|
||||
|
||||
exceptionHandler.accept(key, res.getException());
|
||||
}
|
||||
|
||||
protected void onChildComplete(int index, K key, @NotNull PromiseCompletion<V> res) {
|
||||
results.set(index, new AbstractMap.SimpleImmutableEntry<>(key, res.getResult()));
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user