Skip to content

Commit 454f370

Browse files
author
Abduqodiri Qurbonzoda
committed
Add documentations for public api
1 parent c4a80c3 commit 454f370

File tree

5 files changed

+785
-9
lines changed

5 files changed

+785
-9
lines changed

kotlinx-collections-immutable/src/main/kotlin/kotlinx/collections/immutable/ImmutableCollection.kt

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,106 @@
1616

1717
package kotlinx.collections.immutable
1818

19+
/**
20+
* A generic immutable collection of elements. Methods in this interface support only read-only access to the collection.
21+
*
22+
* Modification operations are supported through the [PersistentCollection] interface.
23+
*
24+
* Implementors of this interface take responsibility to be immutable.
25+
* Once constructed they must contain the same elements in the same order.
26+
*
27+
* @param E the type of elements contained in the collection. The immutable collection is covariant on its element type.
28+
*/
1929
public interface ImmutableCollection<out E>: Collection<E>
2030

31+
/**
32+
* A generic persistent collection of elements that supports adding and removing elements.
33+
*
34+
* Modification operations return new instances of the persistent collection with the modification applied.
35+
*
36+
* @param E the type of elements contained in the collection. The persistent collection is covariant on its element type.
37+
*/
2138
public interface PersistentCollection<out E> : ImmutableCollection<E> {
39+
/**
40+
* Returns the result of adding the specified [element] to this collection.
41+
*
42+
* @returns a new persistent collection with the specified [element] added;
43+
* or this instance if this collection does not support duplicates and it already contains the element.
44+
*/
2245
fun add(element: @UnsafeVariance E): PersistentCollection<E>
2346

47+
/**
48+
* Returns the result of adding all elements of the specified [elements] collection to this collection.
49+
*
50+
* @return a new persistent collection with elements of the specified [elements] collection added;
51+
* or this instance if no modifications were made in the result of this operation.
52+
*/
2453
fun addAll(elements: Collection<@UnsafeVariance E>): PersistentCollection<E>
2554

55+
/**
56+
* Returns the result of removing a single appearance of the specified [element] from this collection.
57+
*
58+
* @return a new persistent collection with a single appearance of the specified [element] removed;
59+
* or this instance if there is no such element in this collection.
60+
*/
2661
fun remove(element: @UnsafeVariance E): PersistentCollection<E>
2762

63+
/**
64+
* Returns the result of removing all elements in this collection that are also
65+
* contained in the specified [elements] collection.
66+
*
67+
* @return a new persistent collection with elements in this collection that are also
68+
* contained in the specified [elements] collection removed;
69+
* or this instance if no modifications were made in the result of this operation.
70+
*/
2871
fun removeAll(elements: Collection<@UnsafeVariance E>): PersistentCollection<E>
2972

73+
/**
74+
* Returns the result of removing all elements in this collection that match the specified [predicate].
75+
*
76+
* @return a new persistent collection with elements matching the specified [predicate] removed;
77+
* or this instance if no elements match the predicate.
78+
*/
3079
fun removeAll(predicate: (E) -> Boolean): PersistentCollection<E>
3180

81+
/**
82+
* Returns an empty persistent collection.
83+
*/
3284
fun clear(): PersistentCollection<E>
3385

86+
/**
87+
* A generic builder of the persistent collection. Builder exposes its modification operations through the [MutableCollection] interface.
88+
*
89+
* Builders are reusable, that is [build] method can be called multiple times with modifications between these calls.
90+
* However, modifications applied do not affect previously built persistent collection instances.
91+
*
92+
* Builder is backed by the same underlying data structure as the persistent collection it was created from.
93+
* Thus, [builder] and [build] methods take constant time consisting of passing the backing storage to the
94+
* new builder and persistent collection instances, respectively.
95+
*
96+
* The builder tracks which nodes in the structure are shared with the persistent collection,
97+
* and which are owned by it exclusively. It owns the nodes it copied during modification
98+
* operations and avoids copying them on subsequent modifications.
99+
*
100+
* When [build] is called the builder forgets about all owned nodes it had created.
101+
*/
34102
interface Builder<E>: MutableCollection<E> {
103+
/**
104+
* Returns a persistent collection with the same contents as this builder.
105+
*
106+
* This method can be called multiple times.
107+
*
108+
* If operations applied on this builder have caused no modifications:
109+
* - on the first call it returns the same persistent collection instance this builder was obtained from.
110+
* - on subsequent calls it returns the same previously returned persistent collection instance.
111+
*/
35112
fun build(): PersistentCollection<E>
36113
}
37114

115+
/**
116+
* Returns a new builder with the same contents as this collection.
117+
*
118+
* The builder can be used to efficiently perform multiple modification operations.
119+
*/
38120
fun builder(): Builder<@UnsafeVariance E>
39-
}
121+
}

kotlinx-collections-immutable/src/main/kotlin/kotlinx/collections/immutable/ImmutableList.kt

Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,29 @@ package kotlinx.collections.immutable
1818

1919
import kotlinx.collections.immutable.internal.ListImplementation
2020

21+
/**
22+
* A generic immutable ordered collection of elements. Methods in this interface support only read-only access to the immutable list.
23+
*
24+
* Modification operations are supported through the [PersistentList] interface.
25+
*
26+
* Implementors of this interface take responsibility to be immutable.
27+
* Once constructed they must contain the same elements in the same order.
28+
*
29+
* @param E the type of elements contained in the list. The immutable list is covariant on its element type.
30+
*/
2131
public interface ImmutableList<out E> : List<E>, ImmutableCollection<E> {
2232

33+
/**
34+
* Returns a view of the portion of this list between the specified [fromIndex] (inclusive) and [toIndex] (exclusive).
35+
*
36+
* The returned list is backed by this list.
37+
*
38+
* @throws IndexOutOfBoundsException if [fromIndex] is less than zero or [toIndex] is greater than the size of this list.
39+
* @throws IllegalArgumentException if [fromIndex] is greater than [toIndex].
40+
*/
2341
override fun subList(fromIndex: Int, toIndex: Int): ImmutableList<E> = SubList(this, fromIndex, toIndex)
2442

25-
public class SubList<E>(private val source: ImmutableList<E>, private val fromIndex: Int, private val toIndex: Int) : ImmutableList<E>, AbstractList<E>() {
43+
private class SubList<E>(private val source: ImmutableList<E>, private val fromIndex: Int, private val toIndex: Int) : ImmutableList<E>, AbstractList<E>() {
2644
private var _size: Int = 0
2745

2846
init {
@@ -42,35 +60,111 @@ public interface ImmutableList<out E> : List<E>, ImmutableCollection<E> {
4260
ListImplementation.checkRangeIndexes(fromIndex, toIndex, this._size)
4361
return SubList(source, this.fromIndex + fromIndex, this.fromIndex + toIndex)
4462
}
45-
4663
}
4764
}
4865

66+
/**
67+
* A generic persistent ordered collection of elements that supports adding and removing elements.
68+
*
69+
* Modification operations return new instances of the persistent list with the modification applied.
70+
*
71+
* @param E the type of elements contained in the list. The persistent list is covariant on its element type.
72+
*/
4973
public interface PersistentList<out E> : ImmutableList<E>, PersistentCollection<E> {
74+
/**
75+
* Returns a new persistent list with the specified [element] appended.
76+
*/
5077
override fun add(element: @UnsafeVariance E): PersistentList<E>
5178

79+
/**
80+
* Returns the result of appending all elements of the specified [elements] collection to this list.
81+
*
82+
* The elements are appended in the order they appear in the specified collection.
83+
*
84+
* @return a new persistent list with elements of the specified [elements] collection appended;
85+
* or this instance if the specified collection is empty.
86+
*/
5287
override fun addAll(elements: Collection<@UnsafeVariance E>): PersistentList<E> // = super<ImmutableCollection>.addAll(elements) as ImmutableList
5388

89+
/**
90+
* Returns the result of removing the first appearance of the specified [element] from this list.
91+
*
92+
* @return a new persistent list with the first appearance of the specified [element] removed;
93+
* or this instance if there is no such element in this list.
94+
*/
5495
override fun remove(element: @UnsafeVariance E): PersistentList<E>
5596

97+
/**
98+
* Returns the result of removing all elements in this list that are also
99+
* contained in the specified [elements] collection.
100+
*
101+
* @return a new persistent list with elements in this list that are also
102+
* contained in the specified [elements] collection removed;
103+
* or this instance if no modifications were made in the result of this operation.
104+
*/
56105
override fun removeAll(elements: Collection<@UnsafeVariance E>): PersistentList<E>
57106

107+
/**
108+
* Returns the result of removing all elements in this list that match the specified [predicate].
109+
*
110+
* @return a new persistent list with elements matching the specified [predicate] removed;
111+
* or this instance if no elements match the predicate.
112+
*/
58113
override fun removeAll(predicate: (E) -> Boolean): PersistentList<E>
59114

115+
/**
116+
* Returns an empty persistent list.
117+
*/
60118
override fun clear(): PersistentList<E>
61119

62120

121+
/**
122+
* Returns the result of inserting the specified [c] collection at the specified [index].
123+
*
124+
* @return a new persistent list with the specified [c] collection inserted at the specified [index];
125+
* or this instance if the specified collection is empty.
126+
*
127+
* @throws IndexOutOfBoundsException if [index] is out of bounds of this list.
128+
*/
63129
fun addAll(index: Int, c: Collection<@UnsafeVariance E>): PersistentList<E> // = builder().apply { addAll(index, c.toList()) }.build()
64130

131+
/**
132+
* Returns a new persistent list with the element at the specified [index] replaced with the specified [element].
133+
*
134+
* @throws IndexOutOfBoundsException if [index] is out of bounds of this list.
135+
*/
65136
fun set(index: Int, element: @UnsafeVariance E): PersistentList<E>
66137

67138
/**
68-
* Inserts an element into the list at the specified [index].
139+
* Returns a new persistent list with the specified [element] inserted at the specified [index].
140+
*
141+
* @throws IndexOutOfBoundsException if [index] is out of bounds of this list.
69142
*/
70143
fun add(index: Int, element: @UnsafeVariance E): PersistentList<E>
71144

145+
/**
146+
* Returns a new persistent list with the element at the specified [index] removed.
147+
*
148+
* @throws IndexOutOfBoundsException if [index] is out of bounds of this list.
149+
*/
72150
fun removeAt(index: Int): PersistentList<E>
73151

152+
/**
153+
* A generic builder of the persistent list. Builder exposes its modification operations through the [MutableList] interface.
154+
*
155+
* Builders are reusable, that is [build] method can be called multiple times with modifications between these calls.
156+
* However, modifications applied do not affect previously built persistent list instances.
157+
*
158+
* Builder is backed by the same underlying data structure as the persistent list it was created from.
159+
* Thus, [builder] and [build] methods take constant time consisting of passing the backing storage to the
160+
* new builder and persistent list instances, respectively.
161+
*
162+
* The builder tracks which nodes in the structure are shared with the persistent list,
163+
* and which are owned by it exclusively. It owns the nodes it copied during modification
164+
* operations and avoids copying them on subsequent modifications.
165+
*
166+
* When [build] is called the builder forgets about all owned nodes it had created.
167+
*/
74168
interface Builder<E>: MutableList<E>, PersistentCollection.Builder<E> {
75169
override fun build(): PersistentList<E>
76170
}

kotlinx-collections-immutable/src/main/kotlin/kotlinx/collections/immutable/ImmutableMap.kt

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,20 @@
1616

1717
package kotlinx.collections.immutable
1818

19-
19+
/**
20+
* A generic immutable collection that holds pairs of objects (keys and values) and supports efficiently retrieving
21+
* the value corresponding to each key. Map keys are unique; the map holds only one value for each key.
22+
* Methods in this interface support only read-only access to the immutable map.
23+
*
24+
* Modification operations are supported through the [PersistentMap] interface.
25+
*
26+
* Implementors of this interface take responsibility to be immutable.
27+
* Once constructed they must contain the same elements in the same order.
28+
*
29+
* @param K the type of map keys. The map is invariant on its key type, as it
30+
* can accept key as a parameter (of [containsKey] for example) and return it in [keys] set.
31+
* @param V the type of map values. The map is covariant on its value type.
32+
*/
2033
public interface ImmutableMap<K, out V>: Map<K, V> {
2134

2235
override val keys: ImmutableSet<K>
@@ -27,21 +40,91 @@ public interface ImmutableMap<K, out V>: Map<K, V> {
2740
}
2841

2942

30-
43+
/**
44+
* A generic persistent collection that holds pairs of objects (keys and values) and supports efficiently retrieving
45+
* the value corresponding to each key. Map keys are unique; the map holds only one value for each key.
46+
*
47+
* Modification operations return new instances of the persistent map with the modification applied.
48+
*
49+
* @param K the type of map keys. The map is invariant on its key type.
50+
* @param V the type of map values. The persistent map is covariant on its value type.
51+
*/
3152
public interface PersistentMap<K, out V> : ImmutableMap<K, V> {
53+
/**
54+
* Returns the result of associating the specified [value] with the specified [key] in this map.
55+
*
56+
* If this map already contains a mapping for the key, the old value is replaced by the specified value.
57+
*
58+
* @return a new persistent map with the specified [value] associated with the specified [key];
59+
* or this instance if no modifications were made in the result of this operation.
60+
*/
3261
fun put(key: K, value: @UnsafeVariance V): PersistentMap<K, V>
3362

63+
/**
64+
* Returns the result of removing the specified [key] and its corresponding value from this map.
65+
*
66+
* @return a new persistent map with the specified [key] and its corresponding value removed;
67+
* or this instance if it contains no mapping for the key.
68+
*/
3469
fun remove(key: K): PersistentMap<K, V>
3570

71+
/**
72+
* Returns the result of removing the entry that maps the specified [key] to the specified [value].
73+
*
74+
* @return a new persistent map with the entry for the specified [key] and [value] removed;
75+
* or this instance if it contains no entry with the specified key and value.
76+
*/
3677
fun remove(key: K, value: @UnsafeVariance V): PersistentMap<K, V>
3778

79+
/**
80+
* Returns the result of merging the specified [m] map with this map.
81+
*
82+
* The effect of this call is equivalent to that of calling `put(k, v)` once for each
83+
* mapping from key `k` to value `v` in the specified map.
84+
*
85+
* @return a new persistent map with keys and values from the specified map [m] associated;
86+
* or this instance if no modifications were made in the result of this operation.
87+
*/
3888
fun putAll(m: Map<out K, @UnsafeVariance V>): PersistentMap<K, V> // m: Iterable<Map.Entry<K, V>> or Map<out K,V> or Iterable<Pair<K, V>>
3989

90+
/**
91+
* Returns an empty persistent map.
92+
*/
4093
fun clear(): PersistentMap<K, V>
4194

95+
/**
96+
* A generic builder of the persistent map. Builder exposes its modification operations through the [MutableMap] interface.
97+
*
98+
* Builders are reusable, that is [build] method can be called multiple times with modifications between these calls.
99+
* However, modifications applied do not affect previously built persistent map instances.
100+
*
101+
* Builder is backed by the same underlying data structure as the persistent map it was created from.
102+
* Thus, [builder] and [build] methods take constant time passing the backing storage to the
103+
* new builder and persistent map instances, respectively.
104+
*
105+
* The builder tracks which nodes in the structure are shared with the persistent map,
106+
* and which are owned by it exclusively. It owns the nodes it copied during modification
107+
* operations and avoids copying them on subsequent modifications.
108+
*
109+
* When [build] is called the builder forgets about all owned nodes it had created.
110+
*/
42111
interface Builder<K, V>: MutableMap<K, V> {
112+
/**
113+
* Returns a persistent map with the same contents as this builder.
114+
*
115+
* This method can be called multiple times.
116+
*
117+
* If operations applied on this builder have caused no modifications:
118+
* - on the first call it returns the same persistent map instance this builder was obtained from.
119+
* - on subsequent calls it returns the same previously returned persistent map instance.
120+
*/
43121
fun build(): PersistentMap<K, V>
44122
}
45123

124+
/**
125+
* Returns a new builder with the same contents as this map.
126+
*
127+
* The builder can be used to efficiently perform multiple modification operations.
128+
*/
46129
fun builder(): Builder<K, @UnsafeVariance V>
47130
}

0 commit comments

Comments
 (0)