J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package com.sun.jmx.remote.util; |
| 27 | |
| 28 | import java.lang.ref.SoftReference; |
| 29 | import java.util.Iterator; |
| 30 | import java.util.LinkedList; |
| 31 | import java.util.List; |
| 32 | import java.util.WeakHashMap; |
| 33 | |
| 34 | import com.sun.jmx.mbeanserver.Util; |
| 35 | |
| 36 | /** |
| 37 | * <p>Like WeakHashMap, except that the keys of the <em>n</em> most |
| 38 | * recently-accessed entries are kept as {@link SoftReference soft |
| 39 | * references}. Accessing an element means creating it, or retrieving |
| 40 | * it with {@link #get(Object) get}. Because these entries are kept |
| 41 | * with soft references, they will tend to remain even if their keys |
| 42 | * are not referenced elsewhere. But if memory is short, they will |
| 43 | * be removed.</p> |
| 44 | */ |
| 45 | public class CacheMap<K, V> extends WeakHashMap<K, V> { |
| 46 | /** |
| 47 | * <p>Create a <code>CacheMap</code> that can keep up to |
| 48 | * <code>nSoftReferences</code> as soft references.</p> |
| 49 | * |
| 50 | * @param nSoftReferences Maximum number of keys to keep as soft |
| 51 | * references. Access times for {@link #get(Object) get} and |
| 52 | * {@link #put(Object, Object) put} have a component that scales |
| 53 | * linearly with <code>nSoftReferences</code>, so this value |
| 54 | * should not be too great. |
| 55 | * |
| 56 | * @throws IllegalArgumentException if |
| 57 | * <code>nSoftReferences</code> is negative. |
| 58 | */ |
| 59 | public CacheMap(int nSoftReferences) { |
| 60 | if (nSoftReferences < 0) { |
| 61 | throw new IllegalArgumentException("nSoftReferences = " + |
| 62 | nSoftReferences); |
| 63 | } |
| 64 | this.nSoftReferences = nSoftReferences; |
| 65 | } |
| 66 | |
| 67 | public V put(K key, V value) { |
| 68 | cache(key); |
| 69 | return super.put(key, value); |
| 70 | } |
| 71 | |
| 72 | public V get(Object key) { |
| 73 | cache(Util.<K>cast(key)); |
| 74 | return super.get(key); |
| 75 | } |
| 76 | |
| 77 | /* We don't override remove(Object) or try to do something with |
| 78 | the map's iterators to detect removal. So we may keep useless |
| 79 | entries in the soft reference list for keys that have since |
| 80 | been removed. The assumption is that entries are added to the |
| 81 | cache but never removed. But the behavior is not wrong if |
| 82 | they are in fact removed -- the caching is just less |
| 83 | performant. */ |
| 84 | |
| 85 | private void cache(K key) { |
| 86 | Iterator<SoftReference<K>> it = cache.iterator(); |
| 87 | while (it.hasNext()) { |
| 88 | SoftReference<K> sref = it.next(); |
| 89 | K key1 = sref.get(); |
| 90 | if (key1 == null) |
| 91 | it.remove(); |
| 92 | else if (key.equals(key1)) { |
| 93 | // Move this element to the head of the LRU list |
| 94 | it.remove(); |
| 95 | cache.add(0, sref); |
| 96 | return; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | int size = cache.size(); |
| 101 | if (size == nSoftReferences) { |
| 102 | if (size == 0) |
| 103 | return; // degenerate case, equivalent to WeakHashMap |
| 104 | it.remove(); |
| 105 | } |
| 106 | |
| 107 | cache.add(0, new SoftReference<K>(key)); |
| 108 | } |
| 109 | |
| 110 | /* List of soft references for the most-recently referenced keys. |
| 111 | The list is in most-recently-used order, i.e. the first element |
| 112 | is the most-recently referenced key. There are never more than |
| 113 | nSoftReferences elements of this list. |
| 114 | |
| 115 | If we didn't care about J2SE 1.3 compatibility, we could use |
| 116 | LinkedHashSet in conjunction with a subclass of SoftReference |
| 117 | whose equals and hashCode reflect the referent. */ |
| 118 | private final LinkedList<SoftReference<K>> cache = |
| 119 | new LinkedList<SoftReference<K>>(); |
| 120 | private final int nSoftReferences; |
| 121 | } |