Skip to content

Commit

Permalink
refactor: rename method asCffu to toCffu, consistant with `Comple…
Browse files Browse the repository at this point in the history
…tionStage#toCompletableFuture`
  • Loading branch information
oldratlee committed Apr 28, 2024
1 parent aab5220 commit 3f441c4
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 63 deletions.
6 changes: 3 additions & 3 deletions cffu-core/src/main/java/io/foldright/cffu/Cffu.java
Original file line number Diff line number Diff line change
Expand Up @@ -1966,7 +1966,7 @@ public boolean forbidObtrudeMethods() {
* <li>{@link CffuFactory#completedStage(Object)}
* <li>{@link CffuFactory#failedStage(Throwable)}
* <li>{@link #minimalCompletionStage()}
* <li>{@link CffuFactory#asCffu(CompletionStage)}, this method return a {@code minimal stage}
* <li>{@link CffuFactory#toCffu(CompletionStage)}, this method return a {@code minimal stage}
* when input a{@code minimal stage}, otherwise return a normal stage.
* </ul>
*/
Expand All @@ -1985,11 +1985,11 @@ public boolean isMinimalStage() {
/**
* Returns the underlying CompletableFuture.
* <p>
* {@link CffuFactory#asCffu(CompletionStage)} is inverse operation to this method.
* {@link CffuFactory#toCffu(CompletionStage)} is inverse operation to this method.
* {@link CffuFactory#cffuArrayUnwrap(Cffu[])} is the batch operation to this method.
*
* @return the underlying CompletableFuture
* @see CffuFactory#asCffu(CompletionStage)
* @see CffuFactory#toCffu(CompletionStage)
* @see CffuFactory#cffuArrayUnwrap(Cffu[])
* @see #toCompletableFuture()
*/
Expand Down
20 changes: 10 additions & 10 deletions cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ public <T> Cffu<T> supplyAsync(Supplier<T> supplier, Executor executor) {
//
// - newIncompleteCffu: equivalent to CompletableFuture constructor
//
// - asCffu: CF/CompletionStage -> Cffu
// - asCffuArray: CF/CompletionStage[] -> Cffu[]
// - toCffu: CF/CompletionStage -> Cffu
// - toCffuArray: CF/CompletionStage[] -> Cffu[]
////////////////////////////////////////////////////////////////////////////////

/**
Expand Down Expand Up @@ -255,13 +255,13 @@ public <T> Cffu<T> newIncompleteCffu() {
* <li>otherwise use input {@code stage.toCompletableFuture} as the underlying cf of returned cffu.
* </ol>
*
* @see #toCffuArray(CompletionStage[])
* @see CompletionStage#toCompletableFuture()
* @see Cffu#cffuUnwrap()
* @see Cffu#resetCffuFactory(CffuFactory)
* @see #asCffuArray(CompletionStage[])
* @see CompletionStage#toCompletableFuture()
*/
@Contract(pure = true)
public <T> Cffu<T> asCffu(CompletionStage<T> stage) {
public <T> Cffu<T> toCffu(CompletionStage<T> stage) {
requireNonNull(stage, "stage is null");

if ("java.util.concurrent.CompletableFuture$MinimalStage".equals(stage.getClass().getName())) {
Expand All @@ -276,17 +276,17 @@ public <T> Cffu<T> asCffu(CompletionStage<T> stage) {

/**
* A convenient util method for wrap input {@link CompletableFuture} / {@link CompletionStage} / {@link Cffu}
* array element by {@link #asCffu(CompletionStage)}.
* array element by {@link #toCffu(CompletionStage)}.
*
* @see #asCffu(CompletionStage)
* @see #toCffu(CompletionStage)
*/
@Contract(pure = true)
@SafeVarargs
public final <T> Cffu<T>[] asCffuArray(CompletionStage<T>... stages) {
public final <T> Cffu<T>[] toCffuArray(CompletionStage<T>... stages) {
@SuppressWarnings("unchecked")
Cffu<T>[] ret = new Cffu[stages.length];
for (int i = 0; i < stages.length; i++) {
ret[i] = asCffu(requireNonNull(stages[i], "stage" + (i + 1) + " is null"));
ret[i] = toCffu(requireNonNull(stages[i], "stage" + (i + 1) + " is null"));
}
return ret;
}
Expand Down Expand Up @@ -636,7 +636,7 @@ public Executor delayedExecutor(long delay, TimeUnit unit, Executor executor) {
* @see Cffu#toCompletableFuture()
* @see CompletableFuture#toCompletableFuture()
* @see CompletionStage#toCompletableFuture()
* @see #asCffuArray(CompletionStage[])
* @see #toCffuArray(CompletionStage[])
*/
@Contract(pure = true)
@SafeVarargs
Expand Down
31 changes: 15 additions & 16 deletions cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.*;
import java.util.function.Function;

import static io.foldright.cffu.CffuFactoryBuilder.newCffuFactoryBuilder;
import static io.foldright.cffu.CompletableFutureUtils.failedFuture;
Expand Down Expand Up @@ -106,8 +105,8 @@ void test_failedStage() throws Exception {
//
// - newIncompleteCffu: equivalent to CompletableFuture constructor
//
// - asCffu: CF/CompletionStage -> Cffu
// - asCffuArray: CF/CompletionStage[] -> Cffu[]
// - toCffu: CF/CompletionStage -> Cffu
// - toCffuArray: CF/CompletionStage[] -> Cffu[]
////////////////////////////////////////////////////////////////////////////////

@Test
Expand All @@ -117,14 +116,14 @@ void test_newIncompleteCffu() {
}

@Test
void test_asCffu() throws Exception {
Cffu<Integer> cf = cffuFactory.asCffu(completedFuture(n));
void test_toCffu() throws Exception {
Cffu<Integer> cf = cffuFactory.toCffu(completedFuture(n));

assertEquals(n, cf.get());
shouldNotBeMinimalStage(cf);

CffuFactory fac = newCffuFactoryBuilder(anotherExecutorService).forbidObtrudeMethods(true).build();
Cffu<Integer> cffu = fac.asCffu(cffuFactory.completedFuture(42));
Cffu<Integer> cffu = fac.toCffu(cffuFactory.completedFuture(42));
assertSame(anotherExecutorService, cffu.defaultExecutor());
assertSame(fac, cffu.cffuFactory());

Expand All @@ -138,24 +137,24 @@ void test_asCffu() throws Exception {

@Test
@EnabledForJreRange(min = JRE.JAVA_9)
void test_asCffu__for_factoryMethods_of_Java9() {
void test_toCffu__for_factoryMethods_of_Java9() {
CompletableFuture<Object> cf1 = CompletableFuture.failedFuture(rte);
assertFalse(cffuFactory.asCffu(cf1).isMinimalStage());
assertFalse(cffuFactory.toCffu(cf1).isMinimalStage());
shouldNotBeMinimalStage(cf1);

Cffu<Integer> cf2 = cffuFactory.asCffu(CompletableFuture.completedStage(n));
Cffu<Integer> cf2 = cffuFactory.toCffu(CompletableFuture.completedStage(n));
assertTrue(cf2.isMinimalStage());
shouldBeMinimalStage(cf2);

Cffu<Object> cf3 = cffuFactory.asCffu(CompletableFuture.failedStage(rte));
Cffu<Object> cf3 = cffuFactory.toCffu(CompletableFuture.failedStage(rte));
assertTrue(cf3.isMinimalStage());
shouldBeMinimalStage(cf3);
}

@Test
@EnabledForJreRange(min = JRE.JAVA_9)
void test_asCffuArray() throws Exception {
Cffu<Integer>[] cffus = cffuFactory.asCffuArray(CompletableFuture.completedStage(n), completedFuture(n));
void test_toCffuArray() throws Exception {
Cffu<Integer>[] cffus = cffuFactory.toCffuArray(CompletableFuture.completedStage(n), completedFuture(n));
assertEquals(n, cffus[1].get());

shouldBeMinimalStage(cffus[0]);
Expand Down Expand Up @@ -734,8 +733,8 @@ void test_toCompletableFutureArray() {
};
@SuppressWarnings("unchecked")
Cffu<Integer>[] cffuArray = new Cffu[]{
cffuFactory.asCffu(cfArray[0]),
cffuFactory.asCffu(cfArray[1]),
cffuFactory.toCffu(cfArray[0]),
cffuFactory.toCffu(cfArray[1]),
};

assertArrayEquals(cfArray, CffuFactory.toCompletableFutureArray(cfArray));
Expand All @@ -752,8 +751,8 @@ void test_cffuArrayUnwrap() {
};
@SuppressWarnings("unchecked")
Cffu<Integer>[] input = new Cffu[]{
cffuFactory.asCffu(cfArray[0]),
cffuFactory.asCffu(cfArray[1]),
cffuFactory.toCffu(cfArray[0]),
cffuFactory.toCffu(cfArray[1]),
};
assertArrayEquals(cfArray, CffuFactory.cffuArrayUnwrap(input));
}
Expand Down
7 changes: 3 additions & 4 deletions cffu-core/src/test/java/io/foldright/cffu/CffuTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.concurrent.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

import static io.foldright.cffu.CffuFactoryBuilder.newCffuFactoryBuilder;
import static io.foldright.test_utils.TestUtils.*;
Expand Down Expand Up @@ -278,7 +277,7 @@ void test_isMinimalStage() {
@Test
void test_cffuUnwrap() {
CompletableFuture<Integer> cf = CompletableFuture.completedFuture(n);
Cffu<Integer> cffu = cffuFactory.asCffu(cf);
Cffu<Integer> cffu = cffuFactory.toCffu(cf);

assertSame(cf, cffu.cffuUnwrap());
}
Expand All @@ -287,15 +286,15 @@ void test_cffuUnwrap() {
@EnabledForJreRange(min = JRE.JAVA_9)
void test_cffuUnwrap_9_completedStage() {
CompletionStage<Integer> stage = CompletableFuture.completedStage(n);
Cffu<Integer> cffu = cffuFactory.asCffu(stage);
Cffu<Integer> cffu = cffuFactory.toCffu(stage);

assertSame(stage, cffu.cffuUnwrap());
}

@Test
void test_toString() {
CompletableFuture<Integer> cf = CompletableFuture.completedFuture(42);
Cffu<Integer> cffu = cffuFactory.asCffu(cf);
Cffu<Integer> cffu = cffuFactory.toCffu(cf);

assertTrue(cffu.toString().contains(cf.toString()));
assertTrue(cffu.toString().startsWith("Cffu@"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,41 @@ import java.util.concurrent.CompletionStage
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////
// asCffu methods
// toCffu methods
////////////////////////////////////////

/**
* Wrap an existed [CompletableFuture]/[CompletionStage] to [Cffu].
*
* This method is the same as [CffuFactory.asCffu], providing this method is convenient for method chaining.
* This method is the same as [CffuFactory.toCffu], providing this method is convenient for method chaining.
*
* @see CffuFactory.asCffu
* @see CffuFactory.toCffu
* @see CompletionStage.toCompletableFuture
*/
fun <T> CompletionStage<T>.asCffu(cffuFactory: CffuFactory): Cffu<T> =
cffuFactory.asCffu(this)
fun <T> CompletionStage<T>.toCffu(cffuFactory: CffuFactory): Cffu<T> =
cffuFactory.toCffu(this)

/**
* Wrap input [CompletableFuture]/[CompletionStage] collection elements to [Cffu] by [CffuFactory.asCffu].
* Wrap input [CompletableFuture]/[CompletionStage] collection elements to [Cffu] by [CffuFactory.toCffu].
*
* This method is the same as [CffuFactory.asCffuArray], providing this method is convenient for method chaining.
* This method is the same as [CffuFactory.toCffuArray], providing this method is convenient for method chaining.
*
* @see CffuFactory.asCffu
* @see CffuFactory.toCffuArray
* @see CffuFactory.toCffu
*/
fun <T> Collection<CompletionStage<T>>.asCffu(cffuFactory: CffuFactory): List<Cffu<T>> =
map { it.asCffu(cffuFactory) }
fun <T> Collection<CompletionStage<T>>.toCffu(cffuFactory: CffuFactory): List<Cffu<T>> =
map { it.toCffu(cffuFactory) }

/**
* Wrap input [CompletableFuture]/[CompletionStage] array elements to [Cffu] by [CffuFactory.asCffu].
* Wrap input [CompletableFuture]/[CompletionStage] array elements to [Cffu] by [CffuFactory.toCffu].
*
* This method is the same as [CffuFactory.asCffuArray], providing this method is convenient for method chaining.
* This method is the same as [CffuFactory.toCffuArray], providing this method is convenient for method chaining.
*
* @see CffuFactory.asCffuArray
* @see CffuFactory.asCffu
* @see CffuFactory.toCffuArray
* @see CffuFactory.toCffu
*/
fun <T> Array<out CompletionStage<T>>.asCffu(cffuFactory: CffuFactory): Array<Cffu<T>> =
cffuFactory.asCffuArray(*this)
fun <T> Array<out CompletionStage<T>>.toCffu(cffuFactory: CffuFactory): Array<Cffu<T>> =
cffuFactory.toCffuArray(*this)

////////////////////////////////////////
// allOf* methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ val rte = RuntimeException("Bang")

class CffuExtensionsTest : FunSpec({
////////////////////////////////////////
// asCffu
// toCffu
////////////////////////////////////////

suspend fun checkAsCffu(cffu: Cffu<Int>, n: Int) {
suspend fun checkToCffu(cffu: Cffu<Int>, n: Int) {
cffu.await() shouldBe n

cffu.defaultExecutor() shouldBeSameInstanceAs testThreadPoolExecutor
Expand All @@ -45,31 +45,31 @@ class CffuExtensionsTest : FunSpec({
}
}

test("asCffu for CompletableFuture") {
test("toCffu for CompletableFuture") {
val cf = CompletableFuture.completedFuture(n)
checkAsCffu(cf.asCffu(testCffuFactory), n)
checkToCffu(cf.toCffu(testCffuFactory), n)
}

test("asCffu for CompletableFuture collection") {
test("toCffu for CompletableFuture collection") {
val range = 0 until 10
val cfs: List<CompletableFuture<Int>> = range.map {
CompletableFuture.completedFuture(it)
}

cfs.asCffu(testCffuFactory).forEachIndexed { index, cffu ->
checkAsCffu(cffu, index)
cfs.toCffu(testCffuFactory).forEachIndexed { index, cffu ->
checkToCffu(cffu, index)
}
cfs.toSet().asCffu(testCffuFactory).forEachIndexed { index, cffu ->
checkAsCffu(cffu, index)
cfs.toSet().toCffu(testCffuFactory).forEachIndexed { index, cffu ->
checkToCffu(cffu, index)
}
}

test("asCffu for CompletableFuture array") {
test("toCffu for CompletableFuture array") {
val cfArray: Array<CompletableFuture<Int>> = Array(10) { CompletableFuture.completedFuture(it) }
cfArray.asCffu(testCffuFactory).forEachIndexed { index, cffu -> checkAsCffu(cffu, index) }
cfArray.toCffu(testCffuFactory).forEachIndexed { index, cffu -> checkToCffu(cffu, index) }

val csArray: Array<CompletionStage<Int>> = Array(10) { CompletableFuture.completedFuture(it) }
csArray.asCffu(testCffuFactory).forEachIndexed { index, cffu -> checkAsCffu(cffu, index) }
csArray.toCffu(testCffuFactory).forEachIndexed { index, cffu -> checkToCffu(cffu, index) }
}

test("allOf*") {
Expand Down Expand Up @@ -460,7 +460,7 @@ class CffuExtensionsTest : FunSpec({
cfArray::class shouldNotBe csArray::class
cfArray shouldBe csArray // shouldBe ignore the array type!

val cffus: List<Cffu<Int>> = cfs.asCffu(testCffuFactory)
val cffus: List<Cffu<Int>> = cfs.toCffu(testCffuFactory)
cffus.toCompletableFuture() shouldBe cfs
cffus.toSet().toCompletableFuture() shouldBe cfs

Expand Down Expand Up @@ -493,7 +493,7 @@ class CffuExtensionsTest : FunSpec({
}
val cfArray = cfs.toTypedArray()

val cffus: List<Cffu<Int>> = cfs.asCffu(testCffuFactory)
val cffus: List<Cffu<Int>> = cfs.toCffu(testCffuFactory)
cffus.cffuUnwrap() shouldBe cfs
cffus.toSet().cffuUnwrap() shouldBe cfs

Expand Down

0 comments on commit 3f441c4

Please sign in to comment.