KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > fjank > jcache > collection > MapAdapter


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 JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25 import javax.util.jcache.Attributes;
26 import javax.util.jcache.CacheException;
27 import javax.util.jcache.CacheFullException;
28 import javax.util.jcache.CacheMap;
29 import javax.util.jcache.NotARetrievableObjectException;
30 import org.fjank.jcache.CacheAccessImpl2;
31 import org.fjank.jcache.CacheGroup;
32 import org.fjank.jcache.CacheObject;
33 import org.fjank.jcache.CacheRegion;
34
35 /**A Map based implementation of CacheAccess.
36  * @author Frank Karlstrøm
37  *
38  */

39 public class MapAdapter implements CacheMap {
40     private final CacheAccessImpl2 acc;
41     public MapAdapter(CacheAccessImpl2 originalAccess) {
42         this.acc=originalAccess;
43     }
44     /**Returns the current number of objects (Raw objects and groups)
45      * in this region. this method is not recursive across the groups,
46      * so any objects under the groups ini the region root is not
47      * accounted for.
48      *
49      * @see java.util.Map#size()
50      */

51     public int size() {
52         return acc.getRegion().getObjectCount();
53     }
54
55     public boolean isEmpty() {
56         return acc.getRegion().getObjectCount()==0;
57     }
58
59     /**Returns true if this map contains a mapping for the specified key.
60      * More formally, returns true if and
61     * only if this map contains at a mapping for a key k such that
62     * (key==null ? k==null : key.equals(k)).
63     * (There can be at most one such mapping.)
64     * @param key key whose presence in this map is to be tested.
65     * @return <code>true</code> if this map contains a mapping for the specified key.
66     * @throws NullPointerException if the key is null.
67      * @see java.util.Map#containsKey(java.lang.Object)
68      */

69     public boolean containsKey(Object JavaDoc key) {
70         if(key==null) throw new NullPointerException JavaDoc("This Map does not permit null keys.");
71         return acc.isPresent(key);
72     }
73
74     public boolean containsValue(Object JavaDoc value) {
75        CacheRegion region = acc.getRegion();
76        return region.containsValue(value);
77     }
78
79     /**
80      * Returns the value to which this map maps the specified key. Returns
81      * <tt>null</tt> if the map contains no mapping for this key.
82      *
83      * <p>More formally, if this map contains a mapping from a key
84      * <tt>k</tt> to a value <tt>v</tt> such that <tt>(key.equals(k))</tt>,
85      * then this method returns <tt>v</tt>; otherwise
86      * it returns <tt>null</tt>. (There can be at most one such mapping.)
87      *
88      * @param key key whose associated value is to be returned.
89      * @return the value to which this map maps the specified key, or
90      * <tt>null</tt> if the map contains no mapping for this key.
91      *
92      * @throws ClassCastException if an attempt is mad to retrieve a group.
93      * @throws NullPointerException key is <tt>null</tt>.
94      * @see java.util.Map#get(java.lang.Object)
95      */

96     public Object JavaDoc get(Object JavaDoc key) {
97         if(key==null) throw new NullPointerException JavaDoc("This Map does not permit null keys.");
98         Object JavaDoc object = acc.get(key);
99         if(object==null) {
100             CacheException ex = acc.getException(true);
101             if(ex instanceof NotARetrievableObjectException) {
102                 throw new ClassCastException JavaDoc(ex.getMessage());
103             }
104             if(ex!=null) {
105                 throw new IllegalStateException JavaDoc(ex.getMessage());
106             }
107             //not an error. the object did not exist.
108
return null;
109         }
110         return object;
111     }
112
113     /**
114      * @see javax.util.jcache.CacheAccess#put(java.lang.Object, java.lang.Object)
115      * @see java.util.Map#put(java.lang.Object, java.lang.Object)
116      * @throws IllegalArgumentException if some aspect of this key or value prevents it from being stored in this map.
117      */

118     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
119         Object JavaDoc prev=null;
120         if(acc.isPresent(key)) {
121             prev = acc.get(key);
122         }
123         boolean success = acc.put(key, value);
124         if(!success) {
125             CacheException ex = acc.getException(true);
126             if(ex instanceof CacheFullException) {
127                 throw new IllegalArgumentException JavaDoc(ex.getMessage());
128             }
129             if(ex!=null) {
130                 throw new IllegalStateException JavaDoc(ex.getMessage());
131             }
132             throw new IllegalStateException JavaDoc("Unknown error.");
133         }
134         return prev;
135     }
136     /**
137      * @see javax.util.jcache.CacheAccess#put(Object, String, Object)
138      * @see CacheMap#put(Object, String, Object)
139      * @throws IllegalArgumentException if some aspect of this key, group or value prevents it from being stored in this map.
140      */

141     public Object JavaDoc put(Object JavaDoc key, String JavaDoc group, Object JavaDoc value) {
142        Object JavaDoc prev = null;
143        
144            if(!acc.isPresent("group")) {
145                acc.defineGroup(group);
146            }
147            if(acc.isPresent(key)) {
148                prev=acc.get(key, group, null);
149            }
150            acc.put(key, group, value);
151     
152            return prev;
153     }
154     /**Removes an object from this map. The removed object is returned.
155      * if the object does not exist in the cache, null is returned.
156      *
157      * @param key the object to remove.
158      * @return the removed object, or <tt>null</tt> if the object does not exist in FKache.
159      * @see java.util.Map#remove(java.lang.Object)
160      */

161     public Object JavaDoc remove(Object JavaDoc key) {
162         if (key==null) {
163             throw new NullPointerException JavaDoc("This Map does not permit null keys.");
164         }
165         CacheObject obj = (CacheObject) acc.getRegion().get(key);
166         if(obj==null) {
167             return null;
168         }
169         obj.invalidate();
170         return obj.get();
171     }
172     public Object JavaDoc remove(Object JavaDoc key, String JavaDoc group) {
173         if (key==null || group==null) {
174             throw new NullPointerException JavaDoc("This Map does not permit null keys.");
175         }
176         CacheRegion region = acc.getRegion();
177         CacheGroup group2 = region.getGroup(group);
178         if (group2 == null) {
179             return null;
180         }
181         CacheObject object = (CacheObject) group2.get(key);
182         if(object==null) {
183             return null;
184         }
185         object.invalidate();
186         return object.get();
187     }
188     /**Adds all elements in th map into FKache.
189      * @param t the map which contains the elements to add.
190      * @throws IllegalArgumentException if some aspect of the map prevents this operation to complete.
191      * @throws NullPointerException if the map is <tt>null</tt>, a key is <tt>null</tt>, or any value is <tt>null</tt>.
192      * @see java.util.Map#putAll(java.util.Map)
193      */

194     public void putAll(Map JavaDoc t) {
195         //naive implementation, but it works. If someone complains, I'll change it.
196
for (Iterator JavaDoc iter = t.keySet().iterator(); iter.hasNext();) {
197             Object JavaDoc key = iter.next();
198             acc.put(key, t.get(key));
199         }
200     }
201
202     public void clear() {
203         acc.invalidate();
204     }
205
206     public Set JavaDoc keySet() {
207         return acc.getRegion().keySet();
208     }
209
210     public Collection JavaDoc values() {
211         return acc.getRegion().values();
212     }
213
214     public Set JavaDoc entrySet() {
215         return acc.getRegion().entrySet();
216     }
217     /* (non-Javadoc)
218      * @see javax.util.jcache.CacheMap#getAttributes()
219      */

220     public Attributes getAttributes() {
221         return acc.getAttributes();
222     }
223     /* (non-Javadoc)
224      * @see javax.util.jcache.CacheMap#getAttributes(java.lang.Object)
225      */

226     public Attributes getAttributes(Object JavaDoc name) throws CacheException {
227         return acc.getAttributes(name);
228     }
229     /* (non-Javadoc)
230      * @see javax.util.jcache.CacheMap#get(java.lang.Object, java.lang.Object)
231      */

232     public Object JavaDoc get(Object JavaDoc name, Object JavaDoc arguments) {
233         return acc.get(name, arguments);
234     }
235     /* (non-Javadoc)
236      * @see javax.util.jcache.CacheMap#defineObject(java.lang.Object, javax.util.jcache.Attributes)
237      */

238     public void defineObject(Object JavaDoc name, Attributes attributes) {
239         acc.defineObject(name, attributes);
240     }
241 }
242
Popular Tags