View Javadoc

1   /*   Open Source Java Caching Service
2    *    Copyright (C) 2002 Frank Karlstrøm
3    *    This library is free software; you can redistribute it and/or
4    *    modify it under the terms of the GNU Lesser General Public
5    *    License as published by the Free Software Foundation; either
6    *    version 2.1 of the License, or (at your option) any later version.
7    *
8    *    This library is distributed in the hope that it will be useful,
9    *    but WITHOUT ANY WARRANTY; without even the implied warranty of
10   *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11   *    Lesser General Public License for more details.
12   *
13   *    You should have received a copy of the GNU Lesser General Public
14   *    License along with this library; if not, write to the Free Software
15   *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   *
17   *    The author can be contacted by email: fjankk@users.sourceforge.net
18   */
19  package org.fjank.jcache.collection;
20  
21  import java.util.Collection;
22  import java.util.Iterator;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import javax.util.jcache.Attributes;
27  import javax.util.jcache.CacheException;
28  import javax.util.jcache.CacheFullException;
29  import javax.util.jcache.CacheMap;
30  import javax.util.jcache.NotARetrievableObjectException;
31  
32  import org.fjank.jcache.CacheAccessImpl2;
33  import org.fjank.jcache.CacheGroup;
34  import org.fjank.jcache.CacheObject;
35  import org.fjank.jcache.CacheRegion;
36  
37  /**
38   * A Map based implementation of CacheAccess.
39   * 
40   * @author Frank Karlstrøm
41   * 
42   */
43  public class MapAdapter implements CacheMap {
44  	private final CacheAccessImpl2 acc;
45  
46  	public MapAdapter(CacheAccessImpl2 originalAccess) {
47  		this.acc = originalAccess;
48  	}
49  
50  	/**
51  	 * Returns the current number of objects (Raw objects and groups) in this
52  	 * region. this method is not recursive across the groups, so any objects
53  	 * under the groups ini the region root is not accounted for.
54  	 * 
55  	 * @see java.util.Map#size()
56  	 */
57  	public int size() {
58  		return acc.getRegion().getObjectCount();
59  	}
60  
61  	public boolean isEmpty() {
62  		return acc.getRegion().getObjectCount() == 0;
63  	}
64  
65  	/**
66  	 * Returns true if this map contains a mapping for the specified key. More
67  	 * formally, returns true if and only if this map contains at a mapping for
68  	 * a key k such that (key==null ? k==null : key.equals(k)). (There can be at
69  	 * most one such mapping.)
70  	 * 
71  	 * @param key
72  	 *            key whose presence in this map is to be tested.
73  	 * @return <code>true</code> if this map contains a mapping for the
74  	 *         specified key.
75  	 * @throws NullPointerException
76  	 *             if the key is null.
77  	 * @see java.util.Map#containsKey(java.lang.Object)
78  	 */
79  	public boolean containsKey(Object key) {
80  		if (key == null)
81  			throw new NullPointerException("This Map does not permit null keys.");
82  		return acc.isPresent(key);
83  	}
84  
85  	public boolean containsValue(Object value) {
86  		CacheRegion region = acc.getRegion();
87  		return region.containsValue(value);
88  	}
89  
90  	/**
91  	 * Returns the value to which this map maps the specified key. Returns
92  	 * <tt>null</tt> if the map contains no mapping for this key.
93  	 * 
94  	 * <p>
95  	 * More formally, if this map contains a mapping from a key <tt>k</tt> to a
96  	 * value <tt>v</tt> such that <tt>(key.equals(k))</tt>, then this method
97  	 * returns <tt>v</tt>; otherwise it returns <tt>null</tt>. (There can be at
98  	 * most one such mapping.)
99  	 * 
100 	 * @param key
101 	 *            key whose associated value is to be returned.
102 	 * @return the value to which this map maps the specified key, or
103 	 *         <tt>null</tt> if the map contains no mapping for this key.
104 	 * 
105 	 * @throws ClassCastException
106 	 *             if an attempt is mad to retrieve a group.
107 	 * @throws NullPointerException
108 	 *             key is <tt>null</tt>.
109 	 * @see java.util.Map#get(java.lang.Object)
110 	 */
111 	public Object get(Object key) {
112 		if (key == null)
113 			throw new NullPointerException("This Map does not permit null keys.");
114 		Object object = acc.get(key);
115 		if (object == null) {
116 			CacheException ex = acc.getException(true);
117 			if (ex instanceof NotARetrievableObjectException) {
118 				throw new ClassCastException(ex.getMessage());
119 			}
120 			if (ex != null) {
121 				throw new IllegalStateException(ex.getMessage());
122 			}
123 			// not an error. the object did not exist.
124 			return null;
125 		}
126 		return object;
127 	}
128 
129 	/**
130 	 * @see javax.util.jcache.CacheAccess#put(java.lang.Object,
131 	 *      java.lang.Object)
132 	 * @see java.util.Map#put(java.lang.Object, java.lang.Object)
133 	 * @throws IllegalArgumentException
134 	 *             if some aspect of this key or value prevents it from being
135 	 *             stored in this map.
136 	 */
137 	public Object put(Object key, Object value) {
138 		Object prev = null;
139 		if (acc.isPresent(key)) {
140 			prev = acc.get(key);
141 		}
142 		boolean success = acc.put(key, value);
143 		if (!success) {
144 			CacheException ex = acc.getException(true);
145 			if (ex instanceof CacheFullException) {
146 				throw new IllegalArgumentException(ex.getMessage());
147 			}
148 			if (ex != null) {
149 				throw new IllegalStateException(ex.getMessage());
150 			}
151 			throw new IllegalStateException("Unknown error.");
152 		}
153 		return prev;
154 	}
155 
156 	/**
157 	 * @see javax.util.jcache.CacheAccess#put(Object, String, Object)
158 	 * @see CacheMap#put(Object, String, Object)
159 	 * @throws IllegalArgumentException
160 	 *             if some aspect of this key, group or value prevents it from
161 	 *             being stored in this map.
162 	 */
163 	public Object put(Object key, String group, Object value) {
164 		Object prev = null;
165 
166 		if (!acc.isPresent("group")) {
167 			acc.defineGroup(group);
168 		}
169 		if (acc.isPresent(key)) {
170 			prev = acc.get(key, group, null);
171 		}
172 		acc.put(key, group, value);
173 
174 		return prev;
175 	}
176 
177 	/**
178 	 * Removes an object from this map. The removed object is returned. if the
179 	 * object does not exist in the cache, null is returned.
180 	 * 
181 	 * @param key
182 	 *            the object to remove.
183 	 * @return the removed object, or <tt>null</tt> if the object does not exist
184 	 *         in FKache.
185 	 * @see java.util.Map#remove(java.lang.Object)
186 	 */
187 	public Object remove(Object key) {
188 		if (key == null) {
189 			throw new NullPointerException("This Map does not permit null keys.");
190 		}
191 		CacheObject obj = (CacheObject) acc.getRegion().get(key);
192 		if (obj == null) {
193 			return null;
194 		}
195 		obj.invalidate();
196 		return obj.get();
197 	}
198 
199 	public Object remove(Object key, String group) {
200 		if (key == null || group == null) {
201 			throw new NullPointerException("This Map does not permit null keys.");
202 		}
203 		CacheRegion region = acc.getRegion();
204 		CacheGroup group2 = region.getGroup(group);
205 		if (group2 == null) {
206 			return null;
207 		}
208 		CacheObject object = (CacheObject) group2.get(key);
209 		if (object == null) {
210 			return null;
211 		}
212 		object.invalidate();
213 		return object.get();
214 	}
215 
216 	/**
217 	 * Adds all elements in th map into FKache.
218 	 * 
219 	 * @param t
220 	 *            the map which contains the elements to add.
221 	 * @throws IllegalArgumentException
222 	 *             if some aspect of the map prevents this operation to
223 	 *             complete.
224 	 * @throws NullPointerException
225 	 *             if the map is <tt>null</tt>, a key is <tt>null</tt>, or any
226 	 *             value is <tt>null</tt>.
227 	 * @see java.util.Map#putAll(java.util.Map)
228 	 */
229 	public void putAll(Map t) {
230 		// naive implementation, but it works. If someone complains, I'll change
231 		// it.
232 		for (Iterator iter = t.keySet().iterator(); iter.hasNext();) {
233 			Object key = iter.next();
234 			acc.put(key, t.get(key));
235 		}
236 	}
237 
238 	public void clear() {
239 		acc.invalidate();
240 	}
241 
242 	public Set keySet() {
243 		return acc.getRegion().keySet();
244 	}
245 
246 	public Collection values() {
247 		return acc.getRegion().values();
248 	}
249 
250 	public Set entrySet() {
251 		return acc.getRegion().entrySet();
252 	}
253 
254 	/*
255 	 * (non-Javadoc)
256 	 * 
257 	 * @see javax.util.jcache.CacheMap#getAttributes()
258 	 */
259 	public Attributes getAttributes() {
260 		return acc.getAttributes();
261 	}
262 
263 	/*
264 	 * (non-Javadoc)
265 	 * 
266 	 * @see javax.util.jcache.CacheMap#getAttributes(java.lang.Object)
267 	 */
268 	public Attributes getAttributes(Object name) throws CacheException {
269 		return acc.getAttributes(name);
270 	}
271 
272 	/*
273 	 * (non-Javadoc)
274 	 * 
275 	 * @see javax.util.jcache.CacheMap#get(java.lang.Object, java.lang.Object)
276 	 */
277 	public Object get(Object name, Object arguments) {
278 		return acc.get(name, arguments);
279 	}
280 
281 	/*
282 	 * (non-Javadoc)
283 	 * 
284 	 * @see javax.util.jcache.CacheMap#defineObject(java.lang.Object,
285 	 * javax.util.jcache.Attributes)
286 	 */
287 	public void defineObject(Object name, Attributes attributes) {
288 		acc.defineObject(name, attributes);
289 	}
290 }