Skip to content

Commit

Permalink
refactor(#12): move updating's operations from NotEmptyMutableCollect…
Browse files Browse the repository at this point in the history
…ion to NotEmptyMutableList
  • Loading branch information
LVMVRQUXL committed Aug 11, 2022
1 parent 0a4dfd2 commit 01c536c
Show file tree
Hide file tree
Showing 4 changed files with 272 additions and 319 deletions.
@@ -1,66 +1,13 @@
package kotools.types.collections

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

// TODO: 26/07/2022 Remove this type
/**
* Represents mutable collections containing at least one element.
*
* @param E The type of elements contained in this collection.
*/
@SinceKotoolsTypes("1.3")
public sealed interface NotEmptyMutableCollection<E> : MutableCollection<E>,
NotEmptyCollection<E> {
// ---------- Positional Access Operations ----------

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

/**
* Replaces the element at the specified [index] in this list with the
* specified [element], or throws an [IndexOutOfBoundsException] if the
* [index] is out of bounds.
*/
@Throws(IndexOutOfBoundsException::class)
public operator fun set(index: PositiveInt, element: E): E =
set(index.value, element)

/**
* Replaces the element at the specified [index] in this list with the
* specified [element], or throws an [IndexOutOfBoundsException] if the
* [index] is out of bounds.
*/
@Throws(IndexOutOfBoundsException::class)
public operator fun set(index: StrictlyPositiveInt, element: E): E =
set(index.value, element)

/**
* Replaces the element at the specified [index] in this list with the
* specified [element], or returns `null` if the [index] is out of bounds.
*/
public fun setOrNull(index: Int, element: E): E? = try {
set(index, element)
} catch (_: IndexOutOfBoundsException) {
null
}

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

/**
* Replaces the element at the specified [index] in this list with the
* specified [element], or returns `null` if the [index] is out of bounds.
*/
public fun setOrNull(index: StrictlyPositiveInt, element: E): E? =
setOrNull(index.value, element)
}
NotEmptyCollection<E>
48 changes: 48 additions & 0 deletions src/main/kotlin/kotools/types/collections/NotEmptyMutableList.kt
Expand Up @@ -224,9 +224,57 @@ public class NotEmptyMutableList<E>(override var head: E, vararg tail: E) :
public infix fun removeAtOrNull(index: StrictlyPositiveInt): E? =
removeAtOrNull(index.value)

/**
* Replaces the element at the specified [index] in this list with the
* specified [element], or throws an [IndexOutOfBoundsException] if the
* [index] is out of bounds.
*/
@Throws(IndexOutOfBoundsException::class)
override fun set(index: Int, element: E): E = when (index) {
in 1 until size -> element.also { tail[index - 1] = it }
0 -> element.also { head = it }
else -> indexOutOfBounds(index, size)
}

/**
* Replaces the element at the specified [index] in this list with the
* specified [element], or throws an [IndexOutOfBoundsException] if the
* [index] is out of bounds.
*/
@Throws(IndexOutOfBoundsException::class)
public operator fun set(index: PositiveInt, element: E): E =
set(index.value, element)

/**
* Replaces the element at the specified [index] in this list with the
* specified [element], or throws an [IndexOutOfBoundsException] if the
* [index] is out of bounds.
*/
@Throws(IndexOutOfBoundsException::class)
public operator fun set(index: StrictlyPositiveInt, element: E): E =
set(index.value, element)

/**
* Replaces the element at the specified [index] in this list with the
* specified [element], or returns `null` if the [index] is out of bounds.
*/
public fun setOrNull(index: Int, element: E): E? = try {
set(index, element)
} catch (_: IndexOutOfBoundsException) {
null
}

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

/**
* Replaces the element at the specified [index] in this list with the
* specified [element], or returns `null` if the [index] is out of bounds.
*/
public fun setOrNull(index: StrictlyPositiveInt, element: E): E? =
setOrNull(index.value, element)
}

This file was deleted.

0 comments on commit 01c536c

Please sign in to comment.