Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 200   Methods: 16
NCLOC: 145   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CachedListInterceptor.java 38.9% 38.3% 31.2% 37.2%
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.CachedListImpl;
 12    import org.jboss.cache.pojo.collection.CollectionInterceptorUtil;
 13    import org.jboss.cache.pojo.impl.PojoCacheImpl;
 14   
 15    import java.util.ArrayList;
 16    import java.util.List;
 17    import java.util.Map;
 18   
 19    /**
 20    * List ineterceptor that delegates to underlying implementation.
 21    *
 22    * @author Ben Wang
 23    */
 24   
 25    @SuppressWarnings({"CanBeFinal"})
 26    public class CachedListInterceptor extends AbstractCollectionInterceptor
 27    {
 28   
 29    // protected static final Log log_ = LogFactory.getLog(CachedListInterceptor.class);
 30    private static final Map managedMethods_ =
 31    CollectionInterceptorUtil.getManagedMethods(List.class);
 32   
 33    private Map methodMap_;
 34    // This is the copy in cache store when it is attached.
 35    private List cacheImpl_;
 36    // This is the copy in-memory when the state is detached.
 37    private List inMemImpl_;
 38    // Whichever is used now.
 39    private List current_;
 40   
 41  1572 public CachedListInterceptor(PojoCacheImpl cache, Fqn fqn, Class clazz, List obj)
 42    {
 43  1572 super(cache, fqn);
 44  1572 methodMap_ = CollectionInterceptorUtil.getMethodMap(clazz);
 45  1572 cacheImpl_ = new CachedListImpl(cache, this);
 46  1572 inMemImpl_ = obj; // lazy initialization here.
 47  1572 current_ = cacheImpl_;
 48    }
 49   
 50  0 private CachedListInterceptor(PojoCacheImpl cache, Fqn fqn)
 51    {
 52  0 super(cache, fqn);
 53    }
 54   
 55  0 public Object clone()
 56    {
 57  0 CachedListInterceptor interceptor = new CachedListInterceptor(cache, fqn);
 58  0 interceptor.setFqn(getFqn());
 59  0 interceptor.setAopInstance(getAopInstance());
 60  0 interceptor.setCurrentCopy(getCurrentCopy());
 61  0 interceptor.setInMemoryCopy(getInMemoryCopy());
 62  0 interceptor.setCacheCopy(getCacheCopy());
 63  0 return interceptor;
 64    }
 65   
 66  0 public void setInterceptor(Interceptor intcptr)
 67    {
 68  0 CachedListInterceptor interceptor = (CachedListInterceptor) intcptr;
 69  0 setFqn(interceptor.getFqn());
 70  0 setAopInstance(interceptor.getAopInstance());
 71  0 setCurrentCopy(interceptor.getCurrentCopy());
 72  0 setInMemoryCopy(interceptor.getInMemoryCopy());
 73  0 setCacheCopy(interceptor.getCacheCopy());
 74    }
 75   
 76    /**
 77    * When we want to associate this proxy with the cache again. We will need to translate the in-memory
 78    * content to the cache store first.
 79    */
 80  0 public void attach(Fqn fqn, boolean copyToCache)
 81    {
 82  0 super.attach(fqn, copyToCache);
 83   
 84  0 if (copyToCache)
 85  0 toCache();
 86  0 current_ = cacheImpl_;
 87    }
 88   
 89  0 private void toCache()
 90    {
 91  0 if (inMemImpl_ == null)
 92  0 throw new IllegalStateException("CachedListInterceptor.toCache(). inMemImpl is null.");
 93   
 94    // TODO This may not be optimal
 95  0 List tmpList = new ArrayList();
 96  0 for (int i = inMemImpl_.size(); i > 0; i--)
 97    {
 98  0 Object obj = inMemImpl_.remove(i - 1);
 99  0 tmpList.add(obj);
 100    }
 101   
 102  0 int size = tmpList.size();
 103  0 for (int i = 0; i < tmpList.size(); i++)
 104    {
 105  0 cacheImpl_.add(tmpList.get(size - i - 1));
 106    }
 107   
 108  0 inMemImpl_ = null; // we are done with this.
 109    }
 110   
 111    /**
 112    * When we want to separate this proxy from the cache. We will destroy the cache content and copy them to
 113    * the in-memory copy.
 114    */
 115  1303 public void detach(boolean removeFromCache)
 116    {
 117  1303 super.detach(removeFromCache);
 118  1303 toMemory(removeFromCache);
 119  1303 current_ = inMemImpl_;
 120    }
 121   
 122  1303 public Object getCurrentCopy()
 123    {
 124  1303 return current_;
 125    }
 126   
 127  0 void setInMemoryCopy(Object obj)
 128    {
 129  0 inMemImpl_ = (List) obj;
 130    }
 131   
 132  0 Object getInMemoryCopy()
 133    {
 134  0 return inMemImpl_;
 135    }
 136   
 137  0 Object getCacheCopy()
 138    {
 139  0 return cacheImpl_;
 140    }
 141   
 142  0 void setCacheCopy(Object obj)
 143    {
 144  0 cacheImpl_ = (List) obj;
 145    }
 146   
 147  0 void setCurrentCopy(Object obj)
 148    {
 149  0 current_ = (List) obj;
 150    }
 151   
 152  1303 private void toMemory(boolean removeFromCache)
 153    {
 154  1303 if (inMemImpl_ == null)
 155    {
 156  0 inMemImpl_ = new ArrayList();
 157    }
 158   
 159    // Optimization since remove from the beginning is very expensive.
 160  1303 List tmpList = new ArrayList();
 161  1303 for (int i = cacheImpl_.size(); i > 0; i--)
 162    {
 163  2872 int j = i - 1;
 164  2872 Object obj = null;
 165  2872 if (removeFromCache)
 166    {
 167  2872 obj = cacheImpl_.remove(j);
 168    }
 169    else
 170    {
 171  0 obj = cacheImpl_.get(j);
 172    }
 173   
 174  2872 tmpList.add(obj);
 175    }
 176   
 177  1303 int size = tmpList.size();
 178  1303 inMemImpl_.clear();
 179  1303 for (int i = 0; i < tmpList.size(); i++)
 180    {
 181  2872 inMemImpl_.add(tmpList.get(size - i - 1));
 182    }
 183    }
 184   
 185  0 public String getName()
 186    {
 187  0 return "CachedListInterceptor";
 188    }
 189   
 190  4319 public Object invoke(org.jboss.aop.joinpoint.Invocation invocation) throws Throwable
 191    {
 192  4319 if (current_ == null)
 193  0 throw new IllegalStateException("CachedListInterceptor.invoke(). current_ is null.");
 194   
 195  4319 return CollectionInterceptorUtil.invoke(invocation,
 196    this,
 197    current_,
 198    methodMap_, managedMethods_);
 199    }
 200    }