
kotlin.collections.JUtil.kt Maven / Gradle / Ivy
package kotlin
import java.util.*
private object EmptyList : List {
private val list = ArrayList()
override fun contains(o: Any?): Boolean = list.contains(o)
override fun containsAll(c: Collection): Boolean = list.containsAll(c)
override fun get(index: Int): Any = list.get(index)
override fun indexOf(o: Any?): Int = list.indexOf(o)
override fun isEmpty(): Boolean = list.isEmpty()
override fun iterator(): Iterator = list.iterator()
override fun lastIndexOf(o: Any?): Int = list.lastIndexOf(o)
override fun listIterator(): ListIterator = list.listIterator()
override fun listIterator(index: Int): ListIterator =list.listIterator(index)
override fun size(): Int = list.size()
override fun subList(fromIndex: Int, toIndex: Int): List = list.subList(fromIndex, toIndex)
override fun equals(other: Any?): Boolean = list.equals(other)
override fun hashCode(): Int = list.hashCode()
override fun toString(): String = list.toString()
}
private object EmptySet : Set {
private val set = HashSet()
override fun contains(o: Any?): Boolean = set.contains(o)
override fun containsAll(c: Collection): Boolean = set.containsAll(c)
override fun isEmpty(): Boolean = set.isEmpty()
override fun iterator(): Iterator = set.iterator()
override fun size(): Int = set.size()
override fun equals(other: Any?): Boolean = set.equals(other)
override fun hashCode(): Int = set.hashCode()
override fun toString(): String = set.toString()
}
/** Returns an empty read-only list. */
public fun emptyList(): List = EmptyList as List
/** Returns an empty read-only set. */
public fun emptySet(): Set = EmptySet as Set
/** Returns a new read-only list of given elements */
public fun listOf(vararg values: T): List = if (values.size() == 0) emptyList() else arrayListOf(*values)
/** Returns an empty read-only list. */
public fun listOf(): List = emptyList()
/** Returns a new read-only ordered set with the given elements. */
public fun setOf(vararg values: T): Set = if (values.size() == 0) emptySet() else values.toCollection(LinkedHashSet())
/** Returns an empty read-only set. */
public fun setOf(): Set = emptySet()
/** Returns a new [LinkedList] with the given elements. */
public fun linkedListOf(vararg values: T): LinkedList = values.toCollection(LinkedList())
/** Returns a new [ArrayList] with the given elements. */
public fun arrayListOf(vararg values: T): ArrayList = values.toCollection(ArrayList(values.size()))
/** Returns a new [HashSet] with the given elements. */
public fun hashSetOf(vararg values: T): HashSet = values.toCollection(HashSet(values.size()))
/** Returns a new [LinkedHashSet] with the given elements. */
public fun linkedSetOf(vararg values: T): LinkedHashSet = values.toCollection(LinkedHashSet(values.size()))
/**
* Returns an [IntRange] of the valid indices for this collection.
*/
public val Collection<*>.indices: IntRange
get() = 0..size() - 1
/**
* Returns an [IntRange] that starts with zero and ends at the value of this number but does not include it.
*/
public val Int.indices: IntRange
get() = 0..this - 1
/**
* Returns the index of the last item in the list or -1 if the list is empty
*
* @sample test.collections.ListSpecificTest.lastIndex
*/
public val List.lastIndex: Int
get() = this.size() - 1
/** Returns true if the collection is not empty */
public fun Collection.isNotEmpty(): Boolean = !isEmpty()
/** Returns this Collection if it's not null and the empty list otherwise. */
public fun Collection?.orEmpty(): Collection = this ?: emptyList()
/** Returns this List if it's not null and the empty list otherwise. */
public fun List?.orEmpty(): List = this ?: emptyList()
/** Returns this Set if it's not null and the empty set otherwise. */
public fun Set?.orEmpty(): Set = this ?: emptySet()
/**
* Returns the size of this iterable if it is known, or `null` otherwise.
*/
public fun Iterable.collectionSizeOrNull(): Int? = if (this is Collection<*>) size() else null
/**
* Returns the size of this iterable if it is known, or the specified [default] value otherwise.
*/
public fun Iterable.collectionSizeOrDefault(default: Int): Int = if (this is Collection<*>) size() else default
© 2015 - 2025 Weber Informatics LLC | Privacy Policy