Provide toMutableList as a replacement for toArrayList.

This commit is contained in:
Ilya Gorbunov
2016-01-22 21:28:34 +03:00
parent a709ba4a6e
commit 90a239e74c
7 changed files with 184 additions and 38 deletions
+106 -18
View File
@@ -4378,7 +4378,7 @@ public fun ShortArray.reverse(): Unit {
*/
public fun <T> Array<out T>.reversed(): List<T> {
if (isEmpty()) return emptyList()
val list = toArrayList()
val list = toMutableList()
Collections.reverse(list)
return list
}
@@ -4388,7 +4388,7 @@ public fun <T> Array<out T>.reversed(): List<T> {
*/
public fun BooleanArray.reversed(): List<Boolean> {
if (isEmpty()) return emptyList()
val list = toArrayList()
val list = toMutableList()
Collections.reverse(list)
return list
}
@@ -4398,7 +4398,7 @@ public fun BooleanArray.reversed(): List<Boolean> {
*/
public fun ByteArray.reversed(): List<Byte> {
if (isEmpty()) return emptyList()
val list = toArrayList()
val list = toMutableList()
Collections.reverse(list)
return list
}
@@ -4408,7 +4408,7 @@ public fun ByteArray.reversed(): List<Byte> {
*/
public fun CharArray.reversed(): List<Char> {
if (isEmpty()) return emptyList()
val list = toArrayList()
val list = toMutableList()
Collections.reverse(list)
return list
}
@@ -4418,7 +4418,7 @@ public fun CharArray.reversed(): List<Char> {
*/
public fun DoubleArray.reversed(): List<Double> {
if (isEmpty()) return emptyList()
val list = toArrayList()
val list = toMutableList()
Collections.reverse(list)
return list
}
@@ -4428,7 +4428,7 @@ public fun DoubleArray.reversed(): List<Double> {
*/
public fun FloatArray.reversed(): List<Float> {
if (isEmpty()) return emptyList()
val list = toArrayList()
val list = toMutableList()
Collections.reverse(list)
return list
}
@@ -4438,7 +4438,7 @@ public fun FloatArray.reversed(): List<Float> {
*/
public fun IntArray.reversed(): List<Int> {
if (isEmpty()) return emptyList()
val list = toArrayList()
val list = toMutableList()
Collections.reverse(list)
return list
}
@@ -4448,7 +4448,7 @@ public fun IntArray.reversed(): List<Int> {
*/
public fun LongArray.reversed(): List<Long> {
if (isEmpty()) return emptyList()
val list = toArrayList()
val list = toMutableList()
Collections.reverse(list)
return list
}
@@ -4458,7 +4458,7 @@ public fun LongArray.reversed(): List<Long> {
*/
public fun ShortArray.reversed(): List<Short> {
if (isEmpty()) return emptyList()
val list = toArrayList()
val list = toMutableList()
Collections.reverse(list)
return list
}
@@ -6019,6 +6019,7 @@ public inline fun <K, V, M : MutableMap<in K, in V>> ShortArray.associateTo(dest
/**
* Returns an [ArrayList] of all elements.
*/
@Deprecated("Use toMutableList instead or toCollection(ArrayList()) if you need ArrayList's ensureCapacity and trimToSize.", ReplaceWith("toCollection(arrayListOf())"))
public fun <T> Array<out T>.toArrayList(): ArrayList<T> {
return ArrayList(this.asCollection())
}
@@ -6026,6 +6027,7 @@ public fun <T> Array<out T>.toArrayList(): ArrayList<T> {
/**
* Returns an [ArrayList] of all elements.
*/
@Deprecated("Use toMutableList instead or toCollection(ArrayList()) if you need ArrayList's ensureCapacity and trimToSize.", ReplaceWith("toCollection(arrayListOf())"))
public fun BooleanArray.toArrayList(): ArrayList<Boolean> {
val list = ArrayList<Boolean>(size)
for (item in this) list.add(item)
@@ -6035,6 +6037,7 @@ public fun BooleanArray.toArrayList(): ArrayList<Boolean> {
/**
* Returns an [ArrayList] of all elements.
*/
@Deprecated("Use toMutableList instead or toCollection(ArrayList()) if you need ArrayList's ensureCapacity and trimToSize.", ReplaceWith("toCollection(arrayListOf())"))
public fun ByteArray.toArrayList(): ArrayList<Byte> {
val list = ArrayList<Byte>(size)
for (item in this) list.add(item)
@@ -6044,6 +6047,7 @@ public fun ByteArray.toArrayList(): ArrayList<Byte> {
/**
* Returns an [ArrayList] of all elements.
*/
@Deprecated("Use toMutableList instead or toCollection(ArrayList()) if you need ArrayList's ensureCapacity and trimToSize.", ReplaceWith("toCollection(arrayListOf())"))
public fun CharArray.toArrayList(): ArrayList<Char> {
val list = ArrayList<Char>(size)
for (item in this) list.add(item)
@@ -6053,6 +6057,7 @@ public fun CharArray.toArrayList(): ArrayList<Char> {
/**
* Returns an [ArrayList] of all elements.
*/
@Deprecated("Use toMutableList instead or toCollection(ArrayList()) if you need ArrayList's ensureCapacity and trimToSize.", ReplaceWith("toCollection(arrayListOf())"))
public fun DoubleArray.toArrayList(): ArrayList<Double> {
val list = ArrayList<Double>(size)
for (item in this) list.add(item)
@@ -6062,6 +6067,7 @@ public fun DoubleArray.toArrayList(): ArrayList<Double> {
/**
* Returns an [ArrayList] of all elements.
*/
@Deprecated("Use toMutableList instead or toCollection(ArrayList()) if you need ArrayList's ensureCapacity and trimToSize.", ReplaceWith("toCollection(arrayListOf())"))
public fun FloatArray.toArrayList(): ArrayList<Float> {
val list = ArrayList<Float>(size)
for (item in this) list.add(item)
@@ -6071,6 +6077,7 @@ public fun FloatArray.toArrayList(): ArrayList<Float> {
/**
* Returns an [ArrayList] of all elements.
*/
@Deprecated("Use toMutableList instead or toCollection(ArrayList()) if you need ArrayList's ensureCapacity and trimToSize.", ReplaceWith("toCollection(arrayListOf())"))
public fun IntArray.toArrayList(): ArrayList<Int> {
val list = ArrayList<Int>(size)
for (item in this) list.add(item)
@@ -6080,6 +6087,7 @@ public fun IntArray.toArrayList(): ArrayList<Int> {
/**
* Returns an [ArrayList] of all elements.
*/
@Deprecated("Use toMutableList instead or toCollection(ArrayList()) if you need ArrayList's ensureCapacity and trimToSize.", ReplaceWith("toCollection(arrayListOf())"))
public fun LongArray.toArrayList(): ArrayList<Long> {
val list = ArrayList<Long>(size)
for (item in this) list.add(item)
@@ -6089,6 +6097,7 @@ public fun LongArray.toArrayList(): ArrayList<Long> {
/**
* Returns an [ArrayList] of all elements.
*/
@Deprecated("Use toMutableList instead or toCollection(ArrayList()) if you need ArrayList's ensureCapacity and trimToSize.", ReplaceWith("toCollection(arrayListOf())"))
public fun ShortArray.toArrayList(): ArrayList<Short> {
val list = ArrayList<Short>(size)
for (item in this) list.add(item)
@@ -6252,63 +6261,63 @@ public fun ShortArray.toHashSet(): HashSet<Short> {
* Returns a [List] containing all elements.
*/
public fun <T> Array<out T>.toList(): List<T> {
return this.toArrayList()
return this.toMutableList()
}
/**
* Returns a [List] containing all elements.
*/
public fun BooleanArray.toList(): List<Boolean> {
return this.toArrayList()
return this.toMutableList()
}
/**
* Returns a [List] containing all elements.
*/
public fun ByteArray.toList(): List<Byte> {
return this.toArrayList()
return this.toMutableList()
}
/**
* Returns a [List] containing all elements.
*/
public fun CharArray.toList(): List<Char> {
return this.toArrayList()
return this.toMutableList()
}
/**
* Returns a [List] containing all elements.
*/
public fun DoubleArray.toList(): List<Double> {
return this.toArrayList()
return this.toMutableList()
}
/**
* Returns a [List] containing all elements.
*/
public fun FloatArray.toList(): List<Float> {
return this.toArrayList()
return this.toMutableList()
}
/**
* Returns a [List] containing all elements.
*/
public fun IntArray.toList(): List<Int> {
return this.toArrayList()
return this.toMutableList()
}
/**
* Returns a [List] containing all elements.
*/
public fun LongArray.toList(): List<Long> {
return this.toArrayList()
return this.toMutableList()
}
/**
* Returns a [List] containing all elements.
*/
public fun ShortArray.toList(): List<Short> {
return this.toArrayList()
return this.toMutableList()
}
/**
@@ -6536,6 +6545,85 @@ public inline fun <K, V> ShortArray.toMapBy(selector: (Short) -> K, transform: (
return associateBy(selector, transform)
}
/**
* Returns a [MutableList] filled with all elements of this array.
*/
public fun <T> Array<out T>.toMutableList(): MutableList<T> {
return ArrayList(this.asCollection())
}
/**
* Returns a [MutableList] filled with all elements of this array.
*/
public fun BooleanArray.toMutableList(): MutableList<Boolean> {
val list = ArrayList<Boolean>(size)
for (item in this) list.add(item)
return list
}
/**
* Returns a [MutableList] filled with all elements of this array.
*/
public fun ByteArray.toMutableList(): MutableList<Byte> {
val list = ArrayList<Byte>(size)
for (item in this) list.add(item)
return list
}
/**
* Returns a [MutableList] filled with all elements of this array.
*/
public fun CharArray.toMutableList(): MutableList<Char> {
val list = ArrayList<Char>(size)
for (item in this) list.add(item)
return list
}
/**
* Returns a [MutableList] filled with all elements of this array.
*/
public fun DoubleArray.toMutableList(): MutableList<Double> {
val list = ArrayList<Double>(size)
for (item in this) list.add(item)
return list
}
/**
* Returns a [MutableList] filled with all elements of this array.
*/
public fun FloatArray.toMutableList(): MutableList<Float> {
val list = ArrayList<Float>(size)
for (item in this) list.add(item)
return list
}
/**
* Returns a [MutableList] filled with all elements of this array.
*/
public fun IntArray.toMutableList(): MutableList<Int> {
val list = ArrayList<Int>(size)
for (item in this) list.add(item)
return list
}
/**
* Returns a [MutableList] filled with all elements of this array.
*/
public fun LongArray.toMutableList(): MutableList<Long> {
val list = ArrayList<Long>(size)
for (item in this) list.add(item)
return list
}
/**
* Returns a [MutableList] filled with all elements of this array.
*/
public fun ShortArray.toMutableList(): MutableList<Short> {
val list = ArrayList<Short>(size)
for (item in this) list.add(item)
return list
}
/**
* Returns a [Set] of all elements.
*/
+24 -6
View File
@@ -756,7 +756,7 @@ public fun <T> MutableList<T>.reverse(): Unit {
*/
public fun <T> Iterable<T>.reversed(): List<T> {
if (this is Collection && isEmpty()) return emptyList()
val list = toArrayList()
val list = toMutableList()
Collections.reverse(list)
return list
}
@@ -787,10 +787,10 @@ public fun <T : Comparable<T>> MutableList<T>.sortDescending(): Unit {
*/
public fun <T : Comparable<T>> Iterable<T>.sorted(): List<T> {
if (this is Collection) {
if (size <= 1) return this.toArrayList()
if (size <= 1) return this.toMutableList()
return (toTypedArray<Comparable<T>>() as Array<T>).apply { sort() }.asList()
}
return toArrayList().apply { sort() }
return toMutableList().apply { sort() }
}
/**
@@ -819,10 +819,10 @@ public fun <T : Comparable<T>> Iterable<T>.sortedDescending(): List<T> {
*/
public fun <T> Iterable<T>.sortedWith(comparator: Comparator<in T>): List<T> {
if (this is Collection) {
if (size <= 1) return this.toArrayList()
if (size <= 1) return this.toMutableList()
return (toTypedArray<Any?>() as Array<T>).apply { sortWith(comparator) }.asList()
}
return toArrayList().apply { sortWith(comparator) }
return toMutableList().apply { sortWith(comparator) }
}
/**
@@ -983,6 +983,7 @@ public inline fun <T, K, V, M : MutableMap<in K, in V>> Iterable<T>.associateTo(
/**
* Returns an [ArrayList] of all elements.
*/
@Deprecated("Use toMutableList instead or toCollection(ArrayList()) if you need ArrayList's ensureCapacity and trimToSize.", ReplaceWith("toCollection(arrayListOf())"))
public fun <T> Collection<T>.toArrayList(): ArrayList<T> {
return ArrayList(this)
}
@@ -990,6 +991,7 @@ public fun <T> Collection<T>.toArrayList(): ArrayList<T> {
/**
* Returns an [ArrayList] of all elements.
*/
@Deprecated("Use toMutableList instead or toCollection(ArrayList()) if you need ArrayList's ensureCapacity and trimToSize.", ReplaceWith("toCollection(arrayListOf())"))
public fun <T> Iterable<T>.toArrayList(): ArrayList<T> {
if (this is Collection<T>)
return this.toArrayList()
@@ -1017,7 +1019,7 @@ public fun <T> Iterable<T>.toHashSet(): HashSet<T> {
* Returns a [List] containing all elements.
*/
public fun <T> Iterable<T>.toList(): List<T> {
return this.toArrayList()
return this.toMutableList()
}
/**
@@ -1045,6 +1047,22 @@ public inline fun <T, K, V> Iterable<T>.toMapBy(selector: (T) -> K, transform: (
return associateBy(selector, transform)
}
/**
* Returns a [MutableList] filled with all elements of this collection.
*/
public fun <T> Collection<T>.toMutableList(): MutableList<T> {
return ArrayList(this)
}
/**
* Returns a [MutableList] filled with all elements of this collection.
*/
public fun <T> Iterable<T>.toMutableList(): MutableList<T> {
if (this is Collection<T>)
return this.toMutableList()
return toCollection(ArrayList<T>())
}
/**
* Returns a [Set] of all elements.
*/
+11 -3
View File
@@ -381,7 +381,7 @@ public fun <T> Sequence<T>.takeWhile(predicate: (T) -> Boolean): Sequence<T> {
public fun <T : Comparable<T>> Sequence<T>.sorted(): Sequence<T> {
return object : Sequence<T> {
override fun iterator(): Iterator<T> {
val sortedList = this@sorted.toArrayList()
val sortedList = this@sorted.toMutableList()
sortedList.sort()
return sortedList.iterator()
}
@@ -415,7 +415,7 @@ public fun <T : Comparable<T>> Sequence<T>.sortedDescending(): Sequence<T> {
public fun <T> Sequence<T>.sortedWith(comparator: Comparator<in T>): Sequence<T> {
return object : Sequence<T> {
override fun iterator(): Iterator<T> {
val sortedList = this@sortedWith.toArrayList()
val sortedList = this@sortedWith.toMutableList()
sortedList.sortWith(comparator)
return sortedList.iterator()
}
@@ -489,6 +489,7 @@ public inline fun <T, K, V, M : MutableMap<in K, in V>> Sequence<T>.associateTo(
/**
* Returns an [ArrayList] of all elements.
*/
@Deprecated("Use toMutableList instead or toCollection(ArrayList()) if you need ArrayList's ensureCapacity and trimToSize.", ReplaceWith("toCollection(arrayListOf())"))
public fun <T> Sequence<T>.toArrayList(): ArrayList<T> {
return toCollection(ArrayList<T>())
}
@@ -514,7 +515,7 @@ public fun <T> Sequence<T>.toHashSet(): HashSet<T> {
* Returns a [List] containing all elements.
*/
public fun <T> Sequence<T>.toList(): List<T> {
return this.toArrayList()
return this.toMutableList()
}
/**
@@ -542,6 +543,13 @@ public inline fun <T, K, V> Sequence<T>.toMapBy(selector: (T) -> K, transform: (
return associateBy(selector, transform)
}
/**
* Returns a [MutableList] filled with all elements of this sequence.
*/
public fun <T> Sequence<T>.toMutableList(): MutableList<T> {
return toCollection(ArrayList<T>())
}
/**
* Returns a [Set] of all elements.
*/
+9 -1
View File
@@ -561,6 +561,7 @@ public inline fun <K, V, M : MutableMap<in K, in V>> CharSequence.associateTo(de
/**
* Returns an [ArrayList] of all characters.
*/
@Deprecated("Use toMutableList instead or toCollection(ArrayList()) if you need ArrayList's ensureCapacity and trimToSize.", ReplaceWith("toCollection(arrayListOf())"))
public fun CharSequence.toArrayList(): ArrayList<Char> {
return toCollection(ArrayList<Char>(length))
}
@@ -586,7 +587,7 @@ public fun CharSequence.toHashSet(): HashSet<Char> {
* Returns a [List] containing all characters.
*/
public fun CharSequence.toList(): List<Char> {
return this.toArrayList()
return this.toMutableList()
}
/**
@@ -614,6 +615,13 @@ public inline fun <K, V> CharSequence.toMapBy(selector: (Char) -> K, transform:
return associateBy(selector, transform)
}
/**
* Returns a [MutableList] filled with all characters of this char sequence.
*/
public fun CharSequence.toMutableList(): MutableList<Char> {
return toCollection(ArrayList<Char>(length))
}
/**
* Returns a [Set] of all characters.
*/