You can use the following version of the buffer operator:
.buffer(long timespan, TimeUnit unit, Scheduler scheduler, int count,
Callable<U> bufferSupplier,
boolean restartTimerOnMaxSize)
It allows you to define your custom bufferSupplier - collection used to store buffered values. You can then create your custom version of the collection, where you store at max one item and, in our case, replace existing value if new, bigger, come:
class SingleItemMaxCollection : ArrayList<Long>() {
override fun add(element: Long): Boolean {
return when {
size == 1 && get(0) < element -> { super.set(0, element); true }
size == 0 -> { super.add(element); true }
else -> false
}
}
}
The demonstration, how can you use it on some mocked data (items emitted every 400 ms):
class SO65020891 {
private fun dataProvider() = Observable.just(1L, 2L, 3L, 4L, 5L, 6L)
.concatMap { Observable.just(it).delay(400, TimeUnit.MILLISECONDS) }
private fun getCollection(): () -> SingleItemMaxCollection = { SingleItemMaxCollection() }
fun getBufferedMax(): Observable<Long> {
return dataProvider()
.buffer(1, TimeUnit.SECONDS, Schedulers.computation(), 2, getCollection(), false)
.filter { it.isNotEmpty() }
.map { it[0] }
}
}
And finally, some verification:
class SO65020891Test {
@Test
fun maxEmittedValuesReturnedWithinWindows() {
val tested = SO65020891()
val values = tested.getBufferedMax().blockingIterable().toList()
assertEquals(listOf(2L, 4L, 6L), values)
}
}