⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fixedsizecache.java

📁 Java的面向对象数据库系统的源代码
💻 JAVA
字号:
// You can redistribute this software and/or modify it under the terms of// the Ozone Core License version 1 published by ozone-db.org.//// Copyright (C) 2003-@year@, Leo Mekenkamp. All rights reserved.//// $Id: FixedSizeCache.java,v 1.4 2004/03/23 10:14:14 leomekenkamp Exp $package org.ozoneDB.core.storage;import java.util.Collection;import java.util.Comparator;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.LinkedHashSet;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;import java.util.Map.Entry;import java.util.SortedSet;import java.util.TreeMap;import java.util.TreeSet;import java.util.logging.Level;import java.util.logging.Logger;import org.ozoneDB.OzoneInternalException;import org.ozoneDB.core.ConfigurationException;/** * <p>A cache with a fixed maximum number of objects in it. This cache grows * until it has reached its maximum, which can be set by maxCapacity. Once that * maximum has been reached, the least recently used object is thrown away when * a new object is put in the cache. This cache has, next to that maximum number * of elements, also a maximum time an element will remain in this cache. If  * that time has expired the element will be removed from this cache also.  * Reinserting an element (using the same key) causes the time for that element * to be reset.</p> * * <p>Note: every instance creates its own thread for asynchronous removal of * items past their 'best before' time. The trimmed() method on the TrimHandler * is often but not always called asynchronously from that thread.</p> *  * @author <a href="mailto:leoATmekenkampD0Tcom">Leo Mekenkamp (mind the anti sp@m)</a> * @version $Id: FixedSizeCache.java,v 1.4 2004/03/23 10:14:14 leomekenkamp Exp $ */public class FixedSizeCache extends AbstractTrimmingCache implements PropertyConfigurable {        private static final Logger log = Logger.getLogger(FixedSizeDelayCache.class.getName());    public static final PropertyInfo MAXCAPACITY = new PropertyInfo(        ".maxCapacity",        "int",        "1",        "number of objects that this cache should hold as a maximum",        new String[] {"100", "100000"}    );    public static final PropertyInfo INITIALCAPACITY = new PropertyInfo(        ".initialCapacity",        "int",        "1",        "number of objects that this cache reserves space for initially",        new String[] {"100", "100000"}    );    public static final PropertyInfo LOADFACTOR = new PropertyInfo(        ".loadFactor",        "float",        "0.75",        "load factor for this cache; see java.util.LinkedHashMap(int initialCapacity, float loadFactor)",        new String[] {"0.5", "0.95"}    );    private int maxCapacity;        private Map map;        /**     * @throws ConfigurationException     */    public FixedSizeCache(Properties properties, String prefix) {        super(properties, prefix);        try {            float loadFactor = Float.parseFloat(properties.getProperty(prefix + LOADFACTOR.getKey(), LOADFACTOR.getDefaultValue()));            log.config(getPrefix() + " using a load factor of " + loadFactor);            int initialCapacity = Integer.parseInt(properties.getProperty(prefix + INITIALCAPACITY.getKey(), INITIALCAPACITY.getDefaultValue()));            log.config(getPrefix() + " using an initial capacity of " + loadFactor);            map = new LinkedHashMap(initialCapacity, loadFactor, true);            setMaxCapacity(Integer.parseInt(properties.getProperty(prefix + MAXCAPACITY.getKey(), MAXCAPACITY.getDefaultValue())));            log.config(getPrefix() + " using a max capacity of " + getMaxCapacity());        } catch (NumberFormatException e) {            throw new ConfigurationException(e);        }    }    private Map getMap() {        return map;    }    public final void setMaxCapacity(int maxCapacity) {        synchronized(getSynchronizer()) {            this.maxCapacity = maxCapacity;            trim();        }    }        public final int getMaxCapacity() {        // maxCapacity is an int so synchronization is not needed        return maxCapacity;    }        public Collection getPropertyInfos() {        Collection result = new LinkedList();        result.add(MAXCAPACITY);        return result;    }        public void put(Object key, Object value) {        synchronized(getSynchronizer()) {            getMap().put(key, value);            trim();        }    }    private void trim() {        int size = getMap().size() - getMaxCapacity();        int countDown = getMaxCapacity();        if (size > countDown) {            for (Iterator i = getMap().entrySet().iterator(); i.hasNext(); ) {                Map.Entry entry = (Map.Entry) i.next();                if (--countDown < 0) {                    if (getTrimHandler() != null) {                        getTrimHandler().trimming(entry.getKey(), entry.getValue());                    }                    i.remove();                }            }        }    }        public Map copyToMap() {        synchronized(getSynchronizer()) {            return new HashMap(getMap());        }    }        public Object get(Object key) {        synchronized(getSynchronizer()) {            return getMap().get(key);        }    }        public Object remove(Object key) {        synchronized(getSynchronizer()) {            return getMap().remove(key);        }    }        public int size() {        synchronized(getSynchronizer()) {            return getMap().size();        }    }        private class OurMap extends LinkedHashMap {        public OurMap(int initialCapacity, float loadFactor) {            super(initialCapacity, loadFactor, true);        }                protected boolean removeEldestEntry(Map.Entry entry) {            if (size() > getMaxCapacity()) {                if (getTrimHandler() != null) {                    getTrimHandler().trimming(entry.getKey(), entry.getValue());                }                remove(entry.getKey());            }            return false;        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -