package com.gtiit.shr.config;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class TimerExpireHashMap<K, V> extends HashMap<K, V> {
    private static final long serialVersionUID = -609074433772310225L;
    private static final long CHECK_TIME_SECOND = 1000L;
    private static final int DEFAULT_INITIAL_CAPACITY = 8;
    private Timer timer = new Timer();
    private Map<Object, Long> timerMap;
    private TimerExpireHashMapCallback timerExpireHashMapCallback;
    private TimerTask timerTask;

    public TimerExpireHashMap(int initialCapacity, float loadFactor) {
        super(initialCapacity, loadFactor);
        this.timerTask = new NamelessClass_1();
        this.init(initialCapacity, loadFactor);
    }

    public TimerExpireHashMap(int initialCapacity) {
        super(initialCapacity);
        this.timerTask = new NamelessClass_1();
        this.init(initialCapacity, 10.0F);
    }

    public TimerExpireHashMap() {
        this.timerTask = new NamelessClass_1();
        this.init(8, 10.0F);
    }

    public TimerExpireHashMap(Map<K, V> map) {
        super(map);
        this.timerTask = new NamelessClass_1();
        this.init(8, 10.0F);
    }



    private void init(int initialCapacity, float loadFactor) {
        this.timerMap = new HashMap(initialCapacity, loadFactor);
        this.timer.scheduleAtFixedRate(this.timerTask, 0L, 1000L);
    }

    public V get(Object key) {
        Long expireTime = this.checkKeyExpireTime(key);
        return expireTime != null && expireTime <= 0L ? null : super.get(key);
    }

    public V put(K key, V value, Long expireSecond) {
        if (expireSecond != null && expireSecond > 0L) {
            this.setKeyExpireTime(key, expireSecond);
        }

        return super.put(key, value);
    }

    public Long checkKeyExpireTime(Object key) {
        Long second = (Long)this.timerMap.get(key);
        if (second == null) {
            return null;
        } else {
            long currentTime = System.currentTimeMillis();
            return (second - currentTime) / 1000L;
        }
    }

    public void setKeyExpireTime(Object key, Long expireSecond) {
        if (expireSecond != null && expireSecond > 0L) {
            long currentTime = System.currentTimeMillis();
            long expireTime = currentTime + expireSecond * 1000L;
            this.timerMap.put(key, expireTime);
        }

    }

    public void setTimerExpireHashMapCallback(TimerExpireHashMapCallback<K, V> timerExpireHashMapCallback) {
        this.timerExpireHashMapCallback = timerExpireHashMapCallback;
    }

    interface TimerExpireHashMapCallback<K, V> {
        void callback(K key, V value) throws RuntimeException;
    }



    class NamelessClass_1 extends TimerTask {
        public void run() {
            long currentTime = System.currentTimeMillis();
            TimerExpireHashMap.this.timerMap.keySet().removeIf((key) -> {
                Long keyTime = (Long)TimerExpireHashMap.this.timerMap.get(key);
                if (currentTime >= keyTime) {
                    if (TimerExpireHashMap.this.timerExpireHashMapCallback != null) {
                        try {
                            TimerExpireHashMap.this.timerExpireHashMapCallback.callback(key, TimerExpireHashMap.super.get(key));
                        } catch (RuntimeException var6) {
                            var6.printStackTrace();
                        }
                    }

                    TimerExpireHashMap.this.remove(key);
                    return true;
                } else {
                    return false;
                }
            });
        }
    }
}
