Skip to content
Bluetape4k docs1.11

Bounded collections

Latest stable Based on Bluetape4k release 1.11.0

BoundedStack and RingBuffer both evict the oldest value to bound memory, but expose opposite read order.

Ordering comparison for capacity-three BoundedStack and RingBuffer

RequirementTypeIndex zeroIteration
newest-first undo/historyBoundedStacknewest/topnewest to oldest
chronological recent historyRingBufferoldest/read headoldest to newest

Both validate a positive capacity and protect public reads/writes with ReentrantLock.

val stack = BoundedStack<Int>(3).apply { pushAll(1, 2, 3, 4) }
val ring = RingBuffer<Int>(3).apply { addAll(1, 2, 3, 4) }
check(stack.toList() == listOf(4, 3, 2))
check(ring.toList() == listOf(2, 3, 4))

Both evict 1. BoundedStack.pop() removes 4 first; RingBuffer.drop(1) removes 2 first.

BoundedStack offers push, pop, peek, insert, update, and remove with top-relative indexes. Empty pop/peek throws NoSuchElementException; invalid indexes throw IndexOutOfBoundsException.

RingBuffer offers add, indexed get/set, drop, removeIf, and clear with chronological indexes. Negative drop is invalid; dropping at least the current size clears the buffer.

Bounded memory is not backpressure. Overflow silently replaces old data, so use a channel, queue, or persistent log when every item must be delivered. These types also do not provide blocking producer/consumer coordination.

Observe eviction count, capacity saturation, and snapshot size. Test wrap-around indexing/iteration, concurrent access, invalid capacity, empty operations, and insert/remove boundaries.

Capacity for running and waiting asynchronous work belongs in Concurrency and lifecycle.