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 77dbddc
Show file tree
Hide file tree
Showing 21 changed files with 61 additions and 78 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
- `allOfFastFail`方法:有`CF`失败时快速返回,而不再等待所有`CF`运行完成(`allOf`
- `anyOfSuccess`方法:返回首个成功的`CF`结果,而不是首个完成(但可能失败)的`CF``anyOf`
- 更安全的使用方式,如
- 支持设置缺省的业务线程池(`CffuFactoryBuilder#newCffuFactoryBuilder(executor)`方法)
- 支持设置缺省的业务线程池(`CffuFactory#builder(executor)`方法)
- `join(timeout, unit)`方法:支持超时的`join`的方法
- 支持禁止强制篡改(`CffuFactoryBuilder#forbidObtrudeMethods`方法)
- 在类方法附加完善的代码质量注解(如`@NonNull``@Nullable``@CheckReturnValue``@Contract`等),在编码时`IDE`能尽早提示出问题
Expand Down Expand Up @@ -148,7 +148,7 @@
public class CffuDemo {
private static final ExecutorService myBizThreadPool = Executors.newCachedThreadPool();
// Create a CffuFactory with configuration of the customized thread pool
private static final CffuFactory cffuFactory = newCffuFactoryBuilder(myBizThreadPool).build();
private static final CffuFactory cffuFactory = CffuFactory.builder(myBizThreadPool).build();

public static void main(String[] args) throws Exception {
final Cffu<Integer> cf42 = cffuFactory
Expand Down Expand Up @@ -232,7 +232,7 @@ public class CompletableFutureUtilsDemo {
private val myBizThreadPool: ExecutorService = Executors.newCachedThreadPool()

// Create a CffuFactory with configuration of the customized thread pool
private val cffuFactory: CffuFactory = newCffuFactoryBuilder(myBizThreadPool).build()
private val cffuFactory: CffuFactory = CffuFactory.builder(myBizThreadPool).build()

fun main() {
val cf42 = cffuFactory
Expand Down Expand Up @@ -282,7 +282,7 @@ fun main() {
```java
public class AllResultsOfDemo {
public static final Executor myBizExecutor = Executors.newCachedThreadPool();
public static final CffuFactory cffuFactory = newCffuFactoryBuilder(myBizExecutor).build();
public static final CffuFactory cffuFactory = CffuFactory.builder(myBizExecutor).build();

public static void main(String[] args) throws Exception {
//////////////////////////////////////////////////
Expand Down Expand Up @@ -324,7 +324,7 @@ public class AllResultsOfDemo {
```java
public class AllTupleOfDemo {
public static final Executor myBizExecutor = Executors.newCachedThreadPool();
public static final CffuFactory cffuFactory = newCffuFactoryBuilder(myBizExecutor).build();
public static final CffuFactory cffuFactory = CffuFactory.builder(myBizExecutor).build();

public static void main(String[] args) throws Exception {
//////////////////////////////////////////////////
Expand Down Expand Up @@ -393,7 +393,7 @@ public class NoDefaultExecutorSettingForCompletableFuture {
```java
public class DefaultExecutorSettingForCffu {
public static final Executor myBizExecutor = Executors.newCachedThreadPool();
public static final CffuFactory cffuFactory = newCffuFactoryBuilder(myBizExecutor).build();
public static final CffuFactory 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 Expand Up @@ -439,7 +439,7 @@ public class DefaultExecutorSettingForCffu {
```java
public class ConcurrencyStrategyDemo {
public static final Executor myBizExecutor = Executors.newCachedThreadPool();
public static final CffuFactory cffuFactory = newCffuFactoryBuilder(myBizExecutor).build();
public static final CffuFactory cffuFactory = CffuFactory.builder(myBizExecutor).build();

public static void main(String[] args) throws Exception {
////////////////////////////////////////////////////////////////////////
Expand Down
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
23 changes: 17 additions & 6 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 @@ -621,7 +632,7 @@ public Executor delayedExecutor(long delay, TimeUnit unit, Executor executor) {
}

////////////////////////////////////////////////////////////////////////////////
//# Conversion (Static) Methods
//# Conversion Methods
//
// - cffuListToArray: List<Cffu> -> Cffu[]
// - completableFutureListToArray: List<CF> -> CF[]
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
13 changes: 6 additions & 7 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,6 @@
import java.util.Collections;
import java.util.concurrent.*;

import static io.foldright.cffu.CffuFactoryBuilder.newCffuFactoryBuilder;
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 +122,7 @@ void test_toCffu() throws Exception {
assertEquals(n, cf.get());
shouldNotBeMinimalStage(cf);

CffuFactory fac = newCffuFactoryBuilder(anotherExecutorService).forbidObtrudeMethods(true).build();
CffuFactory fac = CffuFactory.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 +786,14 @@ void test_getter() {
assertSame(executorService, cffuFactory.defaultExecutor());
assertFalse(cffuFactory.forbidObtrudeMethods());

CffuFactory fac = newCffuFactoryBuilder(anotherExecutorService).forbidObtrudeMethods(true).build();
CffuFactory fac = CffuFactory.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 = CffuFactory.builder(executorService).forbidObtrudeMethods(true).build();

Cffu<Object> cf = fac2.newIncompleteCffu();
try {
Expand All @@ -813,9 +812,9 @@ void test_forbidObtrudeMethods_property() {

@Test
void test_executorSetting_MayBe_ThreadPerTaskExecutor() throws Exception {
final boolean USE_COMMON_POOL = (ForkJoinPool.getCommonPoolParallelism() > 1);
final boolean USE_COMMON_POOL = ForkJoinPool.getCommonPoolParallelism() > 1;

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

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

@AfterAll
Expand Down
6 changes: 2 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 @@ -11,7 +11,6 @@
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import static io.foldright.cffu.CffuFactoryBuilder.newCffuFactoryBuilder;
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,9 +309,8 @@ void test_toString() {
static void beforeAll() {
executorService = TestThreadPoolManager.createThreadPool("CffuTest");

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

@AfterAll
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,10 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

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


public class AllResultsOfDemo {
public static final ExecutorService myBizExecutor = Executors.newCachedThreadPool();
public static final CffuFactory cffuFactory = newCffuFactoryBuilder(myBizExecutor).build();
public static final CffuFactory 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,10 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

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


public class AllTupleOfDemo {
public static final ExecutorService myBizExecutor = Executors.newCachedThreadPool();
public static final CffuFactory cffuFactory = newCffuFactoryBuilder(myBizExecutor).build();
public static final CffuFactory 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,10 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

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


public class ConcurrencyStrategyDemo {
public static final ExecutorService myBizExecutor = Executors.newCachedThreadPool();
public static final CffuFactory cffuFactory = newCffuFactoryBuilder(myBizExecutor).build();
public static final CffuFactory 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,10 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

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


public class DefaultExecutorSettingForCffu {
public static final ExecutorService myBizExecutor = Executors.newCachedThreadPool();
public static final CffuFactory cffuFactory = newCffuFactoryBuilder(myBizExecutor).build();
public static final CffuFactory 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,6 @@
package io.foldright.test_utils

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

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

val testForkJoinPoolExecutor: ExecutorService =
createThreadPool("CompletableFutureUseTest_ForkJoinPool", true)
Expand Down
Loading

0 comments on commit 77dbddc

Please sign in to comment.