|
54 | 54 | * {@code Promise.allSettled()}</li> |
55 | 55 | * <li>{@link #delay(long) Awaitable.delay(ms)} — like |
56 | 56 | * {@code Task.Delay()} / {@code setTimeout}</li> |
57 | | - * <li>{@link #timeout(Object, long) Awaitable.timeout(task, ms)} — like |
| 57 | + * <li>{@link #orTimeoutMillis(Object, long) Awaitable.orTimeoutMillis(task, ms)} — like |
58 | 58 | * Kotlin's {@code withTimeout} or a JavaScript promise raced against a timer</li> |
59 | | - * <li>{@link #timeoutOr(Object, Object, long) Awaitable.timeoutOr(task, fallback, ms)} — |
| 59 | + * <li>{@link #completeOnTimeoutMillis(Object, Object, long) |
| 60 | + * Awaitable.completeOnTimeoutMillis(task, fallback, ms)} — |
60 | 61 | * like a timeout with fallback/default value</li> |
61 | 62 | * </ul> |
62 | 63 | * <p> |
63 | | - * <b>Static factories:</b> |
| 64 | + * <b>Static factories and conversion:</b> |
64 | 65 | * <ul> |
| 66 | + * <li>{@link #from(Object) Awaitable.from(source)} — converts any supported |
| 67 | + * async type (CompletableFuture, CompletionStage, Future, Flow.Publisher, etc.) |
| 68 | + * to an {@code Awaitable}</li> |
65 | 69 | * <li>{@link #of(Object) Awaitable.of(value)} — like |
66 | 70 | * {@code Task.FromResult()} / {@code Promise.resolve()}</li> |
67 | 71 | * <li>{@link #failed(Throwable) Awaitable.failed(error)} — like |
@@ -297,6 +301,35 @@ default Awaitable<T> completeOnTimeout(T fallback, long duration, TimeUnit unit) |
297 | 301 |
|
298 | 302 | // ---- Static factories ---- |
299 | 303 |
|
| 304 | + /** |
| 305 | + * Converts the given source to an {@code Awaitable}. |
| 306 | + * <p> |
| 307 | + * If the source is already an {@code Awaitable}, it is returned as-is. |
| 308 | + * Otherwise, the {@link AwaitableAdapterRegistry} is consulted to find a |
| 309 | + * suitable adapter. Built-in adapters handle {@link CompletableFuture}, |
| 310 | + * {@link java.util.concurrent.CompletionStage}, {@link java.util.concurrent.Future}, |
| 311 | + * and {@link java.util.concurrent.Flow.Publisher}; third-party frameworks |
| 312 | + * can register additional adapters via the registry. |
| 313 | + * <p> |
| 314 | + * This is the recommended entry point for converting external async types |
| 315 | + * to {@code Awaitable}: |
| 316 | + * <pre> |
| 317 | + * Awaitable<String> aw = Awaitable.from(someCompletableFuture) |
| 318 | + * Awaitable<Integer> aw2 = Awaitable.from(someReactorMono) |
| 319 | + * </pre> |
| 320 | + * |
| 321 | + * @param source the source object; must not be {@code null} |
| 322 | + * @param <T> the result type |
| 323 | + * @return an awaitable backed by the source |
| 324 | + * @throws IllegalArgumentException if {@code source} is {@code null} |
| 325 | + * or no adapter supports the source type |
| 326 | + * @see AwaitableAdapterRegistry#toAwaitable(Object) |
| 327 | + * @since 6.0.0 |
| 328 | + */ |
| 329 | + static <T> Awaitable<T> from(Object source) { |
| 330 | + return AwaitableAdapterRegistry.toAwaitable(source); |
| 331 | + } |
| 332 | + |
300 | 333 | /** |
301 | 334 | * Returns an already-completed {@code Awaitable} with the given value. |
302 | 335 | * Analogous to C#'s {@code Task.FromResult()} or JavaScript's |
|
0 commit comments