You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by cr...@apache.org on 2002/07/16 14:25:36 UTC

cvs commit: jakarta-avalon-excalibur/fortress/src/java/org/apache/excalibur/fortress/lifecycle LifecycleExtensionManager.java

crafterm    2002/07/16 05:25:36

  Modified:    fortress/src/java/org/apache/excalibur/fortress/lifecycle
                        LifecycleExtensionManager.java
  Log:
  Added the use of an optimized List class that caches the return value of
  toArray() upon each write operation to the List. This improves performance
  by reducing the number of unnecessary conversions between List & Array inside
  the LifecycleExtensionManager class.
  
  Revision  Changes    Path
  1.4       +138 -3    jakarta-avalon-excalibur/fortress/src/java/org/apache/excalibur/fortress/lifecycle/LifecycleExtensionManager.java
  
  Index: LifecycleExtensionManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/fortress/src/java/org/apache/excalibur/fortress/lifecycle/LifecycleExtensionManager.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- LifecycleExtensionManager.java	15 Jul 2002 17:35:22 -0000	1.3
  +++ LifecycleExtensionManager.java	16 Jul 2002 12:25:36 -0000	1.4
  @@ -8,6 +8,7 @@
   package org.apache.excalibur.fortress.lifecycle;
   
   import java.util.ArrayList;
  +import java.util.Collections;
   import java.util.Iterator;
   import java.util.List;
   import org.apache.avalon.framework.component.Component;
  @@ -38,7 +39,7 @@
       extends AbstractLifecycleExtensionManager
   {
       // extensions objects
  -    private final List m_extensions = new ArrayList();
  +    private final CachedArrayList m_extensions = new CachedArrayList();
   
       /**
        * <code>executeAccessExtensions</code> method, executes all access level
  @@ -130,7 +131,7 @@
        */
       public void insertExtension( int position, LifecycleExtension extension )
       {
  -        m_extensions.add( position, extension );
  +        m_extensions.insert( position, extension );
       }
   
       /**
  @@ -193,5 +194,139 @@
       public void clearExtensions()
       {
           m_extensions.clear();
  +    }
  +
  +    /**
  +     * <code>CachedArrayList</code> class.
  +     *
  +     * <p>
  +     * This class wraps a synchronized ArrayList to provide an optimized
  +     * <code>toArray()</code> method that returns an internally cached array, rather
  +     * than a new array generated per <code>toArray()</code> invocation.
  +     * </p>
  +     *
  +     * <p>
  +     * Use of the class by the Manager results in <code>toArray()</code> being invoked
  +     * far more often than any other method. Caching the value <code>toArray</code>
  +     * normally returns is intended to be a performance optimization.
  +     * </p>
  +     *
  +     * <p>
  +     * The cached array value is updated upon each write operation to the List.
  +     * </p>
  +     *
  +     * <p>
  +     * REVISIT(MC): investigate using FastArrayList from collections ?
  +     * </p>
  +     */
  +    private final class CachedArrayList
  +    {
  +        // Empty array constant
  +        private final Object[] EMPTY_ARRAY = new Object[0];
  +
  +        // Actual list for storing elements
  +        private final List m_proxy = Collections.synchronizedList( new ArrayList() );
  +
  +        // Proxy cache, saves unnecessary conversions from List to Array 
  +        private Object[] m_cache = EMPTY_ARRAY;
  +
  +        /**
  +         * Add an object to the list
  +         *
  +         * @param object an <code>Object</code> value
  +         */
  +        public void add( final Object object )
  +        {
  +            m_proxy.add( object );
  +            m_cache = m_proxy.toArray();
  +        }
  +
  +        /**
  +         * Insert an object into a particular position in the list
  +         *
  +         * @param position an <code>int</code> value
  +         * @param object an <code>Object</code> value
  +         */
  +        public void insert( final int position, final Object object )
  +        {
  +            m_proxy.add( position, object );
  +            m_cache = m_proxy.toArray();
  +        }
  +
  +        /**
  +         * Remove an object from the list
  +         *
  +         * @param position an <code>int</code> value
  +         * @return a <code>Object</code> value
  +         */
  +        public Object remove( final int position )
  +        {
  +            final Object object = m_proxy.remove( position );
  +            m_cache = m_proxy.toArray();
  +            return object;
  +        }
  +
  +        /**
  +         * Obtain an iterator
  +         *
  +         * @return an <code>Iterator</code> value
  +         */
  +        public Iterator iterator()
  +        {
  +            return m_proxy.iterator();
  +        }
  +
  +        /**
  +         * Obtain the size of the list
  +         *
  +         * @return an <code>int</code> value
  +         */
  +        public int size()
  +        {
  +            return m_proxy.size();
  +        }
  +
  +        /**
  +         * Access an object that is in the list
  +         *
  +         * @param index an <code>int</code> value
  +         * @return a <code>Object</code> value
  +         */
  +        public Object get( final int index )
  +        {
  +            return m_proxy.get( index );
  +        }
  +
  +        /**
  +         * Find out the index of an object in the list
  +         *
  +         * @param object an <code>Object</code> value
  +         * @return an <code>int</code> value
  +         */
  +        public int indexOf( final Object object )
  +        {
  +            return m_proxy.indexOf( object );
  +        }
  +
  +        /**
  +         * Clear the list
  +         */
  +        public void clear()
  +        {
  +            m_proxy.clear();
  +            m_cache = EMPTY_ARRAY;
  +        }
  +
  +        /**
  +         * Obtain the list as an array. Subsequents calls to this method
  +         * will return the same array object, until a write operation is
  +         * performed on the list.
  +         *
  +         * @return an <code>Object[]</code> value
  +         */
  +        public Object[] toArray()
  +        {
  +            return m_cache;
  +        }
       }
   }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>