blob: 947644b30dbeaaa5209e34001a9794497bd09dfe (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
/*
* Copyright © 2017-2023 WireGuard LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package com.wireguard.android.databinding
import androidx.databinding.ObservableArrayList
/**
* ArrayList that allows looking up elements by some key property. As the key property must always
* be retrievable, this list cannot hold `null` elements. Because this class places no
* restrictions on the order or duplication of keys, lookup by key, as well as all list modification
* operations, require O(n) time.
*/
open class ObservableKeyedArrayList<K, E : Keyed<out K>> : ObservableArrayList<E>() {
fun containsKey(key: K) = indexOfKey(key) >= 0
operator fun get(key: K): E? {
val index = indexOfKey(key)
return if (index >= 0) get(index) else null
}
open fun indexOfKey(key: K): Int {
val iterator = listIterator()
while (iterator.hasNext()) {
val index = iterator.nextIndex()
if (iterator.next()!!.key == key)
return index
}
return -1
}
}
|