Skip to content

Commit

Permalink
新增CffuFactory中的tupleMApplyMostSuccessAsync方法实现
Browse files Browse the repository at this point in the history
  • Loading branch information
huhaosumail authored and oldratlee committed Jun 24, 2024
1 parent 46c1d99 commit 15929f9
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
60 changes: 60 additions & 0 deletions cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,66 @@ public <T1, T2, T3, T4, T5> Cffu<Tuple5<T1, T2, T3, T4, T5>> tupleMSupplyFastFai
return create(CompletableFutureUtils.tupleMSupplyFastFailAsync(supplier1, supplier2, supplier3, supplier4, supplier5));
}

/**
* Returns a new Cffu that is asynchronously completed
* by tasks running in the {@link #defaultExecutor()} with the values obtained by calling the given Suppliers
* in the <strong>same order</strong> of the given Suppliers arguments.
* <p>
* This method is the same as {@link #tupleMSupplyAsync(Supplier, Supplier)} except for the most-success behavior.
*
* @return the new Cffu
*/
@Contract(pure = true)
public <T1,T2> Cffu<Tuple2<T1,T2>> tupleMSupplyMostSuccessAsync(
long timeout, TimeUnit unit, Supplier<? extends T1> supplier1, Supplier<? extends T2> supplier2) {
return create(CompletableFutureUtils.tupleMSupplyMostSuccessAsync(defaultExecutor, timeout, unit,supplier1, supplier2));
}

/**
* Returns a new Cffu that is asynchronously completed
* by tasks running in the {@link #defaultExecutor()} with the values obtained by calling the given Suppliers
* in the <strong>same order</strong> of the given Suppliers arguments.
* <p>
* This method is the same as {@link #tupleMSupplyAsync(Supplier, Supplier)} except for the most-success behavior.
*
* @return the new Cffu
*/
@Contract(pure = true)
public <T1,T2,T3> Cffu<Tuple3<T1,T2,T3>> tupleMSupplyMostSuccessAsync(
long timeout, TimeUnit unit, Supplier<? extends T1> supplier1, Supplier<? extends T2> supplier2, Supplier<? extends T3> supplier3) {
return create(CompletableFutureUtils.tupleMSupplyMostSuccessAsync(defaultExecutor, timeout, unit,supplier1,supplier2,supplier3));
}

/**
* Returns a new Cffu that is asynchronously completed
* by tasks running in the {@link #defaultExecutor()} with the values obtained by calling the given Suppliers
* in the <strong>same order</strong> of the given Suppliers arguments.
* <p>
* This method is the same as {@link #tupleMSupplyAsync(Supplier, Supplier)} except for the most-success behavior.
*
* @return the new Cffu
*/
@Contract(pure = true)
public <T1,T2,T3,T4> Cffu<Tuple4<T1,T2,T3,T4>> tupleMSupplyMostSuccessAsync(
long timeout, TimeUnit unit, Supplier<? extends T1> supplier1, Supplier<? extends T2> supplier2, Supplier<? extends T3> supplier3, Supplier<? extends T4> supplier4) {
return create(CompletableFutureUtils.tupleMSupplyMostSuccessAsync(defaultExecutor, timeout, unit,supplier1,supplier2,supplier3,supplier4));
}

/**
* Returns a new Cffu that is asynchronously completed
* by tasks running in the {@link #defaultExecutor()} with the values obtained by calling the given Suppliers
* in the <strong>same order</strong> of the given Suppliers arguments.
* <p>
* This method is the same as {@link #tupleMSupplyAsync(Supplier, Supplier)} except for the most-success behavior.
*
* @return the new Cffu
*/
@Contract(pure = true)
public <T1,T2,T3,T4,T5> Cffu<Tuple5<T1,T2,T3,T4,T5>> tupleMSupplyMostSuccessAsync(
long timeout, TimeUnit unit, Supplier<? extends T1> supplier1, Supplier<? extends T2> supplier2, Supplier<? extends T3> supplier3, Supplier<? extends T4> supplier4, Supplier<? extends T5> supplier5) {
return create(CompletableFutureUtils.tupleMSupplyMostSuccessAsync(defaultExecutor, timeout, unit,supplier1,supplier2,supplier3,supplier4,supplier5));
}

/**
* Returns a new Cffu that is asynchronously completed
* by tasks running in the {@link #defaultExecutor()} with the values obtained by calling the given Suppliers
Expand Down
33 changes: 32 additions & 1 deletion 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 java.util.function.Supplier;

import static io.foldright.cffu.CompletableFutureUtils.failedFuture;
Expand Down Expand Up @@ -662,6 +661,38 @@ void test_tupleMSupplyAsync() throws Exception {
assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.tupleMSupplyFastFailAsync(supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get());
}

@Test
void test_tupleMSupplyMostSuccessAsync() throws Exception {
final Supplier<Integer> supplier_n = () -> {
sleep(100);
return n;
};
final Supplier<String> supplier_s = () -> {
sleep(100);
return s;
};

final Supplier<Double> supplier_d = () -> {
sleep(100);
return d;
};
final Supplier<Integer> supplier_an = () -> {
sleep(100);
return anotherN;
};
final Supplier<Integer> supplier_nn = () -> {
sleep(100);
return n+n;
};
assertEquals(Tuple2.of(n, s), cffuFactory.tupleMSupplyMostSuccessAsync(10, TimeUnit.MILLISECONDS,supplier_n, supplier_s).get());

assertEquals(Tuple3.of(n, s, d), cffuFactory.tupleMSupplyMostSuccessAsync(10, TimeUnit.MILLISECONDS,supplier_n, supplier_s, supplier_d).get());

assertEquals(Tuple4.of(n, s, d, anotherN), cffuFactory.tupleMSupplyMostSuccessAsync(10, TimeUnit.MILLISECONDS,supplier_n, supplier_s, supplier_d, supplier_an).get());

assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.tupleMSupplyMostSuccessAsync(10, TimeUnit.MILLISECONDS,supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get());
}

////////////////////////////////////////////////////////////////////////////////
//# Conversion (Static) Methods
//
Expand Down

0 comments on commit 15929f9

Please sign in to comment.