2362N/A * Copyright (c) 2002, 2009, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A * Abstract base class and factory for caches. A cache is a key-value mapping. 0N/A * It has properties that make it more suitable for caching than a Map. 0N/A * The factory methods can be used to obtain two different implementations. 0N/A * They have the following properties: 0N/A * . keys and values reside in memory 0N/A * . keys and values must be non-null 0N/A * . maximum size. Replacements are made in LRU order. 0N/A * . optional lifetime, specified in seconds. 0N/A * . save for concurrent use by multiple threads 0N/A * . values are held by either standard references or via SoftReferences. 0N/A * SoftReferences have the advantage that they are automatically cleared 0N/A * by the garbage collector in response to memory demand. This makes it 0N/A * possible to simple set the maximum size to a very large value and let 0N/A * the GC automatically size the cache dynamically depending on the 0N/A * amount of available memory. 0N/A * However, note that because of the way SoftReferences are implemented in 0N/A * HotSpot at the moment, this may not work perfectly as it clears them fairly 0N/A * eagerly. Performance may be improved if the Java heap size is set to larger 0N/A * value using e.g. java -ms64M -mx128M foo.Test 0N/A * Cache sizing: the memory cache is implemented on top of a LinkedHashMap. 0N/A * In its current implementation, the number of buckets (NOT entries) in 0N/A * (Linked)HashMaps is always a power of two. It is recommended to set the 0N/A * maximum cache size to value that uses those buckets fully. For example, 0N/A * if a cache with somewhere between 500 and 1000 entries is desired, a 0N/A * maximum size of 750 would be a good choice: try 1024 buckets, with a 0N/A * load factor of 0.75f, the number of entries can be calculated as 0N/A * buckets / 4 * 3. As mentioned above, with a SoftReference cache, it is 0N/A * generally reasonable to set the size to a fairly large value. 0N/A * @author Andreas Sterbenz 0N/A * Return the number of currently valid entries in the cache. 0N/A * Remove all entries from the cache. 0N/A * Add an entry to the cache. 0N/A * Get a value from the cache. 0N/A * Remove an entry from the cache. 896N/A * Set the maximum size. 896N/A * Set the timeout(in seconds). 0N/A * Return a new memory cache with the specified maximum size, unlimited 0N/A * lifetime for entries, with the values held by SoftReferences. 0N/A * Return a new memory cache with the specified maximum size, the 0N/A * specified maximum lifetime (in seconds), with the values held 0N/A * by SoftReferences. 0N/A * Return a new memory cache with the specified maximum size, unlimited 0N/A * lifetime for entries, with the values held by standard references. 0N/A * Return a dummy cache that does nothing. 0N/A * Return a new memory cache with the specified maximum size, the 0N/A * specified maximum lifetime (in seconds), with the values held 0N/A * by standard references. 0N/A * Utility class that wraps a byte array and implements the equals() 0N/A * and hashCode() contract in a way suitable for Maps and caches. 0N/A private final byte[] b;
0N/A h += (b[i] &
0xff) *
37;
0N/A private final static boolean DEBUG =
false;
0N/A * Empty the reference queue and remove all corresponding entries 0N/A * This method should be called at the beginning of each public 0N/A // key is null, entry has already been removed 0N/A // check if the entry in the map corresponds to the expired 0N/A // entry. If not, readd the entry 0N/A * Scan all entries and remove all expired ones. 0N/A // if this is a SoftReference cache, first invalidate() all 0N/A // entries so that GC does not have to enqueue them 896N/A // it is a heavyweight method.