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/src/scratchpad/org/apache/avalon/excalibur/event/test QueueTestCase.java
Date Mon, 17 Dec 2001 23:05:00 GMT
bloritsch    01/12/17 15:05:00

  Added:       src/scratchpad/org/apache/avalon/excalibur/event
                        AbstractQueue.java DefaultQueue.java
               src/scratchpad/org/apache/avalon/excalibur/event/test
                        QueueTestCase.java
  Log:
  initial queue implementation
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/event/AbstractQueue.java
  
  Index: AbstractQueue.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.excalibur.event;
  
  import java.util.ArrayList;
  
  /**
   * The default queue implementation is a variabl size queue.
   *
   * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
   */
  public abstract class AbstractQueue implements Queue
  {
      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;
      }
  
      /**
       * Default for isFull()
       */
      public boolean isFull()
      {
          return maxSize() - size() > 0;
      }
  
      /**
       * Set the timeout
       */
      public void setTimeOut( final long millis )
      {
          if ( millis > 0 )
          {
              m_timeout = millis;
          }
          else
          {
              m_timeout = 0;
          }
      }
  
      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/src/scratchpad/org/apache/avalon/excalibur/event/DefaultQueue.java
  
  Index: DefaultQueue.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.excalibur.event;
  
  import java.util.ArrayList;
  
  /**
   * The default queue implementation is a variabl size queue.
   *
   * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
   */
  public class DefaultQueue extends AbstractQueue
  {
      private final ArrayList m_elements;
  
      public DefaultQueue()
      {
          m_elements = new ArrayList();
      }
  
      public int size()
      {
          return m_elements.size();
      }
  
      public PreparedEnqueue prepareEnqueue( final QueueElement[] elements )
          throws SourceException
      {
          return new DefaultPreparedEnqueue( this, elements );
      }
  
      public boolean tryEnqueue( final QueueElement element )
      {
          boolean success = m_elements.add( element );
          m_elements.notifyAll();
  
          return success;
      }
  
      public void enqueue( final QueueElement[] elements )
          throws SourceException
      {
          final int len = elements.length;
  
          for ( int i = 0; i < len; i++ )
          {
              m_elements.add( elements[i] );
          }
  
          m_elements.notifyAll();
      }
  
      public void enqueue( final QueueElement element )
          throws SourceException
      {
          m_elements.add( element );
      }
  
      public QueueElement[] dequeue( final int numElements )
      {
          block( m_elements );
  
          int arraySize = numElements;
  
          if ( size() < numElements )
          {
              arraySize = size();
          }
  
          QueueElement[] elements = new QueueElement[ arraySize ];
  
          for ( int i = 0; i < arraySize; i++ )
          {
              elements[i] = (QueueElement) m_elements.remove( 0 );
          }
  
          return elements;
      }
  
      public QueueElement[] dequeueAll()
      {
          block( m_elements );
  
          QueueElement[] elements = (QueueElement[]) m_elements.toArray( new QueueElement
[] {} );
          m_elements.clear();
  
          return elements;
      }
  
      public QueueElement dequeue()
      {
          block( m_elements );
  
          if ( size() <= 0 )
          {
              return null;
          }
  
          return (QueueElement) m_elements.remove( 0 );
      }
  
      private final static class DefaultPreparedEnqueue implements PreparedEnqueue
      {
          private final DefaultQueue m_parent;
          private final QueueElement[] m_elements;
  
          private DefaultPreparedEnqueue( DefaultQueue parent, QueueElement[] elements )
          {
              m_parent = parent;
              m_elements = elements;
          }
  
          public void commit()
          {
              try
              {
                  m_parent.enqueue( m_elements );
              }
              catch (Exception e)
              {
                  throw new IllegalStateException("Default enqueue did not happen--should
be impossible");
                  // will never happen
              }
          }
  
          public void abort()
          {
              // do nothing.  DefaultQueue is unbounded, so there is nothing to manage.
          }
      }
  }
  
  
  1.1                  jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/event/test/QueueTestCase.java
  
  Index: QueueTestCase.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.excalibur.event.test;
  
  import junit.framework.TestCase;
  import org.apache.avalon.excalibur.event.DefaultQueue;
  import org.apache.avalon.excalibur.event.QueueElement;
  import org.apache.avalon.excalibur.event.SourceException;
  
  /**
   * The default queue implementation is a variabl size queue.
   *
   * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
   */
  public class QueueTestCase extends TestCase
  {
      private static final class TestQueueElement implements QueueElement
      {
           private final Object m_attachment;
  
           private TestQueueElement()
           {
               this( null );
           }
  
           private TestQueueElement( Object attachment )
           {
               m_attachment = attachment;
           }
  
           public Object getAttachment()
           {
               return m_attachment;
           }
  
           public long getType()
           {
               return 1;
           }
      }
  
      public QueueTestCase( String name )
      {
          super( name );
      }
  
      public void testDefaultQueue()
      {
          DefaultQueue queue = new DefaultQueue();
          assertEquals( queue.size(), 0 );
  
          //test enqueue
          try
          {
              queue.enqueue( new TestQueueElement () );
              assertTrue( queue.size() > 0 );
  
              assertNotNull( queue.dequeue() );
          }
          catch ( SourceException se )
          {
          }
      }
  }
  
  

--
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