Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Apr 23, 2024
1 parent 68ab187 commit 6e606f9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 273 deletions.
176 changes: 17 additions & 159 deletions cffu-kotlin/src/main/java/io/foldright/cffu/kotlin/CffuExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private const val ERROR_MSG_FOR_ARRAY = "no cffuFactory argument provided when t
* @see allOfCffuVoid
* @see CffuFactory.allResultsOf
*/
fun <T> Collection<Cffu<T>>.allOfCffu(cffuFactory: CffuFactory = ABSENT): Cffu<List<T>> {
fun <T> Collection<Cffu<out T>>.allOfCffu(cffuFactory: CffuFactory = ABSENT): Cffu<List<T>> {
val factory: CffuFactory = if (cffuFactory !== ABSENT) cffuFactory
else firstOrNull()?.cffuFactory() ?: throw IllegalArgumentException(ERROR_MSG_FOR_COLL)
return factory.allResultsOf(*this.toTypedArray())
Expand All @@ -101,7 +101,7 @@ fun <T> Collection<Cffu<T>>.allOfCffu(cffuFactory: CffuFactory = ABSENT): Cffu<L
* @see allOfCffuVoid
* @see CffuFactory.allResultsOf
*/
fun <T> Array<Cffu<T>>.allOfCffu(cffuFactory: CffuFactory = ABSENT): Cffu<List<T>> {
fun <T> Array<out Cffu<out T>>.allOfCffu(cffuFactory: CffuFactory = ABSENT): Cffu<List<T>> {
val factory: CffuFactory = if (cffuFactory !== ABSENT) cffuFactory
else firstOrNull()?.cffuFactory() ?: throw IllegalArgumentException(ERROR_MSG_FOR_ARRAY)
return factory.allResultsOf(*this)
Expand All @@ -121,7 +121,7 @@ fun <T> Array<Cffu<T>>.allOfCffu(cffuFactory: CffuFactory = ABSENT): Cffu<List<T
* @see CffuFactory.allResultsOf
*/
@JvmName("allOfCffuCf")
fun <T> Collection<CompletableFuture<T>>.allOfCffu(cffuFactory: CffuFactory): Cffu<List<T>> =
fun <T> Collection<CompletableFuture<out T>>.allOfCffu(cffuFactory: CffuFactory): Cffu<List<T>> =
cffuFactory.allResultsOf(*this.toTypedArray())

/**
Expand All @@ -137,7 +137,7 @@ fun <T> Collection<CompletableFuture<T>>.allOfCffu(cffuFactory: CffuFactory): Cf
* @see allOfCffuVoid
* @see CffuFactory.allResultsOf
*/
fun <T> Array<CompletableFuture<T>>.allOfCffu(cffuFactory: CffuFactory): Cffu<List<T>> =
fun <T> Array<out CompletableFuture<out T>>.allOfCffu(cffuFactory: CffuFactory): Cffu<List<T>> =
cffuFactory.allResultsOf(*this)

/**
Expand Down Expand Up @@ -235,7 +235,7 @@ fun Array<out CompletableFuture<*>>.allOfCffuVoid(cffuFactory: CffuFactory): Cff
* @see allOfFastFailCffuVoid
* @see CffuFactory.allResultsOfFastFail
*/
fun <T> Collection<Cffu<T>>.allOfFastFailCffu(cffuFactory: CffuFactory = ABSENT): Cffu<List<T>> {
fun <T> Collection<Cffu<out T>>.allOfFastFailCffu(cffuFactory: CffuFactory = ABSENT): Cffu<List<T>> {
val factory: CffuFactory = if (cffuFactory !== ABSENT) cffuFactory
else firstOrNull()?.cffuFactory() ?: throw IllegalArgumentException(ERROR_MSG_FOR_COLL)
return factory.allResultsOfFastFail(*this.toTypedArray())
Expand All @@ -258,7 +258,7 @@ fun <T> Collection<Cffu<T>>.allOfFastFailCffu(cffuFactory: CffuFactory = ABSENT)
* @see allOfFastFailCffuVoid
* @see CffuFactory.allResultsOfFastFail
*/
fun <T> Array<Cffu<T>>.allOfFastFailCffu(cffuFactory: CffuFactory = ABSENT): Cffu<List<T>> {
fun <T> Array<out Cffu<out T>>.allOfFastFailCffu(cffuFactory: CffuFactory = ABSENT): Cffu<List<T>> {
val factory: CffuFactory = if (cffuFactory !== ABSENT) cffuFactory
else firstOrNull()?.cffuFactory() ?: throw IllegalArgumentException(ERROR_MSG_FOR_ARRAY)
return factory.allResultsOfFastFail(*this)
Expand All @@ -279,7 +279,7 @@ fun <T> Array<Cffu<T>>.allOfFastFailCffu(cffuFactory: CffuFactory = ABSENT): Cff
* @see CffuFactory.allResultsOfFastFail
*/
@JvmName("allOfFastFailCffuCf")
fun <T> Collection<CompletableFuture<T>>.allOfFastFailCffu(cffuFactory: CffuFactory): Cffu<List<T>> =
fun <T> Collection<CompletableFuture<out T>>.allOfFastFailCffu(cffuFactory: CffuFactory): Cffu<List<T>> =
cffuFactory.allResultsOfFastFail(*this.toTypedArray())

/**
Expand All @@ -296,7 +296,7 @@ fun <T> Collection<CompletableFuture<T>>.allOfFastFailCffu(cffuFactory: CffuFact
* @see allOfFastFailCffuVoid
* @see CffuFactory.allResultsOfFastFail
*/
fun <T> Array<out CompletableFuture<T>>.allOfFastFailCffu(cffuFactory: CffuFactory): Cffu<List<T>> =
fun <T> Array<out CompletableFuture<out T>>.allOfFastFailCffu(cffuFactory: CffuFactory): Cffu<List<T>> =
cffuFactory.allResultsOfFastFail(*this)

/**
Expand Down Expand Up @@ -392,16 +392,14 @@ fun Array<out CompletableFuture<*>>.allOfFastFailCffuVoid(cffuFactory: CffuFacto
/**
* Returns a new Cffu that is completed when any of the given Cffus complete, with the same result.
*
* Same as [anyOfCffuAny], but return result type is specified type instead of type `Any`.
* Same as [CffuFactory.anyOf], providing this method is convenient for method chaining.
*
* If this collection is not empty, `cffuFactory` argument is optional, use the `cffuFactory` of the first cffu element.
* If this collection is empty and no`cffuFactory` provided, throw [IllegalArgumentException].
*
* @see anyOfCffuAny
* @see CffuFactory.anyOf
*/
fun <T> Collection<Cffu<T>>.anyOfCffu(cffuFactory: CffuFactory = ABSENT): Cffu<T> {
fun <T> Collection<Cffu<out T>>.anyOfCffu(cffuFactory: CffuFactory = ABSENT): Cffu<T> {
val factory: CffuFactory = if (cffuFactory !== ABSENT) cffuFactory
else firstOrNull()?.cffuFactory() ?: throw IllegalArgumentException(ERROR_MSG_FOR_COLL)
return factory.anyOf(*this.toTypedArray())
Expand All @@ -410,16 +408,14 @@ fun <T> Collection<Cffu<T>>.anyOfCffu(cffuFactory: CffuFactory = ABSENT): Cffu<T
/**
* Returns a new Cffu that is completed when any of the given Cffus complete, with the same result.
*
* Same as [anyOfCffuAny], but return result type is specified type instead of type `Any`.
* Same as [CffuFactory.anyOf], providing this method is convenient for method chaining.
*
* If this array is not empty, `cffuFactory` argument is optional, use the `cffuFactory` of the first cffu element.
* If this array is empty and no`cffuFactory` provided, throw [IllegalArgumentException].
*
* @see anyOfCffuAny
* @see CffuFactory.anyOf
*/
fun <T> Array<Cffu<T>>.anyOfCffu(cffuFactory: CffuFactory = ABSENT): Cffu<T> {
fun <T> Array<out Cffu<out T>>.anyOfCffu(cffuFactory: CffuFactory = ABSENT): Cffu<T> {
val factory: CffuFactory = if (cffuFactory !== ABSENT) cffuFactory
else firstOrNull()?.cffuFactory() ?: throw IllegalArgumentException(ERROR_MSG_FOR_ARRAY)
return factory.anyOf(*this)
Expand All @@ -428,83 +424,22 @@ fun <T> Array<Cffu<T>>.anyOfCffu(cffuFactory: CffuFactory = ABSENT): Cffu<T> {
/**
* Returns a new Cffu that is completed when any of the given CompletableFutures complete, with the same result.
*
* Same as [anyOfCffuAny], but return result type is specified type instead of type `Any`.
* Same as [CffuFactory.anyOf], providing this method is convenient for method chaining.
*
* @see anyOfCffuAny
* @see CffuFactory.anyOf
*/
@JvmName("anyOfCffuCf")
fun <T> Collection<CompletableFuture<T>>.anyOfCffu(cffuFactory: CffuFactory): Cffu<T> =
fun <T> Collection<CompletableFuture<out T>>.anyOfCffu(cffuFactory: CffuFactory): Cffu<T> =
cffuFactory.anyOf(*this.toTypedArray())

/**
* Returns a new Cffu that is completed when any of the given CompletableFutures complete, with the same result.
*
* Same as [anyOfCffuAny], but return result type is specified type instead of type `Any`.
* Same as [CffuFactory.anyOf], providing this method is convenient for method chaining.
*
* @see anyOfCffuAny
* @see CffuFactory.anyOf
*/
fun <T> Array<CompletableFuture<T>>.anyOfCffu(cffuFactory: CffuFactory): Cffu<T> =
cffuFactory.anyOf(*this)

/**
* Returns a new Cffu that is completed when any of the given Cffus complete, with the same result.
*
* Same as [CffuFactory.anyOf], providing this method is convenient for method chaining.
*
* If this collection is not empty, `cffuFactory` argument is optional, use the `cffuFactory` of the first cffu element.
* If this collection is empty and no`cffuFactory` provided, throw [IllegalArgumentException].
*
* @see anyOfCffu
* @see CffuFactory.anyOf
*/
fun Collection<Cffu<*>>.anyOfCffuAny(cffuFactory: CffuFactory = ABSENT): Cffu<Any> {
val factory: CffuFactory = if (cffuFactory !== ABSENT) cffuFactory
else firstOrNull()?.cffuFactory() ?: throw IllegalArgumentException(ERROR_MSG_FOR_COLL)
return factory.anyOf(*this.toTypedArray())
}

/**
* Returns a new Cffu that is completed when any of the given Cffus complete, with the same result.
*
* Same as [CffuFactory.anyOf], providing this method is convenient for method chaining.
*
* If this array is not empty, `cffuFactory` argument is optional, use the `cffuFactory` of the first cffu element.
* If this array is empty and no`cffuFactory` provided, throw [IllegalArgumentException].
*
* @see anyOfCffu
* @see CffuFactory.anyOf
*/
fun Array<out Cffu<*>>.anyOfCffuAny(cffuFactory: CffuFactory = ABSENT): Cffu<Any> {
val factory: CffuFactory = if (cffuFactory !== ABSENT) cffuFactory
else firstOrNull()?.cffuFactory() ?: throw IllegalArgumentException(ERROR_MSG_FOR_ARRAY)
return factory.anyOf(*this)
}

/**
* Returns a new Cffu that is completed when any of the given CompletableFutures complete, with the same result.
*
* Same as [CffuFactory.anyOf], providing this method is convenient for method chaining.
*
* @see anyOfCffu
* @see CffuFactory.anyOf
*/
@JvmName("anyOfCffuAnyCf")
fun Collection<CompletableFuture<*>>.anyOfCffuAny(cffuFactory: CffuFactory): Cffu<Any> =
cffuFactory.anyOf(*this.toTypedArray())

/**
* Returns a new Cffu that is completed when any of the given CompletableFutures complete, with the same result.
*
* Same as [CffuFactory.anyOf], providing this method is convenient for method chaining.
*
* @see anyOfCffu
* @see CffuFactory.anyOf
*/
fun Array<out CompletableFuture<*>>.anyOfCffuAny(cffuFactory: CffuFactory): Cffu<Any> =
fun <T> Array<out CompletableFuture<out T>>.anyOfCffu(cffuFactory: CffuFactory): Cffu<T> =
cffuFactory.anyOf(*this)

/**
Expand All @@ -523,7 +458,7 @@ fun Array<out CompletableFuture<*>>.anyOfCffuAny(cffuFactory: CffuFactory): Cffu
* @see anyOfCffu
* @see CffuFactory.anyOfSuccess
*/
fun <T> Collection<Cffu<T>>.anyOfSuccessCffu(cffuFactory: CffuFactory = ABSENT): Cffu<T> {
fun <T> Collection<Cffu<out T>>.anyOfSuccessCffu(cffuFactory: CffuFactory = ABSENT): Cffu<T> {
val factory: CffuFactory = if (cffuFactory !== ABSENT) cffuFactory
else firstOrNull()?.cffuFactory() ?: throw IllegalArgumentException(ERROR_MSG_FOR_COLL)
return factory.anyOfSuccess(*this.toTypedArray())
Expand All @@ -545,7 +480,7 @@ fun <T> Collection<Cffu<T>>.anyOfSuccessCffu(cffuFactory: CffuFactory = ABSENT):
* @see anyOfCffu
* @see CffuFactory.anyOfSuccess
*/
fun <T> Array<Cffu<T>>.anyOfSuccessCffu(cffuFactory: CffuFactory = ABSENT): Cffu<T> {
fun <T> Array<out Cffu<out T>>.anyOfSuccessCffu(cffuFactory: CffuFactory = ABSENT): Cffu<T> {
val factory: CffuFactory = if (cffuFactory !== ABSENT) cffuFactory
else firstOrNull()?.cffuFactory() ?: throw IllegalArgumentException(ERROR_MSG_FOR_ARRAY)
return factory.anyOfSuccess(*this)
Expand All @@ -565,7 +500,7 @@ fun <T> Array<Cffu<T>>.anyOfSuccessCffu(cffuFactory: CffuFactory = ABSENT): Cffu
* @see CffuFactory.anyOfSuccess
*/
@JvmName("anyOfSuccessCffuCf")
fun <T> Collection<CompletableFuture<T>>.anyOfSuccessCffu(cffuFactory: CffuFactory): Cffu<T> =
fun <T> Collection<CompletableFuture<out T>>.anyOfSuccessCffu(cffuFactory: CffuFactory): Cffu<T> =
cffuFactory.anyOfSuccess(*this.toTypedArray())

/**
Expand All @@ -581,84 +516,7 @@ fun <T> Collection<CompletableFuture<T>>.anyOfSuccessCffu(cffuFactory: CffuFacto
* @see anyOfCffu
* @see CffuFactory.anyOfSuccess
*/
fun <T> Array<CompletableFuture<T>>.anyOfSuccessCffu(cffuFactory: CffuFactory): Cffu<T> =
cffuFactory.anyOfSuccess(*this)

/**
* Returns a new Cffu that is successful when any of the given CompletableFutures success,
* with the same result. Otherwise, all the given CompletableFutures complete exceptionally,
* the returned Cffu also does so, with a CompletionException holding
* an exception from any of the given CompletableFutures as its cause. If no CompletableFutures are provided,
* returns a new Cffu that is already completed exceptionally with a CompletionException
* holding a [NoCfsProvidedException][io.foldright.cffu.NoCfsProvidedException] as its cause.
*
* Same as [CffuFactory.anyOfSuccess], providing this method is convenient for method chaining.
*
* If this collection is not empty, `cffuFactory` argument is optional, use the `cffuFactory` of the first cffu element.
* If this collection is empty and no`cffuFactory` provided, throw [IllegalArgumentException].
*
* @see anyOfSuccessCffu
* @see CffuFactory.anyOfSuccess
*/
fun Collection<Cffu<*>>.anyOfSuccessCffuAny(cffuFactory: CffuFactory = ABSENT): Cffu<Any> {
val factory: CffuFactory = if (cffuFactory !== ABSENT) cffuFactory
else firstOrNull()?.cffuFactory() ?: throw IllegalArgumentException(ERROR_MSG_FOR_COLL)
return factory.anyOfSuccess(*this.toTypedArray())
}

/**
* Returns a new Cffu that is successful when any of the given CompletableFutures success,
* with the same result. Otherwise, all the given CompletableFutures complete exceptionally,
* the returned Cffu also does so, with a CompletionException holding
* an exception from any of the given CompletableFutures as its cause. If no CompletableFutures are provided,
* returns a new Cffu that is already completed exceptionally with a CompletionException
* holding a [NoCfsProvidedException][io.foldright.cffu.NoCfsProvidedException] as its cause.
*
* Same as [CffuFactory.anyOfSuccess], providing this method is convenient for method chaining.
*
* If this array is not empty, `cffuFactory` argument is optional, use the `cffuFactory` of the first cffu element.
* If this array is empty and no`cffuFactory` provided, throw [IllegalArgumentException].
*
* @see anyOfSuccessCffu
* @see CffuFactory.anyOfSuccess
*/
fun Array<out Cffu<*>>.anyOfSuccessCffuAny(cffuFactory: CffuFactory = ABSENT): Cffu<Any> {
val factory: CffuFactory = if (cffuFactory !== ABSENT) cffuFactory
else firstOrNull()?.cffuFactory() ?: throw IllegalArgumentException(ERROR_MSG_FOR_ARRAY)
return factory.anyOfSuccess(*this)
}

/**
* Returns a new Cffu that is successful when any of the given CompletableFutures success,
* with the same result. Otherwise, all the given CompletableFutures complete exceptionally,
* the returned Cffu also does so, with a CompletionException holding
* an exception from any of the given CompletableFutures as its cause. If no CompletableFutures are provided,
* returns a new Cffu that is already completed exceptionally with a CompletionException
* holding a [NoCfsProvidedException][io.foldright.cffu.NoCfsProvidedException] as its cause.
*
* Same as [CffuFactory.anyOfSuccess], providing this method is convenient for method chaining.
*
* @see anyOfSuccessCffu
* @see CffuFactory.anyOfSuccess
*/
@JvmName("anyOfSuccessCffuAnyCf")
fun Collection<CompletableFuture<*>>.anyOfSuccessCffuAny(cffuFactory: CffuFactory): Cffu<Any> =
cffuFactory.anyOfSuccess(*this.toTypedArray())

/**
* Returns a new Cffu that is successful when any of the given CompletableFutures success,
* with the same result. Otherwise, all the given CompletableFutures complete exceptionally,
* the returned Cffu also does so, with a CompletionException holding
* an exception from any of the given CompletableFutures as its cause. If no CompletableFutures are provided,
* returns a new Cffu that is already completed exceptionally with a CompletionException
* holding a [NoCfsProvidedException][io.foldright.cffu.NoCfsProvidedException] as its cause.
*
* Same as [CffuFactory.anyOfSuccess], providing this method is convenient for method chaining.
*
* @see anyOfSuccessCffu
* @see CffuFactory.anyOfSuccess
*/
fun Array<out CompletableFuture<*>>.anyOfSuccessCffuAny(cffuFactory: CffuFactory): Cffu<Any> =
fun <T> Array<out CompletableFuture<out T>>.anyOfSuccessCffu(cffuFactory: CffuFactory): Cffu<T> =
cffuFactory.anyOfSuccess(*this)

////////////////////////////////////////
Expand Down Expand Up @@ -706,5 +564,5 @@ fun <T> Collection<Cffu<T>>.cffuUnwrap(): List<CompletableFuture<T>> =
*
* @see CffuFactory.cffuArrayUnwrap
*/
fun <T> Array<Cffu<T>>.cffuUnwrap(): Array<CompletableFuture<T>> =
fun <T> Array<out Cffu<T>>.cffuUnwrap(): Array<CompletableFuture<T>> =
CffuFactory.cffuArrayUnwrap(*this)
Loading

0 comments on commit 6e606f9

Please sign in to comment.