Skip to content

Commit

Permalink
feat: CffuFactory.toCompletableFutureArray support CompletionStage 🔌
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Mar 29, 2023
1 parent 1370eb9 commit 446b32c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/main/java/io/foldright/cffu/Cffu.java
Original file line number Diff line number Diff line change
Expand Up @@ -1658,12 +1658,12 @@ public Cffu<T> resetCffuFactory(CffuFactory cffuFactory) {
* {@code wrappedCf.toCompletableFuture()}; if you need the underneath wrapped CompletableFuture instance,
* call method {@link #cffuUnwrap()}.
* <p>
* {@link CffuFactory#toCompletableFutureArray(Cffu[])} is the batch operation to this method.
* {@link CffuFactory#toCompletableFutureArray(CompletionStage[])} is the batch operation to this method.
*
* @return the CompletableFuture
* @see CompletionStage#toCompletableFuture()
* @see #cffuUnwrap()
* @see CffuFactory#toCompletableFutureArray(Cffu[])
* @see CffuFactory#toCompletableFutureArray(CompletionStage[])
*/
@Contract(pure = true)
@Override
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/io/foldright/cffu/CffuFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -810,23 +810,26 @@ public <T1, T2, T3, T4, T5> Cffu<Tuple5<T1, T2, T3, T4, T5>> cffuCombine(
}

////////////////////////////////////////////////////////////////////////////////
//# Conversion Methods
//# Conversion (Static) Methods
//
// - toCompletableFutureArray: Cffu -> CF
// - cffuArrayUnwrap: Cffu -> CF
//
// - cffuListToArray: List<Cffu> -> Cffu[]
// - completableFutureListToArray: List<CF> -> CF[]
////////////////////////////////////////////////////////////////////////////////

/**
* A convenient util method for unwrap input {@link Cffu} array elements using {@link Cffu#toCompletableFuture()}.
* A convenient util method for converting input {@link Cffu}/{@link CompletionStage} array element
* by {@link Cffu#toCompletableFuture()}/{@link CompletableFuture#toCompletableFuture()}.
*
* @see Cffu#toCompletableFuture()
* @see CompletionStage#toCompletableFuture()
* @see #asCffuArray(CompletionStage[])
*/
@Contract(pure = true)
@SafeVarargs
public static <T> CompletableFuture<T>[] toCompletableFutureArray(Cffu<T>... cfs) {
public static <T> CompletableFuture<T>[] toCompletableFutureArray(CompletionStage<T>... cfs) {
@SuppressWarnings("unchecked")
CompletableFuture<T>[] ret = new CompletableFuture[cfs.length];
for (int i = 0; i < cfs.length; i++) {
Expand Down
66 changes: 55 additions & 11 deletions src/test/java/io/foldright/cffu/CffuFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,15 @@ void test_failedStage() throws Exception {
// - supplyAsync*
////////////////////////////////////////////////////////////////////////////////

// ...

////////////////////////////////////////////////////////////////////////////////
//# Factory Methods
//
// - newIncompleteCffu: equivalent to CompletableFuture constructor
// - asCffu: wrap an existed CompletableFuture/CompletionStage to Cffu
//
// - asCffu: CF/CompletionStage -> Cffu
// - asCffuArray: CF/CompletionStage[] -> Cffu[]
////////////////////////////////////////////////////////////////////////////////

@Test
Expand Down Expand Up @@ -259,6 +263,24 @@ void test_cffuAnyOf_exceptionally() throws Exception {
).get());
}

////////////////////////////////////////////////////////////////////////////////
//# Delay Execution, equivalent to same name static methods of CompletableFuture
//
// - delayedExecutor
////////////////////////////////////////////////////////////////////////////////

// ...

////////////////////////////////////////////////////////////////////////////////
//# New type-safe allOf/anyOf Factory Methods
// method name prefix with `cffu`
//
// - cffuAllOf
// - cffuAnyOf
////////////////////////////////////////////////////////////////////////////////

// ...

////////////////////////////////////////////////////////////////////////////////
//# New type-safe cffuCombine Factory Methods
// support 2~5 input arguments, method name prefix with `cffu`
Expand Down Expand Up @@ -410,29 +432,51 @@ void test_cffuCombine_exceptionally() throws Exception {
}

////////////////////////////////////////////////////////////////////////////////
//# Conversion Methods
//# Conversion (Static) Methods
//
// - asCffu: CF -> Cffu
// - asCffuArray: CF[] -> Cffu[]
// - toCompletableFutureArray: Cffu -> CF
// - toCompletableFutureArray: Cffu -> CF
// - cffuArrayUnwrap: Cffu -> CF
//
// - cffuListToArray: List<Cffu> -> Cffu[]
// - cffuListToArray: List<Cffu> -> Cffu[]
// - completableFutureListToArray: List<CF> -> CF[]
////////////////////////////////////////////////////////////////////////////////

@Test
void test_toCompletableFutureArray() {
@SuppressWarnings("unchecked")
CompletableFuture<Integer>[] cfArray = new CompletableFuture[]{
CompletableFuture.completedFuture(n),
CompletableFuture.completedFuture(another_n)
};
@SuppressWarnings("unchecked")
CompletionStage<Integer>[] csArray = new CompletableFuture[]{
cfArray[0],
cfArray[1],
};
@SuppressWarnings("unchecked")
Cffu<Integer>[] cffuArray = new Cffu[]{
cffuFactory.asCffu(cfArray[0]),
cffuFactory.asCffu(cfArray[1]),
};

assertArrayEquals(cfArray, CffuFactory.toCompletableFutureArray(cfArray));
assertArrayEquals(cfArray, CffuFactory.toCompletableFutureArray(csArray));
assertArrayEquals(cfArray, CffuFactory.toCompletableFutureArray(cffuArray));
}

@Test
void test_cffuArrayUnwrap() {
@SuppressWarnings("unchecked")
CompletableFuture<Integer>[] cfs = new CompletableFuture[]{
CompletableFuture<Integer>[] cfArray = new CompletableFuture[]{
CompletableFuture.completedFuture(n),
CompletableFuture.completedFuture(another_n)
};
@SuppressWarnings("unchecked")
Cffu<Integer>[] input = new Cffu[]{
cffuFactory.asCffu(cfs[0]),
cffuFactory.asCffu(cfs[1]),
cffuFactory.asCffu(cfArray[0]),
cffuFactory.asCffu(cfArray[1]),
};
assertArrayEquals(cfs, CffuFactory.cffuArrayUnwrap(input));
assertArrayEquals(cfArray, CffuFactory.cffuArrayUnwrap(input));
}

@Test
Expand All @@ -459,7 +503,7 @@ void test_completableFutureListToArray() {
}

////////////////////////////////////////////////////////////////////////////////
//# getter/setter
//# Getter methods of CffuFactory properties
////////////////////////////////////////////////////////////////////////////////

@Test
Expand Down

0 comments on commit 446b32c

Please sign in to comment.