You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2006/05/07 19:33:04 UTC

svn commit: r404804 - in /jakarta/commons/proper/collections/trunk/src: java/org/apache/commons/collections/buffer/BoundedBuffer.java test/org/apache/commons/collections/buffer/TestBoundedBuffer.java

Author: scolebourne
Date: Sun May  7 10:33:02 2006
New Revision: 404804

URL: http://svn.apache.org/viewcvs?rev=404804&view=rev
Log:
Improve ready for release

Modified:
    jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/buffer/BoundedBuffer.java
    jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/buffer/TestBoundedBuffer.java

Modified: jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/buffer/BoundedBuffer.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/buffer/BoundedBuffer.java?rev=404804&r1=404803&r2=404804&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/buffer/BoundedBuffer.java (original)
+++ jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/buffer/BoundedBuffer.java Sun May  7 10:33:02 2006
@@ -45,6 +45,7 @@
 
     /** The serialization version. */
     private static final long serialVersionUID = 1536432911093974264L;
+
     /** The maximum size. */
     private final int maximumSize;
     /** The timeout milliseconds. */
@@ -52,13 +53,17 @@
 
     /**
      * Factory method to create a bounded buffer.
+     * <p>
+     * When the buffer is full, it will immediately throw a
+     * <code>BufferOverflowException</code> on calling <code>add()</code>.
      *
      * @param buffer  the buffer to decorate, must not be null
-     * @param maximumSize  the maximum size
+     * @param maximumSize  the maximum size, must be size one or greater
      * @return a new bounded buffer
      * @throws IllegalArgumentException if the buffer is null
+     * @throws IllegalArgumentException if the maximum size is zero or less
      */
-    public static Buffer decorate(Buffer buffer, int maximumSize) {
+    public static BoundedBuffer decorate(Buffer buffer, int maximumSize) {
         return new BoundedBuffer(buffer, maximumSize, 0L);
     }
 
@@ -67,26 +72,32 @@
      * amount of time.
      *
      * @param buffer  the buffer to decorate, must not be null
-     * @param maximumSize  the maximum size
+     * @param maximumSize  the maximum size, must be size one or greater
      * @param timeout  the maximum amount of time to wait in milliseconds
      * @return a new bounded buffer
      * @throws IllegalArgumentException if the buffer is null
+     * @throws IllegalArgumentException if the maximum size is zero or less
      */
-    public static Buffer decorate(Buffer buffer, int maximumSize, long timeout) {
+    public static BoundedBuffer decorate(Buffer buffer, int maximumSize, long timeout) {
         return new BoundedBuffer(buffer, maximumSize, timeout);
     }
 
     //-----------------------------------------------------------------------
     /**
-     * Constructor that wraps (not copies) another buffer, making it bounded waiting only up to
-     * a maximum amount of time.
-     * @param buffer the buffer to wrap, must not be null
-     * @param maximumSize the maximum size of the buffer
-     * @param timeout the maximum amount of time to wait
+     * Constructor that wraps (not copies) another buffer, making it bounded
+     * waiting only up to a maximum amount of time.
+     *
+     * @param buffer  the buffer to wrap, must not be null
+     * @param maximumSize  the maximum size, must be size one or greater
+     * @param timeout  the maximum amount of time to wait
      * @throws IllegalArgumentException if the buffer is null
+     * @throws IllegalArgumentException if the maximum size is zero or less
      */
-    protected BoundedBuffer( Buffer buffer, int maximumSize, long timeout ) {
-        super( buffer );
+    protected BoundedBuffer(Buffer buffer, int maximumSize, long timeout) {
+        super(buffer);
+        if (maximumSize < 1) {
+            throw new IllegalArgumentException();
+        }
         this.maximumSize = maximumSize;
         this.timeout = timeout;
     }
@@ -119,32 +130,40 @@
     }
 
     private void timeoutWait(final int nAdditions) {
-        synchronized (lock) {
-            if (timeout < 0 && getBuffer().size() + nAdditions > maximumSize) {
-                throw new BufferOverflowException(
+        // method synchronized by callers
+        if (nAdditions > maximumSize) {
+            throw new BufferOverflowException(
                     "Buffer size cannot exceed " + maximumSize);
-            }
-            final long expiration = System.currentTimeMillis() + timeout;
-            long timeLeft = expiration - System.currentTimeMillis();
-            while (timeLeft > 0 && getBuffer().size() + nAdditions > maximumSize) {
-                try {
-                    lock.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 (timeout <= 0) {
+            // no wait period (immediate timeout)
             if (getBuffer().size() + nAdditions > maximumSize) {
-                throw new BufferOverflowException("Timeout expired");
+                throw new BufferOverflowException(
+                        "Buffer size cannot exceed " + maximumSize);
             }
+            return;
+        }
+        final long expiration = System.currentTimeMillis() + timeout;
+        long timeLeft = expiration - System.currentTimeMillis();
+        while (timeLeft > 0 && getBuffer().size() + nAdditions > maximumSize) {
+            try {
+                lock.wait(timeLeft);
+                timeLeft = expiration - System.currentTimeMillis();
+            } catch (InterruptedException ex) {
+                PrintWriter out = new PrintWriter(new StringWriter());
+                ex.printStackTrace(out);
+                throw new BufferUnderflowException(
+                    "Caused by InterruptedException: " + out.toString());
+            }
+        }
+        if (getBuffer().size() + nAdditions > maximumSize) {
+            throw new BufferOverflowException("Timeout expired");
         }
     }
 
     public boolean isFull() {
-        return (collection.size() == maxSize());
+        // size() is synchronized
+        return (size() == maxSize());
     }
 
     public int maxSize() {

Modified: jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/buffer/TestBoundedBuffer.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/buffer/TestBoundedBuffer.java?rev=404804&r1=404803&r2=404804&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/buffer/TestBoundedBuffer.java (original)
+++ jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/buffer/TestBoundedBuffer.java Sun May  7 10:33:02 2006
@@ -66,6 +66,14 @@
         assertEquals(true, bc.isFull());
         bounded.remove();
         assertEquals(false, bc.isFull());
+        try {
+            BoundedBuffer.decorate(new UnboundedFifoBuffer(), 0);
+            fail();
+        } catch (IllegalArgumentException ex) {}
+        try {
+            BoundedBuffer.decorate(new UnboundedFifoBuffer(), -1);
+            fail();
+        } catch (IllegalArgumentException ex) {}
     }
 
     public void testAddToFullBufferNoTimeout() {
@@ -83,6 +91,15 @@
         bounded.add( "Hello" );
         try {
             bounded.addAll(Collections.singleton("World"));
+            fail();
+        } catch (BufferOverflowException e) {
+        }
+    }
+
+    public void testAddAllToEmptyBufferExceedMaxSizeNoTimeout() {
+        final Buffer bounded = BoundedBuffer.decorate(new UnboundedFifoBuffer(), 1);
+        try {
+            bounded.addAll(Collections.nCopies(2, "test"));
             fail();
         } catch (BufferOverflowException e) {
         }



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