using System.Collections.Concurrent;
using System.Collections.Generic;

namespace Server.Core
{
    public class ListPool<T>
    {
        private readonly Queue<List<T>> Queues = new Queue<List<T>>();

        public List<T> Get()
        {
            if (Queues.Count > 0)
            {
                return Queues.Dequeue();
            }
            
            return new List<T>();
        }

        public void Recycle(List<T> list)
        {
            list.Clear();
            Queues.Enqueue(list);
        }
    }

    public class ConcurrentListPool<T>
    {
        private readonly ConcurrentQueue<List<T>> Queues = new ConcurrentQueue<List<T>>();

        public List<T> Get()
        {
            if (Queues.TryDequeue(out var list))
            {
                return list;
            }
            
            return new List<T>();
        }

        public void Recycle(List<T> list)
        {
            list.Clear();
            Queues.Enqueue(list);
        }
    }
}