package link.roadio.riff.userstate /** Immutable indexed records retain their backing snapshot and only a bounded decoded working set. */ class BoundedReadList( override val size: Int, private val read: (Int) -> T, ) : AbstractList(), java.util.RandomAccess { init { require(size >= 0) } private val cache = object : LinkedHashMap(128, 0.75f, true) { override fun removeEldestEntry(eldest: MutableMap.MutableEntry): Boolean = size > 128 } @Synchronized override fun get(index: Int): T { if (index !in indices) throw IndexOutOfBoundsException("Record $index outside $size") return cache.getOrPut(index) { read(index) } } }