Skip to content

Commit

Permalink
refactor: move static methods toCompletableFutureArray/`completable…
Browse files Browse the repository at this point in the history
…FutureListToArray` from class `CffuFactory` to `CompletableFutureUtils`
  • Loading branch information
oldratlee committed Apr 28, 2024
1 parent 9f87bf8 commit a399348
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 78 deletions.
4 changes: 2 additions & 2 deletions cffu-core/src/main/java/io/foldright/cffu/Cffu.java
Original file line number Diff line number Diff line change
Expand Up @@ -1882,12 +1882,12 @@ public Cffu<T> resetCffuFactory(CffuFactory cffuFactory) {
* {@code underlyingCf.toCompletableFuture()}; if you need the underlying CompletableFuture instance,
* call method {@link #cffuUnwrap()}.
* <p>
* {@link CffuFactory#toCompletableFutureArray(CompletionStage[])} is the batch operation to this method.
* {@link CompletableFutureUtils#toCompletableFutureArray(CompletionStage[])} is the batch operation to this method.
*
* @return the CompletableFuture
* @see CompletionStage#toCompletableFuture()
* @see #cffuUnwrap()
* @see CffuFactory#toCompletableFutureArray(CompletionStage[])
* @see CompletableFutureUtils#toCompletableFutureArray(CompletionStage[])
*/
@Contract(pure = true)
@Override
Expand Down
36 changes: 2 additions & 34 deletions cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

import static io.foldright.cffu.CompletableFutureUtils.toCompletableFutureArray;
import static java.util.Objects.requireNonNull;


Expand All @@ -35,7 +36,7 @@
* <li>factory methods return {@link Cffu} instead of {@link CompletableFuture}.
* <li>only provide varargs methods for multiply Cffu/CF input arguments;
* if you have {@code List} input, use static util methods {@link #cffuListToArray(List)}
* or {@link #completableFutureListToArray(List)} to convert it to array first.
* or {@link CompletableFutureUtils#completableFutureListToArray(List)} to convert it to array first.
* </ul>
*
* @author Jerry Lee (oldratlee at gmail dot com)
Expand Down Expand Up @@ -621,34 +622,10 @@ public Executor delayedExecutor(long delay, TimeUnit unit, Executor executor) {
////////////////////////////////////////////////////////////////////////////////
//# Conversion (Static) Methods
//
// - toCompletableFutureArray: Cffu -> CF
// - cffuArrayUnwrap: Cffu -> CF
//
// - cffuListToArray: List<Cffu> -> Cffu[]
// - completableFutureListToArray: List<CF> -> CF[]
////////////////////////////////////////////////////////////////////////////////

/**
* A convenient util method for converting input {@link Cffu} / {@link CompletableFuture} / {@link CompletionStage}
* array element by {@link Cffu#toCompletableFuture()} / {@link CompletableFuture#toCompletableFuture()} /
* {@link CompletionStage#toCompletableFuture()}.
*
* @see Cffu#toCompletableFuture()
* @see CompletableFuture#toCompletableFuture()
* @see CompletionStage#toCompletableFuture()
* @see #toCffuArray(CompletionStage[])
*/
@Contract(pure = true)
@SafeVarargs
public static <T> CompletableFuture<T>[] toCompletableFutureArray(CompletionStage<T>... stages) {
@SuppressWarnings("unchecked")
CompletableFuture<T>[] ret = new CompletableFuture[stages.length];
for (int i = 0; i < stages.length; i++) {
ret[i] = requireNonNull(stages[i], "stage" + (i + 1) + " is null").toCompletableFuture();
}
return ret;
}

/**
* A convenient util method for unwrap input {@link Cffu} array elements by {@link Cffu#cffuUnwrap()}.
*
Expand All @@ -675,15 +652,6 @@ public static <T> Cffu<T>[] cffuListToArray(List<Cffu<T>> cffuList) {
return cffuList.toArray(new Cffu[0]);
}

/**
* Convert CompletableFuture list to CompletableFuture array.
*/
@Contract(pure = true)
@SuppressWarnings("unchecked")
public static <T> CompletableFuture<T>[] completableFutureListToArray(List<CompletableFuture<T>> cfList) {
return cfList.toArray(new CompletableFuture[0]);
}

////////////////////////////////////////////////////////////////////////////////
//# Getter methods of CffuFactory properties
////////////////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.concurrent.*;
import java.util.function.*;

import static io.foldright.cffu.CffuFactory.toCompletableFutureArray;
import static java.util.Objects.requireNonNull;
import static java.util.function.Function.identity;

Expand Down Expand Up @@ -60,8 +59,7 @@ public static <T> CompletableFuture<List<T>> allResultsOf(CompletionStage<? exte
final Object[] result = new Object[size];
final CompletableFuture<Void>[] resultSetterCfs = createResultSetterCfs(cfs, result);

return CompletableFuture.allOf(resultSetterCfs)
.thenApply(unused -> (List<T>) arrayList(result));
return CompletableFuture.allOf(resultSetterCfs).thenApply(unused -> (List<T>) arrayList(result));
}

/**
Expand Down Expand Up @@ -218,7 +216,7 @@ private static void fill(CompletionStage[] cfs,
@SafeVarargs
@SuppressWarnings({"unchecked", "rawtypes"})
public static <T> CompletableFuture<T> anyOf(CompletionStage<? extends T>... cfs) {
return (CompletableFuture<T>) CompletableFuture.anyOf(toCompletableFutureArray((CompletionStage[]) cfs));
return (CompletableFuture) CompletableFuture.anyOf(toCompletableFutureArray((CompletionStage[]) cfs));
}

/**
Expand Down Expand Up @@ -282,9 +280,7 @@ public static <T1, T2> CompletableFuture<Tuple2<T1, T2>> allTupleOf(
final Object[] result = new Object[css.length];
final CompletableFuture<Void>[] resultSetterCfs = createResultSetterCfs(css, result);

return CompletableFuture.allOf(resultSetterCfs).thenApply(unused ->
Tuple2.of((T1) result[0], (T2) result[1])
);
return CompletableFuture.allOf(resultSetterCfs).thenApply(unused -> Tuple2.of((T1) result[0], (T2) result[1]));
}

/**
Expand All @@ -310,9 +306,7 @@ public static <T1, T2> CompletableFuture<Tuple2<T1, T2>> allTupleOfFastFail(
final Object[] result = new Object[css.length];
final CompletableFuture<Void>[] resultSetterCfs = createResultSetterCfs(css, result);

return allOfFastFail(resultSetterCfs).thenApply(unused ->
Tuple2.of((T1) result[0], (T2) result[1])
);
return allOfFastFail(resultSetterCfs).thenApply(unused -> Tuple2.of((T1) result[0], (T2) result[1]));
}

/**
Expand All @@ -334,9 +328,8 @@ public static <T1, T2, T3> CompletableFuture<Tuple3<T1, T2, T3>> allTupleOf(
final Object[] result = new Object[css.length];
final CompletableFuture<Void>[] resultSetterCfs = createResultSetterCfs(css, result);

return CompletableFuture.allOf(resultSetterCfs).thenApply(unused ->
Tuple3.of((T1) result[0], (T2) result[1], (T3) result[2])
);
return CompletableFuture.allOf(resultSetterCfs)
.thenApply(unused -> Tuple3.of((T1) result[0], (T2) result[1], (T3) result[2]));
}

/**
Expand All @@ -362,9 +355,8 @@ public static <T1, T2, T3> CompletableFuture<Tuple3<T1, T2, T3>> allTupleOfFastF
final Object[] result = new Object[css.length];
final CompletableFuture<Void>[] resultSetterCfs = createResultSetterCfs(css, result);

return allOfFastFail(resultSetterCfs).thenApply(unused ->
Tuple3.of((T1) result[0], (T2) result[1], (T3) result[2])
);
return allOfFastFail(resultSetterCfs)
.thenApply(unused -> Tuple3.of((T1) result[0], (T2) result[1], (T3) result[2]));
}

/**
Expand All @@ -387,9 +379,8 @@ public static <T1, T2, T3, T4> CompletableFuture<Tuple4<T1, T2, T3, T4>> allTupl
final Object[] result = new Object[css.length];
final CompletableFuture<Void>[] resultSetterCfs = createResultSetterCfs(css, result);

return CompletableFuture.allOf(resultSetterCfs).thenApply(unused ->
Tuple4.of((T1) result[0], (T2) result[1], (T3) result[2], (T4) result[3])
);
return CompletableFuture.allOf(resultSetterCfs)
.thenApply(unused -> Tuple4.of((T1) result[0], (T2) result[1], (T3) result[2], (T4) result[3]));
}

/**
Expand All @@ -416,9 +407,8 @@ public static <T1, T2, T3, T4> CompletableFuture<Tuple4<T1, T2, T3, T4>> allTupl
final Object[] result = new Object[css.length];
final CompletableFuture<Void>[] resultSetterCfs = createResultSetterCfs(css, result);

return allOfFastFail(resultSetterCfs).thenApply(unused ->
Tuple4.of((T1) result[0], (T2) result[1], (T3) result[2], (T4) result[3])
);
return allOfFastFail(resultSetterCfs)
.thenApply(unused -> Tuple4.of((T1) result[0], (T2) result[1], (T3) result[2], (T4) result[3]));
}

/**
Expand All @@ -442,8 +432,7 @@ public static <T1, T2, T3, T4, T5> CompletableFuture<Tuple5<T1, T2, T3, T4, T5>>
final CompletableFuture<Void>[] resultSetterCfs = createResultSetterCfs(css, result);

return CompletableFuture.allOf(resultSetterCfs).thenApply(unused ->
Tuple5.of((T1) result[0], (T2) result[1], (T3) result[2], (T4) result[3], (T5) result[4])
);
Tuple5.of((T1) result[0], (T2) result[1], (T3) result[2], (T4) result[3], (T5) result[4]));
}

/**
Expand Down Expand Up @@ -471,8 +460,7 @@ public static <T1, T2, T3, T4, T5> CompletableFuture<Tuple5<T1, T2, T3, T4, T5>>
final CompletableFuture<Void>[] resultSetterCfs = createResultSetterCfs(css, result);

return allOfFastFail(resultSetterCfs).thenApply(unused ->
Tuple5.of((T1) result[0], (T2) result[1], (T3) result[2], (T4) result[3], (T5) result[4])
);
Tuple5.of((T1) result[0], (T2) result[1], (T3) result[2], (T4) result[3], (T5) result[4]));
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -573,8 +561,7 @@ public static <T, U> CompletableFuture<Void> thenAcceptBothFastFail(
final Object[] result = new Object[css.length];
final CompletableFuture<Void>[] resultSetterCfs = createResultSetterCfs(css, result);

return allOfFastFail(resultSetterCfs)
.thenAccept(unused -> action.accept((T) result[0], (U) result[1]));
return allOfFastFail(resultSetterCfs).thenAccept(unused -> action.accept((T) result[0], (U) result[1]));
}

/**
Expand Down Expand Up @@ -602,8 +589,7 @@ public static <T, U> CompletableFuture<Void> thenAcceptBothFastFailAsync(
final Object[] result = new Object[css.length];
final CompletableFuture<Void>[] resultSetterCfs = createResultSetterCfs(css, result);

return allOfFastFail(resultSetterCfs)
.thenAcceptAsync(unused -> action.accept((T) result[0], (U) result[1]));
return allOfFastFail(resultSetterCfs).thenAcceptAsync(unused -> action.accept((T) result[0], (U) result[1]));
}

/**
Expand Down Expand Up @@ -660,8 +646,7 @@ public static <T, U, V> CompletableFuture<V> thenCombineFastFail(
final Object[] result = new Object[css.length];
final CompletableFuture<Void>[] resultSetterCfs = createResultSetterCfs(css, result);

return allOfFastFail(resultSetterCfs)
.thenApply(unused -> fn.apply((T) result[0], (U) result[1]));
return allOfFastFail(resultSetterCfs).thenApply(unused -> fn.apply((T) result[0], (U) result[1]));
}

/**
Expand Down Expand Up @@ -690,8 +675,7 @@ public static <T, U, V> CompletableFuture<V> thenCombineFastFailAsync(
final Object[] result = new Object[css.length];
final CompletableFuture<Void>[] resultSetterCfs = createResultSetterCfs(css, result);

return allOfFastFail(resultSetterCfs)
.thenApplyAsync(unused -> fn.apply((T) result[0], (U) result[1]));
return allOfFastFail(resultSetterCfs).thenApplyAsync(unused -> fn.apply((T) result[0], (U) result[1]));
}

/**
Expand Down Expand Up @@ -1428,7 +1412,6 @@ public static <T> CompletableFuture<T> completeAsync(
if (IS_JAVA9_PLUS) {
return cf.completeAsync(supplier, executor);
}

requireNonNull(supplier, "supplier is null");
requireNonNull(executor, "executor is null");
// below code is copied from CompletableFuture#completeAsync with small adoption
Expand Down Expand Up @@ -1504,6 +1487,43 @@ public static Executor defaultExecutor() {
return AsyncPoolHolder.ASYNC_POOL;
}

////////////////////////////////////////////////////////////////////////////////
//# Conversion (Static) Methods
//
// - toCompletableFutureArray: Cffu -> CF
// - cffuArrayUnwrap: Cffu -> CF
////////////////////////////////////////////////////////////////////////////////

/**
* A convenient util method for converting input {@link Cffu} / {@link CompletableFuture} / {@link CompletionStage}
* array element by {@link Cffu#toCompletableFuture()} / {@link CompletableFuture#toCompletableFuture()} /
* {@link CompletionStage#toCompletableFuture()}.
*
* @see Cffu#toCompletableFuture()
* @see CompletableFuture#toCompletableFuture()
* @see CompletionStage#toCompletableFuture()
* @see CffuFactory#toCffuArray(CompletionStage[])
*/
@Contract(pure = true)
@SafeVarargs
public static <T> CompletableFuture<T>[] toCompletableFutureArray(CompletionStage<T>... stages) {
@SuppressWarnings("unchecked")
CompletableFuture<T>[] ret = new CompletableFuture[stages.length];
for (int i = 0; i < stages.length; i++) {
ret[i] = requireNonNull(stages[i], "stage" + (i + 1) + " is null").toCompletableFuture();
}
return ret;
}

/**
* Convert CompletableFuture list to CompletableFuture array.
*/
@Contract(pure = true)
@SuppressWarnings("unchecked")
public static <T> CompletableFuture<T>[] completableFutureListToArray(List<CompletableFuture<T>> cfList) {
return cfList.toArray(new CompletableFuture[0]);
}

////////////////////////////////////////////////////////////////////////////////
//# Helper fields and classes
////////////////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static io.foldright.cffu.CffuFactoryBuilder.newCffuFactoryBuilder;
import static io.foldright.cffu.CompletableFutureUtils.failedFuture;
import static io.foldright.cffu.CompletableFutureUtils.toCompletableFutureArray;
import static io.foldright.test_utils.TestUtils.*;
import static java.util.concurrent.CompletableFuture.completedFuture;
import static java.util.concurrent.ForkJoinPool.commonPool;
Expand Down Expand Up @@ -737,9 +738,9 @@ void test_toCompletableFutureArray() {
cffuFactory.toCffu(cfArray[1]),
};

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

@Test
Expand Down Expand Up @@ -774,7 +775,7 @@ void test_completableFutureListToArray() {
@SuppressWarnings("unchecked")
CompletableFuture<Integer>[] input = new CompletableFuture[]{completedFuture(n), completedFuture(anotherN)};

assertArrayEquals(input, CffuFactory.completableFutureListToArray(Arrays.asList(input)));
assertArrayEquals(input, CompletableFutureUtils.completableFutureListToArray(Arrays.asList(input)));
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.foldright.cffu.kotlin
import io.foldright.cffu.Cffu
import io.foldright.cffu.CffuFactory
import io.foldright.cffu.CffuFactoryBuilder.newCffuFactoryBuilder
import io.foldright.cffu.CompletableFutureUtils
import java.util.concurrent.CompletableFuture
import java.util.concurrent.CompletionStage

Expand Down Expand Up @@ -536,7 +537,7 @@ fun <T> Array<out CompletableFuture<out T>>.anyOfSuccessCffu(cffuFactory: CffuFa
*
* This method is the same as [CffuFactory.anyOf], providing this method is convenient for method chaining.
*
* @see CffuFactory.toCompletableFutureArray
* @see CompletableFutureUtils.toCompletableFutureArray
*/
fun <T> Collection<CompletionStage<T>>.toCompletableFuture(): List<CompletableFuture<T>> =
map { it.toCompletableFuture() }
Expand All @@ -546,10 +547,10 @@ fun <T> Collection<CompletionStage<T>>.toCompletableFuture(): List<CompletableFu
*
* This method is the same as [CffuFactory.anyOf], providing this method is convenient for method chaining.
*
* @see CffuFactory.toCompletableFutureArray
* @see CompletableFutureUtils.toCompletableFutureArray
*/
fun <T> Array<out CompletionStage<T>>.toCompletableFuture(): Array<CompletableFuture<T>> =
CffuFactory.toCompletableFutureArray(*this)
CompletableFutureUtils.toCompletableFutureArray(*this)

////////////////////////////////////////
// cffuUnwrap methods
Expand Down

0 comments on commit a399348

Please sign in to comment.