Collect deprecated code, remove unused private ImmutableArrayList

This commit is contained in:
Ilya Ryzhenkov
2014-12-01 16:47:44 +03:00
parent 31fb24d390
commit fbbb7eced1
10 changed files with 39 additions and 203 deletions
@@ -1,97 +0,0 @@
package kotlin
import java.util.AbstractList
private class ImmutableArrayList<T>(
private val array: Array<T>,
private val offset: Int,
private val length: Int
) : AbstractList<T>() {
{
// 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<T> {
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<T>
}
if (fromIndex == 0 && toIndex == length) {
return this
}
return ImmutableArrayList(array, offset + fromIndex, toIndex - fromIndex)
}
// TODO: efficiently implement iterator and other stuff
}
private val emptyArray = arrayOfNulls<Any?>(0)
private val emptyImmutableArrayList = ImmutableArrayList<Any?>(emptyArray, 0, 0)
public class ImmutableArrayListBuilder<T>() {
private var array = emptyArray
private var length = 0
public fun build(): List<T> {
if (length == 0) {
return emptyImmutableArrayList as List<T>
}
else {
val r = ImmutableArrayList<T>(array as Array<T>, 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 <T> listBuilder(): ImmutableArrayListBuilder<T> = ImmutableArrayListBuilder<T>()
@@ -23,6 +23,32 @@ public fun linkedList<T>(vararg values: T): LinkedList<T> = linkedListOf(*values
deprecated("Use linkedMapOf(...) instead")
public fun <K, V> linkedMap(vararg values: Pair<K, V>): LinkedHashMap<K, V> = linkedMapOf(*values)
/** Copies all characters into a [[Collection] */
deprecated("Use toList() instead.")
public fun String.toCollection(): Collection<Char> = toCollection(ArrayList<Char>(this.length()))
/**
* Returns the first character which matches the given *predicate* or *null* if none matched
*
* @includeFunctionBody ../../test/text/StringTest.kt find
*/
deprecated("Use firstOrNull instead")
public inline fun String.find(predicate: (Char) -> Boolean): Char? {
for (c in this) if (predicate(c)) return c
return null
}
/**
* Returns the first character which does not match the given *predicate* or *null* if none matched
*
* @includeFunctionBody ../../test/text/StringTest.kt findNot
*/
deprecated("Use firstOrNull instead")
public inline fun String.findNot(predicate: (Char) -> Boolean): Char? {
for (c in this) if (!predicate(c)) return c
return null
}
/**
* A helper method for creating a [[Runnable]] from a function
*/
@@ -198,29 +198,6 @@ public fun String.repeat(n: Int): String {
return sb.toString()
}
/**
* Returns the first character which matches the given *predicate* or *null* if none matched
*
* @includeFunctionBody ../../test/text/StringTest.kt find
*/
deprecated("Use firstOrNull instead")
public inline fun String.find(predicate: (Char) -> Boolean): Char? {
for (c in this) if (predicate(c)) return c
return null
}
/**
* Returns the first character which does not match the given *predicate* or *null* if none matched
*
* @includeFunctionBody ../../test/text/StringTest.kt findNot
*/
deprecated("Use firstOrNull instead")
public inline fun String.findNot(predicate: (Char) -> Boolean): Char? {
for (c in this) if (!predicate(c)) return c
return null
}
/**
* Returns an Appendable containing the everything but the first characters that satisfy the given *predicate*
*
@@ -249,10 +226,6 @@ public inline fun <T : Appendable> String.takeWhileTo(result: T, predicate: (Cha
return result
}
/** Copies all characters into a [[Collection] */
deprecated("Use toList() instead.")
public fun String.toCollection(): Collection<Char> = toCollection(ArrayList<Char>(this.length()))
/**
* Replaces every *regexp* occurence in the text with the value retruned by the given function *body* that can handle
* particular occurance using [[MatchResult]] provided.
@@ -1,62 +0,0 @@
package test.collections
import kotlin.test.*
import junit.framework.TestCase
import java.util.Random
class ImmutableArrayListTest() : TestCase() {
fun testSimple() {
val builder = ImmutableArrayListBuilder<Int>()
builder.add(17)
val list = builder.build()
assertEquals(1, list.size())
assertEquals(17, list[0])
}
fun testGet() {
for (length in 0 .. 55) {
val list = buildIntArray(length, 19)
assertEquals(length, list.size())
checkList(list, length, 19)
}
}
private fun buildIntArray(length: Int, firstValue: Int): List<Int> {
val builder = ImmutableArrayListBuilder<Int>()
for (j in 0 .. length - 1) {
builder.add(firstValue + j)
}
return builder.build()
}
private fun checkList(list: List<Int>, expectedLength: Int, expectedFirstValue: Int) {
assertEquals(expectedLength, list.size())
for (i in 0 .. expectedLength - 1) {
assertEquals(expectedFirstValue + i, list[i])
}
try {
list[expectedLength]
fail()
} catch (e: IndexOutOfBoundsException) {
// expected
}
}
fun testSublist() {
val r = Random(1)
for (i in 0 .. 200) {
val length = r.nextInt(55)
val list = buildIntArray(length, 23)
val fromIndex = r.nextInt(length + 1)
val toIndex = fromIndex + r.nextInt(length - fromIndex + 1)
val sublist = list.subList(fromIndex, toIndex)
checkList(sublist, toIndex - fromIndex, 23 + fromIndex)
}
}
}
@@ -6,16 +6,16 @@ private fun PsiElement.getTextChildRelativeOffset() =
getTextRange()!!.getStartOffset() - getParent()!!.getTextRange()!!.getStartOffset()
private fun PsiElement.getAllChildren(): List<PsiElement> {
val r = listBuilder<PsiElement>()
val r = arrayListOf<PsiElement>()
var child = getFirstChild()
while (child != null) {
r.add(child!!)
child = child!!.getNextSibling()
}
return r.build()
return r
}
private fun splitPsiImpl(psi: PsiElement, listBuilder: ImmutableArrayListBuilder<Pair<String, PsiElement>>) {
private fun splitPsiImpl(psi: PsiElement, listBuilder: MutableList<Pair<String, PsiElement>>) {
var lastPos = 0
for (child in psi.getAllChildren()) {
if (child.getTextChildRelativeOffset() > lastPos) {
@@ -32,9 +32,9 @@ private fun splitPsiImpl(psi: PsiElement, listBuilder: ImmutableArrayListBuilder
}
fun splitPsi(psi: PsiElement): List<Pair<String, PsiElement>> {
val listBuilder = listBuilder<Pair<String, PsiElement>>()
val listBuilder = arrayListOf<Pair<String, PsiElement>>()
splitPsiImpl(psi, listBuilder)
return listBuilder.build()
return listBuilder
}
@@ -8,17 +8,13 @@ abstract class HtmlTemplate() : TextTemplate() {
className: String? = null,
attributes: List<Pair<String, String>> = listOf(),
content: () -> Unit) {
val allAttributesBuilder = listBuilder<Pair<String, String>>()
val allAttributes = arrayListOf<Pair<String, String>>()
if (style != null)
allAttributesBuilder.add(Pair<String, String>("style", style))
allAttributes.add(Pair("style", style))
if (className != null)
allAttributesBuilder.add(Pair<String, String>("class", className))
allAttributes.add(Pair("class", className))
// TODO: add addAll to ListBuilder
for (attribute in attributes)
allAttributesBuilder.add(attribute)
val allAttributes = allAttributesBuilder.build()
allAttributes.addAll(attributes)
print(
if (allAttributes.isEmpty()) {
@@ -77,12 +73,12 @@ abstract class HtmlTemplate() : TextTemplate() {
tag(tagName = "span", style = style, className = className, content = content)
fun a(href: String? = null, name: String? = null, content: () -> Unit) {
val attributes = listBuilder<Pair<String, String>>()
val attributes = arrayListOf<Pair<String, String>>()
if (href != null)
attributes.add(Pair<String, String>("href", href))
attributes.add(Pair("href", href))
if (name != null)
attributes.add(Pair<String, String>("name", name))
tag(tagName = "a", attributes = attributes.build(), content = content)
attributes.add(Pair("name", name))
tag(tagName = "a", attributes = attributes, content = content)
}
}