Skip to content

Commit

Permalink
refactor: rename internal factory method names of CffuFactory from …
Browse files Browse the repository at this point in the history
…`new*` to `create*` 🔠
  • Loading branch information
oldratlee committed May 24, 2024
1 parent 8cfcf6d commit 56a3906
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 71 deletions.
88 changes: 43 additions & 45 deletions cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,6 @@ public final class CffuFactory {
this.forbidObtrudeMethods = forbidObtrudeMethods;
}

@Contract(pure = true)
private <T> Cffu<T> new0(CompletableFuture<T> cf) {
return new Cffu<>(this, false, cf);
}

@Contract(pure = true)
private <T> Cffu<T> newMin(CompletableFuture<T> cf) {
return new Cffu<>(this, true, cf);
}

/**
* Returns a {@link CffuFactoryBuilder} with {@code defaultExecutor} setting.
*
Expand All @@ -77,6 +67,16 @@ public static CffuFactoryBuilder builder(Executor defaultExecutor) {
return new CffuFactoryBuilder(CompletableFutureUtils.screenExecutor(defaultExecutor));
}

@Contract(pure = true)
private <T> Cffu<T> create(CompletableFuture<T> cf) {
return new Cffu<>(this, false, cf);
}

@Contract(pure = true)
private <T> Cffu<T> createMin(CompletableFuture<T> cf) {
return new Cffu<>(this, true, cf);
}

////////////////////////////////////////////////////////////////////////////////
//# Factory Methods, equivalent to same name static methods of CompletableFuture
//
Expand All @@ -95,7 +95,7 @@ public static CffuFactoryBuilder builder(Executor defaultExecutor) {
*/
@Contract(pure = true)
public <T> Cffu<T> completedFuture(@Nullable T value) {
return new0(CompletableFuture.completedFuture(value));
return create(CompletableFuture.completedFuture(value));
}

/**
Expand All @@ -114,7 +114,7 @@ public <T> Cffu<T> completedFuture(@Nullable T value) {
*/
@Contract(pure = true)
public <T> CompletionStage<T> completedStage(@Nullable T value) {
return newMin((CompletableFuture<T>) CompletableFutureUtils.completedStage(value));
return createMin((CompletableFuture<T>) CompletableFutureUtils.completedStage(value));
}

/**
Expand All @@ -127,7 +127,7 @@ public <T> CompletionStage<T> completedStage(@Nullable T value) {
*/
@Contract(pure = true)
public <T> Cffu<T> failedFuture(Throwable ex) {
return new0(CompletableFutureUtils.failedFuture(ex));
return create(CompletableFutureUtils.failedFuture(ex));
}

/**
Expand All @@ -146,7 +146,7 @@ public <T> Cffu<T> failedFuture(Throwable ex) {
*/
@Contract(pure = true)
public <T> CompletionStage<T> failedStage(Throwable ex) {
return newMin((CompletableFuture<T>) CompletableFutureUtils.<T>failedStage(ex));
return createMin((CompletableFuture<T>) CompletableFutureUtils.<T>failedStage(ex));
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -179,7 +179,7 @@ public Cffu<Void> runAsync(Runnable action) {
* @see CompletableFuture#runAsync(Runnable, Executor)
*/
public Cffu<Void> runAsync(Runnable action, Executor executor) {
return new0(CompletableFuture.runAsync(action, executor));
return create(CompletableFuture.runAsync(action, executor));
}

/**
Expand Down Expand Up @@ -211,7 +211,7 @@ public <T> Cffu<T> supplyAsync(Supplier<T> supplier) {
@CheckReturnValue(explanation = "should use the returned Cffu; otherwise, prefer method `runAsync`")
@SuppressWarnings("BoundedWildcard")
public <T> Cffu<T> supplyAsync(Supplier<T> supplier, Executor executor) {
return new0(CompletableFuture.supplyAsync(supplier, executor));
return create(CompletableFuture.supplyAsync(supplier, executor));
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -241,7 +241,7 @@ public <T> Cffu<T> supplyAsync(Supplier<T> supplier, Executor executor) {
*/
@Contract(pure = true)
public <T> Cffu<T> newIncompleteCffu() {
return new0(new CompletableFuture<>());
return create(new CompletableFuture<>());
}

/**
Expand All @@ -268,13 +268,13 @@ public <T> Cffu<T> toCffu(CompletionStage<T> stage) {
requireNonNull(stage, "stage is null");

if (CompletableFutureUtils.isMinimalStageCf(stage)) {
return newMin((CompletableFuture<T>) stage);
return createMin((CompletableFuture<T>) stage);
} else if (stage instanceof CompletableFuture) {
return new0((CompletableFuture<T>) stage);
return create((CompletableFuture<T>) stage);
} else if (stage instanceof Cffu) {
return ((Cffu<T>) stage).resetCffuFactory(this);
}
return new0(stage.toCompletableFuture());
return create(stage.toCompletableFuture());
}

/**
Expand Down Expand Up @@ -335,7 +335,7 @@ public final <T> Cffu<T>[] toCffuArray(CompletionStage<T>... stages) {
*/
@Contract(pure = true)
public Cffu<Void> allOf(CompletionStage<?>... cfs) {
return new0(CompletableFutureUtils.allOf(cfs));
return create(CompletableFutureUtils.allOf(cfs));
}

/**
Expand All @@ -357,7 +357,7 @@ public Cffu<Void> allOf(CompletionStage<?>... cfs) {
@Contract(pure = true)
@SafeVarargs
public final <T> Cffu<List<T>> allResultsOf(CompletionStage<? extends T>... cfs) {
return new0(CompletableFutureUtils.allResultsOf(cfs));
return create(CompletableFutureUtils.allResultsOf(cfs));
}

/**
Expand Down Expand Up @@ -390,7 +390,7 @@ public final <T> Cffu<List<T>> allResultsOf(CompletionStage<? extends T>... cfs)
*/
@Contract(pure = true)
public Cffu<Void> allOfFastFail(CompletionStage<?>... cfs) {
return new0(CompletableFutureUtils.allOfFastFail(cfs));
return create(CompletableFutureUtils.allOfFastFail(cfs));
}

/**
Expand All @@ -415,7 +415,7 @@ public Cffu<Void> allOfFastFail(CompletionStage<?>... cfs) {
@Contract(pure = true)
@SafeVarargs
public final <T> Cffu<List<T>> allResultsOfFastFail(CompletionStage<? extends T>... cfs) {
return new0(CompletableFutureUtils.allResultsOfFastFail(cfs));
return create(CompletableFutureUtils.allResultsOfFastFail(cfs));
}

/**
Expand All @@ -434,7 +434,7 @@ public final <T> Cffu<List<T>> allResultsOfFastFail(CompletionStage<? extends T>
@SafeVarargs
public final <T> Cffu<List<T>> mostResultsOfSuccess(
long timeout, TimeUnit unit, @Nullable T valueIfNotSuccess, CompletionStage<? extends T>... cfs) {
return new0(CompletableFutureUtils.mostResultsOfSuccess(timeout, unit, valueIfNotSuccess, cfs));
return create(CompletableFutureUtils.mostResultsOfSuccess(timeout, unit, valueIfNotSuccess, cfs));
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -464,7 +464,7 @@ public final <T> Cffu<List<T>> mostResultsOfSuccess(
@Contract(pure = true)
@SafeVarargs
public final <T> Cffu<T> anyOf(CompletionStage<? extends T>... cfs) {
return new0(CompletableFutureUtils.anyOf(cfs));
return create(CompletableFutureUtils.anyOf(cfs));
}

/**
Expand All @@ -485,7 +485,7 @@ public final <T> Cffu<T> anyOf(CompletionStage<? extends T>... cfs) {
*/
@SafeVarargs
public final <T> Cffu<T> anyOfSuccess(CompletionStage<? extends T>... cfs) {
return new0(CompletableFutureUtils.anyOfSuccess(cfs));
return create(CompletableFutureUtils.anyOfSuccess(cfs));
}

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -508,7 +508,7 @@ public final <T> Cffu<T> anyOfSuccess(CompletionStage<? extends T>... cfs) {
@Contract(pure = true)
public <T1, T2> Cffu<Tuple2<T1, T2>> allTupleOf(
CompletionStage<? extends T1> cf1, CompletionStage<? extends T2> cf2) {
return new0(CompletableFutureUtils.allTupleOf(cf1, cf2));
return create(CompletableFutureUtils.allTupleOf(cf1, cf2));
}

/**
Expand All @@ -526,7 +526,7 @@ public <T1, T2> Cffu<Tuple2<T1, T2>> allTupleOf(
@Contract(pure = true)
public <T1, T2> Cffu<Tuple2<T1, T2>> allTupleOfFastFail(
CompletionStage<? extends T1> cf1, CompletionStage<? extends T2> cf2) {
return new0(CompletableFutureUtils.allTupleOfFastFail(cf1, cf2));
return create(CompletableFutureUtils.allTupleOfFastFail(cf1, cf2));
}

/**
Expand All @@ -541,7 +541,7 @@ public <T1, T2> Cffu<Tuple2<T1, T2>> allTupleOfFastFail(
@Contract(pure = true)
public <T1, T2, T3> Cffu<Tuple3<T1, T2, T3>> allTupleOf(
CompletionStage<? extends T1> cf1, CompletionStage<? extends T2> cf2, CompletionStage<? extends T3> cf3) {
return new0(CompletableFutureUtils.allTupleOf(cf1, cf2, cf3));
return create(CompletableFutureUtils.allTupleOf(cf1, cf2, cf3));
}

/**
Expand All @@ -559,7 +559,7 @@ public <T1, T2, T3> Cffu<Tuple3<T1, T2, T3>> allTupleOf(
@Contract(pure = true)
public <T1, T2, T3> Cffu<Tuple3<T1, T2, T3>> allTupleOfFastFail(
CompletionStage<? extends T1> cf1, CompletionStage<? extends T2> cf2, CompletionStage<? extends T3> cf3) {
return new0(CompletableFutureUtils.allTupleOfFastFail(cf1, cf2, cf3));
return create(CompletableFutureUtils.allTupleOfFastFail(cf1, cf2, cf3));
}

/**
Expand All @@ -575,7 +575,7 @@ public <T1, T2, T3> Cffu<Tuple3<T1, T2, T3>> allTupleOfFastFail(
public <T1, T2, T3, T4> Cffu<Tuple4<T1, T2, T3, T4>> allTupleOf(
CompletionStage<? extends T1> cf1, CompletionStage<? extends T2> cf2,
CompletionStage<? extends T3> cf3, CompletionStage<? extends T4> cf4) {
return new0(CompletableFutureUtils.allTupleOf(cf1, cf2, cf3, cf4));
return create(CompletableFutureUtils.allTupleOf(cf1, cf2, cf3, cf4));
}

/**
Expand All @@ -594,7 +594,7 @@ public <T1, T2, T3, T4> Cffu<Tuple4<T1, T2, T3, T4>> allTupleOf(
public <T1, T2, T3, T4> Cffu<Tuple4<T1, T2, T3, T4>> allTupleOfFastFail(
CompletionStage<? extends T1> cf1, CompletionStage<? extends T2> cf2,
CompletionStage<? extends T3> cf3, CompletionStage<? extends T4> cf4) {
return new0(CompletableFutureUtils.allTupleOfFastFail(cf1, cf2, cf3, cf4));
return create(CompletableFutureUtils.allTupleOfFastFail(cf1, cf2, cf3, cf4));
}

/**
Expand All @@ -610,7 +610,7 @@ public <T1, T2, T3, T4> Cffu<Tuple4<T1, T2, T3, T4>> allTupleOfFastFail(
public <T1, T2, T3, T4, T5> Cffu<Tuple5<T1, T2, T3, T4, T5>> allTupleOf(
CompletionStage<? extends T1> cf1, CompletionStage<? extends T2> cf2, CompletionStage<? extends T3> cf3,
CompletionStage<? extends T4> cf4, CompletionStage<? extends T5> cf5) {
return new0(CompletableFutureUtils.allTupleOf(cf1, cf2, cf3, cf4, cf5));
return create(CompletableFutureUtils.allTupleOf(cf1, cf2, cf3, cf4, cf5));
}

/**
Expand All @@ -629,7 +629,7 @@ public <T1, T2, T3, T4, T5> Cffu<Tuple5<T1, T2, T3, T4, T5>> allTupleOf(
public <T1, T2, T3, T4, T5> Cffu<Tuple5<T1, T2, T3, T4, T5>> allTupleOfFastFail(
CompletionStage<? extends T1> cf1, CompletionStage<? extends T2> cf2, CompletionStage<? extends T3> cf3,
CompletionStage<? extends T4> cf4, CompletionStage<? extends T5> cf5) {
return new0(CompletableFutureUtils.allTupleOfFastFail(cf1, cf2, cf3, cf4, cf5));
return create(CompletableFutureUtils.allTupleOfFastFail(cf1, cf2, cf3, cf4, cf5));
}

/**
Expand All @@ -647,7 +647,7 @@ public <T1, T2, T3, T4, T5> Cffu<Tuple5<T1, T2, T3, T4, T5>> allTupleOfFastFail(
@Contract(pure = true)
public <T1, T2> Cffu<Tuple2<T1, T2>> mostTupleOfSuccess(
long timeout, TimeUnit unit, CompletionStage<? extends T1> cf1, CompletionStage<? extends T2> cf2) {
return new0(CompletableFutureUtils.mostTupleOfSuccess(timeout, unit, cf1, cf2));
return create(CompletableFutureUtils.mostTupleOfSuccess(timeout, unit, cf1, cf2));
}

/**
Expand All @@ -666,7 +666,7 @@ public <T1, T2> Cffu<Tuple2<T1, T2>> mostTupleOfSuccess(
public <T1, T2, T3> Cffu<Tuple3<T1, T2, T3>> mostTupleOfSuccess(
long timeout, TimeUnit unit,
CompletionStage<? extends T1> cf1, CompletionStage<? extends T2> cf2, CompletionStage<? extends T3> cf3) {
return new0(CompletableFutureUtils.mostTupleOfSuccess(timeout, unit, cf1, cf2, cf3));
return create(CompletableFutureUtils.mostTupleOfSuccess(timeout, unit, cf1, cf2, cf3));
}

/**
Expand All @@ -686,7 +686,7 @@ public <T1, T2, T3, T4> Cffu<Tuple4<T1, T2, T3, T4>> mostTupleOfSuccess(
long timeout, TimeUnit unit,
CompletionStage<? extends T1> cf1, CompletionStage<? extends T2> cf2,
CompletionStage<? extends T3> cf3, CompletionStage<? extends T4> cf4) {
return new0(CompletableFutureUtils.mostTupleOfSuccess(timeout, unit, cf1, cf2, cf3, cf4));
return create(CompletableFutureUtils.mostTupleOfSuccess(timeout, unit, cf1, cf2, cf3, cf4));
}

/**
Expand All @@ -706,7 +706,7 @@ public <T1, T2, T3, T4, T5> Cffu<Tuple5<T1, T2, T3, T4, T5>> mostTupleOfSuccess(
long timeout, TimeUnit unit,
CompletionStage<? extends T1> cf1, CompletionStage<? extends T2> cf2, CompletionStage<? extends T3> cf3,
CompletionStage<? extends T4> cf4, CompletionStage<? extends T5> cf5) {
return new0(CompletableFutureUtils.mostTupleOfSuccess(timeout, unit, cf1, cf2, cf3, cf4, cf5));
return create(CompletableFutureUtils.mostTupleOfSuccess(timeout, unit, cf1, cf2, cf3, cf4, cf5));
}

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -721,8 +721,7 @@ public <T1, T2, T3, T4, T5> Cffu<Tuple5<T1, T2, T3, T4, T5>> mostTupleOfSuccess(
* Each delay commences upon invocation of the returned executor's {@code execute} method.
*
* @param delay how long to delay, in units of {@code unit}
* @param unit a {@code TimeUnit} determining how to interpret the
* {@code delay} parameter
* @param unit a {@code TimeUnit} determining how to interpret the {@code delay} parameter
* @return the new delayed executor
*/
@Contract(pure = true)
Expand All @@ -736,8 +735,7 @@ public Executor delayedExecutor(long delay, TimeUnit unit) {
* Each delay commences upon invocation of the returned executor's {@code execute} method.
*
* @param delay how long to delay, in units of {@code unit}
* @param unit a {@code TimeUnit} determining how to interpret the
* {@code delay} parameter
* @param unit a {@code TimeUnit} determining how to interpret the {@code delay} parameter
* @param executor the base executor
* @return the new delayed executor
*/
Expand All @@ -749,8 +747,8 @@ public Executor delayedExecutor(long delay, TimeUnit unit, Executor executor) {
////////////////////////////////////////////////////////////////////////////////
//# Conversion (Static) Methods
//
// - cffuListToArray: List<Cffu> -> Cffu[]
// - completableFutureListToArray: List<CF> -> CF[]
// - cffuArrayUnwrap: Cffu[] -> CompletableFuture[]
// - cffuListToArray: List<Cffu> -> Cffu[]
////////////////////////////////////////////////////////////////////////////////

/**
Expand Down
50 changes: 24 additions & 26 deletions cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -737,34 +737,21 @@ void test_allTupleOfFastFail_exceptionally() throws Exception {
////////////////////////////////////////////////////////////////////////////////
//# Conversion (Static) Methods
//
// - toCompletableFutureArray: CompletionStage[](including Cffu) -> CF[]
// - completableFutureListToArray: List<CF> -> CF[]

// - cffuArrayUnwrap: Cffu -> CF
// - cffuListToArray: List<Cffu> -> Cffu[]
// - toCompletableFutureArray: CompletionStage[](including Cffu) -> CF[]
////////////////////////////////////////////////////////////////////////////////

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

assertArrayEquals(cfArray, toCompletableFutureArray(cfArray));
assertArrayEquals(cfArray, toCompletableFutureArray(csArray));
assertArrayEquals(cfArray, toCompletableFutureArray(cffuArray));
assertArrayEquals(input, CffuFactory.cffuListToArray(Arrays.asList(input)));
}

@Test
Expand All @@ -783,15 +770,26 @@ void test_cffuArrayUnwrap() {
}

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

assertArrayEquals(input, CffuFactory.cffuListToArray(Arrays.asList(input)));
assertArrayEquals(cfArray, toCompletableFutureArray(cfArray));
assertArrayEquals(cfArray, toCompletableFutureArray(csArray));
assertArrayEquals(cfArray, toCompletableFutureArray(cffuArray));
}

////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 56a3906

Please sign in to comment.