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

org.h2.util.SmallLRUCache Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
 * and the EPL 1.0 (https://h2database.com/html/license.html).
 * Initial Developer: H2 Group
 */
package org.h2.util;

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

/**
 * This class implements a small LRU object cache.
 *
 * @param  the key
 * @param  the value
 */
public class SmallLRUCache extends LinkedHashMap {

    private static final long serialVersionUID = 1L;
    private int size;

    private SmallLRUCache(int size) {
        super(size, (float) 0.75, true);
        this.size = size;
    }

    /**
     * Create a new object with all elements of the given collection.
     *
     * @param  the key type
     * @param  the value type
     * @param size the number of elements
     * @return the object
     */
    public static  SmallLRUCache newInstance(int size) {
        return new SmallLRUCache<>(size);
    }

    public void setMaxSize(int size) {
        this.size = size;
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry eldest) {
        return size() > size;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy