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

commonMain.implementations.immutableList.AbstractPersistentList.kt Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2016-2019 JetBrains s.r.o.
 * Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
 */

package kotlinx.collections.immutable.implementations.immutableList

import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.PersistentList
import kotlinx.collections.immutable.mutate
import kotlinx.collections.immutable.internal.ListImplementation.checkPositionIndex

public abstract class AbstractPersistentList : PersistentList, AbstractList() {
    override fun subList(fromIndex: Int, toIndex: Int): ImmutableList {
        return super.subList(fromIndex, toIndex)
    }

    override fun addAll(elements: Collection): PersistentList {
        if (elements.isEmpty()) return this
        return mutate { it.addAll(elements) }
    }

    override fun addAll(index: Int, c: Collection): PersistentList {
        checkPositionIndex(index, size)
        if (c.isEmpty()) return this
        return mutate { it.addAll(index, c) }
    }

    override fun remove(element: E): PersistentList {
        val index = this.indexOf(element)
        if (index != -1) {
            return this.removeAt(index)
        }
        return this
    }

    override fun removeAll(elements: Collection): PersistentList {
        if (elements.isEmpty()) return this
        return removeAll { elements.contains(it) }
    }

    override fun retainAll(elements: Collection): PersistentList {
        if (elements.isEmpty()) return persistentVectorOf()
        return removeAll { !elements.contains(it) }
    }

    override fun clear(): PersistentList {
        return persistentVectorOf()
    }

    override fun contains(element: E): Boolean {
        return this.indexOf(element) != -1
    }

    override fun containsAll(elements: Collection): Boolean {
        return elements.all { this.contains(it) }
    }

    override fun iterator(): Iterator {
        return this.listIterator()
    }

    override fun listIterator(): ListIterator {
        return this.listIterator(0)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy