commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jcar...@apache.org
Subject svn commit: r170761 - in /jakarta/commons/proper/collections/trunk: ./ src/java/org/apache/commons/collections/buffer/BlockingBuffer.java src/test/org/apache/commons/collections/buffer/TestBlockingBuffer.java
Date Wed, 18 May 2005 15:03:03 GMT
Author: jcarman
Date: Wed May 18 08:03:02 2005
New Revision: 170761

URL: http://svn.apache.org/viewcvs?rev=170761&view=rev
Log:
27691: Adding timeout versions of get() and remove()

Modified:
    jakarta/commons/proper/collections/trunk/   (props changed)
    jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/buffer/BlockingBuffer.java
    jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/buffer/TestBlockingBuffer.java

Propchange: jakarta/commons/proper/collections/trunk/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Wed May 18 08:03:02 2005
@@ -17,3 +17,4 @@
 HEAD.xml
 clirr*.txt
 commons-collections*.jar*
+collections.iml

Modified: jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/buffer/BlockingBuffer.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/buffer/BlockingBuffer.java?rev=170761&r1=170760&r2=170761&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/buffer/BlockingBuffer.java
(original)
+++ jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/buffer/BlockingBuffer.java
Wed May 18 08:03:02 2005
@@ -25,35 +25,35 @@
 /**
  * Decorates another <code>Buffer</code> to make {@link #get()} and
  * {@link #remove()} block when the <code>Buffer</code> is empty.
- * <p>
+ * <p/>
  * If either <code>get</code> or <code>remove</code> is called on
an empty
  * <code>Buffer</code>, the calling thread waits for notification that
  * an <code>add</code> or <code>addAll</code> operation has completed.
- * <p>
+ * <p/>
  * When one or more entries are added to an empty <code>Buffer</code>,
  * all threads blocked in <code>get</code> or <code>remove</code>
are notified.
- * There is no guarantee that concurrent blocked <code>get</code> or 
- * <code>remove</code> requests will be "unblocked" and receive data in the 
+ * There is no guarantee that concurrent blocked <code>get</code> or
+ * <code>remove</code> requests will be "unblocked" and receive data in the
  * order that they arrive.
- * <p>
+ * <p/>
  * This class is Serializable from Commons Collections 3.1.
  *
- * @since Commons Collections 3.0
- * @version $Revision$ $Date$
- * 
  * @author Stephen Colebourne
  * @author Janek Bogucki
  * @author Phil Steitz
+ * @version $Revision$ $Date$
+ * @since Commons Collections 3.0
  */
 public class BlockingBuffer extends SynchronizedBuffer {
-
-    /** Serialization version */
+    /**
+     * Serialization version
+     */
     private static final long serialVersionUID = 1719328905017860541L;
 
     /**
      * Factory method to create a blocking buffer.
-     * 
-     * @param buffer  the buffer to decorate, must not be null
+     *
+     * @param buffer the buffer to decorate, must not be null
      * @return a new blocking Buffer
      * @throws IllegalArgumentException if buffer is null
      */
@@ -64,8 +64,8 @@
     //-----------------------------------------------------------------------    
     /**
      * Constructor that wraps (not copies).
-     * 
-     * @param buffer  the buffer to decorate, must not be null
+     *
+     * @param buffer the buffer to decorate, must not be null
      * @throws IllegalArgumentException if the buffer is null
      */
     protected BlockingBuffer(Buffer buffer) {
@@ -74,7 +74,7 @@
 
     //-----------------------------------------------------------------------
     public boolean add(Object o) {
-        synchronized (lock) {
+        synchronized(lock) {
             boolean result = collection.add(o);
             notifyAll();
             return result;
@@ -82,7 +82,7 @@
     }
 
     public boolean addAll(Collection c) {
-        synchronized (lock) {
+        synchronized(lock) {
             boolean result = collection.addAll(c);
             notifyAll();
             return result;
@@ -90,26 +90,50 @@
     }
 
     public Object get() {
-        synchronized (lock) {
-            while (collection.isEmpty()) {
+        synchronized(lock) {
+            while(collection.isEmpty()) {
                 try {
                     wait();
-                } catch (InterruptedException e) {
+                }
+                catch(InterruptedException e) {
+                    PrintWriter out = new PrintWriter(new StringWriter());
+                    e.printStackTrace(out);
+                    throw new BufferUnderflowException("Caused by InterruptedException: "
+ out.toString());
+                }
+            }
+            return getBuffer().get();
+        }
+    }
+
+    public Object get(final long timeout) {
+        synchronized(lock) {
+            final long expiration = System.currentTimeMillis() + timeout;
+            long timeLeft = expiration - System.currentTimeMillis();
+            while(timeLeft > 0 && collection.isEmpty()) {
+                try {
+                    wait(timeLeft);
+                    timeLeft = expiration - System.currentTimeMillis();
+                }
+                catch(InterruptedException e) {
                     PrintWriter out = new PrintWriter(new StringWriter());
                     e.printStackTrace(out);
                     throw new BufferUnderflowException("Caused by InterruptedException: "
+ out.toString());
                 }
             }
+            if(collection.isEmpty()) {
+                throw new BufferUnderflowException("Timeout expired.");
+            }
             return getBuffer().get();
         }
     }
 
     public Object remove() {
-        synchronized (lock) {
-            while (collection.isEmpty()) {
+        synchronized(lock) {
+            while(collection.isEmpty()) {
                 try {
                     wait();
-                } catch (InterruptedException e) {
+                }
+                catch(InterruptedException e) {
                     PrintWriter out = new PrintWriter(new StringWriter());
                     e.printStackTrace(out);
                     throw new BufferUnderflowException("Caused by InterruptedException: "
+ out.toString());
@@ -119,4 +143,25 @@
         }
     }
 
+    public Object remove(final long timeout) {
+        synchronized(lock) {
+            final long expiration = System.currentTimeMillis() + timeout;
+            long timeLeft = expiration - System.currentTimeMillis();
+            while(timeLeft > 0 && collection.isEmpty()) {
+                try {
+                    wait(timeLeft);
+                    timeLeft = expiration - System.currentTimeMillis();
+                }
+                catch(InterruptedException e) {
+                    PrintWriter out = new PrintWriter(new StringWriter());
+                    e.printStackTrace(out);
+                    throw new BufferUnderflowException("Caused by InterruptedException: "
+ out.toString());
+                }
+            }
+            if(collection.isEmpty()) {
+                throw new BufferUnderflowException("Timeout expired.");
+            }
+            return getBuffer().remove();
+        }
+    }
 }

Modified: jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/buffer/TestBlockingBuffer.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/buffer/TestBlockingBuffer.java?rev=170761&r1=170760&r2=170761&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/buffer/TestBlockingBuffer.java
(original)
+++ jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/buffer/TestBlockingBuffer.java
Wed May 18 08:03:02 2005
@@ -364,7 +364,26 @@
         }
         
     }
-    
+
+    public void testTimeoutGet() {
+        final BlockingBuffer buffer = new BlockingBuffer(new MyBuffer());
+        try {
+            buffer.get( 100 );
+            fail( "Get should have timed out." );
+        }
+        catch( BufferUnderflowException e ){
+        }
+    }
+
+    public void testTimeoutRemove() {
+        final BlockingBuffer buffer = new BlockingBuffer(new MyBuffer());
+        try {
+            buffer.remove( 100 );
+            fail( "Get should have timed out." );
+        }
+        catch( BufferUnderflowException e ){
+        }
+    }
     protected static class DelayedAdd extends Thread {
 
         Buffer buffer;



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message