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  import java.util.Map.Entry;
26  import org.fjank.jcache.CacheGroup;
27  import org.fjank.jcache.CacheObject;
28  
29  /**A proxy for a key set returned from the Map implementation.
30   * @author Frank Karlstrøm
31   *
32   */
33  public class SetProxy implements Set {
34  	
35      private class SetProxyIterator implements Iterator {
36      	private SetProxyIterator(Iterator iter) {
37      		this.wrappedIter=iter;
38      	}
39      	private final Iterator wrappedIter;
40      	private Object currObj;
41      	public boolean hasNext() {
42              return wrappedIter.hasNext();
43          }
44          public Object next() {
45             this.currObj= wrappedIter.next();
46             return currObj;
47          }
48          public void remove() {
49             rmv(currObj);
50          }
51  
52      }
53      private CacheGroup group;
54  
55      private Set set;
56  
57      /**Creates a new keySetProxy.
58       * @param set the set to be a proxy for.
59       * @param group
60       */
61      public SetProxy(Set set, CacheGroup group) {
62          this.set=set;
63          this.group=group;
64      }
65      /**the implementation of this operation will always 
66       * throw UnsupportedOperatioinException
67       * @param o the object to add.
68       * @return a boolean indicating wether the operation failed or not.
69       */
70      public boolean add(Object o) {
71          throw new UnsupportedOperationException();
72      }
73  
74  	/**the implementation of this operation will always 
75  	 * throw UnsupportedOperationException
76  	 * @param c the collection to add objects from.
77  	 * @return a boolean indicating wether the operation failed or not.
78  	 */
79      public boolean addAll(Collection c) {
80  		throw new UnsupportedOperationException();
81      }
82  
83      /**
84       * Clears this set, which will clear the cache of all objects.
85       * This method behaves the same as invalidate on CacheAccess.
86       * @see javax.util.jcache.CacheAccess#invalidate()
87       */
88      public void clear() {
89          group.invalidate();
90      }
91  
92      /**Returns a boolean indicating wether the object exists
93       * in the cache or not.
94       * @param o the object to check the existence for.
95       * @return <code>true</code> if the object is present in the cache, <code>false</code> otherwise.
96       */
97      public boolean contains(Object o) {
98          return set.contains(o);
99      }
100 
101     
102     public boolean containsAll(Collection c) {
103         return set.containsAll(c);
104     }
105 
106     public boolean isEmpty() {
107         return set.isEmpty();
108     }
109 
110 
111     public Iterator iterator() {
112         return new SetProxyIterator(set.iterator());
113     }
114 	private void rmv(Object o) {
115 		remove(o);
116 	}
117 
118     public boolean remove(Object o) {
119 		if(!set.contains(o)) return false;
120         if(o instanceof Map.Entry) {
121         	Map.Entry entry = (Entry) o;
122         	CacheObject obj = (CacheObject) entry.getValue();
123         	obj.invalidate();
124         	return true;
125         }
126 		CacheObject obj = (CacheObject) group.get(o);
127 		if(obj==null) return false;
128 		obj.invalidate();
129 		return true;
130     }
131 
132     public boolean removeAll(Collection c) {
133         /*naive implementation, but it works.*/
134         boolean changed = false;
135         for (Iterator iter = c.iterator(); iter.hasNext();) {
136             if(remove(iter.next())) {
137             	changed=true;
138             }
139         }
140         return changed;
141     }
142 
143     /**
144      * @param c
145      * @return
146      */
147     public boolean retainAll(Collection c) {
148 		/*naive implementation, but it works.*/
149 		boolean changed = false;
150 		for (Iterator iter = set.iterator(); iter.hasNext();) {
151 			Object tmp = iter.next();
152             if(!c.contains(tmp)) {
153 				if(remove(tmp)) {
154 					changed=true;
155 				}
156 			}
157 		}
158 		return changed;
159     }
160 
161     /**
162      * @return
163      */
164     public int size() {
165         return set.size();
166     }
167 
168     /**
169      * @return
170      */
171     public Object[] toArray() {
172         return set.toArray();
173     }
174 
175     /**
176      * @param a
177      * @return
178      */
179     public Object[] toArray(Object[] a) {
180         return set.toArray(a);
181     }
182 
183     /* (non-Javadoc)
184      * @see java.lang.Object#toString()
185      */
186     public String toString() {
187         return set.toString();
188     }
189 
190 }