KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > trove > util > WrappedCache


1 /* ====================================================================
2  * Trove - Copyright (c) 1997-2001 Walt Disney Internet Group
3  * ====================================================================
4  * The Tea Software License, Version 1.1
5  *
6  * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Walt Disney Internet Group (http://opensource.go.com/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact opensource@dig.com.
31  *
32  * 5. Products derived from this software may not be called "Tea",
33  * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
34  * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior
35  * written permission of the Walt Disney Internet Group.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS
41  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
43  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
44  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
45  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * For more information about Tea, please see http://opensource.go.com/.
51  */

52
53 package com.go.trove.util;
54
55 import java.util.*;
56
57 /******************************************************************************
58  * A Map that implements a write-through cache to another map. Two maps are
59  * supplied: one for caching and one for main storage. WrappedCache is not
60  * thread-safe and must be wrapped with Collections.synchronizedMap to be made
61  * thread-safe.
62  *
63  * @author Brian S O'Neill
64  * @version
65  * <!--$$Revision:--> 2 <!-- $-->, <!--$$JustDate:--> 01/07/31 <!-- $-->
66  */

67 public class WrappedCache extends AbstractMap implements Map {
68     private final Map mCacheMap;
69     private final Map mBackingMap;
70
71     /**
72      * @param cacheMap the cache map should offer fast access, but it should
73      * automatically limit its maximum size
74      * @param backingMap the backingMap will be read from only if the requested
75      * entry isn't in the cache
76      */

77     public WrappedCache(Map cacheMap, Map backingMap) {
78         mCacheMap = cacheMap;
79         mBackingMap = backingMap;
80     }
81
82     /**
83      * Returns the size of the backing map.
84      */

85     public int size() {
86         return mBackingMap.size();
87     }
88
89     /**
90      * Returns the empty status of the backing map.
91      */

92     public boolean isEmpty() {
93         return mBackingMap.isEmpty();
94     }
95
96     /**
97      * Returns true if the cache contains the key or else if the backing map
98      * contains the key.
99      */

100     public boolean containsKey(Object JavaDoc key) {
101         return mCacheMap.containsKey(key) || mBackingMap.containsKey(key);
102     }
103
104     /**
105      * Returns true if the cache contains the value or else if the backing map
106      * contains the value.
107      */

108     public boolean containsValue(Object JavaDoc value) {
109         return mCacheMap.containsValue(value) ||
110             mBackingMap.containsValue(value);
111     }
112
113     /**
114      * Returns the value from the cache, or if not found, the backing map.
115      * If the backing map is accessed, the value is saved in the cache for
116      * future gets.
117      */

118     public Object JavaDoc get(Object JavaDoc key) {
119         Object JavaDoc value = mCacheMap.get(key);
120         if (value != null || mCacheMap.containsKey(key)) {
121             return value;
122         }
123         value = mBackingMap.get(key);
124         if (value != null || mBackingMap.containsKey(key)) {
125             mCacheMap.put(key, value);
126         }
127         return value;
128     }
129
130     /**
131      * Puts the entry into both the cache and backing map. The old value in
132      * the backing map is returned.
133      */

134     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
135         mCacheMap.put(key, value);
136         return mBackingMap.put(key, value);
137     }
138
139     /**
140      * Removes the key from both the cache and backing map. The old value in
141      * the backing map is returned.
142      */

143     public Object JavaDoc remove(Object JavaDoc key) {
144         mCacheMap.remove(key);
145         return mBackingMap.remove(key);
146     }
147
148     /**
149      * Clears both the cache and backing map.
150      */

151     public void clear() {
152         mCacheMap.clear();
153         mBackingMap.clear();
154     }
155
156     /**
157      * Returns the key set of the backing map.
158      */

159     public Set keySet() {
160         return mBackingMap.keySet();
161     }
162
163     /**
164      * Returns the values of the backing map.
165      */

166     public Collection values() {
167         return mBackingMap.values();
168     }
169
170     /**
171      * Returns the entry set of the backing map.
172      */

173     public Set entrySet() {
174         return mBackingMap.entrySet();
175     }
176 }
177
Popular Tags