Let ContactList<C> implement Collection<C>

This commit is contained in:
Him188 2020-05-10 15:34:06 +08:00
parent a99a83be48
commit aa2805b81f

View File

@ -23,17 +23,17 @@ import kotlin.jvm.JvmField
*/ */
@Suppress("unused") @Suppress("unused")
class ContactList<C : Contact> internal constructor(@JvmField internal val delegate: LockFreeLinkedList<C>) : class ContactList<C : Contact> internal constructor(@JvmField internal val delegate: LockFreeLinkedList<C>) :
Iterable<C> { Iterable<C>, Collection<C> {
operator fun get(id: Long): C = operator fun get(id: Long): C =
delegate.asSequence().firstOrNull { it.id == id } ?: throw NoSuchElementException("Contact id $id") delegate.asSequence().firstOrNull { it.id == id } ?: throw NoSuchElementException("Contact id $id")
fun getOrNull(id: Long): C? = delegate.getOrNull(id) fun getOrNull(id: Long): C? = delegate.getOrNull(id)
val size: Int get() = delegate.size override val size: Int get() = delegate.size
operator fun contains(element: C): Boolean = delegate.contains(element) override operator fun contains(element: C): Boolean = delegate.contains(element)
operator fun contains(id: Long): Boolean = delegate.getOrNull(id) != null operator fun contains(id: Long): Boolean = delegate.getOrNull(id) != null
fun containsAll(elements: Collection<C>): Boolean = elements.all { contains(it) } override fun containsAll(elements: Collection<C>): Boolean = elements.all { contains(it) }
fun isEmpty(): Boolean = delegate.isEmpty() override fun isEmpty(): Boolean = delegate.isEmpty()
override fun toString(): String = override fun toString(): String =
delegate.asSequence().joinToString(separator = ", ", prefix = "ContactList(", postfix = ")") delegate.asSequence().joinToString(separator = ", ", prefix = "ContactList(", postfix = ")")