Skip to content

Commit

Permalink
refactor: reorder internal methods of CompletableFutureUtils 🚞
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Jun 28, 2024
1 parent 664d59e commit 073bd64
Showing 1 changed file with 62 additions and 62 deletions.
124 changes: 62 additions & 62 deletions cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,28 @@ public static <T1, T2, T3, T4, T5> CompletableFuture<Tuple5<T1, T2, T3, T4, T5>>
return allTupleOf0(true, wrapSuppliers(executor, suppliers));
}

private static <T> CompletableFuture<T> allTupleOf0(boolean fastFail, CompletionStage<?>[] css) {
final Object[] result = new Object[css.length];
final CompletableFuture<Void>[] resultSetterCfs = createResultSetterCfs(css, result);

final CompletableFuture<Void> resultSetter;
if (fastFail) resultSetter = allOfFastFail(resultSetterCfs);
else resultSetter = CompletableFuture.allOf(resultSetterCfs);

return resultSetter.thenApply(unused -> tupleOf0(result));
}

@SuppressWarnings("unchecked")
private static <T> T tupleOf0(Object... elements) {
final int len = elements.length;
final Object ret;
if (len == 2) ret = Tuple2.of(elements[0], elements[1]);
else if (len == 3) ret = Tuple3.of(elements[0], elements[1], elements[2]);
else if (len == 4) ret = Tuple4.of(elements[0], elements[1], elements[2], elements[3]);
else ret = Tuple5.of(elements[0], elements[1], elements[2], elements[3], elements[4]);
return (T) ret;
}

/**
* Returns a new CompletableFuture that is asynchronously completed
* by tasks running in the CompletableFuture's default asynchronous execution facility
Expand Down Expand Up @@ -601,6 +623,33 @@ public static <T1, T2, T3, T4, T5> CompletableFuture<Tuple5<T1, T2, T3, T4, T5>>
return mostTupleOfSuccess0(executor, timeout, unit, wrapSuppliers(executor, suppliers));
}

private static <T> CompletableFuture<T> mostTupleOfSuccess0(
Executor executorWhenTimeout, long timeout, TimeUnit unit, CompletionStage<?>... css) {
requireNonNull(executorWhenTimeout, "executorWhenTimeout is null");
requireNonNull(unit, "unit is null");
// MUST be *Non-Minimal* CF instances in order to read results(`getSuccessNow`),
// otherwise UnsupportedOperationException
final CompletableFuture<Object>[] cfArray = toNonMinCfArray(css);
return cffuCompleteOnTimeout(CompletableFuture.allOf(cfArray), null, executorWhenTimeout, timeout, unit)
.handle((unused, ex) -> tupleOf0(MGetSuccessNow0(null, cfArray)));
}

/**
* Multi-Gets(MGet) the results in the <strong>same order</strong> of the given cfs arguments,
* use the result value if the given stage is completed successfully, else use the given valueIfNotSuccess
*
* @param cfs MUST be *Non-Minimal* CF instances in order to read results(`getSuccessNow`),
* otherwise UnsupportedOperationException
*/
@SuppressWarnings("unchecked")
private static <T> T[] MGetSuccessNow0(@Nullable Object valueIfNotSuccess, CompletableFuture<?>... cfs) {
Object[] ret = new Object[cfs.length];
for (int i = 0; i < cfs.length; i++) {
ret[i] = getSuccessNow(cfs[i], valueIfNotSuccess);
}
return (T[]) ret;
}

/**
* Returns a new CompletableFuture that is asynchronously completed
* by tasks running in the CompletableFuture's default asynchronous execution facility
Expand Down Expand Up @@ -833,22 +882,6 @@ public static <T> CompletableFuture<List<T>> mostResultsOfSuccess(
.handle((unused, ex) -> arrayList(MGetSuccessNow0(valueIfNotSuccess, cfArray)));
}

/**
* Multi-Gets(MGet) the results in the <strong>same order</strong> of the given cfs arguments,
* use the result value if the given stage is completed successfully, else use the given valueIfNotSuccess
*
* @param cfs MUST be *Non-Minimal* CF instances in order to read results(`getSuccessNow`),
* otherwise UnsupportedOperationException
*/
@SuppressWarnings("unchecked")
private static <T> T[] MGetSuccessNow0(@Nullable Object valueIfNotSuccess, CompletableFuture<?>... cfs) {
Object[] ret = new Object[cfs.length];
for (int i = 0; i < cfs.length; i++) {
ret[i] = getSuccessNow(cfs[i], valueIfNotSuccess);
}
return (T[]) ret;
}

/**
* Returns a new CompletableFuture with the results in the <strong>same order</strong> of the given stages arguments,
* the new CompletableFuture is completed when all the given stages complete;
Expand Down Expand Up @@ -1464,39 +1497,6 @@ public static <T1, T2, T3, T4, T5> CompletableFuture<Tuple5<T1, T2, T3, T4, T5>>
return allTupleOf0(false, requireCfsAndEleNonNull(cf1, cf2, cf3, cf4, cf5));
}

private static <T> CompletableFuture<T> allTupleOf0(boolean fastFail, CompletionStage<?>[] css) {
final Object[] result = new Object[css.length];
final CompletableFuture<Void>[] resultSetterCfs = createResultSetterCfs(css, result);

final CompletableFuture<Void> resultSetter;
if (fastFail) resultSetter = allOfFastFail(resultSetterCfs);
else resultSetter = CompletableFuture.allOf(resultSetterCfs);

return resultSetter.thenApply(unused -> tupleOf0(result));
}

@SuppressWarnings("unchecked")
private static <T> T tupleOf0(Object... elements) {
final int len = elements.length;
final Object ret;
if (len == 2) ret = Tuple2.of(elements[0], elements[1]);
else if (len == 3) ret = Tuple3.of(elements[0], elements[1], elements[2]);
else if (len == 4) ret = Tuple4.of(elements[0], elements[1], elements[2], elements[3]);
else ret = Tuple5.of(elements[0], elements[1], elements[2], elements[3], elements[4]);
return (T) ret;
}

private static <T> CompletableFuture<T> mostTupleOfSuccess0(
Executor executorWhenTimeout, long timeout, TimeUnit unit, CompletionStage<?>... css) {
requireNonNull(executorWhenTimeout, "executorWhenTimeout is null");
requireNonNull(unit, "unit is null");
// MUST be *Non-Minimal* CF instances in order to read results(`getSuccessNow`),
// otherwise UnsupportedOperationException
final CompletableFuture<Object>[] cfArray = toNonMinCfArray(css);
return cffuCompleteOnTimeout(CompletableFuture.allOf(cfArray), null, executorWhenTimeout, timeout, unit)
.handle((unused, ex) -> tupleOf0(MGetSuccessNow0(null, cfArray)));
}

// endregion
////////////////////////////////////////////////////////////
// region## Immediate Value Argument Factory Methods(backport methods)
Expand Down Expand Up @@ -3036,6 +3036,19 @@ private static void completeCf(CompletableFuture<Object> cf, Object value, @Null
}
}

@Nullable
@SuppressWarnings("SameReturnValue")
private static <T> T reportException(String msg, Throwable ex) {
StringWriter sw = new StringWriter(4096);
PrintWriter writer = new PrintWriter(sw);

writer.println(msg);
ex.printStackTrace(writer);

System.err.println(sw);
return null;
}

// endregion
////////////////////////////////////////////////////////////
// region## Advanced Methods of CompletionStage(compose* and handle-like methods)
Expand Down Expand Up @@ -3179,19 +3192,6 @@ C peekAsync(C cf, BiConsumer<? super T, ? super Throwable> action, Executor exec
return cf;
}

@Nullable
@SuppressWarnings("SameReturnValue")
private static <T> T reportException(String msg, Throwable ex) {
StringWriter sw = new StringWriter(4096);
PrintWriter writer = new PrintWriter(sw);

writer.println(msg);
ex.printStackTrace(writer);

System.err.println(sw);
return null;
}

// endregion
////////////////////////////////////////////////////////////
// region## Read(explicitly) Methods of CompletableFuture(including Future)
Expand Down

0 comments on commit 073bd64

Please sign in to comment.