[prev in list] [next in list] [prev in thread] [next in thread] 

List:       jboss-cvs-commits
Subject:    [jboss-cvs] JBossCache/src/org/jboss/cache/aop/collection        ...
From:       Ben Wang <bwang () jboss ! com>
Date:       2005-07-31 18:07:34
Message-ID: E1DzIDm-0003mw-TJ () committer01 ! frg ! pub ! inap ! atl ! jboss ! com
[Download RAW message or body]

  User: bwang   
  Date: 05/07/31 14:07:34

  Added:       src/org/jboss/cache/aop/collection       
                        AbstractCollectionInterceptor.java CachedList.java
                        CachedListInterceptor.java CachedMap.java
                        CachedMapInterceptor.java CachedSetInterceptor.java
                        CollectionInterceptorUtil.java
  Log:
  Moved Collection to package collection
  
  Revision  Changes    Path
  1.1      date: 2005/07/31 18:07:34;  author: bwang;  state: \
Exp;JBossCache/src/org/jboss/cache/aop/collection/AbstractCollectionInterceptor.java  \
  Index: AbstractCollectionInterceptor.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.cache.aop.collection;
  
  import org.jboss.cache.Fqn;
  import org.jboss.cache.aop.BaseInterceptor;
  
  /**
   * @author Ben Wang
   *         Date: Jul 26, 2005
   * @version $Id: AbstractCollectionInterceptor.java,v 1.1 2005/07/31 18:07:34 bwang \
                Exp $
   */
  public abstract class AbstractCollectionInterceptor implements BaseInterceptor {
     protected Fqn fqn;
     protected boolean attached = true;
  
     public Fqn getFqn()
     {
        return fqn;
     }
  
     public void setFqn(Fqn fqn)
     {
        this.fqn = fqn;
     }
     
     public void setAttached(boolean isTrue)
     {
        attached = isTrue;
     }
  
     public boolean isAttached()
     {
        return attached;
     }
  }
  
  
  
  1.1      date: 2005/07/31 18:07:34;  author: bwang;  state: \
Exp;JBossCache/src/org/jboss/cache/aop/collection/CachedList.java  
  Index: CachedList.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.cache.aop.collection;
  
  import org.jboss.cache.Fqn;
  import org.jboss.cache.Node;
  import org.jboss.cache.aop.TreeCacheAop;
  import org.jboss.util.NestedRuntimeException;
  
  import java.util.AbstractList;
  import java.util.Map;
  
  /**
   * CachedList.java. Currently not used.
   *
   * @author <a href="mailto:harald@gliebe.de">Harald Gliebe</a>
   * @deprecated 1.0
   */
  
  public class CachedList extends AbstractList
  {
  
     protected TreeCacheAop cache;
     protected Fqn fqn;
  
     CachedList(TreeCacheAop cache, Fqn fqn)
     {
        this.cache = cache;
        this.fqn = fqn;
     }
  
     protected Node getNode()
     {
        try {
           return cache.get(fqn);
        } catch (Exception e) {
           throw new NestedRuntimeException(e);
        }
     }
  
     public Object get(int index)
     {
        try {
           // return cache.getObject(((Fqn) fqn.clone()).add(new Integer(index)));
           return cache.getObject(new Fqn(fqn, new Integer(index)));
        } catch (Exception e) {
           throw new NestedRuntimeException(e);
        }
     }
  
     public int size()
     {
        Map children = getNode().getChildren();
        return children == null ? 0 : children.size();
     }
  
     public Object set(int index, Object element)
     {
        try {
           if (index < 0 || index >= size())
              throw new IndexOutOfBoundsException();
           Object oldValue = get(index);
           //return cache.putObject(((Fqn) fqn.clone()).add(new Integer(index)), \
                element);
           return cache.putObject(new Fqn(fqn, new Integer(index)), element);
        } catch (Exception e) {
           throw new NestedRuntimeException(e);
        }
     }
  
     public void add(int index, Object element)
     {
        try {
           if (index < 0 || index > size())
              throw new IndexOutOfBoundsException();
           for (int i = size(); i > index; i--) {
              // Object obj = cache.removeObject(((Fqn) fqn.clone()).add(new \
                Integer(i - 1)));
              Object obj = cache.removeObject(new Fqn(fqn, new Integer(i - 1)));
              // cache.putObject(((Fqn) fqn.clone()).add(new Integer(i)), obj);
              cache.putObject(new Fqn(fqn, new Integer(i)), obj);
           }
           // cache.putObject(((Fqn) fqn.clone()).add(new Integer(index)), element);
           cache.putObject(new Fqn(fqn, new Integer(index)), element);
        } catch (Exception e) {
           throw new NestedRuntimeException(e);
        }
  
     }
  
     public boolean remove(Object o)
     {
        return super.remove(o);
     }
  
     public Object remove(int index)
     {
        try {
           if (index < 0 || index >= size())
              throw new IndexOutOfBoundsException();
           // Object result = cache.removeObject(((Fqn) fqn.clone()).add(new \
                Integer(index)));
           Object result = cache.removeObject(new Fqn(fqn, new Integer(index)));
           int size = size() - 1;
           for (int i = index; i < size; i++) {
              // Object obj = cache.removeObject(((Fqn) fqn.clone()).add(new \
                Integer(i + 1)));
              Object obj = cache.removeObject(new Fqn(fqn, new Integer(i + 1)));
              // cache.putObject(((Fqn) fqn.clone()).add(new Integer(i)), obj);
              cache.putObject(new Fqn(fqn, new Integer(i)), obj);
           }
           return result;
        } catch (Exception e) {
           throw new NestedRuntimeException(e);
        }
  
     }
  
  }
  
  
  
  1.1      date: 2005/07/31 18:07:34;  author: bwang;  state: \
Exp;JBossCache/src/org/jboss/cache/aop/collection/CachedListInterceptor.java  
  Index: CachedListInterceptor.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.cache.aop.collection;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.cache.Fqn;
  import org.jboss.cache.Node;
  import org.jboss.cache.aop.TreeCacheAop;
  import org.jboss.cache.aop.AopOperationNotSupportedException;
  import org.jboss.util.NestedRuntimeException;
  
  import java.util.*;
  
  /**
   * List interceptor to perform list cache storage and retrieval
   *
   * @author <a href="mailto:harald@gliebe.de">Harald Gliebe</a>
   * @author Ben Wang
   */
  
  public class CachedListInterceptor extends AbstractCollectionInterceptor
        implements List
  {
  
     protected static final Log log = LogFactory.getLog(CachedListInterceptor.class);
     protected static final Map managedMethods =
           CollectionInterceptorUtil.getManagedMethods(List.class);
  
     protected TreeCacheAop cache;
     protected Map methodMap;
  
     public CachedListInterceptor(TreeCacheAop cache, Fqn fqn, Class clazz)
     {
        this.cache = cache;
        this.fqn = fqn;
        methodMap = CollectionInterceptorUtil.getMethodMap(clazz);
     }
  
     public String getName()
     {
        return "CachedListInterceptor";
     }
  
     public Object invoke(org.jboss.aop.joinpoint.Invocation invocation) throws \
Throwable  {
        return CollectionInterceptorUtil.invoke(invocation,
              this,
              methodMap,
              managedMethods);
     }
  
     // implementation of the java.util.List interface
     protected Node getNode()
     {
        try {
           return cache.get(fqn);
        } catch (Exception e) {
           throw new NestedRuntimeException(e);
        }
     }
  
     public Object get(int index)
     {
        checkIndex(index);
        try {
           // return cache.getObject(((Fqn) fqn.clone()).add(new Integer(index)));
           return cache.getObject(new Fqn(fqn, Integer.toString(index)));
        } catch (Exception e) {
           throw new NestedRuntimeException(e);
        }
     }
  
     private void checkIndex(int i) {
        if(size() == 0) return; // No need to check here.
        if( i < 0 || i >= size() ) {
           throw new IndexOutOfBoundsException("Index out of bound at \
CachedListInterceptor(). Index is " +i  + " but size is " +size());
        }
     }
  
     private void checkArgument(Object o) {
        if(o == null)
           throw new NullPointerException("Object is null");
     }
  
     public int size()
     {
        Node node = getNode();
        if(node == null) {
           return 0;
        }
  
        Map children = node.getChildren();
        return children == null ? 0 : children.size();
     }
  
     public void clear()
     {
        // TODO Can use optimization here
        for(int i=size()-1; i >= 0; i--) {
           remove(i);
        }
     }
  
     public boolean isEmpty()
     {
        return size() == 0 ? true : false;
     }
  
     public Object[] toArray()
     {
        Object[] objs = new Object[size()];
        for(int i=0; i < size(); i++) {
           objs[i] = get(i);
        }
        return objs;
     }
  
     public Object set(int index, Object element)
     {
        try {
           if(index != 0)
              checkIndex(index-1); // Since index can be size().
           Object oldValue = get(index);
           // return cache.putObject(((Fqn) fqn.clone()).add(new Integer(index)), \
                element);
           return cache.putObject(new Fqn(fqn, Integer.toString(index)), element);
        } catch (Exception e) {
           throw new NestedRuntimeException(e);
        }
     }
  
     public Object[] toArray(Object a[])
     {
        throw new AopOperationNotSupportedException("CachedListInterceptor: \
List.toArray(a) operation not supported");  }
  
     public void add(int index, Object element)
     {
        try {
           if(index != 0)
              checkIndex(index-1); // Since index can be size().
           for (int i = size(); i > index; i--) {
              // Object obj = cache.removeObject(((Fqn) fqn.clone()).add(new \
                Integer(i - 1)));
              Object obj = cache.removeObject(new Fqn(fqn, Integer.toString(i - 1)));
              // cache.putObject(((Fqn) fqn.clone()).add(new Integer(i)), obj);
              cache.putObject(new Fqn(fqn, Integer.toString(i)), obj);
           }
           // cache.putObject(((Fqn) fqn.clone()).add(new Integer(index)), element);
           cache.putObject(new Fqn(fqn, Integer.toString(index)), element);
        } catch (Exception e) {
           throw new NestedRuntimeException(e);
        }
     }
  
     public int indexOf(Object o)
     {
        checkArgument(o);
        for(int i=0; i < size(); i++) {
           if(o.equals(get(i))) return i;
        }
        return -1;
     }
  
     public int lastIndexOf(Object o)
     {
        checkArgument(o);
        int index = -1;
        for(int i=0; i < size(); i++) {
           if(o.equals(get(i))) index = i;
        }
        return index;
     }
  
     public boolean add(Object o)
     {
        add(size(), o);
        return true;
     }
  
     public boolean contains(Object o)
     {
        if (indexOf(o) != -1) return true;
        return false;
     }
  
     public Object remove(int index)
     {
        try {
           checkIndex(index);
           // Object result = cache.removeObject(((Fqn) fqn.clone()).add(new \
                Integer(index)));
           Object result = cache.removeObject(new Fqn(fqn, Integer.toString(index)));
           int size = size();
           for (int i = index; i < size; i++) {
              // Object obj = cache.removeObject(((Fqn) fqn.clone()).add(new \
                Integer(i + 1)));
              Object obj = cache.removeObject(new Fqn(fqn, Integer.toString(i + 1)));
              // cache.putObject(((Fqn) fqn.clone()).add(new Integer(i)), obj);
              cache.putObject(new Fqn(fqn, Integer.toString(i)), obj);
           }
           return result;
        } catch (Exception e) {
           throw new NestedRuntimeException(e);
        }
     }
  
     public boolean remove(Object o)
     {
        int i = indexOf(o);
        if(i == -1)
           return false;
  
        remove(i);
        return true;
     }
  
     public boolean addAll(int index, Collection c)
     {
        throw new AopOperationNotSupportedException("CachedListInterceptor: \
List.addAll() operation not supported");  }
  
     public boolean addAll(Collection c)
     {
        Iterator i = c.iterator();
        while(i.hasNext()) {
           Object o = i.next();
           add(o);
        }
        return true;
     }
  
     public boolean containsAll(Collection c)
     {
        throw new AopOperationNotSupportedException("CachedListInterceptor: \
List.containsAll() operation not supported");  }
  
     public boolean removeAll(Collection c)
     {
        Iterator i = c.iterator();
        while(i.hasNext()) {
           Object o = i.next();
           remove(o);
        }
        return true;
     }
  
     public int hashCode()
     {
        int result = 0;
        for (int i =0; i < size(); i++) {
           result += get(i).hashCode();
        }
        return result;
     }
  
     public boolean equals(Object object)
     {
        if (object == this)
           return true;
        if (!(object instanceof List))
           return false;
        List list = (List) object;
        if (size() != list.size())
           return false;
        for (int i=0; i < list.size(); i++) {
           Object value = list.get(i);
           if (value != null) {
              if( !contains(value) )
                 return false;
           } else { // Null key is not allowed
              return false;
           }
        }
        return true;
     }
  
     public String toString() {
        StringBuffer buf = new StringBuffer();
        int size = size();
        for (int i =0; i < size; i++) {
           Object key = get(i);
           buf.append("[" +key).append("]");
           if(i <= size) buf.append(", ");
        }
  
        return buf.toString();
     }
  
     public boolean retainAll(Collection c)
     {
        throw new AopOperationNotSupportedException("CachedListInterceptor: \
List.retainAll() operation not supported");  }
  
     public Iterator iterator()
     {
        // TODO: check for concurrent modification
        return new Iterator()
        {
           protected int current = 0;
  
           public boolean hasNext()
           {
              return current < size();
           }
  
           public Object next()
           {
              try {
                 return cache.getObject(new Fqn(fqn, Integer.toString(current++)));
              } catch (Exception e) {
                 throw new NestedRuntimeException(e);
              }
           }
  
           public void remove()
           {
              // TODO Need optimization here since set does not care about index
              try {
                 int size = size();
                 if (current < size) {
                    // replace Object with last one
                    // Object last = cache.removeObject(((Fqn) fqn.clone()).add(new \
                Integer(size - 1)));
                    Object last = cache.removeObject(new Fqn(fqn, \
                Integer.toString(size - 1)));
                    // cache.putObject(((Fqn) fqn.clone()).add(new \
                Integer(--current)), last);
                    cache.putObject(new Fqn(fqn, Integer.toString(--current)), last);
                 } else {
                    // cache.removeObject(((Fqn) fqn.clone()).add(new \
                Integer(--current)));
                    cache.removeObject(new Fqn(fqn, Integer.toString(--current)));
                 }
              } catch (Exception e) {
                 throw new NestedRuntimeException(e);
              }
           }
        };
     }
  
     public List subList(int fromIndex, int toIndex)
     {
        throw new AopOperationNotSupportedException("CachedListInterceptor: \
List.subList() operation not supported");  }
  
     public ListIterator listIterator()
     {
        return new MyListIterator(0);
     }
  
     public ListIterator listIterator(int index)
     {
        return new MyListIterator(index);
     }
  
     protected class MyListIterator implements ListIterator {
        protected int index = 0;
  
        public MyListIterator(int index) {
           if(index < 0 || index >= size()) {
              throw new AopOperationNotSupportedException("CachedListInterceptor: \
MyListIterator construction. " +  " Index is out of bound : " +index);
           }
           this.index = index;
        }
  
        public int nextIndex()
        {
           return index;
        }
  
        public int previousIndex()
        {
           return index-1;
        }
  
        public void remove()
        {
           throw new AopOperationNotSupportedException("CachedListInterceptor: \
ListIterator.remove() optional operation not supported");  }
  
        public boolean hasNext()
        {
           return (index == size()-1) ? false : true;
        }
  
        public boolean hasPrevious()
        {
           return (index == 0) ? false : true;
        }
  
        public Object next()
        {
           if( index == size() )
              throw new NoSuchElementException();
  
           index++;
           return get(index-1);
        }
  
        public Object previous()
        {
           if( index == 0 )
              throw new NoSuchElementException();
  
           index--;
           return get(index);
        }
  
        public void add(Object o)
        {
           throw new AopOperationNotSupportedException("CachedListInterceptor: \
ListIterator.add() optional operation not supported");  }
  
        public void set(Object o)
        {
           throw new AopOperationNotSupportedException("CachedListInterceptor: \
ListIterator.set() optional operation not supported");  }
     }
  }
  
  
  
  1.1      date: 2005/07/31 18:07:34;  author: bwang;  state: \
Exp;JBossCache/src/org/jboss/cache/aop/collection/CachedMap.java  
  Index: CachedMap.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.cache.aop.colllection;
  
  import org.jboss.cache.Fqn;
  import org.jboss.cache.Node;
  import org.jboss.cache.aop.TreeCacheAop;
  import org.jboss.util.NestedRuntimeException;
  
  import java.util.*;
  
  /**
   * Currently not used.
   * @deprecated 1.0
   */
  public class CachedMap extends AbstractMap implements Map
  {
  
     protected class Entry implements Map.Entry
     {
  
        Object key;
  
        public Entry(Object key)
        {
           this.key = key;
        }
  
        public Object getKey()
        {
           return key;
        }
  
        public Object getValue()
        {
           try {
              return cache.getObject(new Fqn(fqn, key));
           } catch (Exception e) {
              throw new NestedRuntimeException(e);
           }
        }
  
        public Object setValue(Object value)
        {
           try {
              return cache.putObject(new Fqn(fqn, key), value);
           } catch (Exception e) {
              throw new NestedRuntimeException(e);
           }
        }
  
        public int hashCode()
        {
           Object value = getValue();
           return ((key == null) ? 0 : key.hashCode())
                 ^ ((value == null) ? 0 : value.hashCode());
        }
  
        public boolean equals(Object obj)
        {
           if (!(obj instanceof Map.Entry))
              return false;
           Map.Entry entry = (Map.Entry) obj;
           Object value = getValue();
           return (
                 key == null
                 ? entry.getKey() == null
                 : key.equals(entry.getKey()))
                 && (value == null
                 ? entry.getValue() == null
                 : value.equals(entry.getValue()));
        }
     }
  
     protected TreeCacheAop cache;
     protected Fqn fqn;
  
     protected Node getNode()
     {
        try {
           return cache.get(fqn);
        } catch (Exception e) {
           throw new NestedRuntimeException(e);
        }
     }
  
     protected CachedMap(TreeCacheAop cache, Fqn fqn)
     {
        this.cache = cache;
        this.fqn = fqn;
     }
  
     public Object get(Object key)
     {
        try {
           return cache.getObject(new Fqn(fqn, key));
        } catch (Exception e) {
           throw new NestedRuntimeException(e);
        }
     }
  
     public Object put(Object key, Object value)
     {
        try {
           return cache.putObject(new Fqn(fqn, key), value);
        } catch (Exception e) {
           throw new NestedRuntimeException(e);
        }
     }
  
     public void putAll(Map map)
     {
        for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
           Map.Entry entry = (Map.Entry) i.next();
           put(entry.getKey(), entry.getValue());
        }
     }
  
     public Object remove(Object key)
     {
        try {
           return cache.removeObject(new Fqn(fqn, key));
        } catch (Exception e) {
           throw new NestedRuntimeException(e);
        }
     }
  
     public void clear()
     {
        for (Iterator i = keySet().iterator(); i.hasNext();) {
           remove(i.next());
        }
     }
  
     public int size()
     {
        Map children = getNode().getChildren();
        return children == null ? 0 : children.size();
     }
  
     public boolean isEmpty()
     {
        return size() == 0;
     }
  
     public boolean containsKey(Object object)
     {
        Map children = getNode().getChildren();
        return children == null ? false : children.containsKey(object);
     }
  
     public boolean containsValue(Object object)
     {
        return false;
     }
  
     public Set entrySet()
     {
        final CachedMap map = this;
  
        return new AbstractSet()
        {
  
           public int size()
           {
              Map children = getNode().getChildren();
              return children == null ? 0 : children.size();
           }
  
           public Iterator iterator()
           {
              Map children = getNode().getChildren();
              final Iterator i =
                    children == null
                    ? Collections.EMPTY_LIST.iterator()
                    : children.keySet().iterator();
  
              return new Iterator()
              {
                 Object lastKey; // for remove
  
                 public boolean hasNext()
                 {
                    return i.hasNext();
                 }
  
                 public Object next()
                 {
                    return new Entry(lastKey = i.next());
                 }
  
                 public void remove()
                 {
                    map.remove(lastKey);
                 }
              };
           }
        };
     }
  
     public Collection values()
     {
        return null; //TODO
     }
  
     public Set keySet()
     {
        Map children = getNode().getChildren();
        return children == null ? Collections.EMPTY_SET : children.keySet();
     }
  
     public int hashCode()
     {
        int result = 0;
        for (Iterator i = entrySet().iterator(); i.hasNext();) {
           result += i.next().hashCode();
        }
        return result;
     }
  
     public boolean equals(Object object)
     {
        if (object == this)
           return true;
        if (!(object instanceof Map))
           return false;
        Map map = (Map) object;
        if (size() != map.size())
           return false;
        for (Iterator i = entrySet().iterator(); i.hasNext();) {
           Map.Entry entry = (Map.Entry) i.next();
           Object value = entry.getValue();
           if (value != null) {
              if (map.get(entry.getKey()) != null
                    || !map.containsKey(entry.getKey()))
                 return false;
           } else {
              if (!value.equals(map.get(entry.getKey())))
                 return false;
           }
        }
        return true;
     }
  
  }
  
  
  
  1.1      date: 2005/07/31 18:07:34;  author: bwang;  state: \
Exp;JBossCache/src/org/jboss/cache/aop/collection/CachedMapInterceptor.java  
  Index: CachedMapInterceptor.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.cache.aop.collection;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.cache.Fqn;
  import org.jboss.cache.Node;
  import org.jboss.cache.aop.TreeCacheAop;
  import org.jboss.cache.aop.AopOperationNotSupportedException;
  import org.jboss.util.NestedRuntimeException;
  
  import java.util.*;
  
  /**
   * Map interceptor to perform Map cache storage and retrieval
   *
   * @author <a href="mailto:harald@gliebe.de">Harald Gliebe</a>
   * @author Ben Wang
   */
  public class CachedMapInterceptor extends AbstractCollectionInterceptor implements \
Map  {
  
     protected static final Log log = LogFactory.getLog(CachedMapInterceptor.class);
     protected static final Map managedMethods =
           CollectionInterceptorUtil.getManagedMethods(Map.class);
  
     protected TreeCacheAop cache;
     protected Map methodMap;
  
     protected CachedMapInterceptor(TreeCacheAop cache, Fqn fqn, Class clazz)
     {
        this.cache = cache;
        this.fqn = fqn;
        methodMap = CollectionInterceptorUtil.getMethodMap(clazz);
     }
  
     public String getName()
     {
        return "CachedMapInterceptor";
     }
  
     public Object invoke(org.jboss.aop.joinpoint.Invocation invocation) throws \
Throwable  {
        return CollectionInterceptorUtil.invoke(invocation,
              this,
              methodMap,
              managedMethods);
     }
  
     // implementation of the java.util.Map interface
  
     protected Node getNode()
     {
        try {
           return cache.get(fqn);
        } catch (Exception e) {
           throw new NestedRuntimeException(e);
        }
     }
  
     public Object get(Object key)
     {
        try {
           return cache.getObject(new Fqn(fqn, key));
        } catch (Exception e) {
           throw new NestedRuntimeException(e);
        }
     }
  
     public Object put(Object key, Object value)
     {
        try {
           return cache.putObject(new Fqn(fqn, key), value);
        } catch (Exception e) {
           throw new NestedRuntimeException(e);
        }
     }
  
     public void putAll(Map map)
     {
        for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
           Map.Entry entry = (Map.Entry) i.next();
           put(entry.getKey(), entry.getValue());
        }
     }
  
     public Object remove(Object key)
     {
        try {
           return cache.removeObject(new Fqn(fqn, key));
        } catch (Exception e) {
           throw new NestedRuntimeException(e);
        }
     }
  
     public void clear()
     {
        // Need to clone first to avoid CME
        ArrayList list = new ArrayList(keySet());
        for(int i=0; i < list.size(); i++) {
           remove(list.get(i));
        }
     }
  
     public int size()
     {
        Node node = getNode();
        if(node == null) {
           return 0;
        }
  
        Map children = node.getChildren();
        return children == null ? 0 : children.size();
     }
  
     public boolean isEmpty()
     {
        return size() == 0;
     }
  
     public boolean containsKey(Object object)
     {
        Map children = getNode().getChildren();
        return children == null ? false : children.containsKey(object);
     }
  
     public boolean containsValue(Object object)
     {
        throw new AopOperationNotSupportedException("CachedMapInterceptor: \
map.containsValues() operation not supported");  }
  
     public Set entrySet()
     {
        final CachedMapInterceptor map = this;
  
        return new AbstractSet()
        {
  
           public int size()
           {
              Map children = getNode().getChildren();
              return children == null ? 0 : children.size();
           }
  
           public Iterator iterator()
           {
              Map children = getNode().getChildren();
              final Iterator i =
                    children == null
                    ? Collections.EMPTY_LIST.iterator()
                    : children.keySet().iterator();
  
              return new Iterator()
              {
                 Object lastKey; // for remove
  
                 public boolean hasNext()
                 {
                    return i.hasNext();
                 }
  
                 public Object next()
                 {
                    return new Entry(lastKey = i.next());
                 }
  
                 public void remove()
                 {
                    map.remove(lastKey);
                 }
              };
           }
        };
     }
  
     public Collection values()
     {
        throw new AopOperationNotSupportedException("CachedMapInterceptor: \
map.values() operation not supported");  }
  
     public Set keySet()
     {
        /**
         * TODO Note that return Set is not thread safe because children is not.
         */
        Map children = getNode().getChildren();
        return children == null ? Collections.EMPTY_SET : children.keySet();
     }
  
     public int hashCode()
     {
        int result = 0;
        for (Iterator i = entrySet().iterator(); i.hasNext();) {
           result += i.next().hashCode();
        }
        return result;
     }
  
     public boolean equals(Object object)
     {
        if (object == this)
           return true;
        if (!(object instanceof Map))
           return false;
        Map map = (Map) object;
        if (size() != map.size())
           return false;
        for (Iterator i = entrySet().iterator(); i.hasNext();) {
           Map.Entry entry = (Map.Entry) i.next();
           Object value = entry.getValue();
           if (value != null) {
              if (map.get(entry.getKey()) == null
                    || !map.containsKey(entry.getKey()))
                 return false;
           } else { // Null key is not allowed
              return false;
           }
        }
        return true;
     }
  
     public String toString() {
        StringBuffer buf = new StringBuffer();
        Set set = keySet();
        for(Iterator it = set.iterator(); it.hasNext();) {
           Object key = it.next();
           buf.append("[" +key).append(", ").append(get(key)).append("]");
           if(it.hasNext()) buf.append(", ");
        }
  
        return buf.toString();
     }
  
     protected class Entry implements Map.Entry
     {
  
        Object key;
  
        public Entry(Object key)
        {
           this.key = key;
        }
  
        public Object getKey()
        {
           return key;
        }
  
        public Object getValue()
        {
           try {
              return cache.getObject(new Fqn(fqn, key));
           } catch (Exception e) {
              throw new NestedRuntimeException(e);
           }
        }
  
        public Object setValue(Object value)
        {
           try {
              return cache.putObject(new Fqn(fqn, key), value);
           } catch (Exception e) {
              throw new NestedRuntimeException(e);
           }
        }
  
        public int hashCode()
        {
           Object value = getValue();
           return ((key == null) ? 0 : key.hashCode())
                 ^ ((value == null) ? 0 : value.hashCode());
        }
  
        public boolean equals(Object obj)
        {
           if (!(obj instanceof Map.Entry))
              return false;
           Map.Entry entry = (Map.Entry) obj;
           Object value = getValue();
           return (
                 key == null
                 ? entry.getKey() == null
                 : key.equals(entry.getKey()))
                 && (value == null
                 ? entry.getValue() == null
                 : value.equals(entry.getValue()));
        }
     }
  
  }
  
  
  
  1.1      date: 2005/07/31 18:07:34;  author: bwang;  state: \
Exp;JBossCache/src/org/jboss/cache/aop/collection/CachedSetInterceptor.java  
  Index: CachedSetInterceptor.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.cache.aop.collection;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.cache.Fqn;
  import org.jboss.cache.Node;
  import org.jboss.cache.aop.TreeCacheAop;
  import org.jboss.cache.aop.AopOperationNotSupportedException;
  import org.jboss.util.NestedRuntimeException;
  
  import java.util.Collection;
  import java.util.Iterator;
  import java.util.Map;
  import java.util.Set;
  
  /**
   * Set interceptor to perform Set cache storage and retrieval
   *
   * @author <a href="mailto:harald@gliebe.de">Harald Gliebe</a>
   * @author Ben Wang
   */
  public class CachedSetInterceptor extends AbstractCollectionInterceptor implements \
Set  {
     protected static final Log log=LogFactory.getLog(CachedSetInterceptor.class);
  
     protected TreeCacheAop cache;
     protected Map methodMap;
  
     protected static Map managedMethods =
           CollectionInterceptorUtil.getManagedMethods(Set.class);
  
     public CachedSetInterceptor(TreeCacheAop cache, Fqn fqn, Class clazz)
     {
        this.cache = cache;
        this.fqn = fqn;
        methodMap = CollectionInterceptorUtil.getMethodMap(clazz);
     }
  
     public String getName()
     {
        return "CachedSetInterceptor";
     }
  
     public Object invoke(org.jboss.aop.joinpoint.Invocation invocation) throws \
Throwable  {
        return CollectionInterceptorUtil.invoke(invocation,
              this,
              methodMap,
              managedMethods);
     }
  
     protected Node getNode()
     {
        try {
           return cache.get(fqn);
        } catch (Exception e) {
           throw new NestedRuntimeException(e);
        }
     }
  
     // implementation of the java.util.Set interface
     public int size()
     {
        Node node = getNode();
        if(node == null) {
           return 0;
        }
  
        Map children = node.getChildren();
        return children == null ? 0 : children.size();
     }
  
     public void clear()
     {
        for(Iterator i = iterator(); i.hasNext(); ) {
           remove(i.next());
        }
     }
  
     public boolean isEmpty()
     {
        return size() == 0;
     }
  
     public Object[] toArray()
     {
        Object[] objs = new Object[size()];
  
        int ind = 0;
        for(Iterator i = iterator(); i.hasNext(); ) {
           objs[ind++] = i.next();
        }
        return objs;
     }
  
     public Iterator iterator()
     {
        // TODO: check for concurrent modification
        return new Iterator()
        {
           protected int current = 0;
  
           public boolean hasNext()
           {
              return current < size();
           }
  
           public Object next()
           {
              try {
                 return cache.getObject(new Fqn(fqn, Integer.toString(current++)));
              } catch (Exception e) {
                 throw new NestedRuntimeException(e);
              }
           }
  
           public void remove()
           {
              // TODO Need optimization here since set does not care about index
              try {
                 int size = size();
                 if (current < size) {
                    // replace Object with last one
                    // Object last = cache.removeObject(((Fqn) \
                fqn.clone()).add(Integer.toString(size - 1)));
                    Object last = cache.removeObject(new Fqn(fqn, \
                Integer.toString(size - 1)));
                    // cache.putObject(((Fqn) \
                fqn.clone()).add(Integer.toString(--current)), last);
                    cache.putObject(new Fqn(fqn, Integer.toString(--current)), last);
                 } else {
                    // cache.removeObject(((Fqn) \
                fqn.clone()).add(Integer.toString(--current)));
                    cache.removeObject(new Fqn(fqn, Integer.toString(--current)));
                 }
              } catch (Exception e) {
                 throw new NestedRuntimeException(e);
              }
           }
        };
     }
  
     public Object[] toArray(Object a[])
     {
        throw new AopOperationNotSupportedException("CachedSetInterceptor: \
set.toArray() operation not supported");  }
  
     public boolean add(Object o)
     {
        if( contains(o) ) return false;
  
        try {
           // cache.putObject(((Fqn) fqn.clone()).add(Integer.toString(size())), o);
           cache.putObject(new Fqn(fqn, Integer.toString(size())), o);
           return true;
        } catch (Exception e) {
           throw new NestedRuntimeException(e);
        }
     }
  
     public boolean contains(Object o)
     {
        for (Iterator i = iterator(); i.hasNext();) {
           Object n = i.next();
           // TODO logic correct????
           if ((o == null && n == null) || (o != null && o.equals(n))) {
              return true;
           }
        }
        return false;
     }
  
     public boolean remove(Object o)
     {
        for (Iterator i = iterator(); i.hasNext();) {
           Object n = i.next();
           if ((o == null && n == null) || (o != null && o.equals(n))) {
              i.remove();
              return true;
           }
        }
        return false;
     }
  
     public boolean addAll(Collection c)
     {
        Iterator it = c.iterator();
        while(it.hasNext()) {
           add(it.next());
        }
  
        return true;
     }
  
     public boolean containsAll(Collection c)
     {
        throw new AopOperationNotSupportedException("CachedSetInterceptor: \
set.containsAll() operation not supported");  }
  
     public boolean removeAll(Collection c)
     {
        Iterator it = c.iterator();
        while(it.hasNext()) {
           remove(it.next());
        }
  
        return true;
     }
  
     public boolean retainAll(Collection c)
     {
        throw new AopOperationNotSupportedException("CachedSetInterceptor: \
set.retainAll() operation not supported");  }
  
  
     public int hashCode()
     {
        int result = 0;
        for (Iterator i = iterator(); i.hasNext();) {
           result += i.next().hashCode();
        }
        return result;
     }
  
     public boolean equals(Object object)
     {
        if (object == this)
           return true;
        if (!(object instanceof Set))
           return false;
        Set list = (Set) object;
        if (size() != list.size())
           return false;
        for (Iterator i = iterator(); i.hasNext();) {
           Object value = i.next();
           if (value != null) {
              if( !contains(value) )
                 return false;
           } else { // Null key is not allowed
              return false;
           }
        }
        return true;
     }
  
     public String toString() {
        StringBuffer buf = new StringBuffer();
        for(Iterator it = iterator(); it.hasNext();) {
           Object key = it.next();
           buf.append("[" +key).append("]");
           if(it.hasNext()) buf.append(", ");
        }
  
        return buf.toString();
     }
  
  }
  
  
  
  1.1      date: 2005/07/31 18:07:34;  author: bwang;  state: \
Exp;JBossCache/src/org/jboss/cache/aop/collection/CollectionInterceptorUtil.java  
  Index: CollectionInterceptorUtil.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.cache.aop.collection;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.aop.InstanceAdvisor;
  import org.jboss.aop.advice.Interceptor;
  import org.jboss.aop.joinpoint.Invocation;
  import org.jboss.aop.joinpoint.MethodInvocation;
  import org.jboss.aop.proxy.ClassProxy;
  import org.jboss.aop.proxy.ClassProxyFactory;
  import org.jboss.aop.util.MethodHashing;
  import org.jboss.util.NestedRuntimeException;
  import org.jboss.cache.aop.AopUtil;
  import org.jboss.cache.aop.TreeCacheAop;
  import org.jboss.cache.Fqn;
  
  import java.lang.reflect.Method;
  import java.util.HashMap;
  import java.util.Map;
  
  /**
   * CollectionInterceptorUtil contains helper methods for the interceptors of
   * the different collection types.
   *
   * @author <a href="mailto:harald@gliebe.de">Harald Gliebe</a>
   * @author Ben Wang
   */
  public class CollectionInterceptorUtil
  {
     static Log log=LogFactory.getLog(CollectionInterceptorUtil.class.getName());
  
     public static ClassProxy createProxy(Class clazz, Interceptor interceptor)
           throws Exception
     {
        ClassProxy result = ClassProxyFactory.newInstance(clazz);
        InstanceAdvisor advisor = result._getInstanceAdvisor();
        advisor.appendInterceptor(interceptor);
        return result;
     }
  
     public static ClassProxy createMapProxy(TreeCacheAop cache, Fqn fqn, Class \
                clazz) throws Exception {
        return CollectionInterceptorUtil.createProxy(clazz, new \
CachedMapInterceptor(cache, fqn, clazz));  }
  
     public static ClassProxy createListProxy(TreeCacheAop cache, Fqn fqn, Class \
                clazz) throws Exception {
        return CollectionInterceptorUtil.createProxy(clazz, new \
CachedListInterceptor(cache, fqn, clazz));  }
  
     public static ClassProxy createSetProxy(TreeCacheAop cache, Fqn fqn, Class \
                clazz) throws Exception {
        return CollectionInterceptorUtil.createProxy(clazz, new \
CachedSetInterceptor(cache, fqn, clazz));  }
  
     public static Interceptor getInterceptor(ClassProxy proxy)
     {
        InstanceAdvisor advisor = proxy._getInstanceAdvisor();
        return AopUtil.findCollectionInterceptor(advisor);
     }
  
     public static Map getMethodMap(Class clazz)
     {
        Map result = ClassProxyFactory.getMethodMap(clazz.getName());
        if (result == null) {
           try {
              ClassProxyFactory.newInstance(clazz);
           } catch (Exception e) {
              throw new NestedRuntimeException(e);
           }
           result = ClassProxyFactory.getMethodMap(clazz.getName());
        }
        return result;
     }
  
     public static Map getManagedMethods(Class clazz)
     {
        Method tostring = null;
        try {
           tostring = Object.class.getDeclaredMethod("toString", new Class[0]);
        } catch (NoSuchMethodException e) {
           e.printStackTrace();
           throw new NestedRuntimeException("getManagedMathods: " +e);
        }
  
        Map managedMethods = new HashMap();
        try {
           Method[] methods = clazz.getDeclaredMethods();
           for (int i = 0; i < methods.length; i++) {
              long hash = MethodHashing.methodHash(methods[i]);
              managedMethods.put(new Long(hash), methods[i]);
           }
           // Add toString to ManagedMethod
           long hash = MethodHashing.methodHash(tostring);
           managedMethods.put(new Long(hash), tostring);
        } catch (Exception ignored) {
           ignored.printStackTrace();
        }
        return managedMethods;
     }
  
     public static Object invoke(Invocation invocation,
                                 Object interceptor,
                                 Map methodMap,
                                 Map managedMethods)
           throws Throwable
     {
  
        if (invocation instanceof MethodInvocation) {
           MethodInvocation methodInvocation = (MethodInvocation) invocation;
           Long methodHash = new Long(methodInvocation.getMethodHash());
           Method method = (Method) managedMethods.get(methodHash);
           if (log.isDebugEnabled() && method != null) {
              log.trace("invoke(): method intercepted " + method.getName());
           }
           Object[] args = methodInvocation.getArguments();
           if (method != null) {
              return method.invoke(interceptor, args);
           } else {
              method = methodInvocation.getMethod();
              if (method == null) {
                 method = (Method) methodMap.get(methodHash);
              }
  
              log.trace("invke(): invoke non-managed method: " +method.toString());
              Object target = methodInvocation.getTargetObject();
              if(target == null) {
                 throw new \
NestedRuntimeException("CollectionInterceptorUtil.invoke(): targetObject is null." +  \
" Can't invoke " +method.toString());  }
              return method.invoke(target, args);
  //            return method.invoke(interceptor, args);
           }
        }
        return invocation.invokeNext();
     }
  
  }
  
  
  


-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
_______________________________________________
jboss-cvs-commits mailing list
jboss-cvs-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jboss-cvs-commits


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic