kotlin.IterablesSpecial.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-stdlib Show documentation
Show all versions of kotlin-stdlib Show documentation
Kotlin Standard Library for JVM
package kotlin
// Number of extension function for java.lang.Iterable that shouldn't participate in auto generation
import java.util.AbstractList
import java.util.Comparator
import java.util.ArrayList
/**
* Count the number of elements in collection.
*
* If base collection implements [[Collection]] interface method [[Collection.size()]] will be used.
* Otherwise, this method determines the count by iterating through the all items.
*/
public fun Iterable.count() : Int {
if (this is Collection) {
return this.size()
}
var number : Int = 0
for (elem in this) {
++number
}
return number
}
public fun countTo(n: Int): (T) -> Boolean {
var count = 0
return { ++count; count <= n }
}
/**
* Get the first element in the collection.
*
* Will throw an exception if there are no elements
*/
public fun Iterable.first() : T {
if (this is List) {
return this.first()
}
return this.iterator().next()
}
/**
* Checks if collection contains given item.
*
* Method checks equality of the objects with T.equals method.
* If collection implements [[java.util.AbstractCollection]] an overridden implementation of the contains
* method will be used.
*/
public fun Iterable.containsItem(item : T) : Boolean {
if (this is java.util.AbstractCollection) {
return this.contains(item);
}
for (elem in this) {
if (elem == item) {
return true
}
}
return false
}
public fun > Iterable.sort() : List {
val list = toCollection(ArrayList())
java.util.Collections.sort(list)
return list
}
public fun Iterable.sort(comparator: java.util.Comparator) : List {
val list = toCollection(ArrayList())
java.util.Collections.sort(list, comparator)
return list
}