package com.laseroptek.raman.utils


/*
val queue = FixedSizeLIFOQueue<Int>(3)
queue.add(1)
queue.add(2)
queue.add(3)
queue.add(4) // Drops the oldest element (1)

val elements = queue.toList()
println(elements) // Output: [2, 3, 4]

val itemWithValue = queue.queue.firstOrNull { it.property == targetValue }
 */
class FixedSizeLIFOQueue<T>(private val maxSize: Int) {

    private val queue = ArrayDeque<T>(maxSize)

    val size: Int
        get() = queue.size

    fun add(element: T) {
        if (queue.size == maxSize) {
            queue.removeFirst() // Remove the oldest element
        }
        queue.addLast(element) // Add the new element
    }

    fun addAll(elements: Collection<T>) {
        if (maxSize <= 0 || elements.isEmpty()) {
            return // Nothing to add or no space
        }

        elements.forEach { element ->
            add( element )
        }
    }

    fun remove(): T? {
        return queue.removeLastOrNull() // Remove and return the last element
    }

    fun clear() {
        queue.clear()
    }

    fun peek(): T? {
        return queue.lastOrNull() // Return the last element without removing it
    }

    fun isEmpty(): Boolean {
        return queue.isEmpty()
    }

    fun isNotEmpty(): Boolean {
        return queue.isNotEmpty()
    }

    fun isFull(): Boolean {
        return queue.size == maxSize
    }

    fun toList(): List<T> {
        return queue.toList()
    }
}