mirror of
https://github.com/corda/corda.git
synced 2024-12-21 13:57:54 +00:00
Minor: add a few utilities for working with listenable futures
This commit is contained in:
parent
1f8fd4b578
commit
de4427c240
@ -31,9 +31,19 @@ val Int.seconds: Duration get() = Duration.ofSeconds(this.toLong())
|
||||
*/
|
||||
fun random63BitValue(): Long = Math.abs(SecureRandom.getInstanceStrong().nextLong())
|
||||
|
||||
fun <T> ListenableFuture<T>.whenComplete(executor: Executor? = null, body: (T) -> Unit) {
|
||||
addListener(Runnable { body(get()) }, executor ?: RunOnCallerThread)
|
||||
// Some utilities for working with Guava listenable futures.
|
||||
fun <T> ListenableFuture<T>.then(executor: Executor, body: () -> Unit) = addListener(Runnable(body), executor)
|
||||
fun <T> ListenableFuture<T>.success(executor: Executor, body: (T) -> Unit) = then(executor) {
|
||||
val r = try { get() } catch(e: Throwable) { return@then }
|
||||
body(r)
|
||||
}
|
||||
fun <T> ListenableFuture<T>.failure(executor: Executor, body: (Throwable) -> Unit) = then(executor) {
|
||||
try { get() } catch(e: Throwable) { body(e) }
|
||||
}
|
||||
infix fun <T> ListenableFuture<T>.then(body: () -> Unit): ListenableFuture<T> = apply { then(RunOnCallerThread, body) }
|
||||
infix fun <T> ListenableFuture<T>.success(body: (T) -> Unit): ListenableFuture<T> = apply { success(RunOnCallerThread, body) }
|
||||
infix fun <T> ListenableFuture<T>.failure(body: (Throwable) -> Unit): ListenableFuture<T> = apply { failure(RunOnCallerThread, body) }
|
||||
|
||||
|
||||
/** Executes the given block and sets the future to either the result, or any exception that was thrown. */
|
||||
fun <T> SettableFuture<T>.setFrom(logger: Logger? = null, block: () -> T): SettableFuture<T> {
|
||||
|
@ -106,13 +106,17 @@ fun main(args: Array<String>) {
|
||||
val future = TwoPartyTradeProtocol.runBuyer(node.smm, timestampingAuthority, replyTo, 100.DOLLARS,
|
||||
CommercialPaper.State::class.java, buyerSessionID)
|
||||
|
||||
future.whenComplete {
|
||||
future success {
|
||||
println()
|
||||
println("Purchase complete - we are a happy customer! Final transaction is:")
|
||||
println()
|
||||
println(Emoji.renderIfSupported(it.tx))
|
||||
println()
|
||||
println("Waiting for another seller to connect. Or press Ctrl-C to shut me down.")
|
||||
} failure {
|
||||
println()
|
||||
println("Something went wrong whilst trading!")
|
||||
println()
|
||||
}
|
||||
|
||||
node.net.send("test.junktrade.initiate", replyTo, buyerSessionID)
|
||||
@ -139,7 +143,7 @@ fun main(args: Array<String>) {
|
||||
val future = TwoPartyTradeProtocol.runSeller(node.smm, timestampingAuthority,
|
||||
otherSide, commercialPaper, 100.DOLLARS, cpOwnerKey, sessionID)
|
||||
|
||||
future.whenComplete {
|
||||
future success {
|
||||
println()
|
||||
println("Sale completed - we have a happy customer!")
|
||||
println()
|
||||
@ -148,6 +152,10 @@ fun main(args: Array<String>) {
|
||||
println(Emoji.renderIfSupported(it.tx))
|
||||
println()
|
||||
node.stop()
|
||||
} failure {
|
||||
println()
|
||||
println("Something went wrong whilst trading!")
|
||||
println()
|
||||
}
|
||||
}
|
||||
println()
|
||||
|
Loading…
Reference in New Issue
Block a user