ImmutableCollections
1.5.0indexedImmutable collection implementations prevent external mutation, offering ImmutableList, ImmutableSet, and ImmutableMap types. Changes to underlying collections don't affect their state, ensuring data integrity.
Immutable collection implementations prevent external mutation, offering ImmutableList, ImmutableSet, and ImmutableMap types. Changes to underlying collections don't affect their state, ensuring data integrity.
Immutable Collection implementations for Kotlin Multiplatform for the targets JVM, Android, iOS, macOS, watchOS, tvOS, Linux, Windows, Node.js, JS Browser and WebAssembly.
Often it's not desirable to pass mutable state to the outside. Even though Kotlin's List<E> interface seems to be immutable,
a simple as MutableList<E> makes it mutable so that class internal state can be changed from the outside.
To avoid this all this library's collection implementations - ImmutableList, ImmutableSet, ImmutableMap, ImmutableCollection - are immutable. Also changes to underlying collection don't change their state.
dependencies {
implementation "net.codinux.collections:immutable-collections:1.0.0"
}
Maven does not support automatic platform resolution as Gradle does, therefore the specific platform must be specified here:
<dependency>
<groupId>net.codinux.collections</groupId>
<artifactId>immutable-collections-jvm</artifactId>
<version>1.0.0</version>
</dependency>
val listViaConstructor = ImmutableList("foo", "bar")
val listViaGlobalMethod = immutableListOf("foo", "bar")
val underlyingList = mutableListOf("foo", "bar")
val listViaExtensionMethod = underlyingList.toImmutableList()
// changes to underlying collection to not change ImmutableList
underlyingList.add()
assertEquals(, underlyingList.size)
assertEquals(, listViaExtensionMethod.size)
assertThrows<Throwable> {
(listViaConstructor MutableList<String>)
}
val mapViaConstructor = ImmutableMap("foo" to "bar")
val mapViaGlobalMethod = immutableMapOf("foo" to "bar")
val underlyingMap = mutableMapOf("foo" to "bar")
val mapViaExtensionMethod = underlyingMap.toImmutableMap()
// changes to underlying collection to not change ImmutableMap
underlyingMap.put(, )
assertEquals(, underlyingMap.size)
assertEquals(, mapViaExtensionMethod.size)
assertThrows<Throwable> {
(mapViaConstructor MutableMap<String, String>)
}
val setViaConstructor = ImmutableSet("foo", "bar")
val setViaGlobalMethod = immutableSetOf("foo", "bar")
val underlyingSet = mutableSetOf("foo", "bar")
val setViaExtensionMethod = underlyingSet.toImmutableSet()
// changes to underlying collection to not change ImmutableSet
underlyingSet.add("baz")
assertEquals(, underlyingSet.size)
assertEquals(, setViaExtensionMethod.size)
assertThrows<Throwable> {
(setViaConstructor MutableSet<String>)
}
For more example see KotlinExamples.
For more example see JavaExamples.
Copyright 2023 codinux GmbH & Co. KG
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance the License.
You may obtain a copy of the License at
http:
Unless applicable law agreed to writing, software
distributed under the License distributed an BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied.
See the License the specific language governing permissions
limitations under the License.
List<String> listViaConstructor = new ImmutableList<>("foo", "bar");
List<String> listViaGlobalMethod = net.codinux.collections.ImmutableCollectionExtensionKt.immutableListOf("foo", "bar");
Map<String, String> mapViaConstructor = new ImmutableMap<>(Collections.singletonMap("foo", "bar"));
Map<String, String> mapViaGlobalMethod = net.codinux.collections.ImmutableCollectionExtensionKt.immutableMapOf(Collections.singletonMap("foo", "bar"));
Set<String> setViaConstructor = new ImmutableSet<>("foo", "bar");
Set<String> setViaGlobalMethod = net.codinux.collections.ImmutableCollectionExtensionKt.immutableSetOf("foo", "bar");
// all Collections are immutable, mutable operations are not supported
assertThrows(UnsupportedOperationException.class, () -> listViaConstructor.add("baz"));
assertThrows(UnsupportedOperationException.class, () -> mapViaConstructor.put("baz", "foo-bar"));
assertThrows(UnsupportedOperationException.class, () -> setViaConstructor.add("baz"));
Surfaced from shared tags and platforms — no rankings paid for.