mirror of https://github.com/tuskyapp/Tusky.git
2 changed files with 0 additions and 165 deletions
@ -1,74 +0,0 @@
|
||||
package com.keylesspalace.tusky.util |
||||
|
||||
import androidx.arch.core.util.Function |
||||
|
||||
/** |
||||
* This list implementation can help to keep two lists in sync - like real models and view models. |
||||
* |
||||
* Every operation on the main list triggers update of the supplementary list (but not vice versa). |
||||
* |
||||
* This makes sure that the main list is always the source of truth. |
||||
* |
||||
* Main list is projected to the supplementary list by the passed mapper function. |
||||
* |
||||
* Paired list is newer actually exposed and clients are provided with `getPairedCopy()`, |
||||
* `getPairedItem()` and `setPairedItem()`. This prevents modifications of the |
||||
* supplementary list size so lists are always have the same length. |
||||
* |
||||
* This implementation will not try to recover from exceptional cases so lists may be out of sync |
||||
* after the exception. |
||||
* |
||||
* It is most useful with immutable data because we cannot track changes inside stored objects. |
||||
* |
||||
* @param T type of elements in the main list |
||||
* @param V type of elements in supplementary list |
||||
* @param mapper Function, which will be used to translate items from the main list to the |
||||
* supplementary one. |
||||
* @constructor |
||||
*/ |
||||
class PairedList<T, V> (private val mapper: Function<T, out V>) : AbstractMutableList<T>() { |
||||
private val main: MutableList<T> = ArrayList() |
||||
private val synced: MutableList<V> = ArrayList() |
||||
|
||||
val pairedCopy: List<V> |
||||
get() = ArrayList(synced) |
||||
|
||||
fun getPairedItem(index: Int): V { |
||||
return synced[index] |
||||
} |
||||
|
||||
fun getPairedItemOrNull(index: Int): V? { |
||||
return synced.getOrNull(index) |
||||
} |
||||
|
||||
fun setPairedItem(index: Int, element: V) { |
||||
synced[index] = element |
||||
} |
||||
|
||||
override fun get(index: Int): T { |
||||
return main[index] |
||||
} |
||||
|
||||
override fun set(index: Int, element: T): T { |
||||
synced[index] = mapper.apply(element) |
||||
return main.set(index, element) |
||||
} |
||||
|
||||
override fun add(element: T): Boolean { |
||||
synced.add(mapper.apply(element)) |
||||
return main.add(element) |
||||
} |
||||
|
||||
override fun add(index: Int, element: T) { |
||||
synced.add(index, mapper.apply(element)) |
||||
main.add(index, element) |
||||
} |
||||
|
||||
override fun removeAt(index: Int): T { |
||||
synced.removeAt(index) |
||||
return main.removeAt(index) |
||||
} |
||||
|
||||
override val size: Int |
||||
get() = main.size |
||||
} |
||||
@ -1,91 +0,0 @@
|
||||
package com.keylesspalace.tusky.util |
||||
|
||||
import org.junit.Assert.assertEquals |
||||
import org.junit.Assert.assertNull |
||||
import org.junit.Before |
||||
import org.junit.Test |
||||
|
||||
/** |
||||
* Tests for PairedList, with a mapper that multiples everything by 2. |
||||
*/ |
||||
class PairedListTest { |
||||
private lateinit var pairedList: PairedList<Int, Int> |
||||
|
||||
@Before |
||||
fun beforeEachTest() { |
||||
pairedList = PairedList { it * 2 } |
||||
for (i in 0..10) { |
||||
pairedList.add(i) |
||||
} |
||||
} |
||||
|
||||
@Test |
||||
fun pairedCopy() { |
||||
val copy = pairedList.pairedCopy |
||||
for (i in 0..10) { |
||||
assertEquals(i * 2, copy[i]) |
||||
} |
||||
} |
||||
|
||||
@Test |
||||
fun getPairedItem() { |
||||
for (i in 0..10) { |
||||
assertEquals(i * 2, pairedList.getPairedItem(i)) |
||||
} |
||||
} |
||||
|
||||
@Test |
||||
fun getPairedItemOrNull() { |
||||
for (i in 0..10) { |
||||
assertEquals(i * 2, pairedList.getPairedItem(i)) |
||||
} |
||||
assertNull(pairedList.getPairedItemOrNull(11)) |
||||
} |
||||
|
||||
@Test |
||||
fun setPairedItem() { |
||||
pairedList.setPairedItem(2, 2) |
||||
assertEquals(2, pairedList.getPairedItem(2)) |
||||
} |
||||
|
||||
@Test |
||||
fun get() { |
||||
for (i in 0..10) { |
||||
assertEquals(i, pairedList[i]) |
||||
} |
||||
} |
||||
|
||||
@Test |
||||
fun set() { |
||||
assertEquals(0, pairedList[0]) |
||||
pairedList[0] = 10 |
||||
assertEquals(10, pairedList[0]) |
||||
assertEquals(20, pairedList.getPairedItem(0)) |
||||
} |
||||
|
||||
@Test |
||||
fun add() { |
||||
pairedList.add(11) |
||||
assertEquals(11, pairedList[11]) |
||||
assertEquals(22, pairedList.getPairedItem(11)) |
||||
} |
||||
|
||||
@Test |
||||
fun addAtIndex() { |
||||
pairedList.add(11, 11) |
||||
assertEquals(11, pairedList[11]) |
||||
assertEquals(22, pairedList.getPairedItem(11)) |
||||
} |
||||
|
||||
@Test |
||||
fun removeAt() { |
||||
pairedList.removeAt(5) |
||||
assertEquals(6, pairedList[5]) |
||||
assertEquals(12, pairedList.getPairedItem(5)) |
||||
} |
||||
|
||||
@Test |
||||
fun size() { |
||||
assertEquals(11, pairedList.size) |
||||
} |
||||
} |
||||
Loading…
Reference in new issue