因此,我正在将Android Studio项目迁移到Java 8,Android API级别24和Jack工具链,以检查新功能,尤其是lambdas和CompletableFuture.
不幸的是,CompletableFuture似乎仅在API级别24上可用(我的该项目的最低API级别为16).
你知道有关带到CompletableFutureAndroid支持库的任何计划吗?它似乎是Promises模式的一个很好的解决方案.
Ste*_*bel 18
该streamsupport项目提供了一个补丁包,CompletableFuture在其streamsupport-cfuture组件可用于Android开发,支持所有设备.
要么使用
dependencies {
compile 'net.sourceforge.streamsupport:streamsupport-cfuture:1.7.0'
}
Run Code Online (Sandbox Code Playgroud)
或Android Studio 3.x更现代的分支
dependencies {
compile 'net.sourceforge.streamsupport:android-retrofuture:1.7.0'
}
Run Code Online (Sandbox Code Playgroud)
如果你想使用Android Studio 3.x desugar工具链.
CompletableFuture JDK-8211010的新Java 12异常处理方法已集成在1.7.0版中
如果您不需要CompletableFuture(例如结果链接)的所有功能,则可以使用此类(Kotlin):
/**
* A backport of Java `CompletableFuture` which works with old Androids.
*/
class CompletableFutureCompat<V> : Future<V> {
private sealed class Result<out V> {
abstract val value: V
class Ok<V>(override val value: V) : Result<V>()
class Error(val e: Throwable) : Result<Nothing>() {
override val value: Nothing
get() = throw e
}
object Cancel : Result<Nothing>() {
override val value: Nothing
get() = throw CancellationException()
}
}
/**
* Offers the completion result for [result].
*
* If this queue is not empty, the future is completed.
*/
private val completion = LinkedBlockingQueue<Result<V>>(1)
/**
* Holds the result of the computation. Takes the item from [completion] upon running and provides it as a result.
*/
private val result = FutureTask<V> { completion.peek()!!.value }
/**
* If not already completed, causes invocations of [get]
* and related methods to throw the given exception.
*
* @param ex the exception
* @return `true` if this invocation caused this CompletableFuture
* to transition to a completed state, else `false`
*/
fun completeExceptionally(ex: Throwable): Boolean {
val offered = completion.offer(Result.Error(ex))
if (offered) {
result.run()
}
return offered
}
/**
* If not already completed, completes this CompletableFuture with
* a [CancellationException].
*
* @param mayInterruptIfRunning this value has no effect in this
* implementation because interrupts are not used to control
* processing.
*
* @return `true` if this task is now cancelled
*/
override fun cancel(mayInterruptIfRunning: Boolean): Boolean {
val offered = completion.offer(Result.Cancel)
if (offered) {
result.cancel(mayInterruptIfRunning)
}
return offered
}
/**
* If not already completed, sets the value returned by [get] and related methods to the given value.
*
* @param value the result value
* @return `true` if this invocation caused this CompletableFuture
* to transition to a completed state, else `false`
*/
fun complete(value: V): Boolean {
val offered = completion.offer(Result.Ok(value))
if (offered) {
result.run()
}
return offered
}
override fun isDone(): Boolean = completion.isNotEmpty()
override fun get(): V = result.get()
override fun get(timeout: Long, unit: TimeUnit): V = result.get(timeout, unit)
override fun isCancelled(): Boolean = completion.peek() == Result.Cancel
}
Run Code Online (Sandbox Code Playgroud)