Skip to content

Commit a0115c5

Browse files
committed
Minor tweaks
1 parent 9f2c1a0 commit a0115c5

13 files changed

Lines changed: 804 additions & 296 deletions

File tree

src/main/java/groovy/concurrent/Awaitable.java

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -233,20 +233,18 @@ default <U> Awaitable<U> handle(BiFunction<? super T, Throwable, ? extends U> fn
233233

234234
/**
235235
* Returns a new {@code Awaitable} that fails with {@link TimeoutException}
236-
* if this computation does not complete within the specified duration.
236+
* if this computation does not complete within the specified milliseconds.
237237
* <p>
238238
* Unlike {@link #get(long, TimeUnit)}, this is a non-blocking, composable
239239
* timeout combinator: it returns another {@code Awaitable} that can itself
240240
* be awaited, chained, or passed to {@link #all(Object...)} / {@link #any(Object...)}.
241-
* This plays a role similar to Kotlin's {@code withTimeout} while
242-
* preserving Groovy's awaitable abstraction.
243241
*
244-
* @param duration the timeout duration in milliseconds
242+
* @param timeoutMillis the timeout duration in milliseconds
245243
* @return a new awaitable with timeout semantics
246244
* @since 6.0.0
247245
*/
248-
default Awaitable<T> orTimeout(long duration) {
249-
return Awaitable.timeout(this, duration, TimeUnit.MILLISECONDS);
246+
default Awaitable<T> orTimeoutMillis(long timeoutMillis) {
247+
return Awaitable.orTimeout(this, timeoutMillis, TimeUnit.MILLISECONDS);
250248
}
251249

252250
/**
@@ -259,20 +257,20 @@ default Awaitable<T> orTimeout(long duration) {
259257
* @since 6.0.0
260258
*/
261259
default Awaitable<T> orTimeout(long duration, TimeUnit unit) {
262-
return Awaitable.timeout(this, duration, unit);
260+
return Awaitable.orTimeout(this, duration, unit);
263261
}
264262

265263
/**
266264
* Returns a new {@code Awaitable} that completes with the supplied fallback
267265
* value if this computation does not finish before the timeout expires.
268266
*
269267
* @param fallback the value to use when the timeout expires
270-
* @param duration the timeout duration in milliseconds
268+
* @param timeoutMillis the timeout duration in milliseconds
271269
* @return a new awaitable that yields either the original result or the fallback
272270
* @since 6.0.0
273271
*/
274-
default Awaitable<T> completeOnTimeout(T fallback, long duration) {
275-
return Awaitable.timeoutOr(this, fallback, duration, TimeUnit.MILLISECONDS);
272+
default Awaitable<T> completeOnTimeoutMillis(T fallback, long timeoutMillis) {
273+
return Awaitable.completeOnTimeout(this, fallback, timeoutMillis, TimeUnit.MILLISECONDS);
276274
}
277275

278276
/**
@@ -286,7 +284,7 @@ default Awaitable<T> completeOnTimeout(T fallback, long duration) {
286284
* @since 6.0.0
287285
*/
288286
default Awaitable<T> completeOnTimeout(T fallback, long duration, TimeUnit unit) {
289-
return Awaitable.timeoutOr(this, fallback, duration, unit);
287+
return Awaitable.completeOnTimeout(this, fallback, duration, unit);
290288
}
291289

292290
/**
@@ -406,68 +404,74 @@ static Awaitable<Void> delay(long duration, TimeUnit unit) {
406404
return AsyncSupport.delay(duration, unit);
407405
}
408406

407+
// ---- Timeout combinators ----
408+
409409
/**
410410
* Adapts the given source to an {@code Awaitable} and applies a non-blocking
411-
* timeout to it.
411+
* fail-fast timeout. Returns a new awaitable that fails with
412+
* {@link TimeoutException} if the source does not complete before the
413+
* deadline elapses.
412414
* <p>
413415
* The source may be a Groovy {@link Awaitable}, a JDK
414416
* {@link CompletableFuture}/{@link java.util.concurrent.CompletionStage},
415417
* or any type supported by {@link AwaitableAdapterRegistry}. This provides
416418
* a concise timeout combinator analogous to Kotlin's {@code withTimeout},
417419
* but as a value-level operation that returns another awaitable.
418420
*
419-
* @param source the async source to time out
420-
* @param duration the timeout duration in milliseconds
421-
* @return a new awaitable that fails with {@link TimeoutException} on timeout
421+
* @param source the async source to time out
422+
* @param timeoutMillis the timeout duration in milliseconds
423+
* @param <T> the result type
424+
* @return a new awaitable with timeout semantics
422425
* @since 6.0.0
423426
*/
424-
static <T> Awaitable<T> timeout(Object source, long duration) {
425-
return AsyncSupport.timeout(source, duration, TimeUnit.MILLISECONDS);
427+
static <T> Awaitable<T> orTimeoutMillis(Object source, long timeoutMillis) {
428+
return AsyncSupport.orTimeout(source, timeoutMillis, TimeUnit.MILLISECONDS);
426429
}
427430

428431
/**
429-
* Adapts the given source to an {@code Awaitable} and applies a non-blocking
430-
* timeout to it.
432+
* Adapts the given source and applies a non-blocking fail-fast timeout
433+
* with explicit {@link TimeUnit}.
431434
*
432-
* @param source the async source to time out
435+
* @param source the async source to time out
433436
* @param duration the timeout duration
434-
* @param unit the time unit
435-
* @return a new awaitable that fails with {@link TimeoutException} on timeout
437+
* @param unit the time unit
438+
* @param <T> the result type
439+
* @return a new awaitable with timeout semantics
436440
* @since 6.0.0
437441
*/
438-
static <T> Awaitable<T> timeout(Object source, long duration, TimeUnit unit) {
439-
return AsyncSupport.timeout(source, duration, unit);
442+
static <T> Awaitable<T> orTimeout(Object source, long duration, TimeUnit unit) {
443+
return AsyncSupport.orTimeout(source, duration, unit);
440444
}
441445

442446
/**
443-
* Adapts the given source to an {@code Awaitable} and returns a new
444-
* awaitable that yields the supplied fallback value if the timeout expires
445-
* first.
447+
* Adapts the given source and returns a new awaitable that yields the
448+
* supplied fallback value if the timeout expires first.
446449
*
447-
* @param source the async source to wait for
448-
* @param fallback the fallback value to use on timeout
449-
* @param duration the timeout duration in milliseconds
450+
* @param source the async source to wait for
451+
* @param fallback the fallback value to use on timeout
452+
* @param timeoutMillis the timeout duration in milliseconds
453+
* @param <T> the result type
450454
* @return a new awaitable yielding either the original result or the fallback
451455
* @since 6.0.0
452456
*/
453-
static <T> Awaitable<T> timeoutOr(Object source, T fallback, long duration) {
454-
return AsyncSupport.timeoutOr(source, fallback, duration, TimeUnit.MILLISECONDS);
457+
static <T> Awaitable<T> completeOnTimeoutMillis(Object source, T fallback, long timeoutMillis) {
458+
return AsyncSupport.completeOnTimeout(source, fallback, timeoutMillis, TimeUnit.MILLISECONDS);
455459
}
456460

457461
/**
458-
* Adapts the given source to an {@code Awaitable} and returns a new
459-
* awaitable that yields the supplied fallback value if the timeout expires
460-
* first.
462+
* Adapts the given source and returns a new awaitable that yields the
463+
* supplied fallback value if the timeout expires first.
461464
*
462-
* @param source the async source to wait for
465+
* @param source the async source to wait for
463466
* @param fallback the fallback value to use on timeout
464467
* @param duration the timeout duration
465-
* @param unit the time unit
468+
* @param unit the time unit
469+
* @param <T> the result type
466470
* @return a new awaitable yielding either the original result or the fallback
467471
* @since 6.0.0
468472
*/
469-
static <T> Awaitable<T> timeoutOr(Object source, T fallback, long duration, TimeUnit unit) {
470-
return AsyncSupport.timeoutOr(source, fallback, duration, unit);
473+
static <T> Awaitable<T> completeOnTimeout(Object source, T fallback, long duration, TimeUnit unit) {
474+
return AsyncSupport.completeOnTimeout(source, fallback, duration, unit);
471475
}
472476

473477
// ---- Executor configuration ----

0 commit comments

Comments
 (0)