Skip to content

Commit

Permalink
Merge branch 'kotlin/extension-methods'
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Mar 29, 2023
2 parents 446b32c + efcd7e7 commit c0336bd
Show file tree
Hide file tree
Showing 8 changed files with 457 additions and 37 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,8 @@
Spotbugs support @SuppressWarnings
https://github.com/spotbugs/spotbugs/issues/737#issuecomment-416118033
Is it possible for spotbugs to skip kotlin files from pure maven configuration?
https://stackoverflow.com/questions/55060459
-->
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
Expand All @@ -612,6 +614,9 @@
</goals>
</execution>
</executions>
<configuration>
<excludeFilterFile>${project.basedir}/src/spotbugs-exclude-filter-file.xml</excludeFilterFile>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
42 changes: 26 additions & 16 deletions src/main/java/io/foldright/cffu/CffuFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public <T> Cffu<T> asCffu(CompletionStage<T> stage) {

/**
* A convenient util method for wrap input {@link CompletableFuture}/{@link CompletionStage} array element
* using {@link #asCffu(CompletionStage)}.
* by {@link #asCffu(CompletionStage)}.
*
* @see #asCffu(CompletionStage)
*/
Expand All @@ -318,7 +318,7 @@ public final <T> Cffu<T>[] asCffuArray(CompletionStage<T>... stages) {
////////////////////////////////////////////////////////////////////////////////

/**
* Returns a new Cffu that is completed when all of the given Cffus complete.
* Returns a new Cffu that is completed when all the given Cffus complete.
* If any of the given Cffu complete exceptionally, then the returned
* Cffu also does so, with a CompletionException holding this exception as its cause.<br>
* Otherwise, the results, if any, of the given Cffus are not reflected in
Expand All @@ -337,7 +337,7 @@ public final <T> Cffu<T>[] asCffuArray(CompletionStage<T>... stages) {
* as in: {@code CffuFactory.allOf(c1, c2, c3).join();}.
*
* @param cfs the Cffus
* @return a new Cffu that is completed when all of the given Cffus complete
* @return a new Cffu that is completed when all the given Cffus complete
* @throws NullPointerException if the array or any of its elements are {@code null}
* @see #cffuAllOf(Cffu[])
* @see #cffuCombine(Cffu, Cffu)
Expand All @@ -364,7 +364,7 @@ public Cffu<Void> allOf(Cffu<?>... cfs) {
* </ol>
*
* @param cfs the CompletableFutures
* @return a new Cffu that is completed when all of the given CompletableFutures complete
* @return a new Cffu that is completed when all the given CompletableFutures complete
* @throws NullPointerException if the array or any of its elements are {@code null}
* @see #allOf(Cffu[])
* @see #cffuAllOf(CompletableFuture[])
Expand Down Expand Up @@ -401,8 +401,8 @@ public Cffu<Void> allOf() {
* because {@link #cffuAnyOf(Cffu[])} return type {@code T} instead of {@code Object}, more type safe.
*
* @param cfs the Cffus
* @return a new Cffu that is completed with the result or exception of
* any of the given Cffus when one completes
* @return a new Cffu that is completed with the result
* or exception of any of the given Cffus when one completes
* @throws NullPointerException if the array or any of its elements are {@code null}
* @see #cffuAnyOf(Cffu[])
* @see CompletableFuture#anyOf(CompletableFuture[])
Expand All @@ -421,8 +421,8 @@ public Cffu<Object> anyOf(Cffu<?>... cfs) {
* because {@link #cffuAnyOf(CompletableFuture[])} return type {@code T} instead of {@code Object}, more type safe.
*
* @param cfs the CompletableFutures
* @return a new Cffu that is completed with the result or exception of
* any of the given CompletableFutures when one completes
* @return a new Cffu that is completed with the result
* or exception of any of the given CompletableFutures when one completes
* @throws NullPointerException if the array or any of its elements are {@code null}
* @see #cffuAnyOf(CompletableFuture[])
* @see #anyOf(Cffu[])
Expand Down Expand Up @@ -497,10 +497,13 @@ public Executor delayedExecutor(long delay, TimeUnit unit, Executor executor) {
////////////////////////////////////////////////////////////////////////////////

/**
* Same to {@link #allOf(Cffu[])}, but return the results of input {@link Cffu}.
* Returns a new Cffu with the result of all the given Cffus,
* the new Cffu is completed when all the given Cffus complete.
* <p>
* Same to {@link #allOf(Cffu[])}, but return the results of input Cffus.
*
* @param cfs the Cffus
* @return a new Cffu that is completed when all of the given Cffus complete
* @return a new Cffu that is completed when all the given Cffus complete
* @throws NullPointerException if the array or any of its elements are {@code null}
* @see #allOf(Cffu[])
*/
Expand All @@ -511,10 +514,13 @@ public final <T> Cffu<List<T>> cffuAllOf(Cffu<T>... cfs) {
}

/**
* Returns a new Cffu with the result of all the given CompletableFutures,
* the new Cffu is completed when all the given CompletableFutures complete.
* <p>
* Same as {@link #cffuAllOf(Cffu[])} with overloaded argument type {@link CompletableFuture}.
*
* @param cfs the CompletableFutures
* @return a new Cffu that is completed when all of the given CompletableFutures complete
* @return a new Cffu that is completed when all the given CompletableFutures complete
* @throws NullPointerException if the array or any of its elements are {@code null}
* @see #cffuAllOf(Cffu[])
*/
Expand Down Expand Up @@ -556,11 +562,13 @@ public <T> Cffu<List<T>> cffuAllOf() {
}

/**
* Returns a new Cffu that is completed when any of the given Cffus complete, with the same result.
* <p>
* Same as {@link #anyOf(Cffu[])}, but return result type is specified type instead of {@code Object}.
*
* @param cfs the Cffus
* @return a new Cffu that is completed with the result or exception of
* any of the given Cffus when one completes
* @return a new Cffu that is completed with the result
* or exception of any of the given Cffus when one completes
* @throws NullPointerException if the array or any of its elements are {@code null}
* @see #anyOf(Cffu[])
*/
Expand All @@ -571,11 +579,13 @@ public final <T> Cffu<T> cffuAnyOf(Cffu<T>... cfs) {
}

/**
* Returns a new Cffu that is completed when any of the given CompletableFutures complete, with the same result.
* <p>
* Same as {@link #cffuAllOf(Cffu[])} with overloaded argument type {@link CompletableFuture}.
*
* @param cfs the CompletableFutures
* @return a new Cffu that is completed with the result or exception of
* any of the given CompletableFutures when one completes
* @return a new Cffu that is completed with the result
* or exception of any of the given CompletableFutures when one completes
* @throws NullPointerException if the array or any of its elements are {@code null}
* @see #cffuAnyOf(Cffu[])
*/
Expand Down Expand Up @@ -839,7 +849,7 @@ public static <T> CompletableFuture<T>[] toCompletableFutureArray(CompletionStag
}

/**
* A convenient util method for unwrap input {@link Cffu} array elements using {@link Cffu#cffuUnwrap()}.
* A convenient util method for unwrap input {@link Cffu} array elements by {@link Cffu#cffuUnwrap()}.
*
* @param cfs the Cffus
* @see Cffu#cffuUnwrap()
Expand Down
204 changes: 200 additions & 4 deletions src/main/java/io/foldright/cffu/kotlin/CffuExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,210 @@ package io.foldright.cffu.kotlin

import io.foldright.cffu.Cffu
import io.foldright.cffu.CffuFactory
import java.util.concurrent.CompletableFuture
import java.util.concurrent.CompletionStage


/**
* Returns a new CompletableFuture that is completed when all the given CompletableFutures complete.
*
* Same as [CompletableFuture.allOf], providing this method is convenient for method chaining.
*
* @see CompletableFuture.allOf
* @see CffuFactory.allOf
*/
fun Collection<CompletableFuture<*>>.allCompletableFuture(): CompletableFuture<Void> =
CompletableFuture.allOf(*this.toTypedArray())

/**
* Returns a new CompletableFuture that is completed when all the given CompletableFutures complete.
*
* Same as [CompletableFuture.allOf], providing this method is convenient for method chaining.
*
* @see CompletableFuture.allOf
* @see CffuFactory.allOf
*/
fun Array<CompletableFuture<*>>.allCompletableFuture(): CompletableFuture<Void> =
CompletableFuture.allOf(*this)

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

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

/**
* Wrap this [CompletionStage] to [Cffu].
* Wrap an existed [CompletableFuture]/[CompletionStage] to [Cffu].
*
* reimplement [CffuFactory.asCffu] method as extension of [CompletionStage],
* providing this method convenient for method chaining.
* Same as [CffuFactory.asCffu], providing this method is convenient for method chaining.
*
* @see CffuFactory.asCffu
*/
fun <T> CompletionStage<T>.asCffu(cffuFactory: CffuFactory): Cffu<T> = cffuFactory.asCffu(this)
fun <T> CompletionStage<T>.asCffu(cffuFactory: CffuFactory): Cffu<T> =
cffuFactory.asCffu(this)

/**
* Wrap input [CompletableFuture]/[CompletionStage] collection element to [Cffu] by [CffuFactory.asCffu].
*
* Same as [CffuFactory.asCffu], providing this method is convenient for method chaining.
*
* @see CffuFactory.asCffu
*/
fun <T> Collection<CompletionStage<T>>.asCffu(cffuFactory: CffuFactory): List<Cffu<T>> =
map { it.asCffu(cffuFactory) }

/**
* Wrap input [CompletableFuture]/[CompletionStage] array element to [Cffu] by [CffuFactory.asCffu].
*
* Same as [CffuFactory.asCffu], providing this method is convenient for method chaining.
*
* @see CffuFactory.asCffu
* @see CffuFactory.asCffuArray
*/
fun <T, CS : CompletionStage<T>> Array<CS>.asCffu(cffuFactory: CffuFactory): Array<Cffu<T>> =
cffuFactory.asCffuArray(*this)

/**
* Returns a new Cffu with the result of all the given Cffus,
* the new Cffu is completed when all the given Cffus complete.
*
* Same as [CffuFactory.cffuAllOf], providing this method is convenient for method chaining.
*
* @see CffuFactory.cffuAllOf
*/
fun <T> Collection<Cffu<T>>.allCffu(cffuFactory: CffuFactory): Cffu<List<T>> =
cffuFactory.cffuAllOf(*this.toTypedArray())

/**
* Returns a new Cffu with the result of all the given Cffus,
* the new Cffu is completed when all the given Cffus complete.
*
* Same as [CffuFactory.cffuAllOf], providing this method is convenient for method chaining.
*
* @see CffuFactory.cffuAllOf
*/

fun <T> Array<Cffu<T>>.allCffu(cffuFactory: CffuFactory): Cffu<List<T>> =
cffuFactory.cffuAllOf(*this)

/**
* Returns a new Cffu with the result of all the given CompletableFutures,
* the new Cffu is completed when all the given CompletableFutures complete.
*
* Same as [CffuFactory.cffuAllOf], providing this method is convenient for method chaining.
*
* @see CffuFactory.cffuAllOf
*/
@JvmName("cffuAllOfCf")
fun <T> Collection<CompletableFuture<T>>.allCffu(cffuFactory: CffuFactory): Cffu<List<T>> =
cffuFactory.cffuAllOf(*this.toTypedArray())

/**
* Returns a new Cffu with the result of all the given CompletableFutures,
* the new Cffu is completed when all the given CompletableFutures complete.
*
* Same as [CffuFactory.cffuAllOf], providing this method is convenient for method chaining.
*
* @see CffuFactory.cffuAllOf
*/
fun <T> Array<CompletableFuture<T>>.allCffu(cffuFactory: CffuFactory): Cffu<List<T>> =
cffuFactory.cffuAllOf(*this)

/**
* Returns a new Cffu that is completed when any of the given Cffus complete, with the same result.
*
* Same as [CffuFactory.cffuAnyOf], providing this method is convenient for method chaining.
*
* @see CffuFactory.cffuAnyOf
*/
fun <T> Collection<Cffu<T>>.anyCffu(cffuFactory: CffuFactory): Cffu<T> =
cffuFactory.cffuAnyOf(*this.toTypedArray())

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

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

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

/**
* Convert [Cffu] collection elements to [CompletableFuture] by [Cffu.toCompletableFuture].
*
* Same as [CffuFactory.cffuAnyOf], providing this method is convenient for method chaining.
*
* @see CffuFactory.toCompletableFutureArray
*/
fun <T> Collection<CompletionStage<T>>.toCompletableFuture(): List<CompletableFuture<T>> =
map { it.toCompletableFuture() }

/**
* Convert [Cffu] array elements to [CompletableFuture] by [Cffu.toCompletableFuture].
*
* Same as [CffuFactory.cffuAnyOf], providing this method is convenient for method chaining.
*
* @see CffuFactory.toCompletableFutureArray
*/
fun <T, CS : CompletionStage<T>> Array<CS>.toCompletableFuture(): Array<CompletableFuture<T>> =
CffuFactory.toCompletableFutureArray(*this)

/**
* Unwrap input [Cffu] collection elements by [Cffu.cffuUnwrap].
*
* Same as [CffuFactory.cffuAnyOf], 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].
*
* Same as [CffuFactory.cffuAnyOf], providing this method is convenient for method chaining.
*
* @see CffuFactory.cffuArrayUnwrap
*/
fun <T> Array<Cffu<T>>.cffuUnwrap(): Array<CompletableFuture<T>> =
CffuFactory.cffuArrayUnwrap(*this)
5 changes: 5 additions & 0 deletions src/spotbugs-exclude-filter-file.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<FindBugsFilter>
<Match>
<Source name="~.*\.kt"/>
</Match>
</FindBugsFilter>
Loading

0 comments on commit c0336bd

Please sign in to comment.