Skip to content

Commit

Permalink
refactor: remove tuple* methods in Cffu/`CompletableFutureExtensi…
Browse files Browse the repository at this point in the history
…ons.kt` 🧹 ⚠️
  • Loading branch information
oldratlee committed May 25, 2024
1 parent 0b38c82 commit 077b4aa
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 680 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,7 @@ public class AllTupleOfDemo {
Cffu<String> cffu1 = cffuFactory.completedFuture("21");
Cffu<Integer> cffu2 = cffuFactory.completedFuture(42);

Cffu<Tuple2<String, Integer>> allTuple = cffu1.allTupleOf(cffu2);
// or: cffuFactory.allTupleOf(cffu1, cffu2);
Cffu<Tuple2<String, Integer>> allTuple = cffuFactory.allTupleOf(cffu1, cffu2);
System.out.println(allTuple.get());

//////////////////////////////////////////////////
Expand Down
180 changes: 0 additions & 180 deletions cffu-core/src/main/java/io/foldright/cffu/Cffu.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package io.foldright.cffu;

import edu.umd.cs.findbugs.annotations.*;
import io.foldright.cffu.tuple.Tuple2;
import io.foldright.cffu.tuple.Tuple3;
import io.foldright.cffu.tuple.Tuple4;
import io.foldright.cffu.tuple.Tuple5;
import org.jetbrains.annotations.Blocking;
import org.jetbrains.annotations.Contract;

Expand Down Expand Up @@ -562,182 +558,6 @@ public <U, V> Cffu<V> thenCombineFastFailAsync(CompletionStage<? extends U> othe
return reset0(CompletableFutureUtils.thenCombineFastFailAsync(this, other, fn, executor));
}

////////////////////////////////////////////////////////////////////////////////
//# convenient `allTupleOf` methods:
// providing these method is convenient for method chaining
//
// - allTupleOf(CompletionStage...)
// - allTupleOfFastFail(CompletionStage...)
////////////////////////////////////////////////////////////////////////////////

/**
* This method is the same as {@link CffuFactory#allTupleOf(CompletionStage, CompletionStage)},
* providing this method is convenient for method chaining.
* <p>
* Calling this method
* <p>
* {@code allTuple = cffu.allTupleOf(cf2);}
* <p>
* is the same as:
* <p>
* {@code allTuple = cffu.cffuFactory().allTupleOf(cffu, cf2);}
*
* @return the new Cffu
* @see CffuFactory#allTupleOf(CompletionStage, CompletionStage)
*/
@Contract(pure = true)
public <T2> Cffu<Tuple2<T, T2>> allTupleOf(CompletionStage<? extends T2> cf2) {
return fac.allTupleOf(cf, cf2);
}

/**
* This method is the same as {@link CffuFactory#allTupleOfFastFail(CompletionStage, CompletionStage)},
* providing this method is convenient for method chaining.
* <p>
* Calling this method
* <p>
* {@code allTuple = cffu.allTupleOfFastFail(cf2);}
* <p>
* is the same as:
* <p>
* {@code allTuple = cffu.cffuFactory().allTupleOfFastFail(cffu, cf2);}
*
* @return the new Cffu
* @see CffuFactory#allTupleOfFastFail(CompletionStage, CompletionStage)
*/
@Contract(pure = true)
public <T2> Cffu<Tuple2<T, T2>> allTupleOfFastFail(CompletionStage<? extends T2> cf2) {
return fac.allTupleOfFastFail(cf, cf2);
}

/**
* This method is the same as {@link CffuFactory#allTupleOf(CompletionStage, CompletionStage, CompletionStage)},
* providing this method is convenient for method chaining.
* <p>
* Calling this method
* <p>
* {@code allTuple = cffu.allTupleOf(cf2, cf3);}
* <p>
* is the same as:
* <p>
* {@code allTuple = cffu.cffuFactory().allTupleOf(cffu, cf2, cf3);}
*
* @return the new Cffu
* @see CffuFactory#allTupleOf(CompletionStage, CompletionStage, CompletionStage)
*/
@Contract(pure = true)
public <T2, T3> Cffu<Tuple3<T, T2, T3>> allTupleOf(
CompletionStage<? extends T2> cf2, CompletionStage<? extends T3> cf3) {
return fac.allTupleOf(cf, cf2, cf3);
}

/**
* This method is the same as {@link CffuFactory#allTupleOfFastFail(CompletionStage, CompletionStage, CompletionStage)},
* providing this method is convenient for method chaining.
* <p>
* Calling this method
* <p>
* {@code allTuple = cffu.allTupleOfFastFail(cf2, cf3);}
* <p>
* is the same as:
* <p>
* {@code allTuple = cffu.cffuFactory().allTupleOfFastFail(cffu, cf2, cf3);}
*
* @return the new Cffu
* @see CffuFactory#allTupleOfFastFail(CompletionStage, CompletionStage, CompletionStage)
*/
@Contract(pure = true)
public <T2, T3> Cffu<Tuple3<T, T2, T3>> allTupleOfFastFail(
CompletionStage<? extends T2> cf2, CompletionStage<? extends T3> cf3) {
return fac.allTupleOfFastFail(cf, cf2, cf3);
}

/**
* This method is the same as {@link CffuFactory#allTupleOf(CompletionStage, CompletionStage, CompletionStage, CompletionStage)},
* providing this method is convenient for method chaining.
* <p>
* Calling this method
* <p>
* {@code allTuple = cffu.allTupleOf(cf2, cf3, cf4);}
* <p>
* is the same as:
* <p>
* {@code allTuple = cffu.cffuFactory().allTupleOf(cffu, cf2, cf3, cf4);}
*
* @return the new Cffu
* @see CffuFactory#allTupleOf(CompletionStage, CompletionStage, CompletionStage, CompletionStage)
*/
@Contract(pure = true)
public <T2, T3, T4> Cffu<Tuple4<T, T2, T3, T4>> allTupleOf(
CompletionStage<? extends T2> cf2, CompletionStage<? extends T3> cf3, CompletionStage<? extends T4> cf4) {
return fac.allTupleOf(cf, cf2, cf3, cf4);
}

/**
* This method is the same as {@link CffuFactory#allTupleOfFastFail(CompletionStage, CompletionStage, CompletionStage, CompletionStage)},
* providing this method is convenient for method chaining.
* <p>
* Calling this method
* <p>
* {@code allTuple = cffu.allTupleOfFastFail(cf2, cf3, cf4);}
* <p>
* is the same as:
* <p>
* {@code allTuple = cffu.cffuFactory().allTupleOfFastFail(cffu, cf2, cf3, cf4);}
*
* @return the new Cffu
* @see CffuFactory#allTupleOfFastFail(CompletionStage, CompletionStage, CompletionStage, CompletionStage)
*/
@Contract(pure = true)
public <T2, T3, T4> Cffu<Tuple4<T, T2, T3, T4>> allTupleOfFastFail(
CompletionStage<? extends T2> cf2, CompletionStage<? extends T3> cf3, CompletionStage<? extends T4> cf4) {
return fac.allTupleOfFastFail(cf, cf2, cf3, cf4);
}

/**
* This method is the same as {@link CffuFactory#allTupleOf(CompletionStage, CompletionStage, CompletionStage, CompletionStage, CompletionStage)},
* providing this method is convenient for method chaining.
* <p>
* Calling this method
* <p>
* {@code allTuple = cffu.allTupleOf(cf2, cf3, cf4, cf5);}
* <p>
* is the same as:
* <p>
* {@code allTuple = cffu.cffuFactory().allTupleOf(cffu, cf2, cf3, cf4, cf5);}
*
* @return the new Cffu
* @see CffuFactory#allTupleOf(CompletionStage, CompletionStage, CompletionStage, CompletionStage, CompletionStage)
*/
@Contract(pure = true)
public <T2, T3, T4, T5> Cffu<Tuple5<T, T2, T3, T4, T5>> allTupleOf(
CompletionStage<? extends T2> cf2, CompletionStage<? extends T3> cf3,
CompletionStage<? extends T4> cf4, CompletionStage<? extends T5> cf5) {
return fac.allTupleOf(cf, cf2, cf3, cf4, cf5);
}

/**
* This method is the same as {@link CffuFactory#allTupleOfFastFail(CompletionStage, CompletionStage, CompletionStage, CompletionStage, CompletionStage)},
* providing this method is convenient for method chaining.
* <p>
* Calling this method
* <p>
* {@code allTuple = cffu.allTupleOfFastFail(cf2, cf3, cf4, cf5);}
* <p>
* is the same as:
* <p>
* {@code allTuple = cffu.cffuFactory().allTupleOfFastFail(cffu, cf2, cf3, cf4, cf5);}
*
* @return the new Cffu
* @see CffuFactory#allTupleOfFastFail(CompletionStage, CompletionStage, CompletionStage, CompletionStage, CompletionStage)
*/
@Contract(pure = true)
public <T2, T3, T4, T5> Cffu<Tuple5<T, T2, T3, T4, T5>> allTupleOfFastFail(
CompletionStage<? extends T2> cf2, CompletionStage<? extends T3> cf3,
CompletionStage<? extends T4> cf4, CompletionStage<? extends T5> cf5) {
return fac.allTupleOfFastFail(cf, cf2, cf3, cf4, cf5);
}

////////////////////////////////////////////////////////////////////////////////
//# `then either(binary input)` methods of CompletionStage:
//
Expand Down
99 changes: 2 additions & 97 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,8 @@
import java.util.Collections;
import java.util.concurrent.*;

import static io.foldright.cffu.CompletableFutureUtils.*;
import static io.foldright.cffu.CompletableFutureUtils.failedFuture;
import static io.foldright.cffu.CompletableFutureUtils.toCompletableFutureArray;
import static io.foldright.test_utils.TestUtils.*;
import static java.util.concurrent.CompletableFuture.completedFuture;
import static java.util.concurrent.ForkJoinPool.commonPool;
Expand Down Expand Up @@ -514,54 +515,6 @@ void test_allTupleOf() throws Exception {
cffuFactory.completedFuture(anotherN),
cffuFactory.completedFuture(n + n)
).get());

////////////////////////////////////////////////////////////////////////////////

assertEquals(Tuple2.of(n, s), cffuFactory.completedFuture(n).allTupleOf(
completedFuture(s)
).get());

assertEquals(Tuple3.of(n, s, d), cffuFactory.completedFuture(n).allTupleOf(
completedFuture(s),
completedFuture(d)
).get());

assertEquals(Tuple4.of(n, s, d, anotherN), cffuFactory.completedFuture(n).allTupleOf(
completedFuture(s),
completedFuture(d),
completedFuture(anotherN)
).get());

assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.completedFuture(n).allTupleOf(
completedFuture(s),
completedFuture(d),
completedFuture(anotherN),
completedFuture(n + n)
).get());

////////////////////////////////////////////////////////////////////////////////

assertEquals(Tuple2.of(n, s), cffuFactory.completedFuture(n).allTupleOf(
cffuFactory.completedFuture(s)
).get());

assertEquals(Tuple3.of(n, s, d), cffuFactory.completedFuture(n).allTupleOf(
cffuFactory.completedFuture(s),
cffuFactory.completedFuture(d)
).get());

assertEquals(Tuple4.of(n, s, d, anotherN), cffuFactory.completedFuture(n).allTupleOf(
cffuFactory.completedFuture(s),
cffuFactory.completedFuture(d),
cffuFactory.completedFuture(anotherN)
).get());

assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.completedFuture(n).allTupleOf(
cffuFactory.completedFuture(s),
cffuFactory.completedFuture(d),
cffuFactory.completedFuture(anotherN),
cffuFactory.completedFuture(n + n)
).get());
}

@Test
Expand Down Expand Up @@ -652,54 +605,6 @@ void test_allTupleOfFastFail() throws Exception {
cffuFactory.completedFuture(anotherN),
cffuFactory.completedFuture(n + n)
).get());

////////////////////////////////////////////////////////////////////////////////

assertEquals(Tuple2.of(n, s), cffuFactory.completedFuture(n).allTupleOfFastFail(
completedFuture(s)
).get());

assertEquals(Tuple3.of(n, s, d), cffuFactory.completedFuture(n).allTupleOfFastFail(
completedFuture(s),
completedFuture(d)
).get());

assertEquals(Tuple4.of(n, s, d, anotherN), cffuFactory.completedFuture(n).allTupleOfFastFail(
completedFuture(s),
completedFuture(d),
completedFuture(anotherN)
).get());

assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.completedFuture(n).allTupleOfFastFail(
completedFuture(s),
completedFuture(d),
completedFuture(anotherN),
completedFuture(n + n)
).get());

////////////////////////////////////////////////////////////////////////////////

assertEquals(Tuple2.of(n, s), cffuFactory.completedFuture(n).allTupleOfFastFail(
cffuFactory.completedFuture(s)
).get());

assertEquals(Tuple3.of(n, s, d), cffuFactory.completedFuture(n).allTupleOfFastFail(
cffuFactory.completedFuture(s),
cffuFactory.completedFuture(d)
).get());

assertEquals(Tuple4.of(n, s, d, anotherN), cffuFactory.completedFuture(n).allTupleOfFastFail(
cffuFactory.completedFuture(s),
cffuFactory.completedFuture(d),
cffuFactory.completedFuture(anotherN)
).get());

assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.completedFuture(n).allTupleOfFastFail(
cffuFactory.completedFuture(s),
cffuFactory.completedFuture(d),
cffuFactory.completedFuture(anotherN),
cffuFactory.completedFuture(n + n)
).get());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,33 @@ void test_mostTupleOfSuccess() throws Exception {
assertEquals(Tuple5.of(null, n, s, null, null), mostTupleOfSuccess(
10, TimeUnit.MILLISECONDS, cancelled, completed, anotherCompleted, incomplete, failed
).get());

// with `executorWhenTimeout`

assertEquals(Tuple2.of(n, s), mostTupleOfSuccess(
executorService, 10, TimeUnit.MILLISECONDS, completed, anotherCompleted
).get());
assertEquals(Tuple2.of(n, null), mostTupleOfSuccess(
executorService, 10, TimeUnit.MILLISECONDS, completed, failed
).get());

assertEquals(Tuple3.of(n, s, null), mostTupleOfSuccess(
executorService, 10, TimeUnit.MILLISECONDS, completed, anotherCompleted, cancelled
).get());
assertEquals(Tuple3.of(null, null, s), mostTupleOfSuccess(
executorService, 10, TimeUnit.MILLISECONDS, incomplete, failed, anotherCompleted
).get());

assertEquals(Tuple4.of(n, s, null, null), mostTupleOfSuccess(
executorService, 10, TimeUnit.MILLISECONDS, completed, anotherCompleted, cancelled, incomplete
).get());
assertEquals(Tuple4.of(null, null, null, null), mostTupleOfSuccess(
executorService, 10, TimeUnit.MILLISECONDS, incomplete, failed, cancelled, incomplete
).get());

assertEquals(Tuple5.of(null, n, s, null, null), mostTupleOfSuccess(
executorService, 10, TimeUnit.MILLISECONDS, cancelled, completed, anotherCompleted, incomplete, failed
).get());
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public static void main(String[] args) throws Exception {
Cffu<String> cffu1 = cffuFactory.completedFuture("21");
Cffu<Integer> cffu2 = cffuFactory.completedFuture(42);

Cffu<Tuple2<String, Integer>> allTuple = cffu1.allTupleOf(cffu2);
// or: cffuFactory.allTupleOf(cffu1, cffu2);
Cffu<Tuple2<String, Integer>> allTuple = cffuFactory.allTupleOf(cffu1, cffu2);
System.out.println(allTuple.get());

//////////////////////////////////////////////////
Expand Down
Loading

0 comments on commit 077b4aa

Please sign in to comment.