Minor: add a simple List<T>.indexOfOrThrow() utility and use it in a new LedgerTransaction.outRef helper.

This commit is contained in:
Mike Hearn 2016-02-29 22:00:35 +01:00
parent e2deea598e
commit 02e9473201
2 changed files with 15 additions and 1 deletions

View File

@ -85,8 +85,15 @@ data class WireTransaction(val inputs: List<StateRef>,
return SignedTransaction(serialized, withSigs)
}
/** Returns a [StateAndRef] for the given output index. */
@Suppress("UNCHECKED_CAST")
fun <T : ContractState> outRef(index: Int) = StateAndRef(outputs[index] as T, StateRef(id, index))
fun <T : ContractState> outRef(index: Int): StateAndRef<T> {
require(index >= 0 && index < outputs.size)
return StateAndRef(outputs[index] as T, StateRef(id, index))
}
/** Returns a [StateAndRef] for the requested output state, or throws [IllegalArgumentException] if not found. */
fun <T : ContractState> outRef(state: ContractState): StateAndRef<T> = outRef(outputs.indexOfOrThrow(state))
override fun toString(): String {
val buf = StringBuilder()

View File

@ -64,6 +64,13 @@ fun <T> SettableFuture<T>.setFrom(logger: Logger? = null, block: () -> T): Setta
// Simple infix function to add back null safety that the JDK lacks: timeA until timeB
infix fun Temporal.until(endExclusive: Temporal) = Duration.between(this, endExclusive)
/** Returns the index of the given item or throws [IllegalArgumentException] if not found. */
fun <T> List<T>.indexOfOrThrow(item: T): Int {
val i = indexOf(item)
require(i != -1)
return i
}
// An alias that can sometimes make code clearer to read.
val RunOnCallerThread = MoreExecutors.directExecutor()