package com.guatec.timeseries import java.util.* import java.util.concurrent.ArrayBlockingQueue import java.util.concurrent.BlockingQueue /** * A chainable bounded queue. When a value is evicted from this queue, it will be pushed to the chained queue (if any). */ class ChainableEvictingQueue>(maxCapacity: Int, val chainTo: Queue? = null): BlockingQueue by ArrayBlockingQueue(maxCapacity) { var max: T? = null private set override fun offer(element: T): Boolean { if (remainingCapacity() == 0) { val evicted = poll() if (this.chainTo != null) { return this.chainTo.offer(evicted) && offer(element) } } if (max == null || element > max!!) max = element return offer(element) } override fun put(element: T) { offer(element) } }