/* Open Source Java Caching Service
* Copyright (C) 2002 Frank Karlstr�m
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* The author can be contacted by email: fjankk@users.sourceforge.net
*/
package org.fjank.jcache.collection;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import org.fjank.jcache.CacheGroup;
import org.fjank.jcache.CacheObject;
/**A proxy for a key set returned from the Map implementation.
* @author Frank Karlstr�m
*
*/
public class SetProxy implements Set {
private class SetProxyIterator implements Iterator {
private SetProxyIterator(Iterator iter) {
this.wrappedIter=iter;
}
private final Iterator wrappedIter;
private Object currObj;
public boolean hasNext() {
return wrappedIter.hasNext();
}
public Object next() {
this.currObj= wrappedIter.next();
return currObj;
}
public void remove() {
rmv(currObj);
}
}
private CacheGroup group;
private Set set;
/**Creates a new keySetProxy.
* @param set the set to be a proxy for.
* @param group
*/
public SetProxy(Set set, CacheGroup group) {
this.set=set;
this.group=group;
}
/**the implementation of this operation will always
* throw UnsupportedOperatioinException
* @param o the object to add.
* @return a boolean indicating wether the operation failed or not.
*/
public boolean add(Object o) {
throw new UnsupportedOperationException();
}
/**the implementation of this operation will always
* throw UnsupportedOperationException
* @param c the collection to add objects from.
* @return a boolean indicating wether the operation failed or not.
*/
public boolean addAll(Collection c) {
throw new UnsupportedOperationException();
}
/**
* Clears this set, which will clear the cache of all objects.
* This method behaves the same as invalidate on CacheAccess.
* @see javax.util.jcache.CacheAccess#invalidate()
*/
public void clear() {
group.invalidate();
}
/**Returns a boolean indicating wether the object exists
* in the cache or not.
* @param o the object to check the existence for.
* @return <code>true</code> if the object is present in the cache, <code>false</code> otherwise.
*/
public boolean contains(Object o) {
return set.contains(o);
}
public boolean containsAll(Collection c) {
return set.containsAll(c);
}
public boolean isEmpty() {
return set.isEmpty();
}
public Iterator iterator() {
return new SetProxyIterator(set.iterator());
}
private void rmv(Object o) {
remove(o);
}
public boolean remove(Object o) {
if(!set.contains(o)) return false;
if(o instanceof Map.Entry) {
Map.Entry entry = (Entry) o;
CacheObject obj = (CacheObject) entry.getValue();
obj.invalidate();
return true;
}
CacheObject obj = (CacheObject) group.get(o);
if(obj==null) return false;
obj.invalidate();
return true;
}
public boolean removeAll(Collection c) {
/*naive implementation, but it works.*/
boolean changed = false;
for (Iterator iter = c.iterator(); iter.hasNext();) {
if(remove(iter.next())) {
changed=true;
}
}
return changed;
}
/**
* @param c
* @return
*/
public boolean retainAll(Collection c) {
/*naive implementation, but it works.*/
boolean changed = false;
for (Iterator iter = set.iterator(); iter.hasNext();) {
Object tmp = iter.next();
if(!c.contains(tmp)) {
if(remove(tmp)) {
changed=true;
}
}
}
return changed;
}
/**
* @return
*/
public int size() {
return set.size();
}
/**
* @return
*/
public Object[] toArray() {
return set.toArray();
}
/**
* @param a
* @return
*/
public Object[] toArray(Object[] a) {
return set.toArray(a);
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() {
return set.toString();
}
}