java简单时间窗,数据缓存

public class DataTimeWindow<T> {

    private TimeUnit unit;

    private int lenght;

    private int checkCursor;

    private int dataCursor;

    private WindowBlock<T>[] blocks;

    private Timer timer;

    public DataTimeWindow(TimeUnit unit, int length) {
        this(unit, length, null);
    }

    public DataTimeWindow(TimeUnit unit, int length, String name) {
        if (length < 1) {
            throw new IllegalArgumentException("window length must greater than 1");
        }
        if (null == name) {
            this.timer = new Timer();
        } else {
            this.timer = new Timer(name);
        }
        this.unit = unit;
        this.lenght = length;
        this.timer.schedule(new TimerTask() {

            public void run() {
                polling();
            }
        }, unit.toMillis(1), unit.toMillis(1));
        this.checkCursor = 1;
        this.dataCursor = 0;
        this.blocks = new WindowBlock[length];
        for (int i = 0; i < length; i++) {
            this.blocks[i] = new WindowBlock<T>();
        }
    }

    private void polling() {
        blocks[checkCursor].clear();
        checkCursor++;
        if (checkCursor >= this.lenght) {
            checkCursor = 0;
        }
        dataCursor++;
        if (dataCursor >= this.lenght) {
            dataCursor = 0;
        }
    }

    public void push(T data) {
        if (data == null) {
            throw new NullPointerException();
        }
        blocks[dataCursor].push(data);
        System.out.println(dataCursor);
    }

    /**
     * 查找数据是否存在时间窗内,根据equals方法来比较的
     *
     * @param data
     * @return
     */
    public T find(T data) {
        if (data == null) {
            return null;
        }
        for (int i = 0; i < this.lenght; i++) {
            WindowBlock<T> windowBlock = this.blocks[i];
            T exist = windowBlock.find(data);
            if(exist != null) {
                return exist;
            }
        }
        return null;
    }

    /**
     * 查找第一个匹配的数据并从时间窗内移除,根据equals方法来比较的
     *
     * @param data
     * @return
     */
    public T pick(T data) {
        if (data == null) {
            return null;
        }
        for (int i = 0; i < this.lenght; i++) {
            WindowBlock<T> windowBlock = this.blocks[i];
            T exist = windowBlock.remove(data);
            if(exist != null) {
                return exist;
            }
        }
        return null;
    }

    public TimeUnit getUnit() {
        return unit;
    }

    public int getLenght() {
        return lenght;
    }

    static class WindowBlock<T> {

        private ConcurrentHashMap<T, T> data = new ConcurrentHashMap<T, T>();

        public void push(T data) {
            this.data.put(data, data);
        }

        public void clear() {
            this.data.clear();
        }

        public T find(T obj) {
            return this.data.get(obj);
        }

        public T remove(T obj) {
            return this.data.remove(obj);
        }
    }
}

转载于:https://my.oschina.net/zhangyaxin/blog/3069963

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值