package com.sysu.admin.utils.queue;


import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

@Slf4j
public class MyQueueSimple<T> implements MyQueue<T>{

    Executor executor;
    private int getCount;
    private int warning = 10000;
    private  LinkedList<T> queue;

    protected MyQueueSimple(int getCount){
        this.getCount = getCount;
        queue = new LinkedList();
        executor = Executors.newSingleThreadExecutor();
    }

    public int size(){
        return queue.size();
    }

    public void add(T o){
        executor.execute(()->{
            queue.addFirst(o);
            if(queue.size() > warning){
                log.warn("当前数据【" + queue.size() + "】条超过预警值【"+warning+"】");
                warning *= 1.5;
                getCount *= 1.5;
                log.warn("预警值自动扩容到：" + warning+"， 返回数量扩容到：" + getCount);
            }
        });
    }

    public void get(ResultListener listener){
        executor.execute(()-> {
            int l = queue.size() < getCount ? queue.size() : getCount;
            List<T> list = new ArrayList<>(l);
            for (int i = 0; i < l; i++) {
                list.add(queue.removeLast());
            }
            listener.get(list);
        });
    }



}
