package logbuffer

import "container/list"

type Queue struct {
	queue *list.List
}

func New() *Queue{
	return &Queue{queue: list.New()}
}

func (q Queue)push(value interface{}) {
	q.queue.PushBack(value)
}

func (q Queue)pop() interface{} {
	element := q.queue.Front()
	value := element.Value
	q.queue.Remove(element)
	return value
}

func (q Queue)Length() int {
	return q.queue.Len()
}

// Limit the amount of items in the queue
func (q Queue)Limit(items int) {
	for q.Length() > items {
		// Remove items
		q.pop()
	}
}

func (q Queue)ToSlice() []string {
	var slice []string
	element := q.queue.Front()
	for i := 0; i < q.Length(); i++ {
		slice = append(slice, element.Value.(string))
		element = element.Next()
	}
	return slice
}