All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gitee.starblues.loader.classloader.resource.cache.CacheLinkedHashMap Maven / Gradle / Ivy

There is a newer version: 3.1.2
Show newest version
package com.gitee.starblues.loader.classloader.resource.cache;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 注释
 *
 * @author starBlues
 * @since 1.0.0
 * @version 1.0.0
 */
public class CacheLinkedHashMap extends LinkedHashMap {

    private final int size;
    private final RemoveListener removeListener;

    public CacheLinkedHashMap(int size) {
        this(size, null);
    }

    public CacheLinkedHashMap(int size, RemoveListener removeListener) {
        super(size + 1, 1.0f, true);
        this.size = size;
        this.removeListener = removeListener;
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry eldest) {
        if (size == 0) {
            return false;
        }
        int removeSize = size() - size;
        if(removeSize > 0){
            if(removeListener != null){
                try {
                    removeListener.remove(eldest);
                } catch (Exception e){
                    // 忽略
                }
            }
            return true;
        }
        return false;
    }

    @FunctionalInterface
    public interface RemoveListener{

        void remove(Map.Entry eldest);

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy