All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.co.touchlab.stately.collections.IsoMutableList.kt Maven / Gradle / Ivy

package co.touchlab.stately.collections

import co.touchlab.stately.isolate.IsolateState
import co.touchlab.stately.isolate.StateHolder
import co.touchlab.stately.isolate.StateRunner
import co.touchlab.stately.isolate.createState

open class IsoMutableList internal constructor(stateHolder: StateHolder>) :
    IsoMutableCollection(stateHolder), MutableList {
    constructor(stateRunner: StateRunner? = null, producer: () -> MutableList = { mutableListOf() }) : this(createState(stateRunner, producer))

    override fun get(index: Int): T = asAccess { it.get(index) }
    override fun indexOf(element: T): Int = asAccess { it.indexOf(element) }
    override fun lastIndexOf(element: T): Int = asAccess { it.lastIndexOf(element) }
    override fun add(index: Int, element: T) = asAccess { it.add(index, element) }
    override fun addAll(index: Int, elements: Collection): Boolean =
        asAccess { it.addAll(index, elements) }
    override fun listIterator(): MutableListIterator =
        asAccess { IsoMutableListIterator(fork(it.listIterator())) }
    override fun listIterator(index: Int): MutableListIterator =
        asAccess { IsoMutableListIterator(fork(it.listIterator(index))) }
    override fun removeAt(index: Int): T = asAccess { it.removeAt(index) }
    override fun set(index: Int, element: T): T = asAccess { it.set(index, element) }
    override fun subList(fromIndex: Int, toIndex: Int): MutableList = asAccess {
        IsoMutableList(fork(it.subList(fromIndex, toIndex)))
    }

    private inline fun  asAccess(crossinline block: (MutableList) -> R): R = access { block(it as MutableList) }
}

class IsoMutableListIterator internal constructor(stateHolder: StateHolder>) :
    IsolateState>(stateHolder), MutableListIterator {
    override fun hasPrevious(): Boolean = access { it.hasPrevious() }
    override fun nextIndex(): Int = access { it.nextIndex() }
    override fun previous(): T = access { it.previous() }
    override fun previousIndex(): Int = access { it.previousIndex() }
    override fun add(element: T) = access { it.add(element) }
    override fun hasNext(): Boolean = access { it.hasNext() }
    override fun next(): T = access { it.next() }
    override fun set(element: T) = access { it.set(element) }
    override fun remove() = access { it.remove() }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy