From 61741931450cfd47f08168a5d43eb3b6a1dd7681 Mon Sep 17 00:00:00 2001 From: WhatCats Date: Mon, 6 Jan 2025 23:08:59 +0100 Subject: [PATCH] add generator for futur-lazy --- .gitignore | 1 + build.gradle | 2 +- .../tommyjs/futur/joiner/PromiseJoiner.java | 12 +- .../futur/promise/AbstractPromise.java | 21 +- .../futur/promise/AsyncPromiseListener.java | 1 + .../futur/util/ConcurrentResultArray.java | 3 +- futur-lazy/generator/bun.lockb | Bin 0 -> 3142 bytes futur-lazy/generator/index.ts | 33 + futur-lazy/generator/package.json | 11 + futur-lazy/generator/tsconfig.json | 27 + .../java/dev/tommyjs/futur/lazy/Promises.java | 672 +++++++++++++----- 11 files changed, 595 insertions(+), 188 deletions(-) create mode 100644 futur-lazy/generator/bun.lockb create mode 100644 futur-lazy/generator/index.ts create mode 100644 futur-lazy/generator/package.json create mode 100644 futur-lazy/generator/tsconfig.json diff --git a/.gitignore b/.gitignore index f35ca9e..918bf19 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .gradle build/ +node_modules/ !gradle/wrapper/gradle-wrapper.jar !**/src/main/**/build/ !**/src/test/**/build/ diff --git a/build.gradle b/build.gradle index bf38bbf..180556c 100644 --- a/build.gradle +++ b/build.gradle @@ -32,7 +32,7 @@ subprojects { dependencies { compileOnly 'org.jetbrains:annotations:24.1.0' implementation 'org.slf4j:slf4j-api:2.0.12' -2 + testRuntimeOnly 'org.junit.platform:junit-platform-launcher' testImplementation 'io.projectreactor:reactor-core:3.6.4' testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1' diff --git a/futur-api/src/main/java/dev/tommyjs/futur/joiner/PromiseJoiner.java b/futur-api/src/main/java/dev/tommyjs/futur/joiner/PromiseJoiner.java index 39997c4..e0bff07 100644 --- a/futur-api/src/main/java/dev/tommyjs/futur/joiner/PromiseJoiner.java +++ b/futur-api/src/main/java/dev/tommyjs/futur/joiner/PromiseJoiner.java @@ -9,7 +9,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.Iterator; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; public abstract class PromiseJoiner { @@ -29,7 +28,6 @@ public abstract class PromiseJoiner { protected abstract R getResult(); protected void join(@NotNull Iterator promises, boolean link) { - AtomicBoolean waiting = new AtomicBoolean(); AtomicInteger count = new AtomicInteger(); int i = 0; @@ -50,19 +48,15 @@ public abstract class PromiseJoiner { Throwable e = onChildComplete(index, key, res); if (e != null) { joined.completeExceptionally(e); - } else if (count.decrementAndGet() == 0 && waiting.get()) { + } else if (count.decrementAndGet() == -1) { joined.complete(getResult()); } }); } } while (promises.hasNext()); - if (!joined.isCompleted()) { - count.updateAndGet(v -> { - if (v == 0) joined.complete(getResult()); - else waiting.set(true); - return v; - }); + if (count.decrementAndGet() == -1) { + joined.complete(getResult()); } } diff --git a/futur-api/src/main/java/dev/tommyjs/futur/promise/AbstractPromise.java b/futur-api/src/main/java/dev/tommyjs/futur/promise/AbstractPromise.java index fe55aa1..845069f 100644 --- a/futur-api/src/main/java/dev/tommyjs/futur/promise/AbstractPromise.java +++ b/futur-api/src/main/java/dev/tommyjs/futur/promise/AbstractPromise.java @@ -48,8 +48,9 @@ public abstract class AbstractPromise implements CompletablePromise completer ) { return () -> { - if (promise.isCompleted()) return; - runCompleter(promise, () -> promise.complete(completer.apply(result))); + if (!promise.isCompleted()) { + runCompleter(promise, () -> promise.complete(completer.apply(result))); + } }; } @@ -479,14 +480,14 @@ public abstract class AbstractPromise implements CompletablePromise listener = iter.next(); - if (listener instanceof AsyncPromiseListener) { - callListenerAsync(listener, ctx); - } else { - try { + try { + if (listener instanceof AsyncPromiseListener) { + callListenerAsync(listener, ctx); + } else { callListenerNow(listener, ctx); - } finally { - iter.forEachRemaining(v -> callListenerAsyncLastResort(v, ctx)); } + } finally { + iter.forEachRemaining(v -> callListenerAsyncLastResort(v, ctx)); } } } @@ -531,16 +532,18 @@ public abstract class AbstractPromise implements CompletablePromise toFuture() { CompletableFuture future = new CompletableFuture<>(); - this.addDirectListener(future::complete, future::completeExceptionally); + addDirectListener(future::complete, future::completeExceptionally); future.whenComplete((_, e) -> { if (e instanceof CancellationException) { this.cancel(); } }); + return future; } private static class DeferredExecutionException extends ExecutionException { + } } diff --git a/futur-api/src/main/java/dev/tommyjs/futur/promise/AsyncPromiseListener.java b/futur-api/src/main/java/dev/tommyjs/futur/promise/AsyncPromiseListener.java index 799b6be..c53f86d 100644 --- a/futur-api/src/main/java/dev/tommyjs/futur/promise/AsyncPromiseListener.java +++ b/futur-api/src/main/java/dev/tommyjs/futur/promise/AsyncPromiseListener.java @@ -5,4 +5,5 @@ package dev.tommyjs.futur.promise; * executed asynchronously by the {@link PromiseFactory} that created the completed promise. */ public interface AsyncPromiseListener extends PromiseListener { + } diff --git a/futur-api/src/main/java/dev/tommyjs/futur/util/ConcurrentResultArray.java b/futur-api/src/main/java/dev/tommyjs/futur/util/ConcurrentResultArray.java index ec678a8..ec202f1 100644 --- a/futur-api/src/main/java/dev/tommyjs/futur/util/ConcurrentResultArray.java +++ b/futur-api/src/main/java/dev/tommyjs/futur/util/ConcurrentResultArray.java @@ -8,7 +8,6 @@ import java.util.concurrent.atomic.AtomicReference; public class ConcurrentResultArray { - private static final float RESIZE_THRESHOLD = 0.75F; private static final float RESIZE_FACTOR = 1.2F; private final AtomicReference ref; @@ -20,7 +19,7 @@ public class ConcurrentResultArray { public void set(int index, T element) { ref.updateAndGet(array -> { - if (array.length * RESIZE_THRESHOLD <= index) { + if (array.length <= index) { array = Arrays.copyOf(array, (int) (array.length * RESIZE_FACTOR)); } diff --git a/futur-lazy/generator/bun.lockb b/futur-lazy/generator/bun.lockb new file mode 100644 index 0000000000000000000000000000000000000000..265b64a90c9e5d89166c3205afd891de89686f77 GIT binary patch literal 3142 zcmd5;4^R|k6yL)`^hgAwKO-_6k~E38_ZN=1^GKs{5>pX|RKPrry<_EW@7P^Ht`eBB z29YK-5kiacPl_5UrDZgX7Jn+y3C(eW3Zj3eMwsbD(+It9*$=kHkhB?lGvB=ZcHi%} z?|bj_G2j>CLn{E*cm(6Kz}O_g zBfjwn#=i~5aexm74e|Nc@BbYz^8oJ)dO9qm$0PXtad2n@;8BJVcFK6bV1w~qz?;SQ zqhi4K$3uf`(L!;bAYLOM(0(E;BuJyZw9sx3H#l_=5&f3}d@^E~S4u74S-HG4sorF- zJ=1yK_k8Ns+5QvDm+|`d_HUS;qw77;+Hu65W-D9Xb2Zo2mQgg2a3W_L^Lk5t<&VZ^ zc6{;hn=KMvX*}ir2dU$GOAfK0&a$rP^4FYfc=cD>w!J8#GWmLAcJhonfiP!KvB$zE;zz_}=UJ`~hQKL--F%D;J-BNZqu)dPSjcy1Mka zw>F3W)_XkhKKmu_ZV9inuH`$M<4aa2o9q0ZZvAfY#S32fnVIdam&zlHwp$`Q!b}0C z&MsHJD|AcFKueT2L>}Z(Ri#|+PgruzsR;BmRa8iLah)Nz|M}s$H?D>R(dnU8%0+u_%zts? zI^exqg5!?$?f7vQ-M<}o?-u5;_%CV^wZhD?Hj-vI-s*IcxZ{#3EJazAI*pQ}y_DNN zM`=)6owS2-+o3-SGB`9WXoKqTibyy~Y_|$?o?d4G+)Ws8uF(GhucyL&9rx)#*{Wz#Luv>DPX@Tt zPx5hEd5Yr+0?!J#_xs4Sq?Xhh2?9?GxcmFa*2R+1q*g=Jfj?-jz;gmT(P>Dnj*P{V z2A&+?Ni?a8Aq_Xd%off?5T)V?KOQ-$G^HHn%BNWJiM2e%GFE3Y&(e&;qSTHCA$BAP zJZBR)Py7-52D_yq7!73T-qh}0=AT1z8+neSSYXTeOj)wbo;bijBGFv5J~sN4Y;C~c zfat+NaRP%HfOu>FDcppEVkj2ksPeFG7tKLD3q2HPV`&fnm~g3vF)*9wJzShx%~B4U zi9M~&1fWU4};*BVIP;YB-vVhs3*jffWP1rEItaftKwv#2rX zBvwF(j8(%~9rh@X$)auaEuE5@0gZI45=tQ&xpr$e3OGO)a4MnX^g>94Yz}8|4E$&g TfM9e3FE^4p#3Pp7|4#i0z6479 literal 0 HcmV?d00001 diff --git a/futur-lazy/generator/index.ts b/futur-lazy/generator/index.ts new file mode 100644 index 0000000..ea74d6d --- /dev/null +++ b/futur-lazy/generator/index.ts @@ -0,0 +1,33 @@ +const PACKAGE = "src/main/java/dev/tommyjs/futur" +const SIGNAL = "// Generated delegates to static factory" + +const regex = /( {4}\/\*\*.+?)?((?:\S| )+? )(\S+)(\(.*?\))(?: {.+?}|;)/gs +const content = await Bun.file(`../../futur-api/${PACKAGE}/promise/PromiseFactory.java`).text() + +const methods = [""] +for (const match of content.matchAll(regex)) { + let [_, docs, head, name, params] = match + + head = head.trimStart() + if (head.startsWith("static")) continue + if (head.startsWith("default")) head = head.slice(8); + + const args = Array.from(params.matchAll(/ ([a-zA-Z1-9]+)[,)]/gs)) + .map(v => v[1]).join(", ") + + methods.push( + [ + `${docs} public static ${head}${name}${params} {`, + ` return factory.${name}(${args});`, + " }" + ].join("\n") + ) +} + +const output = Bun.file(`../${PACKAGE}/lazy/Promises.java`) + +const existing = await output.text() +const cutIndex = existing.indexOf(SIGNAL) + SIGNAL.length; +const newContent = existing.slice(0, cutIndex) + methods.join("\n\n") + "\n\n}" + +await Bun.write(output, newContent) \ No newline at end of file diff --git a/futur-lazy/generator/package.json b/futur-lazy/generator/package.json new file mode 100644 index 0000000..56136ad --- /dev/null +++ b/futur-lazy/generator/package.json @@ -0,0 +1,11 @@ +{ + "name": "generate-promises", + "module": "index.ts", + "type": "module", + "devDependencies": { + "@types/bun": "latest" + }, + "peerDependencies": { + "typescript": "latest" + } +} \ No newline at end of file diff --git a/futur-lazy/generator/tsconfig.json b/futur-lazy/generator/tsconfig.json new file mode 100644 index 0000000..238655f --- /dev/null +++ b/futur-lazy/generator/tsconfig.json @@ -0,0 +1,27 @@ +{ + "compilerOptions": { + // Enable latest features + "lib": ["ESNext", "DOM"], + "target": "ESNext", + "module": "ESNext", + "moduleDetection": "force", + "jsx": "react-jsx", + "allowJs": true, + + // Bundler mode + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "verbatimModuleSyntax": true, + "noEmit": true, + + // Best practices + "strict": true, + "skipLibCheck": true, + "noFallthroughCasesInSwitch": true, + + // Some stricter flags (disabled by default) + "noUnusedLocals": false, + "noUnusedParameters": false, + "noPropertyAccessFromIndexSignature": false + } +} diff --git a/futur-lazy/src/main/java/dev/tommyjs/futur/lazy/Promises.java b/futur-lazy/src/main/java/dev/tommyjs/futur/lazy/Promises.java index 5c64399..263b836 100644 --- a/futur-lazy/src/main/java/dev/tommyjs/futur/lazy/Promises.java +++ b/futur-lazy/src/main/java/dev/tommyjs/futur/lazy/Promises.java @@ -27,182 +27,520 @@ public final class Promises { Promises.factory = factory; } + // Generated delegates to static factory + + /** + * Creates a new uncompleted promise. + * + * @return the new promise + */ public static @NotNull CompletablePromise unresolved() { return factory.unresolved(); } - public static @NotNull Promise> combine(@NotNull Promise p1, @NotNull Promise p2, boolean cancelOnError) { - return factory.combine(p1, p2, cancelOnError); - } - - public static @NotNull Promise> combine(@NotNull Promise p1, @NotNull Promise p2) { - return factory.combine(p1, p2); - } - - public static @NotNull Promise> combine( - @NotNull Map> promises, - @Nullable BiConsumer exceptionHandler, - boolean propagateCancel - ) { - return factory.combine(promises, exceptionHandler, propagateCancel); - } - - public static @NotNull Promise> combine(@NotNull Map> promises, @NotNull BiConsumer exceptionHandler) { - return factory.combine(promises, exceptionHandler); - } - - public static @NotNull Promise> combine(@NotNull Map> promises, boolean cancelOnError) { - return factory.combine(promises, cancelOnError); - } - - public static @NotNull Promise> combine(@NotNull Map> promises) { - return factory.combine(promises); - } - - public static @NotNull Promise> combine( - @NotNull Iterator> promises, int expectedSize, - @Nullable BiConsumer exceptionHandler, boolean propagateCancel - ) { - return factory.combine(promises, expectedSize, exceptionHandler, propagateCancel); - } - - public static @NotNull Promise> combine( - @NotNull Collection> promises, - @NotNull BiConsumer exceptionHandler, - boolean propagateCancel - ) { - return factory.combine(promises, exceptionHandler, propagateCancel); - } - - public static @NotNull Promise> combine( - @NotNull Collection> promises, - @NotNull BiConsumer exceptionHandler - ) { - return factory.combine(promises, exceptionHandler); - } - - public static @NotNull Promise> combine(@NotNull Collection> promises, boolean cancelOnError) { - return factory.combine(promises, cancelOnError); - } - - public static @NotNull Promise> combine(@NotNull Collection> promises) { - return factory.combine(promises); - } - - public static @NotNull Promise> combine( - @NotNull Stream> promises, - @NotNull BiConsumer exceptionHandler, - boolean propagateCancel - ) { - return factory.combine(promises, exceptionHandler, propagateCancel); - } - - public static @NotNull Promise> combine( - @NotNull Stream> promises, - @NotNull BiConsumer exceptionHandler - ) { - return factory.combine(promises, exceptionHandler); - } - - public static @NotNull Promise> combine(@NotNull Stream> promises, boolean cancelOnError) { - return factory.combine(promises, cancelOnError); - } - - public static @NotNull Promise> combine(@NotNull Stream> promises) { - return factory.combine(promises); - } - - public static @NotNull Promise>> allSettled( - @NotNull Iterator> promises, int estimatedSize, boolean propagateCancel) { - return factory.allSettled(promises, estimatedSize, propagateCancel); - } - - public static @NotNull Promise>> allSettled(@NotNull Collection> promises, boolean propagateCancel) { - return factory.allSettled(promises, propagateCancel); - } - - public static @NotNull Promise>> allSettled(@NotNull Collection> promises) { - return factory.allSettled(promises); - } - - public static @NotNull Promise>> allSettled(@NotNull Stream> promises, boolean propagateCancel) { - return factory.allSettled(promises, propagateCancel); - } - - public static @NotNull Promise>> allSettled(@NotNull Stream> promises) { - return factory.allSettled(promises); - } - - public static @NotNull Promise>> allSettled(boolean propagateCancel, @NotNull Promise... promises) { - return factory.allSettled(propagateCancel, promises); - } - - public static @NotNull Promise>> allSettled(@NotNull Promise... promises) { - return factory.allSettled(promises); - } - - public static @NotNull Promise all(@NotNull Iterator> promises, boolean cancelAllOnError) { - return factory.all(promises, cancelAllOnError); - } - - public static @NotNull Promise all(@NotNull Iterable> promises, boolean cancelAllOnError) { - return factory.all(promises, cancelAllOnError); - } - - public static @NotNull Promise all(@NotNull Iterable> promises) { - return factory.all(promises); - } - - public static @NotNull Promise all(@NotNull Stream> promises, boolean cancelAllOnError) { - return factory.all(promises, cancelAllOnError); - } - - public static @NotNull Promise all(@NotNull Stream> promises) { - return factory.all(promises); - } - - public static @NotNull Promise all(boolean cancelAllOnError, @NotNull Promise... promises) { - return factory.all(cancelAllOnError, promises); - } - - public static @NotNull Promise all(@NotNull Promise... promises) { - return factory.all(promises); - } - - public static @NotNull Promise race(@NotNull Iterator> promises, boolean cancelLosers) { - return factory.race(promises, cancelLosers); - } - - public static @NotNull Promise race(@NotNull Iterable> promises, boolean cancelLosers) { - return factory.race(promises, cancelLosers); - } - - public static @NotNull Promise race(@NotNull Iterable> promises) { - return factory.race(promises); - } - - public static @NotNull Promise race(@NotNull Stream> promises, boolean cancelLosers) { - return factory.race(promises, cancelLosers); - } - - public static @NotNull Promise race(@NotNull Stream> promises) { - return factory.race(promises); - } - - public static @NotNull Promise wrap(@NotNull CompletableFuture future) { - return factory.wrap(future); - } - - public static @NotNull Promise start() { - return factory.start(); - } - + /** + * Creates a new promise, completed with the given value. + * + * @param value the value to complete the promise with + * @return the new promise + */ public static @NotNull Promise resolve(T value) { return factory.resolve(value); } + /** + * Creates a new promise, completed with {@code null}. + * + * @return the new promise + * @apiNote This method is often useful for starting promise chains. + */ + public static @NotNull Promise start() { + return factory.start(); + } + + /** + * Creates a new promise, completed exceptionally with the given error. + * + * @param error the error to complete the promise with + * @return the new promise + */ public static @NotNull Promise error(@NotNull Throwable error) { return factory.error(error); } -} + /** + * Creates a new promise backed by the given future. The promise will be completed upon completion + * of the future. + * + * @param future the future to wrap + * @return the new promise + */ + public static @NotNull Promise wrap(@NotNull CompletableFuture future) { + return factory.wrap(future); + } + + /** + * Combines two promises into a single promise that completes when both promises complete. If + * {@code link} is {@code true} and either input promise completes exceptionally (including + * cancellation), the other promise will be cancelled and the output promise will complete + * exceptionally. + * + * @param p1 the first promise + * @param p2 the second promise + * @param link whether to cancel the other promise on error + * @return the combined promise + */ + public static @NotNull Promise> combine(@NotNull Promise p1, @NotNull Promise p2, + boolean link) { + return factory.combine(p1, p2, link); + } + + /** + * Combines two promises into a single promise that completes when both promises complete. If either + * input promise completes exceptionally, the other promise will be cancelled and the output promise + * will complete exceptionally. + * + * @param p1 the first promise + * @param p2 the second promise + * @return the combined promise + */ + public static @NotNull Promise> combine(@NotNull Promise p1, @NotNull Promise p2) { + return factory.combine(p1, p2); + } + + /** + * Combines key-value pairs of inputs to promises into a single promise that completes with key-value + * pairs of inputs to outputs when all promises complete. If {@code link} is {@code true} + * and any promise completes exceptionally, the other promises will be cancelled and the output + * promise will complete exceptionally. If an exception handler is present, promises that fail + * will not cause this behaviour, and instead the exception handler will be called with the key + * that failed and the exception. + * + * @param promises the input promises + * @param exceptionHandler the exception handler + * @param link whether to cancel all promises on any exceptional completions + * @return the combined promise + */ + public static @NotNull Promise> combine(@NotNull Map> promises, + @Nullable BiConsumer exceptionHandler, + boolean link) { + return factory.combine(promises, exceptionHandler, link); + } + + /** + * Combines key-value pairs of inputs to promises into a single promise that completes with key-value + * pairs of inputs to outputs when all promises complete. If any promise completes exceptionally, + * the exception handler will be called with the key that failed and the exception. The output promise + * will always complete successfully regardless of whether input promises fail. + * + * @param promises the input promises + * @param exceptionHandler the exception handler + * @return the combined promise + */ + public static @NotNull Promise> combine(@NotNull Map> promises, + @NotNull BiConsumer exceptionHandler) { + return factory.combine(promises, exceptionHandler); + } + + /** + * Combines key-value pairs of inputs to promises into a single promise that completes with key-value + * pairs of inputs to outputs when all promises complete. If {@code link} is {@code true} + * and any promise completes exceptionally, the other promises will be cancelled and the output + * promise will complete exceptionally. + * + * @param promises the input promises + * @param link whether to cancel all promises on any exceptional completions + * @return the combined promise + */ + public static @NotNull Promise> combine(@NotNull Map> promises, boolean link) { + return factory.combine(promises, link); + } + + /** + * Combines key-value pairs of inputs to promises into a single promise that completes with key-value + * pairs of inputs to outputs when all promises complete. If any promise completes exceptionally, + * the output promise will complete exceptionally. + * + * @param promises the input promises + * @return the combined promise + */ + public static @NotNull Promise> combine(@NotNull Map> promises) { + return factory.combine(promises); + } + + /** + * Combines an iterator of promises into a single promise that completes with a list of results when all + * promises complete. If {@code link} is {@code true} and any promise completes exceptionally, all + * other promises will be cancelled and the output promise will complete exceptionally. If an exception + * handler is present, promises that fail will not cause this behaviour, and instead the exception + * handler will be called with the index that failed and the exception. + * + * @param promises the input promises + * @param exceptionHandler the exception handler + * @param link whether to cancel all promises on any exceptional completions + * @return the combined promise + */ + public static @NotNull Promise> combine(@NotNull Iterator> promises, int expectedSize, + @Nullable BiConsumer exceptionHandler, + boolean link) { + return factory.combine(promises, expectedSize, exceptionHandler, link); + } + + /** + * Combines a collection of promises into a single promise that completes with a list of results when all + * promises complete. If any promise completes exceptionally, the exception handler will be called with + * the index that failed and the exception. The output promise will always complete successfully regardless + * of whether input promises fail. + * + * @param promises the input promises + * @param exceptionHandler the exception handler + * @return the combined promise + */ + public static @NotNull Promise> combine(@NotNull Collection> promises, + @NotNull BiConsumer exceptionHandler, + boolean link) { + return factory.combine(promises, exceptionHandler, link); + } + + /** + * Combines a collection of promises into a single promise that completes with a list of results when all + * promises complete. If any promise completes exceptionally, the exception handler will be called with + * the index that failed and the exception. The output promise will always complete successfully regardless + * of whether input promises fail. + * + * @param promises the input promises + * @param exceptionHandler the exception handler + * @return the combined promise + */ + public static @NotNull Promise> combine(@NotNull Collection> promises, + @NotNull BiConsumer exceptionHandler) { + return factory.combine(promises, exceptionHandler); + } + + /** + * Combines a collection of promises into a single promise that completes with a list of results when all + * promises complete. If {@code link} is {@code true} and any promise completes exceptionally, all + * other promises will be cancelled and the output promise will complete exceptionally. + * + * @param promises the input promises + * @param link whether to cancel all promises on any exceptional completions + * @return the combined promise + */ + public static @NotNull Promise> combine(@NotNull Collection> promises, boolean link) { + return factory.combine(promises, link); + } + + /** + * Combines a collection of promises into a single promise that completes with a list of results when all + * promises complete. If any promise completes exceptionally, the output promise will complete exceptionally. + * + * @param promises the input promises + * @return the combined promise + */ + public static @NotNull Promise> combine(@NotNull Collection> promises) { + return factory.combine(promises); + } + + /** + * Combines a stream of promises into a single promise that completes with a list of results when all + * promises complete. If {@code link} is {@code true} and any promise completes exceptionally, all + * other promises will be cancelled and the output promise will complete exceptionally. If an exception + * handler is present, promises that fail will not cause this behaviour, and instead the exception + * handler will be called with the index that failed and the exception. + * + * @param promises the input promises + * @param exceptionHandler the exception handler + * @param link whether to cancel all promises on any exceptional completions + * @return the combined promise + */ + public static @NotNull Promise> combine(@NotNull Stream> promises, + @NotNull BiConsumer exceptionHandler, + boolean link) { + return factory.combine(promises, exceptionHandler, link); + } + + /** + * Combines a stream of promises into a single promise that completes with a list of results when all + * promises complete. If any promise completes exceptionally, the exception handler will be called with + * the index that failed and the exception. The output promise will always complete successfully regardless + * of whether input promises fail. + * + * @param promises the input promises + * @param exceptionHandler the exception handler + * @return the combined promise + */ + public static @NotNull Promise> combine(@NotNull Stream> promises, + @NotNull BiConsumer exceptionHandler) { + return factory.combine(promises, exceptionHandler); + } + + /** + * Combines a stream of promises into a single promise that completes with a list of results when all + * promises complete. If {@code link} is {@code true} and any promise completes exceptionally, all + * other promises will be cancelled and the output promise will complete exceptionally. + * + * @param promises the input promises + * @param link whether to cancel all promises on any exceptional completions + * @return the combined promise + */ + public static @NotNull Promise> combine(@NotNull Stream> promises, boolean link) { + return factory.combine(promises, link); + } + + /** + * Combines a stream of promises into a single promise that completes with a list of results when all + * promises complete. If any promise completes exceptionally, the output promise will complete exceptionally. + * + * @param promises the input promises + * @return the combined promise + */ + public static @NotNull Promise> combine(@NotNull Stream> promises) { + return factory.combine(promises); + } + + /** + * Combines an iterator of promises into a single promise that completes with a list of results when all + * promises complete. If {@code link} is {@code true} and any promise completes exceptionally, all + * other promises will be cancelled. The output promise will always complete successfully regardless + * of whether input promises fail. + * + * @param promises the input promises + * @param expectedSize the expected size of the list (used for optimization) + * @param link whether to cancel all promises on any exceptional completions + * @return the combined promise + */ + public static @NotNull Promise>> allSettled(@NotNull Iterator> promises, + int expectedSize, boolean link) { + return factory.allSettled(promises, expectedSize, link); + } + + /** + * Combines a collection of promises into a single promise that completes with a list of results when all + * promises complete. If {@code link} is {@code true} and any promise completes exceptionally, all + * other promises will be cancelled. The output promise will always complete successfully regardless + * of whether input promises fail. + * + * @param promises the input promises + * @param link whether to cancel all promises on any exceptional completions + * @return the combined promise + */ + public static @NotNull Promise>> allSettled(@NotNull Collection> promises, + boolean link) { + return factory.allSettled(promises, link); + } + + /** + * Combines a collection of promises into a single promise that completes with a list of results when all + * promises complete. If any promise completes exceptionally, all other promises will be cancelled. + * + * @param promises the input promises + * @return the combined promise + */ + public static @NotNull Promise>> allSettled(@NotNull Collection> promises) { + return factory.allSettled(promises); + } + + /** + * Combines a stream of promises into a single promise that completes with a list of results when all + * promises complete. If {@code link} is {@code true} and any promise completes exceptionally, all + * other promises will be cancelled. The output promise will always complete successfully regardless + * of whether input promises fail. + * + * @param promises the input promises + * @param link whether to cancel all promises on any exceptional completions + * @return the combined promise + */ + public static @NotNull Promise>> allSettled(@NotNull Stream> promises, + boolean link) { + return factory.allSettled(promises, link); + } + + /** + * Combines a stream of promises into a single promise that completes with a list of results when all + * promises complete. If any promise completes exceptionally, all other promises will be cancelled. + * + * @param promises the input promises + * @return the combined promise + */ + public static @NotNull Promise>> allSettled(@NotNull Stream> promises) { + return factory.allSettled(promises); + } + + /** + * Combines an array of promises into a single promise that completes with a list of results when all + * promises complete. If {@code link} is {@code true} and any promise completes exceptionally, all + * other promises will be cancelled. The output promise will always complete successfully regardless + * of whether input promises fail. + * + * @param link whether to cancel all promises on any exceptional completions + * @param promises the input promises + * @return the combined promise + */ + public static @NotNull Promise>> allSettled(boolean link, + @NotNull Promise... promises) { + return factory.allSettled(link, promises); + } + + /** + * Combines an array of promises into a single promise that completes with a list of results when all + * promises complete. If any promise completes exceptionally, all other promises will be cancelled. + * + * @param promises the input promises + * @return the combined promise + */ + public static @NotNull Promise>> allSettled(@NotNull Promise... promises) { + return factory.allSettled(promises); + } + + /** + * Combines an iterator of promises into a single promise that completes when all promises complete. + * If {@code link} is {@code true} and any promise completes exceptionally, all other promises will + * be cancelled and the output promise will complete exceptionally. + * + * @param promises the input promises + * @param link whether to cancel all promises on any exceptional completions + * @return the combined promise + */ + public static @NotNull Promise all(@NotNull Iterator> promises, boolean link) { + return factory.all(promises, link); + } + + /** + * Combines an iterable of promises into a single promise that completes when all promises complete. + * If {@code link} is {@code true} and any promise completes exceptionally, all other promises will + * be cancelled and the output promise will complete exceptionally. + * + * @param promises the input promises + * @param link whether to cancel all promises on any exceptional completions + * @return the combined promise + */ + public static @NotNull Promise all(@NotNull Iterable> promises, boolean link) { + return factory.all(promises, link); + } + + /** + * Combines an iterable of promises into a single promise that completes when all promises complete. + * If any promise completes exceptionally, all other promises will be cancelled and the output + * promise will complete exceptionally. + * + * @param promises the input promises + * @return the combined promise + */ + public static @NotNull Promise all(@NotNull Iterable> promises) { + return factory.all(promises); + } + + /** + * Combines a stream of promises into a single promise that completes when all promises complete. + * If {@code link} is {@code true} and any promise completes exceptionally, all other promises will + * be cancelled and the output promise will complete exceptionally. + * + * @param promises the input promises + * @param link whether to cancel all promises on any exceptional completions + * @return the combined promise + */ + public static @NotNull Promise all(@NotNull Stream> promises, boolean link) { + return factory.all(promises, link); + } + + /** + * Combines a stream of promises into a single promise that completes when all promises complete. + * If any promise completes exceptionally, all other promises will be cancelled and the output + * promise willcomplete exceptionally. + * + * @param promises the input promises + * @return the combined promise + */ + public static @NotNull Promise all(@NotNull Stream> promises) { + return factory.all(promises); + } + + /** + * Combines an array of promises into a single promise that completes when all promises complete. + * If {@code link} is {@code true} and any promise completes exceptionally, all other promises will + * be cancelled + * and the output promise will complete exceptionally. + * + * @param link whether to cancel all promises on any exceptional completions + * @param promises the input promises + * @return the combined promise + */ + public static @NotNull Promise all(boolean link, @NotNull Promise... promises) { + return factory.all(link, promises); + } + + /** + * Combines an array of promises into a single promise that completes when all promises complete. + * If any promise completes exceptionally, all other promises will be cancelled and the output + * promise will complete exceptionally. + * + * @param promises the input promises + * @return the combined promise + */ + public static @NotNull Promise all(@NotNull Promise... promises) { + return factory.all(promises); + } + + /** + * Combines an iterator of promises into a single promise that completes when the first promise + * completes (successfully or exceptionally). If {@code cancelLosers} is {@code true}, all other + * promises will be cancelled when the first promise + * completes. + * + * @param promises the input promises + * @param cancelLosers whether to cancel the other promises when the first completes + * @return the combined promise + */ + public static @NotNull Promise race(@NotNull Iterator> promises, boolean cancelLosers) { + return factory.race(promises, cancelLosers); + } + + /** + * Combines an iterable of promises into a single promise that completes when the first promise + * completes (successfully or exceptionally). All other promises will be cancelled when the first + * promise completes. + * + * @param promises the input promises + * @return the combined promise + */ + public static @NotNull Promise race(@NotNull Iterable> promises, boolean cancelLosers) { + return factory.race(promises, cancelLosers); + } + + /** + * Combines an iterable of promises into a single promise that completes when the first promise + * completes (successfully or exceptionally). All other promises will be cancelled when the first + * promise completes. + * + * @param promises the input promises + */ + public static @NotNull Promise race(@NotNull Iterable> promises) { + return factory.race(promises); + } + + /** + * Combines a stream of promises into a single promise that completes when the first promise + * completes (successfully or exceptionally). If {@code cancelLosers} is {@code true}, all other + * promises will be cancelled when the first promise completes. + * + * @param promises the input promises + * @param cancelLosers whether to cancel the other promises when the first completes + * @return the combined promise + */ + public static @NotNull Promise race(@NotNull Stream> promises, boolean cancelLosers) { + return factory.race(promises, cancelLosers); + } + + /** + * Combines a stream of promises into a single promise that completes when the first promise + * completes (successfully or exceptionally). All other promises will be cancelled when the first + * promise completes. + * + * @param promises the input promises + * @return the combined promise + */ + public static @NotNull Promise race(@NotNull Stream> promises) { + return factory.race(promises); + } + +} \ No newline at end of file