Skip to content

Commit

Permalink
refactor: add internal helper methods toCfCopy/isMinimalStageCf 🔄
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed May 24, 2024
1 parent ac90284 commit db4bd22
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
8 changes: 4 additions & 4 deletions cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public <T> Cffu<T> newIncompleteCffu() {
public <T> Cffu<T> toCffu(CompletionStage<T> stage) {
requireNonNull(stage, "stage is null");

if ("java.util.concurrent.CompletableFuture$MinimalStage".equals(stage.getClass().getName())) {
if (CompletableFutureUtils.isMinimalStageCf(stage)) {
return newMin((CompletableFuture<T>) stage);
} else if (stage instanceof CompletableFuture) {
return new0((CompletableFuture<T>) stage);
Expand Down Expand Up @@ -433,10 +433,10 @@ public final <T> Cffu<List<T>> allResultsOfFastFail(CompletionStage<? extends T>
* If the given stage is successful, its result is the completed value; Otherwise the given valueIfNotSuccess.
* (aka the result extraction logic is {@link Cffu#getSuccessNow(Object)}).
*
* @param timeout how long to wait in units of {@code unit}
* @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter
* @param timeout how long to wait in units of {@code unit}
* @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter
* @param valueIfNotSuccess the value to return if not completed successfully
* @param cfs the stages
* @param cfs the stages
* @see Cffu#getSuccessNow(Object)
*/
// TODO * @see CompletableFutureUtils#MGetSuccessNow(Object, CompletionStage[])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public static <T> CompletableFuture<List<T>> mostResultsOfSuccess(

if (cfs.length == 0) return CompletableFuture.completedFuture(arrayList());
if (cfs.length == 1) {
final CompletableFuture<T> f = copy(toNonMinCf(cfs[0]));
final CompletableFuture<T> f = toCfCopy(cfs[0]);
return orTimeout(f, timeout, unit).handle((unused, ex) -> arrayList(getSuccessNow(f, valueIfNotSuccess)));
}

Expand Down Expand Up @@ -310,11 +310,11 @@ private static <T> CompletableFuture<T>[] toCfArray0(
* so the returned CF instances MUST NOT be written(e.g. {@link CompletableFuture#complete(Object)}).
* Otherwise, the caller should defensive copy instead of writing it directly.
*/
@SuppressWarnings({"unchecked", "rawtypes"})
@SuppressWarnings("unchecked")
private static <T> CompletableFuture<T> toCf(CompletionStage<? extends T> s) {
if (s instanceof CompletableFuture) return (CompletableFuture<T>) s;
else if (s instanceof Cffu) return ((Cffu) s).cffuUnwrap();
else return (CompletableFuture) s.toCompletableFuture();
else if (s instanceof Cffu) return ((Cffu<T>) s).cffuUnwrap();
else return (CompletableFuture<T>) s.toCompletableFuture();
}

/**
Expand All @@ -324,15 +324,26 @@ private static <T> CompletableFuture<T> toCf(CompletionStage<? extends T> s) {
* so the returned CF instances MUST NOT be written(e.g. {@link CompletableFuture#complete(Object)}).
* Otherwise, the caller should defensive copy instead of writing it directly.
*/
@SuppressWarnings({"unchecked", "rawtypes"})
@SuppressWarnings("unchecked")
private static <T> CompletableFuture<T> toNonMinCf(CompletionStage<? extends T> s) {
final CompletableFuture<T> f;
if (s instanceof CompletableFuture) f = (CompletableFuture<T>) s;
else if (s instanceof Cffu) f = ((Cffu) s).cffuUnwrap();
else return (CompletableFuture) s.toCompletableFuture();
else if (s instanceof Cffu) f = ((Cffu<T>) s).cffuUnwrap();
else return (CompletableFuture<T>) s.toCompletableFuture();

return isMinimalStageCf(f) ? f.toCompletableFuture() : f;
}

/**
* Converts CompletionStage to CompletableFuture copy.
*/
private static <T> CompletableFuture<T> toCfCopy(CompletionStage<? extends T> s) {
final CompletableFuture<T> f = toCf(s);
return isMinimalStageCf(f) ? f.toCompletableFuture() : copy(f);
}

return "java.util.concurrent.CompletableFuture$MinimalStage".equals(s.getClass().getName())
? f.toCompletableFuture() : f;
static <T> boolean isMinimalStageCf(CompletionStage<? extends T> s) {
return "java.util.concurrent.CompletableFuture$MinimalStage".equals(s.getClass().getName());
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -385,7 +396,7 @@ public static <T> CompletableFuture<T> anyOfSuccess(CompletionStage<? extends T>
requireCfsAndEleNonNull(cfs);
final int size = cfs.length;
if (size == 0) return failedFuture(new NoCfsProvidedException());
if (size == 1) return copy(toCf(cfs[0]));
if (size == 1) return toCfCopy(cfs[0]);

// NOTE: fill ONE MORE element of successOrBeIncompleteCfs LATER
final CompletableFuture<?>[] successOrBeIncomplete = new CompletableFuture[size + 1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.foldright.cffu.kotlin

import io.foldright.cffu.Cffu
import io.foldright.cffu.CffuFactory
import io.foldright.cffu.CompletableFutureUtils
import java.util.concurrent.CompletableFuture
import java.util.concurrent.CompletionStage
import java.util.concurrent.TimeUnit
Expand Down

0 comments on commit db4bd22

Please sign in to comment.