Skip to content

Commit

Permalink
feat: add resetDefaultExecutor method to CffuFactory/Cffu🏭
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Jul 27, 2024
1 parent 127c439 commit 68491d2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
13 changes: 9 additions & 4 deletions cffu-core/src/main/java/io/foldright/cffu/Cffu.java
Original file line number Diff line number Diff line change
Expand Up @@ -2722,10 +2722,15 @@ public CompletionStage<T> minimalCompletionStage() {
}

/**
* Returns a new Cffu with the given CffuFactory(contained configuration)
* that is completed normally with the same value as this Cffu when it completes normally.
* If this Cffu completes exceptionally, then the returned Cffu completes exceptionally
* with a CompletionException with this exception as cause.
* Returns a new Cffu with the given defaultExecutor.
*/
@Contract(pure = true)
public Cffu<T> resetDefaultExecutor(Executor defaultExecutor) {
return new Cffu<>(fac.resetDefaultExecutor(defaultExecutor), isMinimalStage, cf);
}

/**
* Returns a new Cffu with the given CffuFactory(contained configuration).
* <p>
* demo code about re-config methods of Cffu:
*
Expand Down
14 changes: 11 additions & 3 deletions cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ public static CffuFactoryBuilder builder(Executor defaultExecutor) {
return new CffuFactoryBuilder(CompletableFutureUtils.screenExecutor(defaultExecutor));
}

/**
* Returns a new CffuFactory from this CffuFactory that reset the defaultExecutor.
*/
@Contract(pure = true)
CffuFactory resetDefaultExecutor(Executor defaultExecutor) {
return new CffuFactory(defaultExecutor, forbidObtrudeMethods);
}

@Contract(pure = true)
private <T> Cffu<T> create(CompletableFuture<T> cf) {
return new Cffu<>(this, false, cf);
Expand Down Expand Up @@ -1050,9 +1058,9 @@ public final <T> Cffu<List<T>> allSuccessResultsOf(
* If the given stage is successful, its result is the completed value; Otherwise the given valueIfNotSuccess.
*
* @param valueIfNotSuccess the value to return if not completed successfully
* @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 cfs the stages
* @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 cfs the stages
* @see Cffu#getSuccessNow(Object)
*/
@Contract(pure = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,10 @@ void test_getter() {
CffuFactory fac = CffuFactory.builder(anotherExecutorService).forbidObtrudeMethods(true).build();
assertSame(anotherExecutorService, fac.defaultExecutor());
assertTrue(fac.forbidObtrudeMethods());

final CffuFactory fac2 = cffuFactory.resetDefaultExecutor(anotherExecutorService);
assertSame(anotherExecutorService, fac2.defaultExecutor());
assertEquals(cffuFactory.forbidObtrudeMethods(), fac2.forbidObtrudeMethods());
}

@Test
Expand Down
5 changes: 5 additions & 0 deletions cffu-core/src/test/java/io/foldright/cffu/CffuTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,11 @@ void test_resetCffuFactory() {
assertSame(cffuFactory, cf.cffuFactory());

assertSame(forbidObtrudeMethodsCffuFactory, cf.resetCffuFactory(forbidObtrudeMethodsCffuFactory).cffuFactory());

Executor executor = Runnable::run;
final Cffu<Integer> f2 = cf.resetDefaultExecutor(executor);
assertSame(executor, f2.defaultExecutor());
assertEquals(cffuFactory.forbidObtrudeMethods(), f2.cffuFactory().forbidObtrudeMethods());
}

// endregion
Expand Down

0 comments on commit 68491d2

Please sign in to comment.