@procedures/collections 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (205) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/LICENSE +21 -0
  3. package/README.md +394 -2
  4. package/dist/.tsbuildinfo +1 -0
  5. package/dist/async/async-queue.d.ts +155 -0
  6. package/dist/async/async-queue.d.ts.map +1 -0
  7. package/dist/async/async-queue.js +342 -0
  8. package/dist/async/async-queue.js.map +1 -0
  9. package/dist/async/channels.d.ts +188 -0
  10. package/dist/async/channels.d.ts.map +1 -0
  11. package/dist/async/channels.js +314 -0
  12. package/dist/async/channels.js.map +1 -0
  13. package/dist/behaviors/bounded.d.ts +79 -0
  14. package/dist/behaviors/bounded.d.ts.map +1 -0
  15. package/dist/behaviors/bounded.js +384 -0
  16. package/dist/behaviors/bounded.js.map +1 -0
  17. package/dist/behaviors/composite.d.ts +17 -0
  18. package/dist/behaviors/composite.d.ts.map +1 -0
  19. package/dist/behaviors/composite.js +93 -0
  20. package/dist/behaviors/composite.js.map +1 -0
  21. package/dist/behaviors/evented.d.ts +154 -0
  22. package/dist/behaviors/evented.d.ts.map +1 -0
  23. package/dist/behaviors/evented.js +299 -0
  24. package/dist/behaviors/evented.js.map +1 -0
  25. package/dist/behaviors/lru.d.ts +84 -0
  26. package/dist/behaviors/lru.d.ts.map +1 -0
  27. package/dist/behaviors/lru.js +376 -0
  28. package/dist/behaviors/lru.js.map +1 -0
  29. package/dist/behaviors/readonly.d.ts +73 -0
  30. package/dist/behaviors/readonly.d.ts.map +1 -0
  31. package/dist/behaviors/readonly.js +177 -0
  32. package/dist/behaviors/readonly.js.map +1 -0
  33. package/dist/behaviors/safe.d.ts +163 -0
  34. package/dist/behaviors/safe.d.ts.map +1 -0
  35. package/dist/behaviors/safe.js +176 -0
  36. package/dist/behaviors/safe.js.map +1 -0
  37. package/dist/behaviors/synchronized.d.ts +88 -0
  38. package/dist/behaviors/synchronized.d.ts.map +1 -0
  39. package/dist/behaviors/synchronized.js +285 -0
  40. package/dist/behaviors/synchronized.js.map +1 -0
  41. package/dist/behaviors/ttl.d.ts +118 -0
  42. package/dist/behaviors/ttl.d.ts.map +1 -0
  43. package/dist/behaviors/ttl.js +354 -0
  44. package/dist/behaviors/ttl.js.map +1 -0
  45. package/dist/contracts/model.d.ts +29 -0
  46. package/dist/contracts/model.d.ts.map +1 -0
  47. package/dist/contracts/model.js +26 -0
  48. package/dist/contracts/model.js.map +1 -0
  49. package/dist/core/effects.d.ts +176 -0
  50. package/dist/core/effects.d.ts.map +1 -0
  51. package/dist/core/effects.js +180 -0
  52. package/dist/core/effects.js.map +1 -0
  53. package/dist/core/events.d.ts +192 -0
  54. package/dist/core/events.d.ts.map +1 -0
  55. package/dist/core/events.js +291 -0
  56. package/dist/core/events.js.map +1 -0
  57. package/dist/core/middleware.d.ts +231 -0
  58. package/dist/core/middleware.d.ts.map +1 -0
  59. package/dist/core/middleware.js +161 -0
  60. package/dist/core/middleware.js.map +1 -0
  61. package/dist/core/policies.d.ts +220 -0
  62. package/dist/core/policies.d.ts.map +1 -0
  63. package/dist/core/policies.js +48 -0
  64. package/dist/core/policies.js.map +1 -0
  65. package/dist/core/traits.d.ts +142 -0
  66. package/dist/core/traits.d.ts.map +1 -0
  67. package/dist/core/traits.js +8 -0
  68. package/dist/core/traits.js.map +1 -0
  69. package/dist/fx/collectors.d.ts +193 -0
  70. package/dist/fx/collectors.d.ts.map +1 -0
  71. package/dist/fx/collectors.js +375 -0
  72. package/dist/fx/collectors.js.map +1 -0
  73. package/dist/fx/iter.d.ts +166 -0
  74. package/dist/fx/iter.d.ts.map +1 -0
  75. package/dist/fx/iter.js +421 -0
  76. package/dist/fx/iter.js.map +1 -0
  77. package/dist/impl/array-deque.d.ts +112 -0
  78. package/dist/impl/array-deque.d.ts.map +1 -0
  79. package/dist/impl/array-deque.js +380 -0
  80. package/dist/impl/array-deque.js.map +1 -0
  81. package/dist/impl/array-list.d.ts +101 -0
  82. package/dist/impl/array-list.d.ts.map +1 -0
  83. package/dist/impl/array-list.js +387 -0
  84. package/dist/impl/array-list.js.map +1 -0
  85. package/dist/impl/hash-map.d.ts +149 -0
  86. package/dist/impl/hash-map.d.ts.map +1 -0
  87. package/dist/impl/hash-map.js +473 -0
  88. package/dist/impl/hash-map.js.map +1 -0
  89. package/dist/impl/hash-set.d.ts +125 -0
  90. package/dist/impl/hash-set.d.ts.map +1 -0
  91. package/dist/impl/hash-set.js +264 -0
  92. package/dist/impl/hash-set.js.map +1 -0
  93. package/dist/impl/linked-hash-map.d.ts +141 -0
  94. package/dist/impl/linked-hash-map.d.ts.map +1 -0
  95. package/dist/impl/linked-hash-map.js +442 -0
  96. package/dist/impl/linked-hash-map.js.map +1 -0
  97. package/dist/impl/linked-list.d.ts +109 -0
  98. package/dist/impl/linked-list.d.ts.map +1 -0
  99. package/dist/impl/linked-list.js +494 -0
  100. package/dist/impl/linked-list.js.map +1 -0
  101. package/dist/impl/priority-queue.d.ts +139 -0
  102. package/dist/impl/priority-queue.d.ts.map +1 -0
  103. package/dist/impl/priority-queue.js +343 -0
  104. package/dist/impl/priority-queue.js.map +1 -0
  105. package/dist/impl/tree-map.d.ts +153 -0
  106. package/dist/impl/tree-map.d.ts.map +1 -0
  107. package/dist/impl/tree-map.js +788 -0
  108. package/dist/impl/tree-map.js.map +1 -0
  109. package/dist/impl/tree-set.d.ts +112 -0
  110. package/dist/impl/tree-set.d.ts.map +1 -0
  111. package/dist/impl/tree-set.js +374 -0
  112. package/dist/impl/tree-set.js.map +1 -0
  113. package/dist/index.d.ts +106 -0
  114. package/dist/index.d.ts.map +1 -0
  115. package/dist/index.js +139 -0
  116. package/dist/index.js.map +1 -0
  117. package/dist/interfaces/collection.d.ts +119 -0
  118. package/dist/interfaces/collection.d.ts.map +1 -0
  119. package/dist/interfaces/collection.js +8 -0
  120. package/dist/interfaces/collection.js.map +1 -0
  121. package/dist/interfaces/list.d.ts +247 -0
  122. package/dist/interfaces/list.d.ts.map +1 -0
  123. package/dist/interfaces/list.js +8 -0
  124. package/dist/interfaces/list.js.map +1 -0
  125. package/dist/interfaces/map.d.ts +344 -0
  126. package/dist/interfaces/map.d.ts.map +1 -0
  127. package/dist/interfaces/map.js +8 -0
  128. package/dist/interfaces/map.js.map +1 -0
  129. package/dist/interfaces/queue.d.ts +304 -0
  130. package/dist/interfaces/queue.d.ts.map +1 -0
  131. package/dist/interfaces/queue.js +8 -0
  132. package/dist/interfaces/queue.js.map +1 -0
  133. package/dist/interfaces/set.d.ts +137 -0
  134. package/dist/interfaces/set.d.ts.map +1 -0
  135. package/dist/interfaces/set.js +8 -0
  136. package/dist/interfaces/set.js.map +1 -0
  137. package/dist/register.d.ts +8 -0
  138. package/dist/register.d.ts.map +1 -0
  139. package/dist/register.js +8 -0
  140. package/dist/register.js.map +1 -0
  141. package/dist/storage/index.d.ts +15 -0
  142. package/dist/storage/index.d.ts.map +1 -0
  143. package/dist/storage/index.js +14 -0
  144. package/dist/storage/index.js.map +1 -0
  145. package/dist/storage/interface.d.ts +141 -0
  146. package/dist/storage/interface.d.ts.map +1 -0
  147. package/dist/storage/interface.js +23 -0
  148. package/dist/storage/interface.js.map +1 -0
  149. package/dist/storage/memory.d.ts +68 -0
  150. package/dist/storage/memory.d.ts.map +1 -0
  151. package/dist/storage/memory.js +154 -0
  152. package/dist/storage/memory.js.map +1 -0
  153. package/dist/utils/defaults.d.ts +135 -0
  154. package/dist/utils/defaults.d.ts.map +1 -0
  155. package/dist/utils/defaults.js +322 -0
  156. package/dist/utils/defaults.js.map +1 -0
  157. package/dist/utils/factories.d.ts +144 -0
  158. package/dist/utils/factories.d.ts.map +1 -0
  159. package/dist/utils/factories.js +293 -0
  160. package/dist/utils/factories.js.map +1 -0
  161. package/dist/utils/helpers.d.ts +137 -0
  162. package/dist/utils/helpers.d.ts.map +1 -0
  163. package/dist/utils/helpers.js +221 -0
  164. package/dist/utils/helpers.js.map +1 -0
  165. package/package.json +68 -3
  166. package/src/async/async-queue.ts +417 -0
  167. package/src/async/channels.ts +374 -0
  168. package/src/behaviors/bounded.ts +477 -0
  169. package/src/behaviors/composite.ts +99 -0
  170. package/src/behaviors/evented.ts +464 -0
  171. package/src/behaviors/lru.ts +447 -0
  172. package/src/behaviors/readonly.ts +224 -0
  173. package/src/behaviors/safe.ts +372 -0
  174. package/src/behaviors/synchronized.ts +316 -0
  175. package/src/behaviors/ttl.ts +462 -0
  176. package/src/contracts/model.ts +44 -0
  177. package/src/core/effects.ts +323 -0
  178. package/src/core/events.ts +356 -0
  179. package/src/core/middleware.ts +415 -0
  180. package/src/core/policies.ts +316 -0
  181. package/src/core/traits.ts +164 -0
  182. package/src/fx/collectors.ts +500 -0
  183. package/src/fx/iter.ts +536 -0
  184. package/src/impl/array-deque.ts +463 -0
  185. package/src/impl/array-list.ts +490 -0
  186. package/src/impl/hash-map.ts +623 -0
  187. package/src/impl/hash-set.ts +342 -0
  188. package/src/impl/linked-hash-map.ts +572 -0
  189. package/src/impl/linked-list.ts +606 -0
  190. package/src/impl/priority-queue.ts +448 -0
  191. package/src/impl/tree-map.ts +912 -0
  192. package/src/impl/tree-set.ts +469 -0
  193. package/src/index.ts +277 -0
  194. package/src/interfaces/collection.ts +141 -0
  195. package/src/interfaces/list.ts +281 -0
  196. package/src/interfaces/map.ts +405 -0
  197. package/src/interfaces/queue.ts +353 -0
  198. package/src/interfaces/set.ts +158 -0
  199. package/src/register.ts +8 -0
  200. package/src/storage/index.ts +16 -0
  201. package/src/storage/interface.ts +179 -0
  202. package/src/storage/memory.ts +180 -0
  203. package/src/utils/defaults.ts +345 -0
  204. package/src/utils/factories.ts +357 -0
  205. package/src/utils/helpers.ts +248 -0
package/src/index.ts ADDED
@@ -0,0 +1,277 @@
1
+ /**
2
+ * Collections Framework - A comprehensive Java Collections-inspired library for TypeScript.
3
+ *
4
+ * Provides:
5
+ * - Core collection interfaces (List, Set, Queue, Map, etc.)
6
+ * - Rich implementations (ArrayList, LinkedList, HashSet, HashMap, PriorityQueue, etc.)
7
+ * - Composable behaviors (bounded, readonly, safe, evented, synchronized, LRU, TTL)
8
+ * - Functional error handling (Option, Result)
9
+ * - Event-driven collections with typed events
10
+ * - Async/concurrent collections (AsyncQueue, Channels with CSP patterns)
11
+ * - Lazy functional operations (map, filter, reduce, etc.)
12
+ * - Stream-style collectors (groupBy, counting, summarizing, etc.)
13
+ * - Pluggable equality/hashing/comparison
14
+ *
15
+ * @example
16
+ * // Composable behaviors
17
+ * import { arrayList, compose, boundedList, eventedList } from '@common/collections'
18
+ *
19
+ * const list = compose(
20
+ * eventedList(),
21
+ * boundedList({ capacity: 100, policy: 'drop-oldest' })
22
+ * )(arrayList<number>())
23
+ *
24
+ * list.on('add', ({ value }) => console.log('Added:', value))
25
+ * list.add(42)
26
+ *
27
+ * @example
28
+ * // Functional operations with lazy evaluation
29
+ * import { pipe, map, filter, take, collect, toList } from '@common/collections'
30
+ *
31
+ * const result = pipe(
32
+ * [1, 2, 3, 4, 5],
33
+ * (it) => map(it, x => x * 2),
34
+ * (it) => filter(it, x => x > 5),
35
+ * (it) => take(it, 2),
36
+ * (it) => collect(it, toList())
37
+ * )
38
+ *
39
+ * @example
40
+ * // Async channels with Go-style CSP
41
+ * import { channel, select, timeout, pipeline } from '@common/collections'
42
+ *
43
+ * const ch = channel<number>()
44
+ * await ch.send(42)
45
+ * const value = await select(
46
+ * ch.case(v => v),
47
+ * timeout(1000).case(() => null)
48
+ * )
49
+ */
50
+
51
+ // ============================================================================
52
+ // Core types and utilities
53
+ // ============================================================================
54
+
55
+ export * from "./core/traits.js";
56
+ export * from "./core/effects.js";
57
+ export * from "./core/policies.js";
58
+ export * from "./core/middleware.js";
59
+ export * from "./core/events.js";
60
+ // ============================================================================
61
+ // Interfaces
62
+ // ============================================================================
63
+
64
+ export * from "./interfaces/collection.js";
65
+ export * from "./interfaces/list.js";
66
+ export * from "./interfaces/map.js";
67
+ export * from "./interfaces/set.js";
68
+
69
+ // ============================================================================
70
+ // Storage
71
+ // ============================================================================
72
+
73
+ export * from "./storage/index.js";
74
+
75
+ // Export queue interfaces with disambiguation for PriorityQueue
76
+ export type {
77
+ ReadonlyQueue,
78
+ Queue,
79
+ Deque,
80
+ BlockingQueue,
81
+ PriorityQueue as IPriorityQueue,
82
+ AsyncQueue as IAsyncQueue,
83
+ TransferQueue,
84
+ DelayQueue,
85
+ } from "./interfaces/queue.js";
86
+
87
+ // ============================================================================
88
+ // Implementations
89
+ // ============================================================================
90
+
91
+ export * from "./impl/array-list.js";
92
+ export * from "./impl/array-deque.js";
93
+ export * from "./impl/hash-map.js";
94
+ export * from "./impl/linked-hash-map.js";
95
+ export * from "./impl/tree-map.js";
96
+ export * from "./impl/tree-set.js";
97
+ export * from "./impl/linked-list.js";
98
+ export * from "./impl/hash-set.js";
99
+ export * from "./impl/priority-queue.js";
100
+
101
+ // ============================================================================
102
+ // Behaviors
103
+ // ============================================================================
104
+
105
+ // Export bounded behaviors (excluding duplicate BoundedCollection interface)
106
+ export {
107
+ type BoundedOptions,
108
+ boundedList,
109
+ boundedQueue,
110
+ boundedDeque,
111
+ boundedMap,
112
+ bounded,
113
+ } from "./behaviors/bounded.js";
114
+
115
+ export * from "./behaviors/readonly.js";
116
+ export * from "./behaviors/safe.js";
117
+ export * from "./behaviors/evented.js";
118
+ export * from "./behaviors/synchronized.js";
119
+ export * from "./behaviors/lru.js";
120
+ export * from "./behaviors/ttl.js";
121
+
122
+ // ============================================================================
123
+ // Async/Concurrent
124
+ // ============================================================================
125
+
126
+ export * from "./async/async-queue.js";
127
+ export * from "./async/channels.js";
128
+
129
+ // ============================================================================
130
+ // Functional Operations
131
+ // ============================================================================
132
+
133
+ export * from "./fx/iter.js";
134
+ export * from "./fx/collectors.js";
135
+
136
+ // ============================================================================
137
+ // Utilities
138
+ // ============================================================================
139
+
140
+ export * from "./utils/defaults.js";
141
+ export * from "./utils/factories.js";
142
+ export * from "./utils/helpers.js";
143
+
144
+ // ============================================================================
145
+ // Re-exports for convenience
146
+ // ============================================================================
147
+
148
+ // Most commonly used exports
149
+ export { compose, bundle } from "./core/middleware.js";
150
+ export { arrayList } from "./impl/array-list.js";
151
+ export { arrayDeque } from "./impl/array-deque.js";
152
+ export { hashMap } from "./impl/hash-map.js";
153
+ export { linkedHashMap } from "./impl/linked-hash-map.js";
154
+ export { treeMap } from "./impl/tree-map.js";
155
+ export { treeSet } from "./impl/tree-set.js";
156
+ export { linkedList } from "./impl/linked-list.js";
157
+ export { hashSet } from "./impl/hash-set.js";
158
+ export { priorityQueue } from "./impl/priority-queue.js";
159
+ // Common behaviors
160
+ export { readonly, readonlyList, readonlyMap } from "./behaviors/readonly.js";
161
+ export { safeList, safeQueue, safeMap } from "./behaviors/safe.js";
162
+ export { eventedList, eventedQueue, eventedMap } from "./behaviors/evented.js";
163
+ export { synchronized } from "./behaviors/synchronized.js";
164
+ export { lruMap, lruCache, LRUCache } from "./behaviors/lru.js";
165
+ export { ttlMap, ttlCache, TTLCache, ttlCollection } from "./behaviors/ttl.js";
166
+
167
+ // Async/Concurrent
168
+ export { asyncQueue, AsyncQueue } from "./async/async-queue.js";
169
+ export {
170
+ channel,
171
+ Channel,
172
+ select,
173
+ timeout,
174
+ ticker,
175
+ pipeline,
176
+ fanOut,
177
+ fanIn,
178
+ merge,
179
+ workerPool,
180
+ } from "./async/channels.js";
181
+
182
+ // Functional operations
183
+ export {
184
+ map,
185
+ filter,
186
+ flatMap,
187
+ flatten,
188
+ take,
189
+ skip,
190
+ takeWhile,
191
+ skipWhile,
192
+ concat,
193
+ zip,
194
+ enumerate,
195
+ chunk,
196
+ window,
197
+ partition,
198
+ reduce,
199
+ scan,
200
+ sort,
201
+ reverse,
202
+ distinct,
203
+ distinctConsecutive,
204
+ tap,
205
+ some,
206
+ every,
207
+ none,
208
+ find,
209
+ count,
210
+ min,
211
+ max,
212
+ sum,
213
+ average,
214
+ toArray,
215
+ toSet,
216
+ toMap as iterToMap,
217
+ pipe,
218
+ } from "./fx/iter.js";
219
+
220
+ export {
221
+ collect,
222
+ toList as collectToList,
223
+ toArray as collectToArray,
224
+ toSet as collectToSet,
225
+ toMap as collectToMap,
226
+ groupingBy,
227
+ groupingByWith,
228
+ partitioningBy,
229
+ counting,
230
+ summingNumber,
231
+ averagingNumber,
232
+ minBy,
233
+ maxBy,
234
+ joining,
235
+ mapping,
236
+ filtering,
237
+ flatMapping,
238
+ reducing,
239
+ reducingWith,
240
+ teeing,
241
+ first,
242
+ last,
243
+ summarizingNumber,
244
+ } from "./fx/collectors.js";
245
+
246
+ // Common factories
247
+ export {
248
+ emptyList,
249
+ emptyMap,
250
+ emptyQueue,
251
+ singletonList,
252
+ singletonMap,
253
+ nCopies,
254
+ listOf,
255
+ mapOf,
256
+ range,
257
+ rangeTo,
258
+ toList,
259
+ toMap,
260
+ groupBy,
261
+ } from "./utils/factories.js";
262
+
263
+ // Effects
264
+ export {
265
+ None,
266
+ Some,
267
+ type Option,
268
+ Ok,
269
+ Err,
270
+ type Result,
271
+ isSome,
272
+ isNone,
273
+ isOk,
274
+ isErr,
275
+ getOrElse,
276
+ unwrap,
277
+ } from "./core/effects.js";
@@ -0,0 +1,141 @@
1
+ /**
2
+ * Base collection interfaces that mirror Java Collections Framework.
3
+ *
4
+ * These form the root of the collection hierarchy and define the
5
+ * fundamental operations common to all collections.
6
+ */
7
+
8
+ import type { Eq } from "../core/traits.js";
9
+
10
+ /**
11
+ * ReadonlyCollection<T> - The root interface for readonly collections.
12
+ *
13
+ * Mirrors java.util.Collection but without mutation operations.
14
+ * All collections should extend this or a more specific readonly interface.
15
+ */
16
+ export interface ReadonlyCollection<T> extends Iterable<T> {
17
+ /**
18
+ * Returns the number of elements in this collection.
19
+ */
20
+ readonly size: number;
21
+
22
+ /**
23
+ * Returns true if this collection contains no elements.
24
+ */
25
+ readonly isEmpty: boolean;
26
+
27
+ /**
28
+ * Returns true if this collection contains the specified element.
29
+ * Uses the collection's equality function to compare elements.
30
+ *
31
+ * @param element The element to check for
32
+ */
33
+ contains(element: T): boolean;
34
+
35
+ /**
36
+ * Returns true if this collection contains all elements in the specified collection.
37
+ *
38
+ * @param other The collection whose elements to check for
39
+ */
40
+ containsAll(other: Iterable<T>): boolean;
41
+
42
+ /**
43
+ * Returns an array containing all elements in this collection.
44
+ * The returned array is safe to modify (it's a copy).
45
+ */
46
+ toArray(): T[];
47
+
48
+ /**
49
+ * Performs the given action for each element.
50
+ *
51
+ * @param action The action to perform for each element
52
+ */
53
+ forEach(action: (element: T, index: number) => void): void;
54
+
55
+ /**
56
+ * Returns the equality function used by this collection.
57
+ */
58
+ readonly eq: Eq<T>;
59
+ }
60
+
61
+ /**
62
+ * Collection<T> - The root interface for mutable collections.
63
+ *
64
+ * Mirrors java.util.Collection with mutation operations.
65
+ * All mutable collections should extend this or a more specific interface.
66
+ */
67
+ export interface Collection<T> extends ReadonlyCollection<T> {
68
+ /**
69
+ * Adds an element to this collection.
70
+ *
71
+ * Returns true if the collection was modified as a result of this call.
72
+ * Some collections (like Set) may return false if the element already exists.
73
+ *
74
+ * @param element The element to add
75
+ * @returns true if the collection changed
76
+ * @throws Error if collection is full (bounded) and policy is 'throw'
77
+ */
78
+ add(element: T): boolean;
79
+
80
+ /**
81
+ * Adds all elements from the specified collection to this collection.
82
+ *
83
+ * @param other The collection whose elements to add
84
+ * @returns true if the collection changed
85
+ */
86
+ addAll(other: Iterable<T>): boolean;
87
+
88
+ /**
89
+ * Removes a single instance of the specified element from this collection.
90
+ *
91
+ * Returns true if an element was removed as a result of this call.
92
+ *
93
+ * @param element The element to remove
94
+ * @returns true if an element was removed
95
+ */
96
+ remove(element: T): boolean;
97
+
98
+ /**
99
+ * Removes all elements from this collection that are also in the specified collection.
100
+ *
101
+ * @param other The collection whose elements to remove
102
+ * @returns true if the collection changed
103
+ */
104
+ removeAll(other: Iterable<T>): boolean;
105
+
106
+ /**
107
+ * Retains only the elements in this collection that are in the specified collection.
108
+ * In other words, removes all elements not in the specified collection.
109
+ *
110
+ * @param other The collection of elements to retain
111
+ * @returns true if the collection changed
112
+ */
113
+ retainAll(other: Iterable<T>): boolean;
114
+
115
+ /**
116
+ * Removes all elements from this collection.
117
+ * The collection will be empty after this call returns.
118
+ */
119
+ clear(): void;
120
+ }
121
+
122
+ /**
123
+ * Utility type to check if a collection is readonly.
124
+ */
125
+ export type IsReadonly<C> = C extends ReadonlyCollection<any>
126
+ ? C extends Collection<any>
127
+ ? false
128
+ : true
129
+ : false;
130
+
131
+ /**
132
+ * Extract element type from a collection.
133
+ */
134
+ export type ElementOf<C> = C extends ReadonlyCollection<infer T> ? T : never;
135
+
136
+ /**
137
+ * Make a collection readonly by removing mutation methods.
138
+ */
139
+ export type Readonly<C extends Collection<any>> = C extends Collection<infer T>
140
+ ? ReadonlyCollection<T>
141
+ : never;
@@ -0,0 +1,281 @@
1
+ /**
2
+ * List interfaces that mirror Java's List interface.
3
+ *
4
+ * Lists are ordered collections that allow duplicate elements
5
+ * and provide positional (indexed) access to elements.
6
+ */
7
+
8
+ import type { Collection, ReadonlyCollection } from "./collection.js";
9
+ import type { Compare } from "../core/traits.js";
10
+
11
+ /**
12
+ * ReadonlyList<T> - Readonly ordered collection with indexed access.
13
+ *
14
+ * Mirrors java.util.List but without mutation operations.
15
+ * Elements maintain their insertion order and can be accessed by index.
16
+ */
17
+ export interface ReadonlyList<T> extends ReadonlyCollection<T> {
18
+ /**
19
+ * Returns the element at the specified position.
20
+ *
21
+ * @param index The index of the element to return (0-based)
22
+ * @returns The element at the specified position
23
+ * @throws RangeError if index is out of bounds
24
+ */
25
+ get(index: number): T;
26
+
27
+ /**
28
+ * Returns the index of the first occurrence of the specified element,
29
+ * or -1 if not found.
30
+ *
31
+ * @param element The element to search for
32
+ * @param fromIndex Optional starting index for the search
33
+ */
34
+ indexOf(element: T, fromIndex?: number): number;
35
+
36
+ /**
37
+ * Returns the index of the last occurrence of the specified element,
38
+ * or -1 if not found.
39
+ *
40
+ * @param element The element to search for
41
+ * @param fromIndex Optional starting index for backwards search
42
+ */
43
+ lastIndexOf(element: T, fromIndex?: number): number;
44
+
45
+ /**
46
+ * Returns a view of the portion of this list between fromIndex (inclusive)
47
+ * and toIndex (exclusive).
48
+ *
49
+ * Changes to the sublist are reflected in the original list.
50
+ *
51
+ * @param fromIndex Start index (inclusive)
52
+ * @param toIndex End index (exclusive)
53
+ */
54
+ subList(fromIndex: number, toIndex: number): ReadonlyList<T>;
55
+
56
+ /**
57
+ * Returns the first element in the list.
58
+ *
59
+ * @throws Error if list is empty
60
+ */
61
+ first(): T;
62
+
63
+ /**
64
+ * Returns the last element in the list.
65
+ *
66
+ * @throws Error if list is empty
67
+ */
68
+ last(): T;
69
+ }
70
+
71
+ /**
72
+ * List<T> - Mutable ordered collection with indexed access.
73
+ *
74
+ * Mirrors java.util.List with full mutation capabilities.
75
+ * Provides methods to insert, replace, and remove elements by index.
76
+ */
77
+ export interface List<T> extends ReadonlyList<T>, Collection<T> {
78
+ /**
79
+ * Replaces the element at the specified position.
80
+ *
81
+ * @param index The index of the element to replace
82
+ * @param element The new element
83
+ * @returns The element previously at the specified position
84
+ * @throws RangeError if index is out of bounds
85
+ */
86
+ set(index: number, element: T): T;
87
+
88
+ /**
89
+ * Inserts an element at the specified position.
90
+ * Shifts elements at and after the position to the right.
91
+ *
92
+ * @param index The index at which to insert
93
+ * @param element The element to insert
94
+ * @throws RangeError if index is out of bounds (0 to size inclusive)
95
+ */
96
+ insert(index: number, element: T): void;
97
+
98
+ /**
99
+ * Inserts all elements from the specified collection at the specified position.
100
+ *
101
+ * @param index The index at which to insert
102
+ * @param other The collection whose elements to insert
103
+ * @returns true if the list changed
104
+ */
105
+ insertAll(index: number, other: Iterable<T>): boolean;
106
+
107
+ /**
108
+ * Removes the element at the specified position.
109
+ * Shifts elements after the position to the left.
110
+ *
111
+ * @param index The index of the element to remove
112
+ * @returns The element that was removed
113
+ * @throws RangeError if index is out of bounds
114
+ */
115
+ removeAt(index: number): T;
116
+
117
+ /**
118
+ * Removes elements in the specified index range.
119
+ *
120
+ * @param fromIndex Start index (inclusive)
121
+ * @param toIndex End index (exclusive)
122
+ */
123
+ removeRange(fromIndex: number, toIndex: number): void;
124
+
125
+ /**
126
+ * Sorts this list according to the specified comparator.
127
+ *
128
+ * @param compare The comparison function
129
+ */
130
+ sort(compare?: Compare<T>): void;
131
+
132
+ /**
133
+ * Reverses the order of elements in this list.
134
+ */
135
+ reverse(): void;
136
+
137
+ /**
138
+ * Returns a mutable sublist view.
139
+ *
140
+ * @param fromIndex Start index (inclusive)
141
+ * @param toIndex End index (exclusive)
142
+ */
143
+ subList(fromIndex: number, toIndex: number): List<T>;
144
+
145
+ // Stack-like operations
146
+
147
+ /**
148
+ * Appends an element to the end of this list.
149
+ * Equivalent to add() but doesn't return boolean.
150
+ *
151
+ * @param element The element to append
152
+ */
153
+ push(element: T): void;
154
+
155
+ /**
156
+ * Removes and returns the last element in this list.
157
+ *
158
+ * @returns The last element
159
+ * @throws Error if list is empty
160
+ */
161
+ pop(): T;
162
+
163
+ /**
164
+ * Removes and returns the first element in this list.
165
+ *
166
+ * @returns The first element
167
+ * @throws Error if list is empty
168
+ */
169
+ shift(): T;
170
+
171
+ /**
172
+ * Prepends an element to the beginning of this list.
173
+ *
174
+ * @param element The element to prepend
175
+ */
176
+ unshift(element: T): void;
177
+ }
178
+
179
+ /**
180
+ * RandomAccess - Marker interface for lists with O(1) indexed access.
181
+ *
182
+ * Indicates that the list provides fast (constant-time) random access.
183
+ * Examples: ArrayList (yes), LinkedList (no).
184
+ */
185
+ export interface RandomAccess {
186
+ /**
187
+ * Marker property to indicate random access capability.
188
+ * Always true for collections implementing this interface.
189
+ */
190
+ readonly supportsRandomAccess: true;
191
+ }
192
+
193
+ /**
194
+ * Stack<T> - LIFO (Last-In-First-Out) collection.
195
+ *
196
+ * A stack of elements with push/pop operations.
197
+ * Can be implemented as a specialized list or standalone.
198
+ */
199
+ export interface Stack<T> extends Iterable<T> {
200
+ /** Number of elements */
201
+ readonly size: number;
202
+
203
+ /** True if empty */
204
+ readonly isEmpty: boolean;
205
+
206
+ /**
207
+ * Pushes an element onto the stack.
208
+ */
209
+ push(element: T): void;
210
+
211
+ /**
212
+ * Removes and returns the top element.
213
+ *
214
+ * @throws Error if stack is empty
215
+ */
216
+ pop(): T;
217
+
218
+ /**
219
+ * Returns the top element without removing it.
220
+ *
221
+ * @throws Error if stack is empty
222
+ */
223
+ peek(): T;
224
+
225
+ /**
226
+ * Removes all elements.
227
+ */
228
+ clear(): void;
229
+
230
+ /**
231
+ * Searches for an element and returns its distance from the top.
232
+ * Returns -1 if not found.
233
+ */
234
+ search(element: T): number;
235
+ }
236
+
237
+ /**
238
+ * Vector<T> - Synchronized growable array.
239
+ *
240
+ * Like ArrayList but with synchronized access.
241
+ * Legacy interface from Java; in our framework, use compose(synchronized())(arrayList()) instead.
242
+ */
243
+ export interface Vector<T> extends List<T> {
244
+ /**
245
+ * Current capacity of the underlying array.
246
+ */
247
+ readonly capacity: number;
248
+
249
+ /**
250
+ * Trims capacity to current size to minimize memory usage.
251
+ */
252
+ trimToSize(): void;
253
+
254
+ /**
255
+ * Increases capacity to ensure it can hold at least the specified number of elements.
256
+ */
257
+ ensureCapacity(minCapacity: number): void;
258
+ }
259
+
260
+ /**
261
+ * CircularList<T> - List with circular/ring buffer behavior.
262
+ *
263
+ * When capacity is reached, oldest elements are overwritten.
264
+ * Useful for fixed-size buffers like logs or history.
265
+ */
266
+ export interface CircularList<T> extends List<T> {
267
+ /**
268
+ * Maximum capacity of the circular list.
269
+ */
270
+ readonly capacity: number;
271
+
272
+ /**
273
+ * Returns true if the list has reached capacity.
274
+ */
275
+ readonly isFull: boolean;
276
+
277
+ /**
278
+ * Number of elements that have been overwritten.
279
+ */
280
+ readonly overwriteCount: number;
281
+ }