Use bare types in check and remove unneeded cast after.

Do not check whether a sequence is actually a collection or list.
This commit is contained in:
Ilya Gorbunov
2015-07-22 23:19:22 +03:00
parent e799c92131
commit 065945176e
2 changed files with 95 additions and 93 deletions
+39 -75
View File
@@ -477,7 +477,7 @@ public fun ShortArray.contains(element: Short): Boolean {
* Returns `true` if [element] is found in the collection. * Returns `true` if [element] is found in the collection.
*/ */
public fun <T> Iterable<T>.contains(element: T): Boolean { public fun <T> Iterable<T>.contains(element: T): Boolean {
if (this is Collection<*>) if (this is Collection)
return contains(element) return contains(element)
return indexOf(element) >= 0 return indexOf(element) >= 0
} }
@@ -486,8 +486,6 @@ public fun <T> Iterable<T>.contains(element: T): Boolean {
* Returns `true` if [element] is found in the collection. * Returns `true` if [element] is found in the collection.
*/ */
public fun <T> Sequence<T>.contains(element: T): Boolean { public fun <T> Sequence<T>.contains(element: T): Boolean {
if (this is Collection<*>)
return contains(element)
return indexOf(element) >= 0 return indexOf(element) >= 0
} }
@@ -558,7 +556,7 @@ public fun ShortArray.elementAt(index: Int): Short {
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection.
*/ */
public fun <T> Iterable<T>.elementAt(index: Int): T { public fun <T> Iterable<T>.elementAt(index: Int): T {
if (this is List<T>) if (this is List)
return get(index) return get(index)
return elementAtOrElse(index) { throw IndexOutOfBoundsException("Collection doesn't contain element at index $index.") } return elementAtOrElse(index) { throw IndexOutOfBoundsException("Collection doesn't contain element at index $index.") }
} }
@@ -651,7 +649,7 @@ public inline fun ShortArray.elementAtOrElse(index: Int, defaultValue: (Int) ->
* Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection.
*/ */
public fun <T> Iterable<T>.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { public fun <T> Iterable<T>.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T {
if (this is List<T>) if (this is List)
return this.getOrElse(index, defaultValue) return this.getOrElse(index, defaultValue)
if (index < 0) if (index < 0)
return defaultValue(index) return defaultValue(index)
@@ -762,7 +760,7 @@ public fun ShortArray.elementAtOrNull(index: Int): Short? {
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection.
*/ */
public fun <T> Iterable<T>.elementAtOrNull(index: Int): T? { public fun <T> Iterable<T>.elementAtOrNull(index: Int): T? {
if (this is List<T>) if (this is List)
return this.getOrNull(index) return this.getOrNull(index)
if (index < 0) if (index < 0)
return null return null
@@ -1077,11 +1075,11 @@ public fun ShortArray.first(): Short {
*/ */
public fun <T> Iterable<T>.first(): T { public fun <T> Iterable<T>.first(): T {
when (this) { when (this) {
is List<*> -> { is List -> {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Collection is empty.")
else else
return this[0] as T return this[0]
} }
else -> { else -> {
val iterator = iterator() val iterator = iterator()
@@ -1107,20 +1105,10 @@ public fun <T> List<T>.first(): T {
* @throws [NoSuchElementException] if the collection is empty. * @throws [NoSuchElementException] if the collection is empty.
*/ */
public fun <T> Sequence<T>.first(): T { public fun <T> Sequence<T>.first(): T {
when (this) { val iterator = iterator()
is List<*> -> { if (!iterator.hasNext())
if (isEmpty()) throw NoSuchElementException("Sequence is empty.")
throw NoSuchElementException("Collection is empty.") return iterator.next()
else
return this[0] as T
}
else -> {
val iterator = iterator()
if (!iterator.hasNext())
throw NoSuchElementException("Collection is empty.")
return iterator.next()
}
}
} }
/** /**
@@ -1309,11 +1297,11 @@ public fun ShortArray.firstOrNull(): Short? {
*/ */
public fun <T> Iterable<T>.firstOrNull(): T? { public fun <T> Iterable<T>.firstOrNull(): T? {
when (this) { when (this) {
is List<*> -> { is List -> {
if (isEmpty()) if (isEmpty())
return null return null
else else
return this[0] as T return this[0]
} }
else -> { else -> {
val iterator = iterator() val iterator = iterator()
@@ -1335,20 +1323,10 @@ public fun <T> List<T>.firstOrNull(): T? {
* Returns the first element, or `null` if the collection is empty. * Returns the first element, or `null` if the collection is empty.
*/ */
public fun <T> Sequence<T>.firstOrNull(): T? { public fun <T> Sequence<T>.firstOrNull(): T? {
when (this) { val iterator = iterator()
is List<*> -> { if (!iterator.hasNext())
if (isEmpty()) return null
return null return iterator.next()
else
return this[0] as T
}
else -> {
val iterator = iterator()
if (!iterator.hasNext())
return null
return iterator.next()
}
}
} }
/** /**
@@ -2164,11 +2142,11 @@ public fun ShortArray.last(): Short {
*/ */
public fun <T> Iterable<T>.last(): T { public fun <T> Iterable<T>.last(): T {
when (this) { when (this) {
is List<*> -> { is List -> {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Collection is empty.")
else else
return this[this.lastIndex] as T return this[this.lastIndex]
} }
else -> { else -> {
val iterator = iterator() val iterator = iterator()
@@ -2199,7 +2177,7 @@ public fun <T> List<T>.last(): T {
public fun <T> Sequence<T>.last(): T { public fun <T> Sequence<T>.last(): T {
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) if (!iterator.hasNext())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Sequence is empty.")
var last = iterator.next() var last = iterator.next()
while (iterator.hasNext()) while (iterator.hasNext())
last = iterator.next() last = iterator.next()
@@ -2329,7 +2307,7 @@ public inline fun ShortArray.last(predicate: (Short) -> Boolean): Short {
* @throws [NoSuchElementException] if no such element is found. * @throws [NoSuchElementException] if no such element is found.
*/ */
public inline fun <T> Iterable<T>.last(predicate: (T) -> Boolean): T { public inline fun <T> Iterable<T>.last(predicate: (T) -> Boolean): T {
if (this is List<T>) if (this is List)
return this.last(predicate) return this.last(predicate)
var last: T? = null var last: T? = null
var found = false var found = false
@@ -2601,7 +2579,7 @@ public fun ShortArray.lastOrNull(): Short? {
*/ */
public fun <T> Iterable<T>.lastOrNull(): T? { public fun <T> Iterable<T>.lastOrNull(): T? {
when (this) { when (this) {
is List<*> -> return if (isEmpty()) null else this[size() - 1] as T is List -> return if (isEmpty()) null else this[size() - 1]
else -> { else -> {
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) if (!iterator.hasNext())
@@ -2744,7 +2722,7 @@ public inline fun ShortArray.lastOrNull(predicate: (Short) -> Boolean): Short? {
* Returns the last element matching the given [predicate], or `null` if no such element was found. * Returns the last element matching the given [predicate], or `null` if no such element was found.
*/ */
public inline fun <T> Iterable<T>.lastOrNull(predicate: (T) -> Boolean): T? { public inline fun <T> Iterable<T>.lastOrNull(predicate: (T) -> Boolean): T? {
if (this is List<T>) if (this is List)
return this.lastOrNull(predicate) return this.lastOrNull(predicate)
var last: T? = null var last: T? = null
for (element in this) { for (element in this) {
@@ -2896,9 +2874,9 @@ public fun ShortArray.single(): Short {
*/ */
public fun <T> Iterable<T>.single(): T { public fun <T> Iterable<T>.single(): T {
when (this) { when (this) {
is List<*> -> return when (size()) { is List -> return when (size()) {
0 -> throw NoSuchElementException("Collection is empty.") 0 -> throw NoSuchElementException("Collection is empty.")
1 -> this[0] as T 1 -> this[0]
else -> throw IllegalArgumentException("Collection has more than one element.") else -> throw IllegalArgumentException("Collection has more than one element.")
} }
else -> { else -> {
@@ -2928,22 +2906,13 @@ public fun <T> List<T>.single(): T {
* Returns the single element, or throws an exception if the collection is empty or has more than one element. * Returns the single element, or throws an exception if the collection is empty or has more than one element.
*/ */
public fun <T> Sequence<T>.single(): T { public fun <T> Sequence<T>.single(): T {
when (this) { val iterator = iterator()
is List<*> -> return when (size()) { if (!iterator.hasNext())
0 -> throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Sequence is empty.")
1 -> this[0] as T var single = iterator.next()
else -> throw IllegalArgumentException("Collection has more than one element.") if (iterator.hasNext())
} throw IllegalArgumentException("Sequence has more than one element.")
else -> { return single
val iterator = iterator()
if (!iterator.hasNext())
throw NoSuchElementException("Collection is empty.")
var single = iterator.next()
if (iterator.hasNext())
throw IllegalArgumentException("Collection has more than one element.")
return single
}
}
} }
/** /**
@@ -3229,7 +3198,7 @@ public fun ShortArray.singleOrNull(): Short? {
*/ */
public fun <T> Iterable<T>.singleOrNull(): T? { public fun <T> Iterable<T>.singleOrNull(): T? {
when (this) { when (this) {
is List<*> -> return if (size() == 1) this[0] as T else null is List -> return if (size() == 1) this[0] else null
else -> { else -> {
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) if (!iterator.hasNext())
@@ -3253,18 +3222,13 @@ public fun <T> List<T>.singleOrNull(): T? {
* Returns single element, or `null` if the collection is empty or has more than one element. * Returns single element, or `null` if the collection is empty or has more than one element.
*/ */
public fun <T> Sequence<T>.singleOrNull(): T? { public fun <T> Sequence<T>.singleOrNull(): T? {
when (this) { val iterator = iterator()
is List<*> -> return if (size() == 1) this[0] as T else null if (!iterator.hasNext())
else -> { return null
val iterator = iterator() var single = iterator.next()
if (!iterator.hasNext()) if (iterator.hasNext())
return null return null
var single = iterator.next() return single
if (iterator.hasNext())
return null
return single
}
}
} }
/** /**
@@ -10,13 +10,13 @@ fun elements(): List<GenericFunction> {
returns("Boolean") returns("Boolean")
body { body {
""" """
if (this is Collection<*>) if (this is Collection)
return contains(element) return contains(element)
return indexOf(element) >= 0 return indexOf(element) >= 0
""" """
} }
exclude(Strings, Lists, Collections) exclude(Strings, Lists, Collections)
body(ArraysOfPrimitives, ArraysOfObjects) { body(ArraysOfPrimitives, ArraysOfObjects, Sequences) {
""" """
return indexOf(element) >= 0 return indexOf(element) >= 0
""" """
@@ -181,7 +181,7 @@ fun elements(): List<GenericFunction> {
returns("T") returns("T")
body { body {
""" """
if (this is List<T>) if (this is List)
return get(index) return get(index)
return elementAtOrElse(index) { throw IndexOutOfBoundsException("Collection doesn't contain element at index $index.") } return elementAtOrElse(index) { throw IndexOutOfBoundsException("Collection doesn't contain element at index $index.") }
@@ -204,7 +204,7 @@ fun elements(): List<GenericFunction> {
returns("T") returns("T")
body { body {
""" """
if (this is List<T>) if (this is List)
return this.getOrElse(index, defaultValue) return this.getOrElse(index, defaultValue)
if (index < 0) if (index < 0)
return defaultValue(index) return defaultValue(index)
@@ -258,7 +258,7 @@ fun elements(): List<GenericFunction> {
returns("T?") returns("T?")
body { body {
""" """
if (this is List<T>) if (this is List)
return this.getOrNull(index) return this.getOrNull(index)
if (index < 0) if (index < 0)
return null return null
@@ -316,11 +316,11 @@ fun elements(): List<GenericFunction> {
body { body {
""" """
when (this) { when (this) {
is List<*> -> { is List -> {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Collection is empty.")
else else
return this[0] as T return this[0]
} }
else -> { else -> {
val iterator = iterator() val iterator = iterator()
@@ -338,6 +338,14 @@ fun elements(): List<GenericFunction> {
return this[0] return this[0]
""" """
} }
body(Sequences) {
"""
val iterator = iterator()
if (!iterator.hasNext())
throw NoSuchElementException("Sequence is empty.")
return iterator.next()
"""
}
} }
templates add f("firstOrNull()") { templates add f("firstOrNull()") {
doc { "Returns the first element, or `null` if the collection is empty." } doc { "Returns the first element, or `null` if the collection is empty." }
@@ -346,11 +354,11 @@ fun elements(): List<GenericFunction> {
body { body {
""" """
when (this) { when (this) {
is List<*> -> { is List -> {
if (isEmpty()) if (isEmpty())
return null return null
else else
return this[0] as T return this[0]
} }
else -> { else -> {
val iterator = iterator() val iterator = iterator()
@@ -366,6 +374,14 @@ fun elements(): List<GenericFunction> {
return if (isEmpty()) null else this[0] return if (isEmpty()) null else this[0]
""" """
} }
body(Sequences) {
"""
val iterator = iterator()
if (!iterator.hasNext())
return null
return iterator.next()
"""
}
} }
templates add f("first(predicate: (T) -> Boolean)") { templates add f("first(predicate: (T) -> Boolean)") {
@@ -415,11 +431,11 @@ fun elements(): List<GenericFunction> {
body { body {
""" """
when (this) { when (this) {
is List<*> -> { is List -> {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Collection is empty.")
else else
return this[this.lastIndex] as T return this[this.lastIndex]
} }
else -> { else -> {
val iterator = iterator() val iterator = iterator()
@@ -437,7 +453,7 @@ fun elements(): List<GenericFunction> {
""" """
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) if (!iterator.hasNext())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Sequence is empty.")
var last = iterator.next() var last = iterator.next()
while (iterator.hasNext()) while (iterator.hasNext())
last = iterator.next() last = iterator.next()
@@ -460,7 +476,7 @@ fun elements(): List<GenericFunction> {
body { body {
""" """
when (this) { when (this) {
is List<*> -> return if (isEmpty()) null else this[size() - 1] as T is List -> return if (isEmpty()) null else this[size() - 1]
else -> { else -> {
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) if (!iterator.hasNext())
@@ -520,7 +536,7 @@ fun elements(): List<GenericFunction> {
body(Iterables) { body(Iterables) {
""" """
if (this is List<T>) if (this is List)
return this.last(predicate) return this.last(predicate)
var last: T? = null var last: T? = null
@@ -566,7 +582,7 @@ fun elements(): List<GenericFunction> {
body(Iterables) { body(Iterables) {
""" """
if (this is List<T>) if (this is List)
return this.lastOrNull(predicate) return this.lastOrNull(predicate)
var last: T? = null var last: T? = null
@@ -608,9 +624,9 @@ fun elements(): List<GenericFunction> {
body { body {
""" """
when (this) { when (this) {
is List<*> -> return when (size()) { is List -> return when (size()) {
0 -> throw NoSuchElementException("Collection is empty.") 0 -> throw NoSuchElementException("Collection is empty.")
1 -> this[0] as T 1 -> this[0]
else -> throw IllegalArgumentException("Collection has more than one element.") else -> throw IllegalArgumentException("Collection has more than one element.")
} }
else -> { else -> {
@@ -625,6 +641,17 @@ fun elements(): List<GenericFunction> {
} }
""" """
} }
body(Sequences) {
"""
val iterator = iterator()
if (!iterator.hasNext())
throw NoSuchElementException("Sequence is empty.")
var single = iterator.next()
if (iterator.hasNext())
throw IllegalArgumentException("Sequence has more than one element.")
return single
"""
}
body(Strings) { body(Strings) {
""" """
return when (length()) { return when (length()) {
@@ -652,7 +679,7 @@ fun elements(): List<GenericFunction> {
body { body {
""" """
when (this) { when (this) {
is List<*> -> return if (size() == 1) this[0] as T else null is List -> return if (size() == 1) this[0] else null
else -> { else -> {
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) if (!iterator.hasNext())
@@ -665,6 +692,17 @@ fun elements(): List<GenericFunction> {
} }
""" """
} }
body(Sequences) {
"""
val iterator = iterator()
if (!iterator.hasNext())
return null
var single = iterator.next()
if (iterator.hasNext())
return null
return single
"""
}
body(Strings) { body(Strings) {
""" """
return if (length() == 1) this[0] else null return if (length() == 1) this[0] else null