Skip to content

Commit

Permalink
refactor: remove convenient util methods for dangerous cffuUnwrap m…
Browse files Browse the repository at this point in the history
…ethod 🧹 ⚠️
  • Loading branch information
oldratlee committed Jul 27, 2024
1 parent 69b3554 commit ffad13a
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 84 deletions.
4 changes: 0 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 @@ -2903,13 +2903,9 @@ public void obtrudeException(Throwable ex) {
/**
* Returns the underlying CompletableFuture.
* In general, you should NEVER use this method, use {@link #toCompletableFuture()} instead.
* <p>
* {@link CffuFactory#cffuArrayUnwrap(Cffu[])} is the batch operation to this method.
*
* @return the underlying CompletableFuture
* @see #toCompletableFuture()
* @see CffuFactory#cffuArrayUnwrap(Cffu[])
* @see #toCompletableFuture()
*/
@Contract(pure = true)
@SuppressFBWarnings("EI_EXPOSE_REP")
Expand Down
21 changes: 1 addition & 20 deletions cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -1611,30 +1611,11 @@ public Executor delayedExecutor(long delay, TimeUnit unit, Executor executor) {

// endregion
////////////////////////////////////////////////////////////////////////////////
// region# Conversion Methods(static methods)
// region# Conversion Methods
//
// - cffuArrayUnwrap: Cffu[] -> CompletableFuture[]
// - cffuListToArray: List<Cffu> -> Cffu[]
////////////////////////////////////////////////////////////////////////////////

/**
* A convenient util method for unwrap input {@link Cffu} array elements by {@link Cffu#cffuUnwrap()}.
*
* @param cfs the Cffus
* @see CompletableFutureUtils#toCompletableFutureArray(CompletionStage[])
* @see Cffu#cffuUnwrap()
*/
@Contract(pure = true)
@SafeVarargs
public static <T> CompletableFuture<T>[] cffuArrayUnwrap(Cffu<T>... cfs) {
@SuppressWarnings("unchecked")
CompletableFuture<T>[] ret = new CompletableFuture[cfs.length];
for (int i = 0; i < cfs.length; i++) {
ret[i] = requireNonNull(cfs[i], "cf" + (i + 1) + " is null").cffuUnwrap();
}
return ret;
}

/**
* Convert Cffu list to Cffu array.
*
Expand Down
16 changes: 0 additions & 16 deletions cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -860,25 +860,9 @@ void test_toCffuArray() throws Exception {
////////////////////////////////////////////////////////////////////////////////
// region# Conversion Methods(static methods)
//
// - cffuArrayUnwrap: Cffu[] -> CompletableFuture[]
// - cffuListToArray: List<Cffu> -> Cffu[]
////////////////////////////////////////////////////////////////////////////////

@Test
void test_cffuArrayUnwrap() {
@SuppressWarnings("unchecked")
CompletableFuture<Integer>[] cfArray = new CompletableFuture[]{
completedFuture(n),
completedFuture(anotherN)
};
@SuppressWarnings("unchecked")
Cffu<Integer>[] input = new Cffu[]{
cffuFactory.toCffu(cfArray[0]),
cffuFactory.toCffu(cfArray[1]),
};
assertArrayEquals(cfArray, CffuFactory.cffuArrayUnwrap(input));
}

@Test
void test_toCompletableFutureArray() {
@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,28 +600,3 @@ fun <T> Collection<CompletionStage<out T>>.anyOfCffu(cffuFactory: CffuFactory):
*/
fun <T> Array<out CompletionStage<out T>>.anyOfCffu(cffuFactory: CffuFactory): Cffu<T> =
cffuFactory.anyOf(*this)

// endregion
////////////////////////////////////////////////////////////////////////////////
// region# cffuUnwrap methods for Collection/Array
////////////////////////////////////////////////////////////////////////////////

/**
* Unwrap input [Cffu] collection elements by [Cffu.cffuUnwrap].
*
* This method is the same as [CffuFactory.cffuArrayUnwrap], providing this method is convenient for method chaining.
*
* @see CffuFactory.cffuArrayUnwrap
*/
fun <T> Collection<Cffu<T>>.cffuUnwrap(): List<CompletableFuture<T>> =
map { it.cffuUnwrap() }

/**
* Unwrap input [Cffu] array elements by [Cffu.cffuUnwrap].
*
* This method is the same as [CffuFactory.cffuArrayUnwrap], providing this method is convenient for method chaining.
*
* @see CffuFactory.cffuArrayUnwrap
*/
fun <T> Array<out Cffu<T>>.cffuUnwrap(): Array<CompletableFuture<T>> =
CffuFactory.cffuArrayUnwrap(*this)
Original file line number Diff line number Diff line change
Expand Up @@ -655,23 +655,4 @@ class CffuExtensionsTest : FunSpec({
it.shouldNotBeTypeOf<Array<CompletionStage<*>>>()
}
}

////////////////////////////////////////
// cffuUnwrap
////////////////////////////////////////

test("cffuUnwrap for Cffu collection/array") {
val range = 0 until 10
val cfs: List<CompletableFuture<Int>> = range.map {
CompletableFuture.completedFuture(it)
}
val cfArray = cfs.toTypedArray()

val cffus: List<Cffu<Int>> = cfs.toCffu(testCffuFactory)
cffus.cffuUnwrap() shouldBe cfs
cffus.toSet().cffuUnwrap() shouldBe cfs

val cffuArray: Array<Cffu<Int>> = cffus.toTypedArray()
cffuArray.cffuUnwrap() shouldBe cfArray
}
})

0 comments on commit ffad13a

Please sign in to comment.