Skip to content

Commit 483e6db

Browse files
committed
Provide proof-of-concept ImmutableArrayList implementation
1 parent 59cdbbb commit 483e6db

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

kotlinx-collections-immutable/tests/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<artifactId>kotlinx-collections-immutable-tests</artifactId>
1313

1414
<build>
15+
<sourceDirectory>src/main/kotlin</sourceDirectory>
1516
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
1617
<plugins>
1718
<plugin>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package kotlinx.collections.immutable
2+
3+
import java.util.*
4+
5+
public class ImmutableArrayList<out E> private constructor(private val impl: Array<E>): ImmutableList<E> {
6+
7+
private var _implAsList: List<E>? = null
8+
private val implAsList: List<E> get() = _implAsList ?: impl.asList().apply { _implAsList = this }
9+
10+
override val size: Int get() = impl.size
11+
override fun isEmpty(): Boolean = impl.isEmpty()
12+
override fun contains(element: @UnsafeVariance E): Boolean = impl.contains(element)
13+
override fun containsAll(elements: Collection<@UnsafeVariance E>): Boolean = elements.all { impl.contains(it) }
14+
override fun get(index: Int): E = impl[index]
15+
override fun indexOf(element: @UnsafeVariance E): Int = impl.indexOf(element)
16+
override fun lastIndexOf(element: @UnsafeVariance E): Int = impl.lastIndexOf(element)
17+
18+
19+
override fun iterator(): Iterator<E> = impl.iterator()
20+
override fun listIterator(): ListIterator<E> = implAsList.listIterator()
21+
22+
override fun listIterator(index: Int): ListIterator<E> = implAsList.listIterator(index)
23+
24+
override fun subList(fromIndex: Int, toIndex: Int): ImmutableList<E> = ImmutableArrayList(impl.copyOfRange(fromIndex, toIndex))
25+
26+
override fun add(element: @UnsafeVariance E): ImmutableList<E> = ImmutableArrayList(impl + element)
27+
28+
override fun addAll(elements: Collection<@UnsafeVariance E>): ImmutableList<E> = ImmutableArrayList(impl + elements)
29+
30+
override fun remove(element: @UnsafeVariance E): ImmutableList<E> {
31+
val values = implAsList - element
32+
return if (values.size < impl.size) ImmutableArrayList(values.toTypedArray<Any?>() as Array<E>) else this
33+
}
34+
35+
override fun removeAll(elements: Collection<@UnsafeVariance E>): ImmutableList<E> {
36+
val values = impl.filter { it in elements }
37+
return if (values.size < impl.size) ImmutableArrayList(values.toTypedArray<Any?>() as Array<E>) else this
38+
}
39+
40+
override fun removeAll(predicate: (E) -> Boolean): ImmutableList<E> {
41+
val values = impl.filterNot(predicate)
42+
return if (values.size < impl.size) ImmutableArrayList(values.toTypedArray<Any?>() as Array<E>) else this
43+
}
44+
45+
override fun clear(): ImmutableList<E> = if (impl.isEmpty()) this else EMPTY
46+
47+
override fun addAll(index: Int, c: Collection<@UnsafeVariance E>): ImmutableList<E> = mutate { it.addAll(index, c) }
48+
49+
override fun set(index: Int, element: @UnsafeVariance E): ImmutableList<E> = ImmutableArrayList(impl.copyOf().apply { set(index, element) })
50+
51+
override fun add(index: Int, element: @UnsafeVariance E): ImmutableList<E> = mutate { it.add(index, element) }
52+
53+
override fun removeAt(index: Int): ImmutableList<E> = mutate { it.removeAt(index) }
54+
55+
override fun builder(): ImmutableList.Builder<@UnsafeVariance E> = object : ArrayList<E>(implAsList), ImmutableList.Builder<E> {
56+
override fun build(): ImmutableList<E> = if (this.modCount == 0) this@ImmutableArrayList else ImmutableArrayList(this.toArray() as Array<E>)
57+
}
58+
59+
60+
override fun equals(other: Any?): Boolean = (other as? List<*>)?.equals(implAsList) ?: false
61+
override fun hashCode(): Int = Arrays.hashCode(impl)
62+
override fun toString(): String = Arrays.toString(impl)
63+
64+
companion object {
65+
public val EMPTY: ImmutableArrayList<Nothing> = ImmutableArrayList(emptyArray<Any?>()) as ImmutableArrayList<Nothing>
66+
}
67+
}

0 commit comments

Comments
 (0)