package com.laseroptek.raman.utils

import java.util.LinkedList


class FixedSizeFIFOQueue<E>(private val maxSize: Int) {

    private val queue = LinkedList<E>()

    val size: Int
        get() = queue.size

    fun add(element: E) {
        if (queue.size >= maxSize) {
            // Remove the OLDEST item (from the front)
            queue.pollFirst() // was pollLast()
        }
        // Add the NEWEST item to the end.
        queue.addLast(element) // was addFirst()
    }

    fun addAll(elements: Collection<E>) {
        elements.forEach { element ->
            if (queue.size >= maxSize) {
                queue.pollFirst() // was pollLast()
            }
            queue.addLast(element) // was addFirst()
        }
    }

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

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

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