Skip to content

Commit

Permalink
feat(#12): overload element's insertion in NotEmptyMutableCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
LVMVRQUXL committed Aug 11, 2022
1 parent 755fc7d commit 0b40da5
Show file tree
Hide file tree
Showing 2 changed files with 465 additions and 0 deletions.
@@ -1,6 +1,8 @@
package kotools.types.collections

import kotools.types.annotations.SinceKotoolsTypes
import kotools.types.number.PositiveInt
import kotools.types.number.StrictlyPositiveInt

/**
* Represents mutable collections containing at least one element.
Expand All @@ -10,13 +12,55 @@ import kotools.types.annotations.SinceKotoolsTypes
@SinceKotoolsTypes("1.3")
public sealed interface NotEmptyMutableCollection<E> : MutableCollection<E>,
NotEmptyCollection<E> {
// ---------- Positional Access Operations ----------

/**
* Inserts the [element] into this list at the specified [index], or throws
* an [IndexOutOfBoundsException] if the [index] is out of bounds.
*/
@Throws(IndexOutOfBoundsException::class)
public fun add(index: Int, element: E)

/**
* Inserts the [element] into this list at the specified [index], or throws
* an [IndexOutOfBoundsException] if the [index] is out of bounds.
*/
@Throws(IndexOutOfBoundsException::class)
public fun add(index: PositiveInt, element: E): Unit =
add(index.value, element)

/**
* Inserts the [element] into this list at the specified [index], or throws
* an [IndexOutOfBoundsException] if the [index] is out of bounds.
*/
@Throws(IndexOutOfBoundsException::class)
public fun add(index: StrictlyPositiveInt, element: E): Unit =
add(index.value, element)

/**
* Inserts the [element] into this list at the specified [index], or returns
* `null` if the [index] is out of bounds.
*/
public fun addOrNull(index: Int, element: E): Unit? = try {
add(index, element)
} catch (_: IndexOutOfBoundsException) {
null
}

/**
* Inserts the [element] into this list at the specified [index], or returns
* `null` if the [index] is out of bounds.
*/
public fun addOrNull(index: PositiveInt, element: E): Unit? =
addOrNull(index.value, element)

/**
* Inserts the [element] into this list at the specified [index], or returns
* `null` if the [index] is out of bounds.
*/
public fun addOrNull(index: StrictlyPositiveInt, element: E): Unit? =
addOrNull(index.value, element)

/**
* Removes an element at the specified [index] from the list, or throws an
* [IndexOutOfBoundsException] if the index is out of bounds.
Expand Down

0 comments on commit 0b40da5

Please sign in to comment.