All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kotlin.collections.MutableCollections.kt Maven / Gradle / Ivy

There is a newer version: 1.0.7
Show newest version
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("CollectionsKt")

package kotlin

import java.util.*

/**
 * Checks if all elements in the specified collection are contained in this collection.
 *
 * Allows to overcome type-safety restriction of `containsAll` that requires to pass a collection of type `Collection`.
 */
@Suppress("NOTHING_TO_INLINE")
public inline fun Collection<*>.containsAllRaw(collection: Collection): Boolean = (this as Collection).containsAll(collection)

/**
 * Removes a single instance of the specified element from this
 * collection, if it is present.
 *
 * Allows to overcome type-safety restriction of `remove` that requires to pass an element of type `E`.
 *
 * @return `true` if the element has been successfully removed; `false` if it was not present in the collection.
 */
@Suppress("NOTHING_TO_INLINE")
public inline fun  MutableCollection.removeRaw(element: Any?): Boolean = (this as MutableCollection).remove(element)

/**
 * Removes all of this collection's elements that are also contained in the specified collection.

 * Allows to overcome type-safety restriction of `removeAll` that requires to pass a collection of type `Collection`.
 *
 * @return `true` if any of the specified elements was removed from the collection, `false` if the collection was not modified.
 */
@Suppress("NOTHING_TO_INLINE")
public inline fun  MutableCollection.removeAllRaw(collection: Collection): Boolean = (this as MutableCollection).removeAll(collection)

/**
 * Retains only the elements in this collection that are contained in the specified collection.
 *
 * Allows to overcome type-safety restriction of `retailAll` that requires to pass a collection of type `Collection`.
 *
 * @return `true` if any element was removed from the collection, `false` if the collection was not modified.
 */
@Suppress("NOTHING_TO_INLINE")
public inline fun  MutableCollection.retainAllRaw(collection: Collection): Boolean = (this as MutableCollection).retainAll(collection)



@Deprecated("Use operator 'get' instead", ReplaceWith("this[index]"))
public fun CharSequence.charAt(index: Int): Char = this[index]

@Deprecated("Use property 'size' instead", ReplaceWith("size"))
public inline fun Collection<*>.size() = size

@Deprecated("Use property 'size' instead", ReplaceWith("size"))
public inline fun Map<*, *>.size() = size

@Deprecated("Use property 'key' instead", ReplaceWith("key"))
public fun  Map.Entry.getKey(): K = key

@Deprecated("Use containsAllRaw() instead.", ReplaceWith("containsAllRaw(collection)"))
public fun  Collection.containsAll(collection: Collection): Boolean = containsAllRaw(collection)

@Deprecated("Use property 'value' instead.", ReplaceWith("value"))
public fun  Map.Entry.getValue(): V = value

@Deprecated("Use 'removeAt' instead.", ReplaceWith("removeAt(index)"))
public fun  MutableList.remove(index: Int): E = removeAt(index)

@Deprecated("Use 'removeRaw' instead.", ReplaceWith("removeRaw(o)"))
public fun  MutableCollection.remove(o: Any?): Boolean = removeRaw(o)

@Deprecated("Use 'removeAllRaw' instead.", ReplaceWith("removeAllRaw(c)"))
public fun  MutableCollection.removeAll(c: Collection): Boolean = removeAllRaw(c)

@Deprecated("Use 'retainAllRaw' instead.", ReplaceWith("retainAllRaw(c)"))
public fun  MutableCollection.retainAll(c: Collection): Boolean = retainAllRaw(c)

@Deprecated("Use 'indexOfRaw' instead.", ReplaceWith("indexOfRaw(o)"))
public fun  List.indexOf(o: Any?): Int = indexOfRaw(o)

@Deprecated("Use 'lastIndexOfRaw' instead.", ReplaceWith("lastIndexOfRaw(o)"))
public fun  List.lastIndexOf(o: Any?): Int = lastIndexOfRaw(o)

@Deprecated("Use property 'length' instead.", ReplaceWith("length"))
public fun CharSequence.length(): Int = length

@Deprecated("Use 'getRaw' instead.", ReplaceWith("getRaw(key)"))
public inline operator fun  Map.get(key: Any?): V? = getRaw(key)

@Deprecated("Use 'containsKeyRaw' instead.", ReplaceWith("containsKeyRaw(key)"))
public inline fun  Map.containsKey(key: Any?): Boolean = containsKeyRaw(key)

@Deprecated("Use 'containsValueRaw' instead.", ReplaceWith("containsValueRaw(value)"))
public inline fun  Map.containsValue(value: Any?): Boolean = containsValueRaw(value)

@Deprecated("Use property 'keys' instead.", ReplaceWith("keys"))
public inline fun  Map.keySet(): Set = keys

@kotlin.jvm.JvmName("mutableKeys")
@Deprecated("Use property 'keys' instead.", ReplaceWith("keys"))
public fun  MutableMap.keySet(): MutableSet = keys

@Deprecated("Use property 'entries' instead.", ReplaceWith("entries"))
public inline fun  Map.entrySet(): Set> = entries

@kotlin.jvm.JvmName("mutableEntrySet")
@Deprecated("Use property 'entries' instead.", ReplaceWith("entries"))
public fun  MutableMap.entrySet(): MutableSet> = entries

@Deprecated("Use property 'values' instead.", ReplaceWith("values"))
public inline fun  Map.values(): Collection = values

@kotlin.jvm.JvmName("mutableValues")
@Deprecated("Use property 'values' instead.", ReplaceWith("values"))
public fun  MutableMap.values(): MutableCollection = values

/**
 * Adds the specified [element] to this mutable collection.
 */
public operator fun  MutableCollection.plusAssign(element: T) {
    this.add(element)
}

/**
 * Adds all elements of the given [collection] to this mutable collection.
 */
public operator fun  MutableCollection.plusAssign(collection: Iterable) {
    this.addAll(collection)
}

/**
 * Adds all elements of the given [array] to this mutable collection.
 */
public operator fun  MutableCollection.plusAssign(array: Array) {
    this.addAll(array)
}

/**
 * Adds all elements of the given [sequence] to this mutable collection.
 */
public operator fun  MutableCollection.plusAssign(sequence: Sequence) {
    this.addAll(sequence)
}

/**
 * Removes a single instance of the specified [element] from this mutable collection.
 */
public operator fun  MutableCollection.minusAssign(element: T) {
    this.remove(element)
}

/**
 * Removes all elements contained in the given [collection] from this mutable collection.
 */
public operator fun  MutableCollection.minusAssign(collection: Iterable) {
    this.removeAll(collection)
}

/**
 * Removes all elements contained in the given [array] from this mutable collection.
 */
public operator fun  MutableCollection.minusAssign(array: Array) {
    this.removeAll(array)
}

/**
 * Removes all elements contained in the given [sequence] from this mutable collection.
 */
public operator fun  MutableCollection.minusAssign(sequence: Sequence) {
    this.removeAll(sequence)
}

/**
 * Adds all elements of the given [iterable] to this [MutableCollection].
 */
public fun  MutableCollection.addAll(iterable: Iterable) {
    when (iterable) {
        is Collection -> addAll(iterable)
        else -> for (item in iterable) add(item)
    }
}

/**
 * Adds all elements of the given [sequence] to this [MutableCollection].
 */
public fun  MutableCollection.addAll(sequence: Sequence) {
    for (item in sequence) add(item)
}

/**
 * Adds all elements of the given [array] to this [MutableCollection].
 */
public fun  MutableCollection.addAll(array: Array) {
    addAll(array.asList())
}

/**
 * Removes all elements from this [MutableCollection] that are also contained in the given [iterable].
 */
public fun  MutableCollection.removeAll(iterable: Iterable) {
    removeAll(iterable.convertToSetForSetOperationWith(this))
}

/**
 * Removes all elements from this [MutableCollection] that are also contained in the given [sequence].
 */
public fun  MutableCollection.removeAll(sequence: Sequence) {
    val set = sequence.toHashSet()
    if (set.isNotEmpty())
        removeAll(set)
}

/**
 * Removes all elements from this [MutableCollection] that are also contained in the given [array].
 */
public fun  MutableCollection.removeAll(array: Array) {
    if (array.isNotEmpty())
        removeAll(array.toHashSet())
//    else
//        removeAll(emptyList())
}

/**
 * Retains only elements of this [MutableCollection] that are contained in the given [iterable].
 */
public fun  MutableCollection.retainAll(iterable: Iterable) {
    retainAll(iterable.convertToSetForSetOperationWith(this))
}

/**
 * Retains only elements of this [MutableCollection] that are contained in the given [array].
 */
public fun  MutableCollection.retainAll(array: Array) {
    if (array.isNotEmpty())
        retainAll(array.toHashSet())
    else
        clear()
//        retainAll(emptyList())
}

/**
 * Retains only elements of this [MutableCollection] that are contained in the given [sequence].
 */
public fun  MutableCollection.retainAll(sequence: Sequence) {
    val set = sequence.toHashSet()
    if (set.isNotEmpty())
        retainAll(set)
    else
        clear()
}

/**
 * Sorts elements in the list in-place according to their natural sort order.
 * */
public fun > MutableList.sort(): Unit {
    if (size > 1) java.util.Collections.sort(this)
}

/**
 *  Sorts elements in the list in-place according to order specified with [comparator].
 */
public fun  MutableList.sortWith(comparator: Comparator): Unit {
    if (size > 1) java.util.Collections.sort(this, comparator)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy