avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From blorit...@apache.org
Subject cvs commit: jakarta-avalon-excalibur/event/src/test/org/apache/excalibur/event/test DefaultQueuePerformanceTestCase.java FixedSizeQueuePerformanceTestCase.java FixedSizeQueueTestCase.java QueueTestCase.java ThreadedQueueTestCase.java
Date Tue, 03 Sep 2002 17:43:01 GMT
bloritsch    2002/09/03 10:43:01

  Modified:    event/src/java/org/apache/excalibur/event/command
                        CommandManager.java
               event/src/test QueueTest.java
               event/src/test/org/apache/excalibur/event/command/test
                        TPCThreadManagerTestCase.java
               event/src/test/org/apache/excalibur/event/test
                        DefaultQueuePerformanceTestCase.java
                        FixedSizeQueuePerformanceTestCase.java
                        FixedSizeQueueTestCase.java QueueTestCase.java
                        ThreadedQueueTestCase.java
  Added:       event/src/java/org/apache/excalibur/event/impl
                        AbstractQueue.java DefaultQueue.java
                        FixedSizeQueue.java
  Removed:     event/src/java/org/apache/excalibur/event AbstractQueue.java
                        DefaultQueue.java FixedSizeQueue.java
  Log:
  move the implementation specific implementations to a sub package
  
  Revision  Changes    Path
  1.12      +1 -1      jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/event/command/CommandManager.java
  
  Index: CommandManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/event/command/CommandManager.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- CommandManager.java	3 Sep 2002 17:10:56 -0000	1.11
  +++ CommandManager.java	3 Sep 2002 17:43:00 -0000	1.12
  @@ -57,7 +57,7 @@
   import org.apache.avalon.excalibur.collections.Buffer;
   import org.apache.avalon.excalibur.collections.VariableSizeBuffer;
   import org.apache.avalon.excalibur.concurrent.Mutex;
  -import org.apache.excalibur.event.DefaultQueue;
  +import org.apache.excalibur.event.impl.DefaultQueue;
   import org.apache.excalibur.event.EventHandler;
   import org.apache.excalibur.event.Queue;
   import org.apache.excalibur.event.Signal;
  
  
  
  1.1                  jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/event/impl/AbstractQueue.java
  
  Index: AbstractQueue.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
      must not be used to endorse or promote products derived from this  software
      without  prior written permission. For written permission, please contact
      apache@apache.org.
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation. For more  information on the
   Apache Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.excalibur.event.impl;
  
  import org.apache.excalibur.event.*;
  
  /**
   * Provides the base functionality for the other <code>Queue</code> types.
   *
   * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
   * @author <a href="mailto:leo.sutic@inspireinfrastructure.com">Leo Sutic</a>
   */
  public abstract class AbstractQueue implements Queue
  {
      // this object is immutable, so it can be safely shared
      protected final static Object[] EMPTY_ARRAY = new Object[ 0 ];
      protected long m_timeout = 0;
  
      /**
       * Default for canAccept()
       */
      public int canAccept()
      {
          return ( maxSize() > 0 ) ? maxSize() - size() : maxSize();
      }
  
      /**
       * Default maxSize to -1 which is unbounded
       */
      public int maxSize()
      {
          return -1;
      }
  
      /**
       * Check to see if the <code>Queue</code> is full. The method uses the
       * <code>maxSize</code> and <code>size</code> methods to determine
       * whether the queue is full.
       */
      public boolean isFull()
      {
          return maxSize() != -1  /* There exists an upper bound... */
              && maxSize() - size() <= 0; /* ...and it is reached. */
      }
  
      /**
       * Set the timeout for the <code>Queue</code> in milliseconds.  The
       * default timeout is 0, which means that we don't wait at all.
       */
      public void setTimeout( final long millis )
      {
          if( millis > 0 )
          {
              m_timeout = millis;
          }
          else
          {
              m_timeout = 0;
          }
      }
  
      /**
       * Encapsulates the logic to block the <code>Queue</code> for the amount
       * of time specified by the timeout.
       */
      protected void block( Object lock )
      {
          if( m_timeout > 0 )
          {
              long start = System.currentTimeMillis();
              long end = start + m_timeout;
  
              while( start < end || size() > 0 )
              {
                  try
                  {
                      lock.wait( m_timeout );
                  }
                  catch( InterruptedException ie )
                  {
                      // ignore
                  }
              }
          }
      }
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/event/impl/DefaultQueue.java
  
  Index: DefaultQueue.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
      must not be used to endorse or promote products derived from this  software
      without  prior written permission. For written permission, please contact
      apache@apache.org.
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation. For more  information on the
   Apache Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.excalibur.event.impl;
  
  import org.apache.excalibur.event.*;
  
  import org.apache.avalon.excalibur.collections.Buffer;
  import org.apache.avalon.excalibur.collections.VariableSizeBuffer;
  import org.apache.avalon.excalibur.concurrent.Mutex;
  
  /**
   * The default queue implementation is a variable size queue.  This queue is
   * thread safe, however the overhead in synchronization costs a few extra
   * milliseconds.
   *
   * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
   */
  public final class DefaultQueue extends AbstractQueue
  {
      private final Buffer m_elements;
      private final Mutex m_mutex;
      private int m_reserve;
      private final int m_maxSize;
  
      /**
       * Construct a new DefaultQueue with the specified number of elements.
       * if the number of elements is greater than zero, then the
       * <code>Queue</code> is bounded by that number.  Otherwise, the
       * <code>Queue</code> is not bounded at all.
       *
       * @param  size  The maximum number of elements in the <code>Queue</code>.
       *               Any number less than 1 means there is no limit.
       */
      public DefaultQueue( int size )
      {
          int maxSize;
  
          if( size > 0 )
          {
              m_elements = new VariableSizeBuffer( size );
              maxSize = size;
          }
          else
          {
              m_elements = new VariableSizeBuffer();
              maxSize = -1;
          }
  
          m_mutex = new Mutex();
          m_reserve = 0;
          m_maxSize = maxSize;
      }
  
      /**
       * Create an unbounded DefaultQueue.
       */
      public DefaultQueue()
      {
          this( -1 );
      }
  
      /**
       * Return the number of elements currently in the <code>Queue</code>.
       *
       * @return <code>int</code> representing the number of elements.
       */
      public int size()
      {
          return m_elements.size();
      }
  
      /**
       * Return the maximum number of elements that will fit in the
       * <code>Queue</code>.  A number below 1 indecates an unbounded
       * <code>Queue</code>, which means there is no limit.
       *
       * @return <code>int</code> representing the maximum number of elements
       */
      public int maxSize()
      {
          return m_maxSize;
      }
  
      public PreparedEnqueue prepareEnqueue( final Object[] elements )
          throws SinkException
      {
          PreparedEnqueue enqueue = null;
  
          try
          {
              m_mutex.acquire();
              try
              {
  
                  if( maxSize() > 0 && elements.length + m_reserve + size() >
maxSize() )
                  {
                      throw new SinkFullException( "Not enough room to enqueue these elements."
);
                  }
  
                  enqueue = new DefaultPreparedEnqueue( this, elements );
              }
              finally
              {
                  m_mutex.release();
              }
          }
          catch( InterruptedException ie )
          {
          }
  
          return enqueue;
      }
  
      public boolean tryEnqueue( final Object element )
      {
          boolean success = false;
  
          try
          {
              m_mutex.acquire();
              try
              {
  
                  if( maxSize() > 0 && 1 + m_reserve + size() > maxSize() )
                  {
                      return false;
                  }
  
                  m_elements.add( element );
                  success = true;
              }
              finally
              {
                  m_mutex.release();
              }
          }
          catch( InterruptedException ie )
          {
          }
  
          return success;
      }
  
      public void enqueue( final Object[] elements )
          throws SinkException
      {
          final int len = elements.length;
  
          try
          {
              m_mutex.acquire();
              try
              {
                  if( maxSize() > 0 && elements.length + m_reserve + size() >
maxSize() )
                  {
                      throw new SinkFullException( "Not enough room to enqueue these elements."
);
                  }
  
                  for( int i = 0; i < len; i++ )
                  {
                      m_elements.add( elements[ i ] );
                  }
              }
              finally
              {
                  m_mutex.release();
              }
          }
          catch( InterruptedException ie )
          {
          }
      }
  
      public void enqueue( final Object element )
          throws SinkException
      {
          try
          {
              m_mutex.acquire();
              try
              {
                  if( maxSize() > 0 && 1 + m_reserve + size() > maxSize() )
                  {
                      throw new SinkFullException( "Not enough room to enqueue these elements."
);
                  }
  
                  m_elements.add( element );
              }
              finally
              {
                  m_mutex.release();
              }
          }
          catch( InterruptedException ie )
          {
          }
      }
  
      public Object[] dequeue( final int numElements )
      {
          Object[] elements = EMPTY_ARRAY;
  
          try
          {
              if( m_mutex.attempt( m_timeout ) )
              {
                  try
                  {
                      elements = retrieveElements( m_elements,
                                                   Math.min( size(),
                                                             numElements ) );
                  }
                  finally
                  {
                      m_mutex.release();
                  }
              }
          }
          catch( InterruptedException ie )
          {
          }
  
          return elements;
      }
  
      public Object[] dequeueAll()
      {
          Object[] elements = EMPTY_ARRAY;
  
          try
          {
              if( m_mutex.attempt( m_timeout ) )
              {
                  try
                  {
                      elements = retrieveElements( m_elements, size() );
                  }
                  finally
                  {
                      m_mutex.release();
                  }
              }
          }
          catch( InterruptedException ie )
          {
          }
  
          return elements;
      }
  
      /**
       * Removes the given number of elements from the given <code>buf</code>
       * and returns them in an array. Trusts the caller to pass in a buffer
       * full of <code>Object</code>s and with at least
       * <code>count</code> elements available.
       * <p>
       * @param buf to remove elements from, the caller is responsible
       *            for synchronizing access
       * @param count number of elements to remove/return
       * @return requested number of elements
       */
      private static Object[] retrieveElements( Buffer buf, int count )
      {
          Object[] elements = new Object[ count ];
  
          for( int i = 0; i < count; i++ )
          {
              elements[ i ] = (Object) buf.remove();
          }
  
          return elements;
      }
  
      public Object dequeue()
      {
          Object element = null;
  
          try
          {
              if( m_mutex.attempt( m_timeout ) )
              {
                  try
                  {
                      if( size() > 0 )
                      {
                          element = (Object)m_elements.remove();
                      }
                  }
                  finally
                  {
                      m_mutex.release();
                  }
              }
          }
          catch( InterruptedException ie )
          {
          }
  
          return element;
      }
  
      private static final class DefaultPreparedEnqueue implements PreparedEnqueue
      {
          private final DefaultQueue m_parent;
          private Object[] m_elements;
  
          private DefaultPreparedEnqueue( DefaultQueue parent, Object[] elements )
          {
              m_parent = parent;
              m_elements = elements;
          }
  
          public void commit()
          {
              if( null == m_elements )
              {
                  throw new IllegalStateException( "This PreparedEnqueue has already been
processed!" );
              }
  
              try
              {
                  m_parent.enqueue( m_elements );
                  m_parent.m_reserve -= m_elements.length;
                  m_elements = null;
              }
              catch( Exception e )
              {
                  throw new IllegalStateException( "Default enqueue did not happen--should
be impossible" );
                  // will never happen
              }
          }
  
          public void abort()
          {
              if( null == m_elements )
              {
                  throw new IllegalStateException( "This PreparedEnqueue has already been
processed!" );
              }
  
              m_parent.m_reserve -= m_elements.length;
              m_elements = null;
          }
      }
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/event/impl/FixedSizeQueue.java
  
  Index: FixedSizeQueue.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
      must not be used to endorse or promote products derived from this  software
      without  prior written permission. For written permission, please contact
      apache@apache.org.
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation. For more  information on the
   Apache Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.excalibur.event.impl;
  
  import org.apache.excalibur.event.*;
  
  import org.apache.avalon.excalibur.concurrent.Mutex;
  
  /**
   * An implementation of the <code>Queue</code> that has a fixed size.  Once
   * the maximum number of elements are set, this <code>Queue</code> cannot be
   * changed.
   *
   * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
   */
  public final class FixedSizeQueue
      extends AbstractQueue
  {
      private final Object[] m_elements;
      private final Mutex m_mutex;
      private int m_start = 0;
      private int m_end = 0;
      private int m_reserve = 0;
  
      /**
       * Create a <code>FixedSizedQueue</code> with the specified maximum size.
       * The maximum size must be 1 or more.
       */
      public FixedSizeQueue( int size )
      {
          if ( size < 1 )
              throw new IllegalArgumentException("Cannot specify an unbounded Queue");
  
          m_elements = new Object[ size + 1 ];
          m_mutex = new Mutex();
      }
  
      public int size()
      {
          int size = 0;
  
          if( m_end < m_start )
          {
              size = maxSize() - m_start + m_end;
          }
          else
          {
              size = m_end - m_start;
          }
  
          return size;
      }
  
      public int maxSize()
      {
          return m_elements.length;
      }
  
      public PreparedEnqueue prepareEnqueue( final Object[] elements )
          throws SinkException
      {
          PreparedEnqueue enqueue = null;
  
          try
          {
              m_mutex.acquire();
              try
              {
                  if( elements.length + m_reserve + size() > maxSize() )
                  {
                      throw new SinkFullException( "Not enough room to enqueue these elements."
);
                  }
  
                  enqueue = new FixedSizePreparedEnqueue( this, elements );
              }
              finally
              {
                  m_mutex.release();
              }
          }
          catch( InterruptedException ie )
          {
          }
  
          return enqueue;
      }
  
      public boolean tryEnqueue( final Object element )
      {
          boolean success = false;
  
          try
          {
              m_mutex.acquire();
              try
              {
                  if( 1 + m_reserve + size() > maxSize() )
                  {
                      return false;
                  }
  
                  addElement( element );
                  success = true;
              }
              finally
              {
                  m_mutex.release();
              }
          }
          catch( InterruptedException ie )
          {
          }
  
          return success;
      }
  
      public void enqueue( final Object[] elements )
          throws SinkException
      {
          final int len = elements.length;
  
          try
          {
              m_mutex.acquire();
              try
              {
                  if( elements.length + m_reserve + size() > maxSize() )
                  {
                      throw new SinkFullException( "Not enough room to enqueue these elements."
);
                  }
  
                  for( int i = 0; i < len; i++ )
                  {
                      addElement( elements[ i ] );
                  }
              }
              finally
              {
                  m_mutex.release();
              }
          }
          catch( InterruptedException ie )
          {
          }
      }
  
      public void enqueue( final Object element )
          throws SinkException
      {
          try
          {
              m_mutex.acquire();
              try
              {
                  if( 1 + m_reserve + size() > maxSize() )
                  {
                      throw new SinkFullException( "Not enough room to enqueue these elements."
);
                  }
  
                  addElement( element );
              }
              finally
              {
                  m_mutex.release();
              }
          }
          catch( InterruptedException ie )
          {
          }
      }
  
      public Object[] dequeue( final int numElements )
      {
          Object[] elements = EMPTY_ARRAY;
  
          try
          {
              if( m_mutex.attempt( m_timeout ) )
              {
                  try
                  {
                      elements = retrieveElements( Math.min( size(),
                                                             numElements ) );
                  }
                  finally
                  {
                      m_mutex.release();
                  }
              }
          }
          catch( InterruptedException ie )
          {
          }
  
          return elements;
      }
  
      private final void addElement( Object element )
      {
          m_elements[ m_end ] = element;
  
          m_end++;
          if( m_end >= maxSize() )
          {
              m_end = 0;
          }
      }
  
      private final Object removeElement()
      {
          Object element = m_elements[ m_start ];
  
          if( null != element )
          {
              m_elements[ m_start ] = null;
  
              m_start++;
              if( m_start >= maxSize() )
              {
                  m_start = 0;
              }
          }
  
          return element;
      }
  
      /**
       * Removes exactly <code>count</code> elements from the underlying
       * element store and returns them as an array of Objects.
       * The caller is responsible for synchronizing access to the
       * element store and passing the correct value for
       * <code>count</code>.
       * <p>
       * The method can be further optimized by using System.arraycopy
       * if it is found to underperform.
       *
       * @param count number of elements to return
       * @return requested number of elements
       */
      private final Object[] retrieveElements( int count )
      {
          Object[] elements = new Object[ count ];
  
          for( int i = 0; i < count; i++ )
          {
              elements[ i ] = removeElement();
          }
  
          return elements;
      }
  
      public Object[] dequeueAll()
      {
          Object[] elements = EMPTY_ARRAY;
  
          try
          {
              if( m_mutex.attempt( m_timeout ) )
              {
                  try
                  {
                      elements = retrieveElements( size() );
                  }
                  finally
                  {
                      m_mutex.release();
                  }
              }
          }
          catch( InterruptedException ie )
          {
          }
  
          return elements;
      }
  
      public Object dequeue()
      {
          Object element = null;
  
          try
          {
              if( m_mutex.attempt( m_timeout ) )
              {
                  try
                  {
                      if( size() > 0 )
                      {
                          element = removeElement();
                      }
                  }
                  finally
                  {
                      m_mutex.release();
                  }
              }
          }
          catch( InterruptedException ie )
          {
          }
  
          return element;
      }
  
      private static final class FixedSizePreparedEnqueue implements PreparedEnqueue
      {
          private final FixedSizeQueue m_parent;
          private Object[] m_elements;
  
          private FixedSizePreparedEnqueue( FixedSizeQueue parent, Object[] elements )
          {
              m_parent = parent;
              m_elements = elements;
          }
  
          public void commit()
          {
              if( null == m_elements )
              {
                  throw new IllegalStateException( "This PreparedEnqueue has already been
processed!" );
              }
  
              try
              {
                  m_parent.enqueue( m_elements );
                  m_parent.m_reserve -= m_elements.length;
                  m_elements = null;
              }
              catch( Exception e )
              {
                  throw new IllegalStateException( "Default enqueue did not happen--should
be impossible" );
                  // will never happen
              }
          }
  
          public void abort()
          {
              if( null == m_elements )
              {
                  throw new IllegalStateException( "This PreparedEnqueue has already been
processed!" );
              }
  
              m_parent.m_reserve -= m_elements.length;
              m_elements = null;
          }
      }
  }
  
  
  
  1.8       +2 -2      jakarta-avalon-excalibur/event/src/test/QueueTest.java
  
  Index: QueueTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/event/src/test/QueueTest.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- QueueTest.java	3 Sep 2002 17:10:56 -0000	1.7
  +++ QueueTest.java	3 Sep 2002 17:43:00 -0000	1.8
  @@ -49,7 +49,7 @@
   */
   
   import org.apache.avalon.framework.CascadingRuntimeException;
  -import org.apache.excalibur.event.DefaultQueue;
  +import org.apache.excalibur.event.impl.DefaultQueue;
   import org.apache.excalibur.event.Queue;
   import org.apache.excalibur.event.Sink;
   import org.apache.excalibur.event.SinkException;
  
  
  
  1.9       +1 -1      jakarta-avalon-excalibur/event/src/test/org/apache/excalibur/event/command/test/TPCThreadManagerTestCase.java
  
  Index: TPCThreadManagerTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/event/src/test/org/apache/excalibur/event/command/test/TPCThreadManagerTestCase.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- TPCThreadManagerTestCase.java	3 Sep 2002 17:10:56 -0000	1.8
  +++ TPCThreadManagerTestCase.java	3 Sep 2002 17:43:00 -0000	1.9
  @@ -53,7 +53,7 @@
   import java.io.StringWriter;
   
   import org.apache.avalon.framework.parameters.Parameters;
  -import org.apache.excalibur.event.DefaultQueue;
  +import org.apache.excalibur.event.impl.DefaultQueue;
   import org.apache.excalibur.event.EventHandler;
   import org.apache.excalibur.event.Queue;
   import org.apache.excalibur.event.Sink;
  
  
  
  1.6       +1 -1      jakarta-avalon-excalibur/event/src/test/org/apache/excalibur/event/test/DefaultQueuePerformanceTestCase.java
  
  Index: DefaultQueuePerformanceTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/event/src/test/org/apache/excalibur/event/test/DefaultQueuePerformanceTestCase.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- DefaultQueuePerformanceTestCase.java	7 Aug 2002 23:08:26 -0000	1.5
  +++ DefaultQueuePerformanceTestCase.java	3 Sep 2002 17:43:01 -0000	1.6
  @@ -49,7 +49,7 @@
   */
   package org.apache.excalibur.event.test;
   
  -import org.apache.excalibur.event.DefaultQueue;
  +import org.apache.excalibur.event.impl.DefaultQueue;
   
   /**
    * The default queue implementation is a variabl size queue.
  
  
  
  1.5       +1 -1      jakarta-avalon-excalibur/event/src/test/org/apache/excalibur/event/test/FixedSizeQueuePerformanceTestCase.java
  
  Index: FixedSizeQueuePerformanceTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/event/src/test/org/apache/excalibur/event/test/FixedSizeQueuePerformanceTestCase.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- FixedSizeQueuePerformanceTestCase.java	7 Aug 2002 23:08:26 -0000	1.4
  +++ FixedSizeQueuePerformanceTestCase.java	3 Sep 2002 17:43:01 -0000	1.5
  @@ -49,7 +49,7 @@
   */
   package org.apache.excalibur.event.test;
   
  -import org.apache.excalibur.event.FixedSizeQueue;
  +import org.apache.excalibur.event.impl.FixedSizeQueue;
   
   /**
    * The default queue implementation is a variabl size queue.
  
  
  
  1.10      +1 -1      jakarta-avalon-excalibur/event/src/test/org/apache/excalibur/event/test/FixedSizeQueueTestCase.java
  
  Index: FixedSizeQueueTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/event/src/test/org/apache/excalibur/event/test/FixedSizeQueueTestCase.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- FixedSizeQueueTestCase.java	7 Aug 2002 23:08:26 -0000	1.9
  +++ FixedSizeQueueTestCase.java	3 Sep 2002 17:43:01 -0000	1.10
  @@ -49,7 +49,7 @@
   */
   package org.apache.excalibur.event.test;
   
  -import org.apache.excalibur.event.FixedSizeQueue;
  +import org.apache.excalibur.event.impl.FixedSizeQueue;
   
   /**
    * The default queue implementation is a variabl size queue.
  
  
  
  1.10      +1 -1      jakarta-avalon-excalibur/event/src/test/org/apache/excalibur/event/test/QueueTestCase.java
  
  Index: QueueTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/event/src/test/org/apache/excalibur/event/test/QueueTestCase.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- QueueTestCase.java	7 Aug 2002 23:08:26 -0000	1.9
  +++ QueueTestCase.java	3 Sep 2002 17:43:01 -0000	1.10
  @@ -49,7 +49,7 @@
   */
   package org.apache.excalibur.event.test;
   
  -import org.apache.excalibur.event.DefaultQueue;
  +import org.apache.excalibur.event.impl.DefaultQueue;
   
   /**
    * The default queue implementation is a variabl size queue.
  
  
  
  1.9       +2 -2      jakarta-avalon-excalibur/event/src/test/org/apache/excalibur/event/test/ThreadedQueueTestCase.java
  
  Index: ThreadedQueueTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/event/src/test/org/apache/excalibur/event/test/ThreadedQueueTestCase.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- ThreadedQueueTestCase.java	3 Sep 2002 17:10:56 -0000	1.8
  +++ ThreadedQueueTestCase.java	3 Sep 2002 17:43:01 -0000	1.9
  @@ -50,7 +50,7 @@
   package org.apache.excalibur.event.test;
   
   import junit.framework.TestCase;
  -import org.apache.excalibur.event.DefaultQueue;
  +import org.apache.excalibur.event.impl.DefaultQueue;
   import org.apache.excalibur.event.Queue;
   import org.apache.excalibur.event.Sink;
   import org.apache.excalibur.event.SinkException;
  
  
  

--
To unsubscribe, e-mail:   <mailto:avalon-cvs-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@jakarta.apache.org>


Mime
View raw message