Skip to content

Commit

Permalink
feat: add type safe anyOf methods to class Cffu
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Feb 13, 2023
1 parent 4f02a2a commit d9df93f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/main/java/io/foldright/cffu/Cffu.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,15 @@ public static <T1, T2, T3> CompletableFuture<Triple<T1, T2, T3>> resultOf(
Triple.of((T1) ret[0], (T2) ret[1], (T3) ret[2])
);
}

@SuppressWarnings("unchecked")
public static <T> CompletableFuture<T> anyOf(CompletableFuture<T>... cfs) {
return (CompletableFuture<T>) CompletableFuture.anyOf(cfs);
}

public static <T> CompletableFuture<T> anyOf(List<? extends CompletableFuture<T>> cfs) {
@SuppressWarnings("unchecked")
CompletableFuture<T>[] arr = cfs.toArray(new CompletableFuture[0]);
return anyOf(arr);
}
}
9 changes: 9 additions & 0 deletions src/test/java/io/foldright/cffu/CffuTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,13 @@ class CffuTest : AnnotationSpec() {
CompletableFuture.completedFuture(d),
).get() shouldBe Triple.of(n, s, d)
}

@Test
fun test_anyOf() {
val gen: (Int) -> CompletableFuture<Int> = { CompletableFuture.supplyAsync { Thread.sleep(10); it } }
val f = CompletableFuture.completedFuture(42)

Cffu.anyOf(gen(44), gen(43), f).get() shouldBe 42
Cffu.anyOf(listOf(gen(44), f, gen(43))).get() shouldBe 42
}
}

0 comments on commit d9df93f

Please sign in to comment.