Skip to content

Commit a786dfe

Browse files
committed
Fixed decay queue set max size
1 parent 5c7cd6a commit a786dfe

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

common/src/main/kotlin/com/lambda/util/collections/LimitedDecayQueue.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,17 @@ class LimitedDecayQueue<E>(
106106
* Updates the maximum allowed size for the queue and triggers a cleanup operation
107107
* to remove elements exceeding the new size or falling outside the allowed time interval.
108108
*
109+
* Elements starting from the head will be removed.
110+
*
109111
* @param newSize The new maximum size for the queue. Must be a non-negative integer.
110112
*/
111113
fun setMaxSize(newSize: Int) {
112114
sizeLimit = newSize
113115
cleanUp()
116+
117+
while (queue.size > newSize) {
118+
queue.poll()
119+
}
114120
}
115121

116122
/**
@@ -131,4 +137,4 @@ class LimitedDecayQueue<E>(
131137
onDecay(queue.poll().first)
132138
}
133139
}
134-
}
140+
}

common/src/test/kotlin/LimitedDecayQueueTest.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,7 @@ class LimitedDecayQueueTest {
137137

138138
queue.setMaxSize(2) // Reduce size limit to 2
139139

140-
queue.add("Element4")
141-
142-
// Verify that the queue is limited to 2 elements, and oldest element is removed
143140
assertEquals(2, queue.size)
144-
assertTrue(onDecayCalled.contains("Element1")) // "Element1" should be decayed
145141
}
146142

147143
@Test

0 commit comments

Comments
 (0)