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

kotlin.ImmutableArrayList.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-Beta1
Show newest version
package kotlin

import java.util.AbstractList

private class ImmutableArrayList(
        private val array: Array,
        private val offset: Int,
        private val length: Int
) : AbstractList() {
    {
        // impossible
        if (offset < 0) {
            throw IllegalArgumentException("Negative offset ($offset)")
        }
        // impossible
        if (length < 0) {
            throw IllegalArgumentException("Negative length ($length)")
        }
        // possible when builder is used from different threads
        if (offset + length > array.size) {
            throw IllegalArgumentException("offset ($offset) + length ($length) > array.length (${array.size})")
        }
    }

    protected fun indexInArray(index: Int): Int {
        if (index < 0) {
            throw IndexOutOfBoundsException("Negative index ($index)")
        }
        if (index >= length) {
            throw IndexOutOfBoundsException("index ($index) >= length ($length)")
        }
        return index + offset
    }

    public override fun get(index: Int): T = array[indexInArray(index)]

    public override fun size() : Int = length

    public override fun subList(fromIndex: Int, toIndex: Int) : MutableList {
        if (fromIndex < 0) {
            throw IndexOutOfBoundsException("Negative from index ($fromIndex)")
        }
        if (toIndex < fromIndex) {
            throw IndexOutOfBoundsException("toIndex ($toIndex) < fromIndex ($fromIndex)")
        }
        if (toIndex > length) {
            throw IndexOutOfBoundsException("fromIndex ($fromIndex) + toIndex ($toIndex) > length ($length)")
        }
        if (fromIndex == toIndex) {
            return emptyImmutableArrayList as MutableList
        }
        if (fromIndex == 0 && toIndex == length) {
            return this
        }
        return ImmutableArrayList(array, offset + fromIndex, toIndex - fromIndex)
    }

    // TODO: efficiently implement iterator and other stuff
}

// TODO: make private val, see http://youtrack.jetbrains.com/issue/KT-2028
internal val emptyArray = arrayOfNulls(0)
internal val emptyImmutableArrayList = ImmutableArrayList(emptyArray, 0, 0)

public class ImmutableArrayListBuilder() {

    private var array = emptyArray
    private var length = 0

    public fun build(): List {
        if (length == 0) {
            return emptyImmutableArrayList as List
        }
        else {
            val r = ImmutableArrayList(array as Array, 0, length)
            array = emptyArray
            length = 0
            return r
        }
    }

    public fun ensureCapacity(capacity: Int) {
        if (array.size < capacity) {
            val newSize = Math.max(capacity, Math.max(array.size * 2, 11))
            array = array.copyOf(newSize)
        }
    }

    public fun add(item: T) {
        ensureCapacity(length + 1)
        array[length] = item
        ++length
    }

}

// default list builder
public fun  listBuilder(): ImmutableArrayListBuilder = ImmutableArrayListBuilder()




© 2015 - 2024 Weber Informatics LLC | Privacy Policy