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,7 +5,6 @@ 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.Iterator;
|
||||
import java.util.List;
|
||||
@@ -36,9 +35,8 @@ public class CompletionJoiner extends PromiseJoiner<Promise<?>, Void, Void, List
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @Nullable Throwable onChildComplete(int index, Void key, @NotNull PromiseCompletion<Void> res) {
|
||||
protected void onChildComplete(int index, Void key, @NotNull PromiseCompletion<Void> res) {
|
||||
results.set(index, res);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -6,7 +6,6 @@ import dev.tommyjs.futur.promise.PromiseCompletion;
|
||||
import dev.tommyjs.futur.promise.PromiseFactory;
|
||||
import dev.tommyjs.futur.util.PromiseUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
@@ -23,7 +22,7 @@ public abstract class PromiseJoiner<V, K, T, R> {
|
||||
|
||||
protected abstract @NotNull Promise<T> getChildPromise(V value);
|
||||
|
||||
protected abstract @Nullable Throwable onChildComplete(int index, K key, @NotNull PromiseCompletion<T> completion);
|
||||
protected abstract void onChildComplete(int index, K key, @NotNull PromiseCompletion<T> completion);
|
||||
|
||||
protected abstract R getResult();
|
||||
|
||||
@@ -45,9 +44,10 @@ public abstract class PromiseJoiner<V, K, T, R> {
|
||||
int index = i++;
|
||||
|
||||
p.addAsyncListener(res -> {
|
||||
Throwable e = onChildComplete(index, key, res);
|
||||
if (e != null) {
|
||||
joined.completeExceptionally(e);
|
||||
onChildComplete(index, key, res);
|
||||
if (res.isError()) {
|
||||
assert res.getException() != null;
|
||||
joined.completeExceptionally(res.getException());
|
||||
} else if (count.decrementAndGet() == -1) {
|
||||
joined.complete(getResult());
|
||||
}
|
||||
|
||||
@@ -5,25 +5,20 @@ 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.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public class ResultJoiner<T> extends PromiseJoiner<Promise<T>, Void, T, List<T>> {
|
||||
|
||||
private final @Nullable BiConsumer<Integer, Throwable> exceptionHandler;
|
||||
private final ConcurrentResultArray<T> results;
|
||||
|
||||
public ResultJoiner(
|
||||
@NotNull PromiseFactory factory,
|
||||
@NotNull Iterator<Promise<T>> promises,
|
||||
@Nullable BiConsumer<Integer, Throwable> exceptionHandler,
|
||||
int expectedSize, boolean link
|
||||
) {
|
||||
super(factory);
|
||||
this.exceptionHandler = exceptionHandler;
|
||||
this.results = new ConcurrentResultArray<>(expectedSize);
|
||||
join(promises, link);
|
||||
}
|
||||
@@ -39,17 +34,8 @@ public class ResultJoiner<T> extends PromiseJoiner<Promise<T>, Void, T, List<T>>
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @Nullable Throwable onChildComplete(int index, Void key, @NotNull PromiseCompletion<T> res) {
|
||||
if (res.isError()) {
|
||||
if (exceptionHandler == null) {
|
||||
return res.getException();
|
||||
}
|
||||
|
||||
exceptionHandler.accept(index, res.getException());
|
||||
}
|
||||
|
||||
protected void onChildComplete(int index, Void key, @NotNull PromiseCompletion<T> res) {
|
||||
results.set(index, res.getResult());
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -4,7 +4,6 @@ import dev.tommyjs.futur.promise.Promise;
|
||||
import dev.tommyjs.futur.promise.PromiseCompletion;
|
||||
import dev.tommyjs.futur.promise.PromiseFactory;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
@@ -27,8 +26,8 @@ public class VoidJoiner extends PromiseJoiner<Promise<?>, Void, Void, Void> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @Nullable Throwable onChildComplete(int index, Void key, @NotNull PromiseCompletion<Void> completion) {
|
||||
return completion.getException();
|
||||
protected void onChildComplete(int index, Void key, @NotNull PromiseCompletion<Void> completion) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user