001: /* ====================================================================
002: * Trove - Copyright (c) 1997-2000 Walt Disney Internet Group
003: * ====================================================================
004: * The Tea Software License, Version 1.1
005: *
006: * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Walt Disney Internet Group (http://opensource.go.com/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact opensource@dig.com.
031: *
032: * 5. Products derived from this software may not be called "Tea",
033: * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
034: * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior
035: * written permission of the Walt Disney Internet Group.
036: *
037: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
038: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
039: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
040: * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS
041: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
042: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
043: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
044: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
045: * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
046: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
047: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
048: * ====================================================================
049: *
050: * For more information about Tea, please see http://opensource.go.com/.
051: */
052:
053: package com.go.trove.util;
054:
055: import java.util.*;
056:
057: /******************************************************************************
058: * Cache is a SoftHashMap that is guaranteed to have the most recently used
059: * entries available. Calling "get" or "put" updates the internal MRU list,
060: * but calling "containsKey" or "containsValue" will not.
061: * <p>
062: * Like its base class, Cache is not thread-safe and must be wrapped with
063: * Collections.synchronizedMap to be made thread-safe.
064: *
065: * parametrized for GJ by Stefan Reich (doc@drjava.de)
066: *
067: * @author Brian S O'Neill
068: * @version
069: * <!--$$Revision: 1.2 $-->, <!--$$JustDate:--> 01/05/30 <!-- $-->
070: */
071: public class Cache<A, B> extends SoftHashMap<A, B> {
072: private final int mMaxRecent;
073: // Contains hard references to entries.
074: private final UsageMap<A, B> mUsageMap;
075:
076: /**
077: * Construct a Cache with an amount of recently used entries that are
078: * guaranteed to always be in the Cache.
079: *
080: * @param maxRecent maximum amount of recently used entries guaranteed to
081: * be in the Cache.
082: * @throws IllegalArgumentException if maxRecent is less than or equal to
083: * zero.
084: */
085: public Cache(int maxRecent) {
086: if (maxRecent <= 0) {
087: throw new IllegalArgumentException(
088: "Max recent must be greater than zero: "
089: + maxRecent);
090: }
091: mMaxRecent = maxRecent;
092: mUsageMap = new UsageMap();
093: }
094:
095: /**
096: * Piggyback this Cache onto another one in order for the map of recently
097: * used entries to be shared. If this Cache is more active than the one
098: * it attaches to, then more of its most recently used entries will be
099: * guaranteed to be in the Cache, possibly bumping out entries from the
100: * other Cache.
101: */
102: public Cache(Cache cache) {
103: mMaxRecent = cache.mMaxRecent;
104: mUsageMap = cache.mUsageMap;
105: }
106:
107: public B get(A key) {
108: B value = super .get(key);
109: if (value != null || super .containsKey(key)) {
110: adjustMRU(key, value);
111: }
112: return value;
113: }
114:
115: public B put(A key, B value) {
116: /*if (value == null) {
117: value = new Null();
118: }*/
119: adjustMRU(key, value);
120: return super .put(key, value);
121: }
122:
123: public B remove(A key) {
124: synchronized (mUsageMap) {
125: mUsageMap.remove(key);
126: }
127: return super .remove(key);
128: }
129:
130: public void clear() {
131: super .clear();
132: synchronized (mUsageMap) {
133: mUsageMap.clear();
134: }
135: }
136:
137: private void adjustMRU(A key, B value) {
138: synchronized (mUsageMap) {
139: B existing = mUsageMap.get(key);
140:
141: if (existing != null) {
142: /*if (value == null && existing instanceof Null) {
143: value = existing;
144: }*/
145: } else {
146: if (!mUsageMap.containsKey(key)) {
147: // A new entry will be put into the UsageMap, so remove
148: // least recently used if MRU is too big.
149: while (mUsageMap.size() >= mMaxRecent) {
150: mUsageMap.remove(mUsageMap.lastKey());
151: }
152: }
153: }
154:
155: mUsageMap.put(key, value);
156: }
157: }
158: }
|