add orDefault methods to promises & more factory overloads

This commit is contained in:
tommyskeff
2025-01-09 09:59:19 +00:00
parent e10db36a63
commit df9e418091
11 changed files with 346 additions and 247 deletions

View File

@@ -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