mirror of
https://github.com/corda/corda.git
synced 2025-06-19 07:38:22 +00:00
Added noneOrSingle extension method, which returns a single element, null if no elements found and throws if more than one element found
This commit is contained in:
@ -83,6 +83,33 @@ fun <T> List<T>.indexOfOrThrow(item: T): Int {
|
|||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the single element matching the given [predicate], or `null` if element was not found,
|
||||||
|
* or throws if more than one element was found.
|
||||||
|
*/
|
||||||
|
fun <T> Iterable<T>.noneOrSingle(predicate: (T) -> Boolean): T? {
|
||||||
|
var single: T? = null
|
||||||
|
for (element in this) {
|
||||||
|
if (predicate(element)) {
|
||||||
|
if (single == null) {
|
||||||
|
single = element
|
||||||
|
} else throw IllegalArgumentException("Collection contains more than one matching element.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return single
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns single element, or `null` if element was not found, or throws if more than one element was found. */
|
||||||
|
fun <T> Iterable<T>.noneOrSingle(): T? {
|
||||||
|
var single: T? = null
|
||||||
|
for (element in this) {
|
||||||
|
if (single == null) {
|
||||||
|
single = element
|
||||||
|
} else throw IllegalArgumentException("Collection contains more than one matching element.")
|
||||||
|
}
|
||||||
|
return single
|
||||||
|
}
|
||||||
|
|
||||||
// An alias that can sometimes make code clearer to read.
|
// An alias that can sometimes make code clearer to read.
|
||||||
val RunOnCallerThread = MoreExecutors.directExecutor()
|
val RunOnCallerThread = MoreExecutors.directExecutor()
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ import core.crypto.DigitalSignature
|
|||||||
import core.crypto.SignedData
|
import core.crypto.SignedData
|
||||||
import core.crypto.signWithECDSA
|
import core.crypto.signWithECDSA
|
||||||
import core.messaging.MessagingService
|
import core.messaging.MessagingService
|
||||||
|
import core.noneOrSingle
|
||||||
import core.serialization.SerializedBytes
|
import core.serialization.SerializedBytes
|
||||||
import core.serialization.deserialize
|
import core.serialization.deserialize
|
||||||
import core.serialization.serialize
|
import core.serialization.serialize
|
||||||
@ -64,12 +65,11 @@ class NotaryService(net: MessagingService,
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun validateTimestamp(tx: WireTransaction) {
|
private fun validateTimestamp(tx: WireTransaction) {
|
||||||
// Need to have at most one timestamp command
|
val timestampCmd = try {
|
||||||
val timestampCmds = tx.commands.filter { it.value is TimestampCommand }
|
tx.commands.noneOrSingle { it.value is TimestampCommand } ?: return
|
||||||
if (timestampCmds.count() > 1)
|
} catch (e: IllegalArgumentException) {
|
||||||
throw NotaryException(NotaryError.MoreThanOneTimestamp())
|
throw NotaryException(NotaryError.MoreThanOneTimestamp())
|
||||||
|
}
|
||||||
val timestampCmd = timestampCmds.singleOrNull() ?: return
|
|
||||||
if (!timestampCmd.signers.contains(identity.owningKey))
|
if (!timestampCmd.signers.contains(identity.owningKey))
|
||||||
throw NotaryException(NotaryError.NotForMe())
|
throw NotaryException(NotaryError.NotForMe())
|
||||||
if (!timestampChecker.isValid(timestampCmd.value as TimestampCommand))
|
if (!timestampChecker.isValid(timestampCmd.value as TimestampCommand))
|
||||||
|
42
src/test/kotlin/core/utilities/CollectionExtensionTests.kt
Normal file
42
src/test/kotlin/core/utilities/CollectionExtensionTests.kt
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package core.utilities
|
||||||
|
|
||||||
|
import core.indexOfOrThrow
|
||||||
|
import core.noneOrSingle
|
||||||
|
import org.junit.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
import kotlin.test.assertFailsWith
|
||||||
|
|
||||||
|
class CollectionExtensionTests {
|
||||||
|
@Test
|
||||||
|
fun `noneOrSingle returns a single item`() {
|
||||||
|
val collection = listOf(1)
|
||||||
|
assertEquals(collection.noneOrSingle(), 1)
|
||||||
|
assertEquals(collection.noneOrSingle { it == 1 }, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `noneOrSingle returns null if item not found`() {
|
||||||
|
val collection = emptyList<Int>()
|
||||||
|
assertEquals(collection.noneOrSingle(), null)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `noneOrSingle throws if more than one item found`() {
|
||||||
|
val collection = listOf(1, 2)
|
||||||
|
assertFailsWith<IllegalArgumentException> { collection.noneOrSingle() }
|
||||||
|
assertFailsWith<IllegalArgumentException> { collection.noneOrSingle { it > 0 } }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `indexOfOrThrow returns index of the given item`() {
|
||||||
|
val collection = listOf(1, 2)
|
||||||
|
assertEquals(collection.indexOfOrThrow(1), 0)
|
||||||
|
assertEquals(collection.indexOfOrThrow(2), 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `indexOfOrThrow throws if the given item is not found`() {
|
||||||
|
val collection = listOf(1)
|
||||||
|
assertFailsWith<IllegalArgumentException> { collection.indexOfOrThrow(2) }
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user