directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r326060 - in /directory/network: branches/0.7/src/java/org/apache/mina/util/ branches/0.7/src/test/org/apache/mina/util/ trunk/src/java/org/apache/mina/util/ trunk/src/test/org/apache/mina/util/
Date Tue, 18 Oct 2005 09:31:12 GMT
Author: trustin
Date: Tue Oct 18 02:30:48 2005
New Revision: 326060

URL: http://svn.apache.org/viewcvs?rev=326060&view=rev
Log:
* Made Queue a java.util.List
* Modified BlockingQueue to catch up this change

* Resolved DIRMINA-97 - ThreadPoolFilter doesn't shut down immediately
** by adding additional checks



Modified:
    directory/network/branches/0.7/src/java/org/apache/mina/util/BaseThreadPool.java
    directory/network/branches/0.7/src/java/org/apache/mina/util/BlockingQueue.java
    directory/network/branches/0.7/src/java/org/apache/mina/util/Queue.java
    directory/network/branches/0.7/src/test/org/apache/mina/util/QueueTest.java
    directory/network/trunk/src/java/org/apache/mina/util/BlockingQueue.java
    directory/network/trunk/src/java/org/apache/mina/util/Queue.java
    directory/network/trunk/src/test/org/apache/mina/util/QueueTest.java

Modified: directory/network/branches/0.7/src/java/org/apache/mina/util/BaseThreadPool.java
URL: http://svn.apache.org/viewcvs/directory/network/branches/0.7/src/java/org/apache/mina/util/BaseThreadPool.java?rev=326060&r1=326059&r2=326060&view=diff
==============================================================================
--- directory/network/branches/0.7/src/java/org/apache/mina/util/BaseThreadPool.java (original)
+++ directory/network/branches/0.7/src/java/org/apache/mina/util/BaseThreadPool.java Tue Oct
18 02:30:48 2005
@@ -368,6 +368,11 @@
             {
                 for( ;; )
                 {
+                    if( shuttingDown )
+                    {
+                        return null;
+                    }
+
                     try
                     {
                         unfetchedSessionBuffers.waitForNewItem();
@@ -484,7 +489,7 @@
                     currentTime = System.currentTimeMillis();
                 }
 
-                boolean timeToLead = this == leader;
+                boolean timeToLead = this == leader && !shuttingDown;
 
                 if( !timeToLead )
                 {

Modified: directory/network/branches/0.7/src/java/org/apache/mina/util/BlockingQueue.java
URL: http://svn.apache.org/viewcvs/directory/network/branches/0.7/src/java/org/apache/mina/util/BlockingQueue.java?rev=326060&r1=326059&r2=326060&view=diff
==============================================================================
--- directory/network/branches/0.7/src/java/org/apache/mina/util/BlockingQueue.java (original)
+++ directory/network/branches/0.7/src/java/org/apache/mina/util/BlockingQueue.java Tue Oct
18 02:30:48 2005
@@ -1,109 +1,250 @@
-/*
- *   @(#) $Id$
- *
- *   Copyright 2004 The Apache Software Foundation
- *
- *   Licensed under the Apache License, Version 2.0 (the "License");
- *   you may not use this file except in compliance with the License.
- *   You may obtain a copy of the License at
- *
- *       http://www.apache.org/licenses/LICENSE-2.0
- *
- *   Unless required by applicable law or agreed to in writing, software
- *   distributed under the License is distributed on an "AS IS" BASIS,
- *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *   See the License for the specific language governing permissions and
- *   limitations under the License.
- *
- */
-package org.apache.mina.util;
-
-/**
- * A synchronized version of {@link Queue}.
- *
- * @author Trustin Lee
- * @version $Rev$, $Date$
- */
-public class BlockingQueue extends Queue
-{
-    private static final long serialVersionUID = 5516588196355725567L;
-
-    private int waiters = 0;
-
-    public BlockingQueue()
-    {
-    }
-
-    public synchronized int capacity()
-    {
-        return super.capacity();
-    }
-
-    public synchronized void clear()
-    {
-        super.clear();
-    }
-
-    public synchronized Object first()
-    {
-        return super.first();
-    }
-
-    public synchronized Object get( int idx )
-    {
-        return super.get( idx );
-    }
-
-    public synchronized boolean isEmpty()
-    {
-        return super.isEmpty();
-    }
-
-    public synchronized Object last()
-    {
-        return super.last();
-    }
-
-    public synchronized Object pop()
-    {
-        return super.pop();
-    }
-
-    public synchronized void push( Object obj )
-    {
-        super.push( obj );
-        if( waiters > 0 )
-            notify();
-    }
-
-    public synchronized int size()
-    {
-        return super.size();
-    }
-
-    public synchronized String toString()
-    {
-        return super.toString();
-    }
-
-    /**
-     * Waits until any elements are in this queue.
-     * 
-     * @throws InterruptedException if the current thread is interrupted
-     */
-    public synchronized void waitForNewItem() throws InterruptedException
-    {
-        waiters++;
-        try
-        {
-            while( super.isEmpty() )
-            {
-                wait();
-            }
-        }
-        finally
-        {
-            waiters--;
-        }
-    }
-}
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.mina.util;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+
+/**
+ * A synchronized version of {@link Queue}.
+ * 
+ * @author Trustin Lee
+ * @version $Rev$, $Date$
+ */
+public class BlockingQueue extends Queue
+{
+    private static final long serialVersionUID = 5516588196355725567L;
+
+    private int waiters = 0;
+
+    public BlockingQueue()
+    {
+    }
+
+    /**
+     * Waits until any elements are in this queue.
+     * 
+     * @throws InterruptedException
+     *             if the current thread is interrupted
+     */
+    public synchronized void waitForNewItem() throws InterruptedException
+    {
+        waiters++;
+        try
+        {
+            while( super.isEmpty() )
+            {
+                wait();
+            }
+        }
+        finally
+        {
+            waiters--;
+        }
+    }
+
+    public synchronized void push( Object obj )
+    {
+        super.push( obj );
+        notifyAdded();
+    }
+
+    public synchronized void add( int idx, Object o )
+    {
+        super.add( idx, o );
+        notifyAdded();
+    }
+
+    public synchronized boolean add( Object o )
+    {
+        if( super.add( o ) )
+        {
+            notifyAdded();
+            return true;
+        }
+        else
+        {
+            return false;
+        }
+    }
+
+    public synchronized boolean addAll( int arg0, Collection arg1 )
+    {
+        if( super.addAll( arg0, arg1 ) )
+        {
+            notifyAdded();
+            return true;
+        }
+        else
+        {
+            return false;
+        }
+    }
+
+    public synchronized boolean addAll( Collection arg0 )
+    {
+        if( super.addAll( arg0 ) )
+        {
+            notifyAdded();
+            return true;
+        }
+        else
+        {
+            return false;
+        }
+    }
+
+    private void notifyAdded()
+    {
+        if( waiters > 0 )
+            notify();
+    }
+
+    public synchronized int capacity()
+    {
+        return super.capacity();
+    }
+
+    public synchronized void clear()
+    {
+        super.clear();
+    }
+
+    public synchronized Object first()
+    {
+        return super.first();
+    }
+
+    public synchronized Object get( int idx )
+    {
+        return super.get( idx );
+    }
+
+    public synchronized boolean isEmpty()
+    {
+        return super.isEmpty();
+    }
+
+    public synchronized Object last()
+    {
+        return super.last();
+    }
+
+    public synchronized Object pop()
+    {
+        return super.pop();
+    }
+
+    public synchronized int size()
+    {
+        return super.size();
+    }
+
+    public synchronized String toString()
+    {
+        return super.toString();
+    }
+
+    public synchronized Object remove( int idx )
+    {
+        return super.remove( idx );
+    }
+
+    public synchronized Object set( int idx, Object o )
+    {
+        return super.set( idx, o );
+    }
+
+    public synchronized boolean equals( Object o )
+    {
+        return super.equals( o );
+    }
+
+    public synchronized int hashCode()
+    {
+        return super.hashCode();
+    }
+
+    public synchronized int indexOf( Object o )
+    {
+        return super.indexOf( o );
+    }
+
+    public synchronized Iterator iterator()
+    {
+        return super.iterator();
+    }
+
+    public synchronized int lastIndexOf( Object o )
+    {
+        return super.lastIndexOf( o );
+    }
+
+    public synchronized ListIterator listIterator()
+    {
+        return super.listIterator();
+    }
+
+    public synchronized ListIterator listIterator( int index )
+    {
+        return super.listIterator( index );
+    }
+
+    public synchronized List subList( int fromIndex, int toIndex )
+    {
+        return super.subList( fromIndex, toIndex );
+    }
+
+    public synchronized boolean contains( Object o )
+    {
+        return super.contains( o );
+    }
+
+    public synchronized boolean containsAll( Collection arg0 )
+    {
+        return super.containsAll( arg0 );
+    }
+
+    public synchronized boolean remove( Object o )
+    {
+        return super.remove( o );
+    }
+
+    public synchronized boolean removeAll( Collection arg0 )
+    {
+        return super.removeAll( arg0 );
+    }
+
+    public synchronized boolean retainAll( Collection arg0 )
+    {
+        return super.retainAll( arg0 );
+    }
+
+    public synchronized Object[] toArray()
+    {
+        return super.toArray();
+    }
+
+    public synchronized Object[] toArray( Object[] arg0 )
+    {
+        return super.toArray( arg0 );
+    }
+}

Modified: directory/network/branches/0.7/src/java/org/apache/mina/util/Queue.java
URL: http://svn.apache.org/viewcvs/directory/network/branches/0.7/src/java/org/apache/mina/util/Queue.java?rev=326060&r1=326059&r2=326060&view=diff
==============================================================================
--- directory/network/branches/0.7/src/java/org/apache/mina/util/Queue.java (original)
+++ directory/network/branches/0.7/src/java/org/apache/mina/util/Queue.java Tue Oct 18 02:30:48
2005
@@ -19,7 +19,9 @@
 package org.apache.mina.util;
 
 import java.io.Serializable;
+import java.util.AbstractList;
 import java.util.Arrays;
+import java.util.List;
 
 /**
  * A unbounded circular queue.
@@ -27,7 +29,7 @@
  * @author Trustin Lee (trustin@apache.org)
  * @version $Rev$, $Date$
  */
-public class Queue implements Serializable
+public class Queue extends AbstractList implements List, Serializable
 {
     private static final long serialVersionUID = 3835151744526464313L;
 
@@ -88,9 +90,7 @@
 
         Object ret = items[ first ];
         items[ first ] = null;
-        first = ( first + 1 ) & mask;
-
-        size--;
+        decreaseSize();
 
         return ret;
     }
@@ -100,31 +100,9 @@
      */
     public void push( Object obj )
     {
-        if( size == items.length )
-        {
-            // expand queue
-            final int oldLen = items.length;
-            Object[] tmp = new Object[ oldLen * 2 ];
-
-            if( first < last )
-            {
-                System.arraycopy( items, first, tmp, 0, last - first );
-            }
-            else
-            {
-                System.arraycopy( items, first, tmp, 0, oldLen - first );
-                System.arraycopy( items, 0, tmp, oldLen - first, last );
-            }
-
-            first = 0;
-            last = oldLen;
-            items = tmp;
-            mask = tmp.length - 1;
-        }
-
+        ensureCapacity();
         items[ last ] = obj;
-        last = ( last + 1 ) & mask;
-        size++;
+        increaseSize();
     }
 
     /**
@@ -143,6 +121,12 @@
         return items[ first ];
     }
 
+    /**
+     * Returns the last element of the queue.
+     * 
+     * @return <code>null</code>, if the queue is empty, or the element is
+     *         really <code>null</code>.
+     */
     public Object last()
     {
         if( size == 0 )
@@ -155,7 +139,8 @@
     
     public Object get( int idx )
     {
-        return items[ ( first + idx ) & mask ];
+        checkIndex(idx);
+        return items[ getRealIndex(idx) ];
     }
 
     /**
@@ -177,5 +162,148 @@
     public String toString()
     {
         return "first=" + first + ", last=" + last + ", size=" + size + ", mask = " + mask;
+    }
+
+    private void checkIndex( int idx )
+    {
+        if( idx < 0 || idx >= size )
+        {
+            throw new IndexOutOfBoundsException( String.valueOf( idx ) );
+        }
+    }
+
+    private int getRealIndex( int idx )
+    {
+        return ( first + idx ) & mask;
+    }
+
+    private void increaseSize()
+    {
+        last = ( last + 1 ) & mask;
+        size++;
+    }
+
+    private void decreaseSize() {
+        first = ( first + 1 ) & mask;
+        size--;
+    }
+
+    private void ensureCapacity()
+    {
+        if( size < items.length )
+        {
+            return;
+        }
+        
+        // expand queue
+        final int oldLen = items.length;
+        Object[] tmp = new Object[ oldLen * 2 ];
+
+        if( first < last )
+        {
+            System.arraycopy( items, first, tmp, 0, last - first );
+        }
+        else
+        {
+            System.arraycopy( items, first, tmp, 0, oldLen - first );
+            System.arraycopy( items, 0, tmp, oldLen - first, last );
+        }
+
+        first = 0;
+        last = oldLen;
+        items = tmp;
+        mask = tmp.length - 1;
+    }
+
+    //////////////////////////////////////////
+    // java.util.List compatibility methods //
+    //////////////////////////////////////////
+
+    public boolean add( Object o )
+    {
+        push( o );
+        return true;
+    }
+
+    public Object set(int idx, Object o) {
+        checkIndex(idx);
+        
+        int realIdx = getRealIndex(idx);
+        Object old = items[ realIdx ];
+        items[ realIdx ] = o;
+        return old;
+    }
+
+    public void add( int idx, Object o )
+    {
+        if( idx == size )
+        {
+            push( o );
+            return;
+        }
+        
+        checkIndex( idx );
+        ensureCapacity();
+        
+        int realIdx = getRealIndex( idx );
+        
+        // Make a room for a new element.
+        if( first < last )
+        {
+            System.arraycopy( items, realIdx, items, realIdx + 1, last - realIdx );
+        }
+        else
+        {
+            if( realIdx >= first )
+            {
+                System.arraycopy( items, 0, items, 1, last );
+                items[ 0 ] = items[ items.length - 1 ];
+                System.arraycopy( items, realIdx, items, realIdx + 1, items.length - realIdx
- 1 );
+            }
+            else
+            {
+                System.arraycopy( items, realIdx, items, realIdx + 1, last - realIdx );
+            }
+        }
+        
+        items[ realIdx ] = o;
+        increaseSize();
+    }
+
+    public Object remove( int idx )
+    {
+        if( idx == 0 )
+        {
+            return pop();
+        }
+        
+        checkIndex( idx );
+        
+        int realIdx = getRealIndex( idx );
+        Object removed = items[ realIdx ];
+        
+        // Remove a room for the removed element.
+        if( first < last )
+        {
+            System.arraycopy( items, first, items, first + 1, realIdx - first );
+        }
+        else
+        {
+            if( realIdx >= first )
+            {
+                System.arraycopy( items, first, items, first + 1, realIdx - first );
+            }
+            else
+            {
+                System.arraycopy( items, 0, items, 1, realIdx );
+                items[ 0 ] = items[ items.length - 1 ];
+                System.arraycopy( items, first, items, first + 1, items.length - first -
1 );
+            }
+        }
+        
+        items[ first ] = null;
+        decreaseSize();
+
+        return removed;
     }
 }

Modified: directory/network/branches/0.7/src/test/org/apache/mina/util/QueueTest.java
URL: http://svn.apache.org/viewcvs/directory/network/branches/0.7/src/test/org/apache/mina/util/QueueTest.java?rev=326060&r1=326059&r2=326060&view=diff
==============================================================================
--- directory/network/branches/0.7/src/test/org/apache/mina/util/QueueTest.java (original)
+++ directory/network/branches/0.7/src/test/org/apache/mina/util/QueueTest.java Tue Oct 18
02:30:48 2005
@@ -18,6 +18,8 @@
  */
 package org.apache.mina.util;
 
+import java.util.Iterator;
+
 import junit.framework.Assert;
 import junit.framework.TestCase;
 
@@ -71,6 +73,162 @@
             q.push( new Integer( ++pushCount ) );
             Assert.assertEquals( ++popCount, ( ( Integer ) q.pop() ).intValue() );
         }
+    }
+    
+    public void testRandomAddOnQueue()
+    {
+        Queue q = new Queue();
+        // Create a queue with 5 elements and capacity 8;
+        for( int i = 0; i < 5; i ++ )
+        {
+            q.push( new Integer( i ) );
+        }
+        
+        q.add( 0, new Integer( 100 ) );
+        q.add( 3, new Integer( 200 ) );
+        q.add( 7, new Integer( 300 ) );
+        
+        Iterator i = q.iterator();
+        Assert.assertEquals( 8, q.size() );
+        Assert.assertEquals( new Integer( 100 ), i.next() );
+        Assert.assertEquals( new Integer( 0 ), i.next() );
+        Assert.assertEquals( new Integer( 1 ), i.next() );
+        Assert.assertEquals( new Integer( 200 ), i.next() );
+        Assert.assertEquals( new Integer( 2 ), i.next() );
+        Assert.assertEquals( new Integer( 3 ), i.next() );
+        Assert.assertEquals( new Integer( 4 ), i.next() );
+        Assert.assertEquals( new Integer( 300 ), i.next() );
+
+        try
+        {
+            i.next();
+            Assert.fail();
+        }
+        catch( Exception e )
+        {
+            // OK
+        }
+    }
+    
+    public void testRandomAddOnRotatedQueue()
+    {
+        Queue q = getRotatedQueue();
+        
+        q.add( 0, new Integer( 100 ) );  // addFirst
+        q.add( 2, new Integer( 200 ) );
+        q.add( 4, new Integer( 300 ) );
+        q.add( 10, new Integer( 400 ) );
+        q.add( 12, new Integer( 500 ) ); // addLast
+        
+        Iterator i = q.iterator();
+        Assert.assertEquals( 13, q.size() );
+        Assert.assertEquals( new Integer( 100 ), i.next() );
+        Assert.assertEquals( new Integer( 0 ), i.next() );
+        Assert.assertEquals( new Integer( 200 ), i.next() );
+        Assert.assertEquals( new Integer( 1 ), i.next() );
+        Assert.assertEquals( new Integer( 300 ), i.next() );
+        Assert.assertEquals( new Integer( 2 ), i.next() );
+        Assert.assertEquals( new Integer( 3 ), i.next() );
+        Assert.assertEquals( new Integer( 4 ), i.next() );
+        Assert.assertEquals( new Integer( 5 ), i.next() );
+        Assert.assertEquals( new Integer( 6 ), i.next() );
+        Assert.assertEquals( new Integer( 400 ), i.next() );
+        Assert.assertEquals( new Integer( 7 ), i.next() );
+        Assert.assertEquals( new Integer( 500 ), i.next() );
+
+        try
+        {
+            i.next();
+            Assert.fail();
+        }
+        catch( Exception e )
+        {
+            // OK
+        }
+    }
+    
+    public void testRandomRemoveOnQueue()
+    {
+        Queue q = new Queue();
+
+        // Create a queue with 5 elements and capacity 8;
+        for( int i = 0; i < 5; i ++ )
+        {
+            q.push( new Integer( i ) );
+        }
+        
+        q.remove( 0 );
+        q.remove( 2 );
+        q.remove( 2 );
+        
+        Iterator i = q.iterator();
+        Assert.assertEquals( 2, q.size() );
+        Assert.assertEquals( new Integer( 1 ), i.next() );
+        Assert.assertEquals( new Integer( 2 ), i.next() );
+
+        try
+        {
+            i.next();
+            Assert.fail();
+        }
+        catch( Exception e )
+        {
+            // OK
+        }
+    }
+    
+    public void testRandomRemoveOnRotatedQueue()
+    {
+        Queue q = getRotatedQueue();
+        
+        q.remove( 0 ); // removeFirst
+        q.remove( 2 ); // removeLast in the first half
+        q.remove( 2 ); // removeFirst in the first half
+        q.remove( 4 ); // removeLast
+        
+        Iterator i = q.iterator();
+        Assert.assertEquals( 4, q.size() );
+        Assert.assertEquals( new Integer( 1 ), i.next() );
+        Assert.assertEquals( new Integer( 2 ), i.next() );
+        Assert.assertEquals( new Integer( 5 ), i.next() );
+        Assert.assertEquals( new Integer( 6 ), i.next() );
+
+        try
+        {
+            i.next();
+            Assert.fail();
+        }
+        catch( Exception e )
+        {
+            // OK
+        }
+    }
+    
+    private Queue getRotatedQueue()
+    {
+        Queue q = new Queue();
+        
+        // Ensure capacity: 16
+        for( int i = 0; i < 16; i ++ )
+        {
+            q.push( new Object() );
+        }
+        q.clear();
+
+        // Rotate it
+        for( int i = 0; i < 12; i ++ )
+        {
+            q.push( new Object() );
+            q.pop();
+        }
+        
+        // Now push items
+        for( int i = 0; i < 8; i ++ )
+        {
+            q.push( new Integer( i ) );
+        }
+        
+        return q;
     }
     
     public static void main( String[] args )

Modified: directory/network/trunk/src/java/org/apache/mina/util/BlockingQueue.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/util/BlockingQueue.java?rev=326060&r1=326059&r2=326060&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/util/BlockingQueue.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/util/BlockingQueue.java Tue Oct 18 02:30:48
2005
@@ -18,9 +18,14 @@
  */
 package org.apache.mina.util;
 
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+
 /**
  * A synchronized version of {@link Queue}.
- *
+ * 
  * @author Trustin Lee
  * @version $Rev$, $Date$
  */
@@ -34,6 +39,85 @@
     {
     }
 
+    /**
+     * Waits until any elements are in this queue.
+     * 
+     * @throws InterruptedException
+     *             if the current thread is interrupted
+     */
+    public synchronized void waitForNewItem() throws InterruptedException
+    {
+        waiters++;
+        try
+        {
+            while( super.isEmpty() )
+            {
+                wait();
+            }
+        }
+        finally
+        {
+            waiters--;
+        }
+    }
+
+    public synchronized void push( Object obj )
+    {
+        super.push( obj );
+        notifyAdded();
+    }
+
+    public synchronized void add( int idx, Object o )
+    {
+        super.add( idx, o );
+        notifyAdded();
+    }
+
+    public synchronized boolean add( Object o )
+    {
+        if( super.add( o ) )
+        {
+            notifyAdded();
+            return true;
+        }
+        else
+        {
+            return false;
+        }
+    }
+
+    public synchronized boolean addAll( int arg0, Collection arg1 )
+    {
+        if( super.addAll( arg0, arg1 ) )
+        {
+            notifyAdded();
+            return true;
+        }
+        else
+        {
+            return false;
+        }
+    }
+
+    public synchronized boolean addAll( Collection arg0 )
+    {
+        if( super.addAll( arg0 ) )
+        {
+            notifyAdded();
+            return true;
+        }
+        else
+        {
+            return false;
+        }
+    }
+
+    private void notifyAdded()
+    {
+        if( waiters > 0 )
+            notify();
+    }
+
     public synchronized int capacity()
     {
         return super.capacity();
@@ -69,13 +153,6 @@
         return super.pop();
     }
 
-    public synchronized void push( Object obj )
-    {
-        super.push( obj );
-        if( waiters > 0 )
-            notify();
-    }
-
     public synchronized int size()
     {
         return super.size();
@@ -86,24 +163,88 @@
         return super.toString();
     }
 
-    /**
-     * Waits until any elements are in this queue.
-     * 
-     * @throws InterruptedException if the current thread is interrupted
-     */
-    public synchronized void waitForNewItem() throws InterruptedException
+    public synchronized Object remove( int idx )
     {
-        waiters++;
-        try
-        {
-            while( super.isEmpty() )
-            {
-                wait();
-            }
-        }
-        finally
-        {
-            waiters--;
-        }
+        return super.remove( idx );
+    }
+
+    public synchronized Object set( int idx, Object o )
+    {
+        return super.set( idx, o );
+    }
+
+    public synchronized boolean equals( Object o )
+    {
+        return super.equals( o );
+    }
+
+    public synchronized int hashCode()
+    {
+        return super.hashCode();
+    }
+
+    public synchronized int indexOf( Object o )
+    {
+        return super.indexOf( o );
+    }
+
+    public synchronized Iterator iterator()
+    {
+        return super.iterator();
+    }
+
+    public synchronized int lastIndexOf( Object o )
+    {
+        return super.lastIndexOf( o );
+    }
+
+    public synchronized ListIterator listIterator()
+    {
+        return super.listIterator();
+    }
+
+    public synchronized ListIterator listIterator( int index )
+    {
+        return super.listIterator( index );
+    }
+
+    public synchronized List subList( int fromIndex, int toIndex )
+    {
+        return super.subList( fromIndex, toIndex );
+    }
+
+    public synchronized boolean contains( Object o )
+    {
+        return super.contains( o );
+    }
+
+    public synchronized boolean containsAll( Collection arg0 )
+    {
+        return super.containsAll( arg0 );
+    }
+
+    public synchronized boolean remove( Object o )
+    {
+        return super.remove( o );
+    }
+
+    public synchronized boolean removeAll( Collection arg0 )
+    {
+        return super.removeAll( arg0 );
+    }
+
+    public synchronized boolean retainAll( Collection arg0 )
+    {
+        return super.retainAll( arg0 );
+    }
+
+    public synchronized Object[] toArray()
+    {
+        return super.toArray();
+    }
+
+    public synchronized Object[] toArray( Object[] arg0 )
+    {
+        return super.toArray( arg0 );
     }
 }

Modified: directory/network/trunk/src/java/org/apache/mina/util/Queue.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/util/Queue.java?rev=326060&r1=326059&r2=326060&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/util/Queue.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/util/Queue.java Tue Oct 18 02:30:48 2005
@@ -19,7 +19,9 @@
 package org.apache.mina.util;
 
 import java.io.Serializable;
+import java.util.AbstractList;
 import java.util.Arrays;
+import java.util.List;
 
 /**
  * A unbounded circular queue.
@@ -27,7 +29,7 @@
  * @author Trustin Lee (trustin@apache.org)
  * @version $Rev$, $Date$
  */
-public class Queue implements Serializable
+public class Queue extends AbstractList implements List, Serializable
 {
     private static final long serialVersionUID = 3835151744526464313L;
 
@@ -88,9 +90,7 @@
 
         Object ret = items[ first ];
         items[ first ] = null;
-        first = ( first + 1 ) & mask;
-
-        size--;
+        decreaseSize();
 
         return ret;
     }
@@ -100,31 +100,9 @@
      */
     public void push( Object obj )
     {
-        if( size == items.length )
-        {
-            // expand queue
-            final int oldLen = items.length;
-            Object[] tmp = new Object[ oldLen * 2 ];
-
-            if( first < last )
-            {
-                System.arraycopy( items, first, tmp, 0, last - first );
-            }
-            else
-            {
-                System.arraycopy( items, first, tmp, 0, oldLen - first );
-                System.arraycopy( items, 0, tmp, oldLen - first, last );
-            }
-
-            first = 0;
-            last = oldLen;
-            items = tmp;
-            mask = tmp.length - 1;
-        }
-
+        ensureCapacity();
         items[ last ] = obj;
-        last = ( last + 1 ) & mask;
-        size++;
+        increaseSize();
     }
 
     /**
@@ -143,6 +121,12 @@
         return items[ first ];
     }
 
+    /**
+     * Returns the last element of the queue.
+     * 
+     * @return <code>null</code>, if the queue is empty, or the element is
+     *         really <code>null</code>.
+     */
     public Object last()
     {
         if( size == 0 )
@@ -155,7 +139,8 @@
     
     public Object get( int idx )
     {
-        return items[ ( first + idx ) & mask ];
+        checkIndex(idx);
+        return items[ getRealIndex(idx) ];
     }
 
     /**
@@ -177,5 +162,148 @@
     public String toString()
     {
         return "first=" + first + ", last=" + last + ", size=" + size + ", mask = " + mask;
+    }
+
+    private void checkIndex( int idx )
+    {
+        if( idx < 0 || idx >= size )
+        {
+            throw new IndexOutOfBoundsException( String.valueOf( idx ) );
+        }
+    }
+
+    private int getRealIndex( int idx )
+    {
+        return ( first + idx ) & mask;
+    }
+
+    private void increaseSize()
+    {
+        last = ( last + 1 ) & mask;
+        size++;
+    }
+
+    private void decreaseSize() {
+        first = ( first + 1 ) & mask;
+        size--;
+    }
+
+    private void ensureCapacity()
+    {
+        if( size < items.length )
+        {
+            return;
+        }
+        
+        // expand queue
+        final int oldLen = items.length;
+        Object[] tmp = new Object[ oldLen * 2 ];
+
+        if( first < last )
+        {
+            System.arraycopy( items, first, tmp, 0, last - first );
+        }
+        else
+        {
+            System.arraycopy( items, first, tmp, 0, oldLen - first );
+            System.arraycopy( items, 0, tmp, oldLen - first, last );
+        }
+
+        first = 0;
+        last = oldLen;
+        items = tmp;
+        mask = tmp.length - 1;
+    }
+
+    //////////////////////////////////////////
+    // java.util.List compatibility methods //
+    //////////////////////////////////////////
+
+    public boolean add( Object o )
+    {
+        push( o );
+        return true;
+    }
+
+    public Object set(int idx, Object o) {
+        checkIndex(idx);
+        
+        int realIdx = getRealIndex(idx);
+        Object old = items[ realIdx ];
+        items[ realIdx ] = o;
+        return old;
+    }
+
+    public void add( int idx, Object o )
+    {
+        if( idx == size )
+        {
+            push( o );
+            return;
+        }
+        
+        checkIndex( idx );
+        ensureCapacity();
+        
+        int realIdx = getRealIndex( idx );
+        
+        // Make a room for a new element.
+        if( first < last )
+        {
+            System.arraycopy( items, realIdx, items, realIdx + 1, last - realIdx );
+        }
+        else
+        {
+            if( realIdx >= first )
+            {
+                System.arraycopy( items, 0, items, 1, last );
+                items[ 0 ] = items[ items.length - 1 ];
+                System.arraycopy( items, realIdx, items, realIdx + 1, items.length - realIdx
- 1 );
+            }
+            else
+            {
+                System.arraycopy( items, realIdx, items, realIdx + 1, last - realIdx );
+            }
+        }
+        
+        items[ realIdx ] = o;
+        increaseSize();
+    }
+
+    public Object remove( int idx )
+    {
+        if( idx == 0 )
+        {
+            return pop();
+        }
+        
+        checkIndex( idx );
+        
+        int realIdx = getRealIndex( idx );
+        Object removed = items[ realIdx ];
+        
+        // Remove a room for the removed element.
+        if( first < last )
+        {
+            System.arraycopy( items, first, items, first + 1, realIdx - first );
+        }
+        else
+        {
+            if( realIdx >= first )
+            {
+                System.arraycopy( items, first, items, first + 1, realIdx - first );
+            }
+            else
+            {
+                System.arraycopy( items, 0, items, 1, realIdx );
+                items[ 0 ] = items[ items.length - 1 ];
+                System.arraycopy( items, first, items, first + 1, items.length - first -
1 );
+            }
+        }
+        
+        items[ first ] = null;
+        decreaseSize();
+
+        return removed;
     }
 }

Modified: directory/network/trunk/src/test/org/apache/mina/util/QueueTest.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/test/org/apache/mina/util/QueueTest.java?rev=326060&r1=326059&r2=326060&view=diff
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/util/QueueTest.java (original)
+++ directory/network/trunk/src/test/org/apache/mina/util/QueueTest.java Tue Oct 18 02:30:48
2005
@@ -18,6 +18,8 @@
  */
 package org.apache.mina.util;
 
+import java.util.Iterator;
+
 import junit.framework.Assert;
 import junit.framework.TestCase;
 
@@ -71,6 +73,162 @@
             q.push( new Integer( ++pushCount ) );
             Assert.assertEquals( ++popCount, ( ( Integer ) q.pop() ).intValue() );
         }
+    }
+    
+    public void testRandomAddOnQueue()
+    {
+        Queue q = new Queue();
+        // Create a queue with 5 elements and capacity 8;
+        for( int i = 0; i < 5; i ++ )
+        {
+            q.push( new Integer( i ) );
+        }
+        
+        q.add( 0, new Integer( 100 ) );
+        q.add( 3, new Integer( 200 ) );
+        q.add( 7, new Integer( 300 ) );
+        
+        Iterator i = q.iterator();
+        Assert.assertEquals( 8, q.size() );
+        Assert.assertEquals( new Integer( 100 ), i.next() );
+        Assert.assertEquals( new Integer( 0 ), i.next() );
+        Assert.assertEquals( new Integer( 1 ), i.next() );
+        Assert.assertEquals( new Integer( 200 ), i.next() );
+        Assert.assertEquals( new Integer( 2 ), i.next() );
+        Assert.assertEquals( new Integer( 3 ), i.next() );
+        Assert.assertEquals( new Integer( 4 ), i.next() );
+        Assert.assertEquals( new Integer( 300 ), i.next() );
+
+        try
+        {
+            i.next();
+            Assert.fail();
+        }
+        catch( Exception e )
+        {
+            // OK
+        }
+    }
+    
+    public void testRandomAddOnRotatedQueue()
+    {
+        Queue q = getRotatedQueue();
+        
+        q.add( 0, new Integer( 100 ) );  // addFirst
+        q.add( 2, new Integer( 200 ) );
+        q.add( 4, new Integer( 300 ) );
+        q.add( 10, new Integer( 400 ) );
+        q.add( 12, new Integer( 500 ) ); // addLast
+        
+        Iterator i = q.iterator();
+        Assert.assertEquals( 13, q.size() );
+        Assert.assertEquals( new Integer( 100 ), i.next() );
+        Assert.assertEquals( new Integer( 0 ), i.next() );
+        Assert.assertEquals( new Integer( 200 ), i.next() );
+        Assert.assertEquals( new Integer( 1 ), i.next() );
+        Assert.assertEquals( new Integer( 300 ), i.next() );
+        Assert.assertEquals( new Integer( 2 ), i.next() );
+        Assert.assertEquals( new Integer( 3 ), i.next() );
+        Assert.assertEquals( new Integer( 4 ), i.next() );
+        Assert.assertEquals( new Integer( 5 ), i.next() );
+        Assert.assertEquals( new Integer( 6 ), i.next() );
+        Assert.assertEquals( new Integer( 400 ), i.next() );
+        Assert.assertEquals( new Integer( 7 ), i.next() );
+        Assert.assertEquals( new Integer( 500 ), i.next() );
+
+        try
+        {
+            i.next();
+            Assert.fail();
+        }
+        catch( Exception e )
+        {
+            // OK
+        }
+    }
+    
+    public void testRandomRemoveOnQueue()
+    {
+        Queue q = new Queue();
+
+        // Create a queue with 5 elements and capacity 8;
+        for( int i = 0; i < 5; i ++ )
+        {
+            q.push( new Integer( i ) );
+        }
+        
+        q.remove( 0 );
+        q.remove( 2 );
+        q.remove( 2 );
+        
+        Iterator i = q.iterator();
+        Assert.assertEquals( 2, q.size() );
+        Assert.assertEquals( new Integer( 1 ), i.next() );
+        Assert.assertEquals( new Integer( 2 ), i.next() );
+
+        try
+        {
+            i.next();
+            Assert.fail();
+        }
+        catch( Exception e )
+        {
+            // OK
+        }
+    }
+    
+    public void testRandomRemoveOnRotatedQueue()
+    {
+        Queue q = getRotatedQueue();
+        
+        q.remove( 0 ); // removeFirst
+        q.remove( 2 ); // removeLast in the first half
+        q.remove( 2 ); // removeFirst in the first half
+        q.remove( 4 ); // removeLast
+        
+        Iterator i = q.iterator();
+        Assert.assertEquals( 4, q.size() );
+        Assert.assertEquals( new Integer( 1 ), i.next() );
+        Assert.assertEquals( new Integer( 2 ), i.next() );
+        Assert.assertEquals( new Integer( 5 ), i.next() );
+        Assert.assertEquals( new Integer( 6 ), i.next() );
+
+        try
+        {
+            i.next();
+            Assert.fail();
+        }
+        catch( Exception e )
+        {
+            // OK
+        }
+    }
+    
+    private Queue getRotatedQueue()
+    {
+        Queue q = new Queue();
+        
+        // Ensure capacity: 16
+        for( int i = 0; i < 16; i ++ )
+        {
+            q.push( new Object() );
+        }
+        q.clear();
+
+        // Rotate it
+        for( int i = 0; i < 12; i ++ )
+        {
+            q.push( new Object() );
+            q.pop();
+        }
+        
+        // Now push items
+        for( int i = 0; i < 8; i ++ )
+        {
+            q.push( new Integer( i ) );
+        }
+        
+        return q;
     }
     
     public static void main( String[] args )



Mime
View raw message