Skip to content

Commit

Permalink
feat(#12): add abstractions in NotEmptyMutableCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
LVMVRQUXL committed Aug 11, 2022
1 parent 9f58ad2 commit 8619328
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
Expand Up @@ -8,5 +8,30 @@ import kotools.types.annotations.SinceKotoolsTypes
* @param E The type of elements contained in this collection.
*/
@SinceKotoolsTypes("1.3")
internal sealed interface NotEmptyMutableCollection<E> : MutableCollection<E>,
NotEmptyCollection<E>
public sealed interface NotEmptyMutableCollection<E> : MutableCollection<E>,
NotEmptyCollection<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: Int, element: E)

/**
* Removes an element at the specified [index] from the list, or throws an
* [IndexOutOfBoundsException] if the index is out of bounds.
*
* Because this list shouldn't be empty, the element will not be removed if
* this list contains only one element.
*/
@Throws(IndexOutOfBoundsException::class)
public infix fun removeAt(index: Int): 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 fun set(index: Int, element: E): E
}
21 changes: 1 addition & 20 deletions src/main/kotlin/kotools/types/collections/NotEmptyMutableList.kt
Expand Up @@ -96,11 +96,6 @@ public class NotEmptyMutableList<E>(override var head: E, vararg tail: 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)
override fun add(index: Int, element: E): Unit = when (index) {
in 1 until size -> tail.add(index - 1, element)
0 -> {
Expand All @@ -113,26 +108,12 @@ public class NotEmptyMutableList<E>(override var head: E, vararg tail: E) :

override fun get(index: Int): E = if (index == 0) head else tail[index - 1]

/**
* Removes an element at the specified [index] from the list, or throws an
* [IndexOutOfBoundsException] if the index is out of bounds.
*
* Because this list shouldn't be empty, the element will not be removed if
* this list contains only one element.
*/
@Throws(IndexOutOfBoundsException::class)
override infix fun removeAt(index: Int): E = when (index) {
override fun removeAt(index: Int): E = when (index) {
in 1 until size -> tail.removeAt(index - 1)
0 -> head.also { if (tail.isNotEmpty()) head = tail.removeFirst() }
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)
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 }
Expand Down
Expand Up @@ -92,7 +92,7 @@ class NotEmptyMutableListTest {
}

@Test
fun `should insert the element in the middle of the list with an index between 1 until the list's size`() {
fun `should insert the element into the list with an index in 1 until the list's size`() {
// GIVEN
val expectedList: NotEmptyList<String> =
NotEmptyList("one", "two", "three")
Expand Down

0 comments on commit 8619328

Please sign in to comment.