Skip to content

Commit

Permalink
refactor: move CffuFactoryBuilder#newCffuFactoryBuilder(Executor) m…
Browse files Browse the repository at this point in the history
…ethod to `CffuFactory#builder(Executor)` 🏭
  • Loading branch information
oldratlee committed May 4, 2024
1 parent b6b8df0 commit ff821cb
Show file tree
Hide file tree
Showing 19 changed files with 64 additions and 61 deletions.
2 changes: 1 addition & 1 deletion cffu-core/src/main/java/io/foldright/cffu/Cffu.java
Original file line number Diff line number Diff line change
Expand Up @@ -1924,7 +1924,7 @@ public Cffu<T> copy() {
*
* @return the default executor
* @see CffuFactory#defaultExecutor()
* @see CffuFactoryBuilder#newCffuFactoryBuilder(Executor)
* @see CffuFactory#builder(Executor)
* @see #cffuFactory()
*/
@Contract(pure = true)
Expand Down
21 changes: 16 additions & 5 deletions cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* <p>
* The methods that equivalent to the instance methods of {@link CompletableFuture} is in {@link Cffu} class.
* <p>
* Use {@link CffuFactoryBuilder} to config and build {@link CffuFactory}.
* Use {@link #builder(Executor)} to config and build {@link CffuFactory}.
* <p>
* About factory methods conventions of {@link CffuFactory}:
* <ul>
Expand All @@ -39,7 +39,6 @@
* </ul>
*
* @author Jerry Lee (oldratlee at gmail dot com)
* @see CffuFactoryBuilder
* @see Cffu
* @see CompletableFuture
*/
Expand Down Expand Up @@ -67,6 +66,18 @@ private <T> Cffu<T> newMin(CompletableFuture<T> cf) {
return new Cffu<>(this, true, cf);
}

/**
* Returns a {@link CffuFactoryBuilder} with {@code defaultExecutor} setting.
*
* @see Cffu#defaultExecutor()
* @see CffuFactory#defaultExecutor()
*/
@Contract(pure = true)
public static CffuFactoryBuilder builder(Executor defaultExecutor) {
Executor executor = CffuFactoryBuilder.wrapExecutor(requireNonNull(defaultExecutor, "defaultExecutor is null"));
return new CffuFactoryBuilder(executor);
}

////////////////////////////////////////////////////////////////////////////////
//# Factory Methods, equivalent to same name static methods of CompletableFuture
//
Expand Down Expand Up @@ -250,7 +261,7 @@ public <T> Cffu<T> newIncompleteCffu() {
* <strong>NOTE</strong>, keep input stage unchanged if possible when wrap:<br>
* <ol>
* <li>if input stage is a {@link Cffu}, re-wrapped with the config of
* this {@link CffuFactory} from {@link CffuFactoryBuilder} by {@link Cffu#resetCffuFactory(CffuFactory)}.
* this {@link CffuFactory} by {@link Cffu#resetCffuFactory(CffuFactory)}.
* <li>if input stage is a CompletableFuture, wrap it by setting it as the underlying cf of returned cffu.
* <li>otherwise use input {@code stage.toCompletableFuture} as the underlying cf of returned cffu.
* </ol>
Expand Down Expand Up @@ -659,11 +670,11 @@ public static <T> Cffu<T>[] cffuListToArray(List<Cffu<T>> cffuList) {

/**
* Returns the default Executor used for async methods that do not specify an Executor.
* Configured by {@link CffuFactoryBuilder#newCffuFactoryBuilder(Executor)}.
* Configured by {@link CffuFactory#builder(Executor)}.
*
* @return the default executor
* @see Cffu#defaultExecutor()
* @see CffuFactoryBuilder#newCffuFactoryBuilder(Executor)
* @see CffuFactory#builder(Executor)
*/
@Contract(pure = true)
public Executor defaultExecutor() {
Expand Down
18 changes: 3 additions & 15 deletions cffu-core/src/main/java/io/foldright/cffu/CffuFactoryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


/**
* {@link CffuFactoryBuilder} is the builder of {@link CffuFactory}.
* {@link CffuFactoryBuilder} is the builder of {@link CffuFactory}, creates by {@link CffuFactory#builder(Executor)}.
*
* @author Jerry Lee (oldratlee at gmail dot com)
* @see CffuFactory
Expand All @@ -33,26 +33,14 @@ public final class CffuFactoryBuilder {

private volatile boolean forbidObtrudeMethods = false;

private CffuFactoryBuilder(Executor defaultExecutor) {
CffuFactoryBuilder(Executor defaultExecutor) {
this.defaultExecutor = defaultExecutor;
}

////////////////////////////////////////////////////////////////////////////////
// Builder Methods
////////////////////////////////////////////////////////////////////////////////

/**
* Returns a {@link CffuFactoryBuilder} with {@code defaultExecutor} setting.
*
* @see CffuFactory#defaultExecutor()
* @see Cffu#defaultExecutor()
*/
@Contract(pure = true)
public static CffuFactoryBuilder newCffuFactoryBuilder(Executor defaultExecutor) {
Executor executor = wrapExecutor(requireNonNull(defaultExecutor, "defaultExecutor is null"));
return new CffuFactoryBuilder(executor);
}

/**
* Sets {@code forbidObtrudeMethods} or not.
*
Expand All @@ -75,7 +63,7 @@ public CffuFactory build() {
return new CffuFactory(defaultExecutor, forbidObtrudeMethods);
}

private static Executor wrapExecutor(Executor executor) {
static Executor wrapExecutor(Executor executor) {
for (ExecutorWrapperProvider provider : EXECUTOR_WRAPPER_PROVIDERS) {
Supplier<String> msg = () -> provider + "(class: " + provider.getClass().getName() + ") return null";
executor = requireNonNull(provider.wrap(executor), msg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* An SPI for wrapping the executor when {@link CffuFactoryBuilder}
* {@link CffuFactoryBuilder#build() build} {@link CffuFactory}.
*
* @see CffuFactoryBuilder#newCffuFactoryBuilder(Executor)
* @see CffuFactory#builder(Executor)
* @see CffuFactoryBuilder#build()
*/
@FunctionalInterface
Expand Down
12 changes: 6 additions & 6 deletions cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.Collections;
import java.util.concurrent.*;

import static io.foldright.cffu.CffuFactoryBuilder.newCffuFactoryBuilder;
import static io.foldright.cffu.CffuFactory.builder;
import static io.foldright.cffu.CompletableFutureUtils.failedFuture;
import static io.foldright.cffu.CompletableFutureUtils.toCompletableFutureArray;
import static io.foldright.test_utils.TestUtils.*;
Expand Down Expand Up @@ -123,7 +123,7 @@ void test_toCffu() throws Exception {
assertEquals(n, cf.get());
shouldNotBeMinimalStage(cf);

CffuFactory fac = newCffuFactoryBuilder(anotherExecutorService).forbidObtrudeMethods(true).build();
CffuFactory fac = builder(anotherExecutorService).forbidObtrudeMethods(true).build();
Cffu<Integer> cffu = fac.toCffu(cffuFactory.completedFuture(42));
assertSame(anotherExecutorService, cffu.defaultExecutor());
assertSame(fac, cffu.cffuFactory());
Expand Down Expand Up @@ -787,14 +787,14 @@ void test_getter() {
assertSame(executorService, cffuFactory.defaultExecutor());
assertFalse(cffuFactory.forbidObtrudeMethods());

CffuFactory fac = newCffuFactoryBuilder(anotherExecutorService).forbidObtrudeMethods(true).build();
CffuFactory fac = builder(anotherExecutorService).forbidObtrudeMethods(true).build();
assertSame(anotherExecutorService, fac.defaultExecutor());
assertTrue(fac.forbidObtrudeMethods());
}

@Test
void test_forbidObtrudeMethods_property() {
CffuFactory fac2 = newCffuFactoryBuilder(executorService).forbidObtrudeMethods(true).build();
CffuFactory fac2 = builder(executorService).forbidObtrudeMethods(true).build();

Cffu<Object> cf = fac2.newIncompleteCffu();
try {
Expand All @@ -815,7 +815,7 @@ void test_forbidObtrudeMethods_property() {
void test_executorSetting_MayBe_ThreadPerTaskExecutor() throws Exception {
final boolean USE_COMMON_POOL = (ForkJoinPool.getCommonPoolParallelism() > 1);

CffuFactory fac = newCffuFactoryBuilder(commonPool()).build();
CffuFactory fac = builder(commonPool()).build();
if (USE_COMMON_POOL) {
assertSame(commonPool(), fac.defaultExecutor());
} else {
Expand All @@ -841,7 +841,7 @@ static void beforeAll() {
executorService = TestThreadPoolManager.createThreadPool("CffuFactoryTest");
anotherExecutorService = TestThreadPoolManager.createThreadPool("CffuFactoryTest-Another", true);

cffuFactory = newCffuFactoryBuilder(executorService).build();
cffuFactory = builder(executorService).build();
}

@AfterAll
Expand Down
6 changes: 3 additions & 3 deletions cffu-core/src/test/java/io/foldright/cffu/CffuTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import static io.foldright.cffu.CffuFactoryBuilder.newCffuFactoryBuilder;
import static io.foldright.cffu.CffuFactory.builder;
import static io.foldright.test_utils.TestUtils.*;
import static java.util.function.Function.identity;
import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -310,8 +310,8 @@ void test_toString() {
static void beforeAll() {
executorService = TestThreadPoolManager.createThreadPool("CffuTest");

cffuFactory = newCffuFactoryBuilder(executorService).build();
forbidObtrudeMethodsCffuFactory = newCffuFactoryBuilder(executorService)
cffuFactory = builder(executorService).build();
forbidObtrudeMethodsCffuFactory = builder(executorService)
.forbidObtrudeMethods(true).build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package io.foldright.cffu.spi

import io.foldright.cffu.CffuFactoryBuilder
import io.foldright.cffu.CffuFactory
import io.foldright.test_utils.testThreadPoolExecutor
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.types.shouldBeSameInstanceAs
import io.kotest.matchers.types.shouldNotBeSameInstanceAs

class ExecutorWrapperProviderTest : FunSpec({
test("disable TestExecutorWrapper") {
val factory = CffuFactoryBuilder.newCffuFactoryBuilder(testThreadPoolExecutor).build()
val factory = CffuFactory.builder(testThreadPoolExecutor).build()
val cffu = factory.runAsync {}
cffu.defaultExecutor().shouldBeSameInstanceAs(testThreadPoolExecutor)
}

test("enable TestExecutorWrapper") {
enableTestExecutorWrapper()

val factory = CffuFactoryBuilder.newCffuFactoryBuilder(testThreadPoolExecutor).build()
val factory = CffuFactory.builder(testThreadPoolExecutor).build()
val cffu = factory.runAsync {}
cffu.defaultExecutor().shouldNotBeSameInstanceAs(testThreadPoolExecutor)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import io.foldright.cffu.Cffu;
import io.foldright.cffu.CffuFactory;
import io.foldright.cffu.CffuFactoryBuilder;
import io.foldright.test_utils.TestThreadPoolManager;
import io.foldright.test_utils.TestUtils;
import org.junit.jupiter.api.AfterAll;
Expand Down Expand Up @@ -881,7 +880,7 @@ private <T> void check1MoreLevelForExecutionException(Cffu<T> cf) throws Excepti
@BeforeAll
static void beforeAll() {
executorService = TestThreadPoolManager.createThreadPool(hello);
cffuFactory = CffuFactoryBuilder.newCffuFactoryBuilder(executorService).build();
cffuFactory = CffuFactory.builder(executorService).build();

anotherExecutorService = TestThreadPoolManager.createThreadPool(hello, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static io.foldright.cffu.CffuFactoryBuilder.newCffuFactoryBuilder;
import static io.foldright.cffu.CffuFactory.builder;


public class AllResultsOfDemo {
public static final ExecutorService myBizExecutor = Executors.newCachedThreadPool();
public static final CffuFactory cffuFactory = newCffuFactoryBuilder(myBizExecutor).build();
public static final CffuFactory cffuFactory = builder(myBizExecutor).build();

public static void main(String[] args) throws Exception {
//////////////////////////////////////////////////
Expand Down
4 changes: 2 additions & 2 deletions cffu-core/src/test/java/io/foldright/demo/AllTupleOfDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static io.foldright.cffu.CffuFactoryBuilder.newCffuFactoryBuilder;
import static io.foldright.cffu.CffuFactory.builder;


public class AllTupleOfDemo {
public static final ExecutorService myBizExecutor = Executors.newCachedThreadPool();
public static final CffuFactory cffuFactory = newCffuFactoryBuilder(myBizExecutor).build();
public static final CffuFactory cffuFactory = builder(myBizExecutor).build();

public static void main(String[] args) throws Exception {
//////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static io.foldright.cffu.CffuFactoryBuilder.newCffuFactoryBuilder;
import static io.foldright.cffu.CffuFactory.builder;


public class ConcurrencyStrategyDemo {
public static final ExecutorService myBizExecutor = Executors.newCachedThreadPool();
public static final CffuFactory cffuFactory = newCffuFactoryBuilder(myBizExecutor).build();
public static final CffuFactory cffuFactory = builder(myBizExecutor).build();

public static void main(String[] args) throws Exception {
////////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static io.foldright.cffu.CffuFactoryBuilder.newCffuFactoryBuilder;
import static io.foldright.cffu.CffuFactory.builder;


public class DefaultExecutorSettingForCffu {
public static final ExecutorService myBizExecutor = Executors.newCachedThreadPool();
public static final CffuFactory cffuFactory = newCffuFactoryBuilder(myBizExecutor).build();
public static final CffuFactory cffuFactory = builder(myBizExecutor).build();

public static void main(String[] args) {
Cffu<Void> cf1 = cffuFactory.runAsync(() -> System.out.println("doing a long time work!"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package io.foldright.test_utils

import io.foldright.cffu.CffuFactory
import io.foldright.cffu.CffuFactoryBuilder.newCffuFactoryBuilder
import io.foldright.cffu.CffuFactory.builder
import io.kotest.core.annotation.AutoScan
import io.kotest.core.listeners.AfterProjectListener
import io.kotest.core.listeners.BeforeProjectListener
Expand Down Expand Up @@ -93,7 +93,9 @@ fun shutdownExecutorService(vararg executors: ExecutorService) {
val testThreadPoolExecutor: ExecutorService =
createThreadPool("CompletableFutureUseTest_ThreadPool")

val testCffuFactory: CffuFactory = newCffuFactoryBuilder(testThreadPoolExecutor).build()
val testCffuFactory: CffuFactory = builder(
testThreadPoolExecutor
).build()

val testForkJoinPoolExecutor: ExecutorService =
createThreadPool("CompletableFutureUseTest_ForkJoinPool", true)
Expand Down
8 changes: 4 additions & 4 deletions cffu-core/src/test/java/io/foldright/test_utils/TestUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package io.foldright.test_utils

import io.foldright.cffu.Cffu
import io.foldright.cffu.CffuFactoryBuilder.newCffuFactoryBuilder
import io.foldright.cffu.CffuFactory.builder
import io.kotest.assertions.throwables.shouldNotThrow
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.test.TestCase
Expand Down Expand Up @@ -75,7 +75,7 @@ fun <T> safeNewFailedCompletableFuture(executorService: ExecutorService, t: Thro
* safe means works under java 8
*/
fun <T> safeNewFailedCffu(executorService: ExecutorService, t: Throwable): Cffu<T> {
return newCffuFactoryBuilder(executorService).build().failedFuture(t)
return builder(executorService).build().failedFuture(t)
}

@Suppress("UNUSED_PARAMETER")
Expand Down Expand Up @@ -413,7 +413,7 @@ private fun <T> Cffu<T>.shouldMinCffu(recursive: Boolean = false) {

//# Cffu Re-Config methods
if (recursive)
resetCffuFactory(newCffuFactoryBuilder(blackHoleExecutor).build()).shouldMinCffu()
resetCffuFactory(builder(blackHoleExecutor).build()).shouldMinCffu()

//# Getter methods of properties
cffuFactory()
Expand Down Expand Up @@ -508,7 +508,7 @@ private fun <T> Cffu<T>.shouldNotMinCffu(recursive: Boolean = false) {

//# Cffu Re-Config methods
if (recursive)
resetCffuFactory(newCffuFactoryBuilder(blackHoleExecutor).build()).shouldNotMinCffu()
resetCffuFactory(builder(blackHoleExecutor).build()).shouldNotMinCffu()

//# Getter methods of properties
cffuFactory()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,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.CffuFactory.builder
import io.foldright.cffu.CompletableFutureUtils
import java.util.concurrent.CompletableFuture
import java.util.concurrent.CompletionStage
Expand Down Expand Up @@ -62,7 +62,7 @@ fun <T> Array<out CompletionStage<T>>.toCffu(cffuFactory: CffuFactory): Array<Cf
/**
* Placeholder for optional [CffuFactory] argument.
*/
private val ABSENT: CffuFactory = newCffuFactoryBuilder { }.build()
private val ABSENT: CffuFactory = builder { }.build()
private const val ERROR_MSG_FOR_COLL = "no cffuFactory argument provided when this collection is empty"
private const val ERROR_MSG_FOR_ARRAY = "no cffuFactory argument provided when this array is empty"

Expand Down
Loading

0 comments on commit ff821cb

Please sign in to comment.