Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 178   Methods: 16
NCLOC: 128   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CachedSetInterceptor.java 35.7% 35.3% 31.2% 34.6%
coverage coverage
 1    /*
 2    * JBoss, the OpenSource J2EE webOS
 3    *
 4    * Distributable under LGPL license.
 5    * See terms of license at gnu.org.
 6    */
 7    package org.jboss.cache.pojo.interceptors.dynamic;
 8   
 9    import org.jboss.aop.advice.Interceptor;
 10    import org.jboss.cache.Fqn;
 11    import org.jboss.cache.pojo.collection.CachedSetImpl;
 12    import org.jboss.cache.pojo.collection.CollectionInterceptorUtil;
 13    import org.jboss.cache.pojo.impl.PojoCacheImpl;
 14   
 15    import java.util.HashSet;
 16    import java.util.Iterator;
 17    import java.util.Map;
 18    import java.util.Set;
 19   
 20    /**
 21    * Set interceptor that delegates underlying impl.
 22    *
 23    * @author Ben Wang
 24    */
 25    @SuppressWarnings({"CanBeFinal"})
 26    public class CachedSetInterceptor extends AbstractCollectionInterceptor
 27    {
 28    // protected static final Log log_=LogFactory.getLog(CachedSetInterceptor.class);
 29   
 30    private Map methodMap_;
 31    private static final Map managedMethods_ =
 32    CollectionInterceptorUtil.getManagedMethods(Set.class);
 33    private Set cacheImpl_;
 34    private Set current_;
 35    private Set inMemImpl_;
 36   
 37  66 public CachedSetInterceptor(PojoCacheImpl cache, Fqn fqn, Class clazz, Set obj)
 38    {
 39  66 super(cache, fqn);
 40  66 methodMap_ = CollectionInterceptorUtil.getMethodMap(clazz);
 41  66 cacheImpl_ = new CachedSetImpl(cache, this);
 42  66 inMemImpl_ = obj;
 43  66 current_ = cacheImpl_;
 44    }
 45   
 46  0 private CachedSetInterceptor(PojoCacheImpl cache, Fqn fqn)
 47    {
 48  0 super(cache, fqn);
 49    }
 50   
 51  0 public Object clone()
 52    {
 53  0 CachedSetInterceptor interceptor = new CachedSetInterceptor(cache, fqn);
 54  0 interceptor.setFqn(getFqn());
 55  0 interceptor.setAopInstance(getAopInstance());
 56  0 interceptor.setCurrentCopy(getCurrentCopy());
 57  0 interceptor.setInMemoryCopy(getInMemoryCopy());
 58  0 interceptor.setCacheCopy(getCacheCopy());
 59  0 return interceptor;
 60    }
 61   
 62  0 public void setInterceptor(Interceptor intcptr)
 63    {
 64  0 CachedSetInterceptor interceptor = (CachedSetInterceptor) intcptr;
 65  0 setFqn(interceptor.getFqn());
 66  0 setAopInstance(interceptor.getAopInstance());
 67  0 setCurrentCopy(interceptor.getCurrentCopy());
 68  0 setInMemoryCopy(interceptor.getInMemoryCopy());
 69  0 setCacheCopy(interceptor.getCacheCopy());
 70    }
 71   
 72  11 public Object getCurrentCopy()
 73    {
 74  11 return current_;
 75    }
 76   
 77  0 void setInMemoryCopy(Object obj)
 78    {
 79  0 inMemImpl_ = (Set) obj;
 80    }
 81   
 82  0 Object getInMemoryCopy()
 83    {
 84  0 return inMemImpl_;
 85    }
 86   
 87  0 void setCacheCopy(Object obj)
 88    {
 89  0 cacheImpl_ = (Set) obj;
 90    }
 91   
 92  0 Object getCacheCopy()
 93    {
 94  0 return cacheImpl_;
 95    }
 96   
 97  0 void setCurrentCopy(Object obj)
 98    {
 99  0 current_ = (Set) obj;
 100    }
 101   
 102    /**
 103    * When we want to associate this proxy with the cache again. We will need to translate the in-memory
 104    * content to the cache store first.
 105    */
 106  0 public void attach(Fqn fqn, boolean copyToCache)
 107    {
 108  0 super.attach(fqn, copyToCache);
 109   
 110  0 if (copyToCache)
 111  0 toCache();
 112   
 113  0 current_ = cacheImpl_;
 114    }
 115   
 116  0 private void toCache()
 117    {
 118  0 if (inMemImpl_ == null)
 119  0 throw new IllegalStateException("CachedSetInterceptor.toCache(). inMemImpl is null.");
 120   
 121  0 for (Iterator it = inMemImpl_.iterator(); it.hasNext();)
 122    {
 123  0 Object obj = it.next();
 124  0 it.remove();
 125  0 cacheImpl_.add(obj);
 126    }
 127   
 128  0 inMemImpl_ = null; // we are done with this.
 129    }
 130   
 131    /**
 132    * When we want to separate this proxy from the cache. We will destroy the cache content and copy them to
 133    * the in-memory copy.
 134    */
 135  11 public void detach(boolean removeFromCache)
 136    {
 137  11 super.detach(removeFromCache);
 138   
 139  11 toMemory(removeFromCache);
 140   
 141  11 current_ = inMemImpl_;
 142    }
 143   
 144  11 private void toMemory(boolean removeFromCache)
 145    {
 146  11 if (inMemImpl_ == null)
 147    {
 148  0 inMemImpl_ = new HashSet();
 149    }
 150   
 151    // TODO. This needs optimization.
 152  11 inMemImpl_.clear();
 153  11 for (Iterator it = cacheImpl_.iterator(); it.hasNext();)
 154    {
 155  21 Object obj = it.next();
 156  21 if (removeFromCache)
 157  21 it.remove();
 158  21 inMemImpl_.add(obj);
 159    }
 160    }
 161   
 162   
 163  0 public String getName()
 164    {
 165  0 return "CachedSetInterceptor";
 166    }
 167   
 168  267 public Object invoke(org.jboss.aop.joinpoint.Invocation invocation) throws Throwable
 169    {
 170  267 if (current_ == null)
 171  0 throw new IllegalStateException("CachedSetInterceptor.invoke(). current_ is null.");
 172   
 173  267 return CollectionInterceptorUtil.invoke(invocation,
 174    this,
 175    current_,
 176    methodMap_, managedMethods_);
 177    }
 178    }