Tidy up formatting for BoundedBuffer and BlockingBuffer
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@348808 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
c67b1d895f
commit
3121915ee8
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2004 The Apache Software Foundation
|
||||
* Copyright 2002-2005 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.
|
||||
|
@ -88,48 +88,57 @@ public class BufferUtils {
|
|||
|
||||
/**
|
||||
* Returns a synchronized buffer backed by the given buffer that will
|
||||
* block on {@link Buffer#get()} and {@link Buffer#remove()} operations until
|
||||
* <code>timeout</code> expires. If the buffer is empty, then the
|
||||
* block on {@link Buffer#get()} and {@link Buffer#remove()} operations
|
||||
* until <code>timeout</code> expires. If the buffer is empty, then the
|
||||
* {@link Buffer#get()} and {@link Buffer#remove()} operations will block
|
||||
* until new elements are added to the buffer, rather than immediately throwing a
|
||||
* <code>BufferUnderflowException</code>.
|
||||
* until new elements are added to the buffer, rather than immediately
|
||||
* throwing a <code>BufferUnderflowException</code>.
|
||||
*
|
||||
* @param buffer the buffer to synchronize, must not be null
|
||||
* @param timeoutMillis the timeout value in milliseconds, zero or less for no timeout
|
||||
* @return a blocking buffer backed by that buffer
|
||||
* @throws IllegalArgumentException if the Buffer is null
|
||||
* @since Commons Collections 3.2
|
||||
*/
|
||||
public static Buffer blockingBuffer(Buffer buffer, long timeout) {
|
||||
return BlockingBuffer.decorate(buffer, timeout);
|
||||
public static Buffer blockingBuffer(Buffer buffer, long timeoutMillis) {
|
||||
return BlockingBuffer.decorate(buffer, timeoutMillis);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a synchronized buffer backed by the given buffer that will block on {@link Buffer#add(Object)} and
|
||||
* {@link Buffer#addAll(java.util.Collection)} until enough object(s) are removed from the buffer to allow
|
||||
* the object(s) to be added and still maintain the maximum size.
|
||||
* @param buffer the buffer to make bounded
|
||||
* @param maximumSize the maximum size
|
||||
* Returns a synchronized buffer backed by the given buffer that will
|
||||
* block on {@link Buffer#add(Object)} and
|
||||
* {@link Buffer#addAll(java.util.Collection)} until enough object(s) are
|
||||
* removed from the buffer to allow the object(s) to be added and still
|
||||
* maintain the maximum size.
|
||||
*
|
||||
* @param buffer the buffer to make bounded, must not be null
|
||||
* @param maximumSize the maximum size
|
||||
* @return a bounded buffer backed by the given buffer
|
||||
* @throws IllegalArgumentException if the given buffer is null
|
||||
* @since Commons Collections 3.2
|
||||
*/
|
||||
public static Buffer boundedBuffer( Buffer buffer, int maximumSize ) {
|
||||
return BoundedBuffer.decorate( buffer, maximumSize );
|
||||
public static Buffer boundedBuffer(Buffer buffer, int maximumSize) {
|
||||
return BoundedBuffer.decorate(buffer, maximumSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a synchronized buffer backed by the given buffer that will block on {@link Buffer#add(Object)} and
|
||||
* {@link Buffer#addAll(java.util.Collection)} until enough object(s) are removed from the buffer to allow
|
||||
* the object(s) to be added and still maintain the maximum size or the timeout expires.
|
||||
* @param buffer the buffer to make bounded
|
||||
* Returns a synchronized buffer backed by the given buffer that will
|
||||
* block on {@link Buffer#add(Object)} and
|
||||
* {@link Buffer#addAll(java.util.Collection)} until enough object(s) are
|
||||
* removed from the buffer to allow the object(s) to be added and still
|
||||
* maintain the maximum size or the timeout expires.
|
||||
*
|
||||
* @param buffer the buffer to make bounded, must not be null
|
||||
* @param maximumSize the maximum size
|
||||
* @param timeout the maximum time to wait
|
||||
* @param timeoutMillis the timeout value in milliseconds, zero or less for no timeout
|
||||
* @return a bounded buffer backed by the given buffer
|
||||
* @throws IllegalArgumentException if the given buffer is null
|
||||
* @since Commons Collections 3.2
|
||||
*/
|
||||
public static Buffer boundedBuffer( Buffer buffer, int maximumSize, long timeout ) {
|
||||
return BoundedBuffer.decorate( buffer, maximumSize, timeout );
|
||||
public static Buffer boundedBuffer(Buffer buffer, int maximumSize, long timeoutMillis) {
|
||||
return BoundedBuffer.decorate(buffer, maximumSize, timeoutMillis);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns an unmodifiable buffer backed by the given buffer.
|
||||
*
|
||||
|
|
|
@ -15,25 +15,30 @@
|
|||
*/
|
||||
package org.apache.commons.collections.buffer;
|
||||
|
||||
import org.apache.commons.collections.Buffer;
|
||||
import org.apache.commons.collections.BufferUnderflowException;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.apache.commons.collections.Buffer;
|
||||
import org.apache.commons.collections.BufferUnderflowException;
|
||||
|
||||
/**
|
||||
* Decorates another <code>Buffer</code> to make {@link #get()} and {@link #remove()} block when the <code>Buffer</code>
|
||||
* is empty.
|
||||
* <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/>
|
||||
* 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 order that they arrive.
|
||||
* <p/>
|
||||
* Decorates another <code>Buffer</code> to make {@link #get()} and
|
||||
* {@link #remove()} block when the <code>Buffer</code> is empty.
|
||||
* <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>
|
||||
* 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
|
||||
* order that they arrive.
|
||||
* <p>
|
||||
* This class is Serializable from Commons Collections 3.1.
|
||||
* This class contains an extra field in 3.2, however the serialization
|
||||
* specification will handle this gracefully.
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
* @author Janek Bogucki
|
||||
|
@ -44,12 +49,10 @@ import java.util.Collection;
|
|||
*/
|
||||
public class BlockingBuffer extends SynchronizedBuffer {
|
||||
|
||||
/**
|
||||
* Serialization version
|
||||
*/
|
||||
/** Serialization version. */
|
||||
private static final long serialVersionUID = 1719328905017860541L;
|
||||
|
||||
private long timeout;
|
||||
/** The timeout value in milliseconds. */
|
||||
private final long timeout;
|
||||
|
||||
/**
|
||||
* Factory method to create a blocking buffer.
|
||||
|
@ -58,83 +61,85 @@ public class BlockingBuffer extends SynchronizedBuffer {
|
|||
* @return a new blocking Buffer
|
||||
* @throws IllegalArgumentException if buffer is null
|
||||
*/
|
||||
public static Buffer decorate( Buffer buffer ) {
|
||||
return new BlockingBuffer( buffer );
|
||||
public static Buffer decorate(Buffer buffer) {
|
||||
return new BlockingBuffer(buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method to create a blocking buffer with a timeout value.
|
||||
*
|
||||
* @param buffer the buffer to decorate, must not be null
|
||||
* @param timeout the maximum amount of time to block
|
||||
* @param timeoutMillis the timeout value in milliseconds, zero or less for no timeout
|
||||
* @return a new blocking buffer
|
||||
* @throws IllegalArgumentException if the buffer is null
|
||||
* @since Commons Collections 3.2
|
||||
*/
|
||||
public static Buffer decorate( Buffer buffer, long timeout ) {
|
||||
return new BlockingBuffer( buffer, timeout );
|
||||
public static Buffer decorate(Buffer buffer, long timeoutMillis) {
|
||||
return new BlockingBuffer(buffer, timeoutMillis);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Constructor that wraps (not copies).
|
||||
*
|
||||
* @param buffer the buffer to decorate, must not be null
|
||||
* @throws IllegalArgumentException if the buffer is null
|
||||
*/
|
||||
protected BlockingBuffer( Buffer buffer ) {
|
||||
super( buffer );
|
||||
protected BlockingBuffer(Buffer buffer) {
|
||||
super(buffer);
|
||||
this.timeout = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor that wraps (not copies).
|
||||
*
|
||||
* @param buffer the buffer to decorate, must not be null
|
||||
* @param timeout the maximum amount of time to block
|
||||
* @param timeoutMillis the timeout value in milliseconds, zero or less for no timeout
|
||||
* @throws IllegalArgumentException if the buffer is null
|
||||
* @since Commons Collections 3.2
|
||||
*/
|
||||
protected BlockingBuffer( Buffer buffer, long timeout ) {
|
||||
super( buffer );
|
||||
this.timeout = timeout < 0 ? 0 : timeout;
|
||||
protected BlockingBuffer(Buffer buffer, long timeoutMillis) {
|
||||
super(buffer);
|
||||
this.timeout = (timeoutMillis < 0 ? 0 : timeoutMillis);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public boolean add( Object o ) {
|
||||
synchronized( lock ) {
|
||||
boolean result = collection.add( o );
|
||||
public boolean add(Object o) {
|
||||
synchronized (lock) {
|
||||
boolean result = collection.add(o);
|
||||
lock.notifyAll();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean addAll( Collection c ) {
|
||||
synchronized( lock ) {
|
||||
boolean result = collection.addAll( c );
|
||||
public boolean addAll(Collection c) {
|
||||
synchronized (lock) {
|
||||
boolean result = collection.addAll(c);
|
||||
lock.notifyAll();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the next value from the buffer, waiting until an object is added if the buffer is empty.
|
||||
* Gets the next value from the buffer, waiting until an object is
|
||||
* added if the buffer is empty. This method uses the default timeout
|
||||
* set in the constructor.
|
||||
*
|
||||
* @throws BufferUnderflowException if an interrupt is received
|
||||
*/
|
||||
public Object get() {
|
||||
synchronized( lock ) {
|
||||
while( collection.isEmpty() ) {
|
||||
synchronized (lock) {
|
||||
while (collection.isEmpty()) {
|
||||
try {
|
||||
if( timeout <= 0 ) {
|
||||
if (timeout <= 0) {
|
||||
lock.wait();
|
||||
} else {
|
||||
return get(timeout);
|
||||
}
|
||||
else {
|
||||
return get( timeout );
|
||||
}
|
||||
}
|
||||
catch( InterruptedException e ) {
|
||||
PrintWriter out = new PrintWriter( new StringWriter() );
|
||||
e.printStackTrace( out );
|
||||
throw new BufferUnderflowException( "Caused by InterruptedException: " + out.toString() );
|
||||
} catch (InterruptedException e) {
|
||||
PrintWriter out = new PrintWriter(new StringWriter());
|
||||
e.printStackTrace(out);
|
||||
throw new BufferUnderflowException("Caused by InterruptedException: " + out.toString());
|
||||
}
|
||||
}
|
||||
return getBuffer().get();
|
||||
|
@ -142,56 +147,55 @@ public class BlockingBuffer extends SynchronizedBuffer {
|
|||
}
|
||||
|
||||
/**
|
||||
* Gets the next value from the buffer, waiting until an object is added for up to the specified timeout value if
|
||||
* the buffer is empty.
|
||||
* Gets the next value from the buffer, waiting until an object is
|
||||
* added for up to the specified timeout value if the buffer is empty.
|
||||
*
|
||||
* @param timeout the timeout value in milliseconds
|
||||
* @param timeout the timeout value in milliseconds
|
||||
* @throws BufferUnderflowException if an interrupt is received
|
||||
* @throws BufferUnderflowException if the timeout expires
|
||||
* @since Commons Collections 3.2
|
||||
*/
|
||||
public Object get( final long timeout ) {
|
||||
synchronized( lock ) {
|
||||
public Object get(final long timeout) {
|
||||
synchronized (lock) {
|
||||
final long expiration = System.currentTimeMillis() + timeout;
|
||||
long timeLeft = expiration - System.currentTimeMillis();
|
||||
while( timeLeft > 0 && collection.isEmpty() ) {
|
||||
while (timeLeft > 0 && collection.isEmpty()) {
|
||||
try {
|
||||
lock.wait( timeLeft );
|
||||
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() );
|
||||
} 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." );
|
||||
if (collection.isEmpty()) {
|
||||
throw new BufferUnderflowException("Timeout expired");
|
||||
}
|
||||
return getBuffer().get();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the next value from the buffer, waiting until an object is added if the buffer is empty.
|
||||
* Removes the next value from the buffer, waiting until an object is
|
||||
* added if the buffer is empty. This method uses the default timeout
|
||||
* set in the constructor.
|
||||
*
|
||||
* @throws BufferUnderflowException if an interrupt is received
|
||||
*/
|
||||
public Object remove() {
|
||||
synchronized( lock ) {
|
||||
while( collection.isEmpty() ) {
|
||||
synchronized (lock) {
|
||||
while (collection.isEmpty()) {
|
||||
try {
|
||||
if( timeout <= 0 ) {
|
||||
if (timeout <= 0) {
|
||||
lock.wait();
|
||||
} else {
|
||||
return remove(timeout);
|
||||
}
|
||||
else {
|
||||
return remove( timeout );
|
||||
}
|
||||
}
|
||||
catch( InterruptedException e ) {
|
||||
PrintWriter out = new PrintWriter( new StringWriter() );
|
||||
e.printStackTrace( out );
|
||||
throw new BufferUnderflowException( "Caused by InterruptedException: " + out.toString() );
|
||||
} catch (InterruptedException e) {
|
||||
PrintWriter out = new PrintWriter(new StringWriter());
|
||||
e.printStackTrace(out);
|
||||
throw new BufferUnderflowException("Caused by InterruptedException: " + out.toString());
|
||||
}
|
||||
}
|
||||
return getBuffer().remove();
|
||||
|
@ -199,33 +203,33 @@ public class BlockingBuffer extends SynchronizedBuffer {
|
|||
}
|
||||
|
||||
/**
|
||||
* Removes the next value from the buffer, waiting until an object is added for up to the specified timeout value if
|
||||
* the buffer is empty.
|
||||
* Removes the next value from the buffer, waiting until an object is
|
||||
* added for up to the specified timeout value if the buffer is empty.
|
||||
*
|
||||
* @param timeout the timeout value in milliseconds
|
||||
* @param timeout the timeout value in milliseconds
|
||||
* @throws BufferUnderflowException if an interrupt is received
|
||||
* @throws BufferUnderflowException if the timeout expires
|
||||
* @since Commons Collections 3.2
|
||||
*/
|
||||
public Object remove( final long timeout ) {
|
||||
synchronized( lock ) {
|
||||
public Object remove(final long timeout) {
|
||||
synchronized (lock) {
|
||||
final long expiration = System.currentTimeMillis() + timeout;
|
||||
long timeLeft = expiration - System.currentTimeMillis();
|
||||
while( timeLeft > 0 && collection.isEmpty() ) {
|
||||
while (timeLeft > 0 && collection.isEmpty()) {
|
||||
try {
|
||||
lock.wait( timeLeft );
|
||||
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() );
|
||||
} 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." );
|
||||
if (collection.isEmpty()) {
|
||||
throw new BufferUnderflowException("Timeout expired");
|
||||
}
|
||||
return getBuffer().remove();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2001-2005 The Apache Software Foundation
|
||||
* Copyright 2005 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.
|
||||
|
@ -18,6 +18,7 @@ package org.apache.commons.collections.buffer;
|
|||
import org.apache.commons.collections.Buffer;
|
||||
import org.apache.commons.collections.BufferOverflowException;
|
||||
import org.apache.commons.collections.BufferUnderflowException;
|
||||
import org.apache.commons.collections.iterators.AbstractIteratorDecorator;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
|
@ -26,50 +27,47 @@ import java.util.Iterator;
|
|||
|
||||
/**
|
||||
* A wrapper class for buffers which makes them bounded.
|
||||
*
|
||||
* @author James Carman
|
||||
* @since 3.2
|
||||
* @version $Revision: $ $Date: $
|
||||
* @since Commons Collections 3.2
|
||||
*/
|
||||
public class BoundedBuffer extends SynchronizedBuffer {
|
||||
|
||||
/** The serialization version. */
|
||||
private static final long serialVersionUID = 1536432911093974264L;
|
||||
|
||||
/** The maximum size. */
|
||||
private final int maximumSize;
|
||||
/** The timeout milliseconds. */
|
||||
private final long timeout;
|
||||
|
||||
/**
|
||||
* Factory method to create a bounded buffer.
|
||||
* @param buffer the buffer to decorate, must not be null
|
||||
* @param maximumSize the maximum size
|
||||
*
|
||||
* @param buffer the buffer to decorate, must not be null
|
||||
* @param maximumSize the maximum size
|
||||
* @return a new bounded buffer
|
||||
* @throws IllegalArgumentException if the buffer is null
|
||||
*/
|
||||
public static Buffer decorate( Buffer buffer, int maximumSize ) {
|
||||
return new BoundedBuffer( buffer, maximumSize );
|
||||
public static Buffer decorate(Buffer buffer, int maximumSize) {
|
||||
return new BoundedBuffer(buffer, maximumSize, 0L);
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method to create a bounded buffer that blocks for a maximum
|
||||
* amount of time.
|
||||
* @param buffer the buffer to decorate, must not be null
|
||||
* @param maximumSize the maximum size
|
||||
* @param timeout the maximum amount of time to wait.
|
||||
*
|
||||
* @param buffer the buffer to decorate, must not be null
|
||||
* @param maximumSize the maximum size
|
||||
* @param timeout the maximum amount of time to wait in milliseconds
|
||||
* @return a new bounded buffer
|
||||
* @throws IllegalArgumentException if the buffer is null
|
||||
*/
|
||||
public static Buffer decorate( Buffer buffer, int maximumSize, long timeout ) {
|
||||
return new BoundedBuffer( buffer, maximumSize, timeout );
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor that wraps (not copies) another buffer, making it bounded.
|
||||
* @param buffer the buffer to wrap, must not be null
|
||||
* @param maximumSize the maximum size of the buffer
|
||||
* @throws IllegalArgumentException if the buffer is null
|
||||
*/
|
||||
protected BoundedBuffer( Buffer buffer, int maximumSize ) {
|
||||
this( buffer, maximumSize, -1 );
|
||||
public static Buffer 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.
|
||||
|
@ -84,78 +82,73 @@ public class BoundedBuffer extends SynchronizedBuffer {
|
|||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public Object remove() {
|
||||
synchronized( lock ) {
|
||||
synchronized (lock) {
|
||||
Object returnValue = getBuffer().remove();
|
||||
lock.notifyAll();
|
||||
return returnValue;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean add( Object o ) {
|
||||
synchronized( lock ) {
|
||||
timeoutWait( 1 );
|
||||
return getBuffer().add( o );
|
||||
public boolean add(Object o) {
|
||||
synchronized (lock) {
|
||||
timeoutWait(1);
|
||||
return getBuffer().add(o);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean addAll( final Collection c ) {
|
||||
synchronized( lock ) {
|
||||
timeoutWait( c.size() );
|
||||
return getBuffer().addAll( c );
|
||||
public boolean addAll(final Collection c) {
|
||||
synchronized (lock) {
|
||||
timeoutWait(c.size());
|
||||
return getBuffer().addAll(c);
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator iterator() {
|
||||
return new NotifyingIterator( collection.iterator() );
|
||||
return new NotifyingIterator(collection.iterator());
|
||||
}
|
||||
|
||||
private void timeoutWait( final int nAdditions ) {
|
||||
synchronized( lock ) {
|
||||
if( timeout < 0 && getBuffer().size() + nAdditions > maximumSize ) {
|
||||
throw new BufferOverflowException( "Buffer size cannot exceed " + maximumSize + "." );
|
||||
private void timeoutWait(final int nAdditions) {
|
||||
synchronized (lock) {
|
||||
if (timeout < 0 && getBuffer().size() + 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 ) {
|
||||
while (timeLeft > 0 && getBuffer().size() + nAdditions > maximumSize) {
|
||||
try {
|
||||
lock.wait( timeLeft );
|
||||
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() );
|
||||
} catch (InterruptedException e) {
|
||||
PrintWriter out = new PrintWriter(new StringWriter());
|
||||
e.printStackTrace(out);
|
||||
throw new BufferUnderflowException(
|
||||
"Caused by InterruptedException: " + out.toString());
|
||||
}
|
||||
}
|
||||
if( getBuffer().size() + nAdditions > maximumSize ) {
|
||||
throw new BufferOverflowException( "Timeout expired." );
|
||||
if (getBuffer().size() + nAdditions > maximumSize) {
|
||||
throw new BufferOverflowException("Timeout expired");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class NotifyingIterator implements Iterator {
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* BoundedBuffer iterator.
|
||||
*/
|
||||
private class NotifyingIterator extends AbstractIteratorDecorator {
|
||||
|
||||
private final Iterator i;
|
||||
|
||||
public NotifyingIterator( Iterator i ) {
|
||||
this.i = i;
|
||||
public NotifyingIterator(Iterator it) {
|
||||
super(it);
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
synchronized( lock ) {
|
||||
i.remove();
|
||||
synchronized (lock) {
|
||||
iterator.remove();
|
||||
lock.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return i.hasNext();
|
||||
}
|
||||
|
||||
public Object next() {
|
||||
return i.next();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2001-2005 The Apache Software Foundation
|
||||
* Copyright 2005 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.
|
||||
|
@ -25,8 +25,8 @@ import java.util.Arrays;
|
|||
|
||||
public class TestBoundedBuffer extends AbstractTestObject {
|
||||
|
||||
public TestBoundedBuffer( String testName ) {
|
||||
super( testName );
|
||||
public TestBoundedBuffer(String testName) {
|
||||
super(testName);
|
||||
}
|
||||
|
||||
public String getCompatibilityVersion() {
|
||||
|
@ -38,33 +38,31 @@ public class TestBoundedBuffer extends AbstractTestObject {
|
|||
}
|
||||
|
||||
public Object makeObject() {
|
||||
return BoundedBuffer.decorate( new UnboundedFifoBuffer(), 1 );
|
||||
return BoundedBuffer.decorate(new UnboundedFifoBuffer(), 1);
|
||||
}
|
||||
|
||||
public void testAddToFullBufferNoTimeout() {
|
||||
final Buffer bounded = BoundedBuffer.decorate( new UnboundedFifoBuffer(), 1 );
|
||||
final Buffer bounded = BoundedBuffer.decorate(new UnboundedFifoBuffer(), 1);
|
||||
bounded.add( "Hello" );
|
||||
try {
|
||||
bounded.add( "World" );
|
||||
bounded.add("World");
|
||||
fail();
|
||||
}
|
||||
catch( BufferOverflowException e ) {
|
||||
} catch (BufferOverflowException e) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testAddAllToFullBufferNoTimeout() {
|
||||
final Buffer bounded = BoundedBuffer.decorate( new UnboundedFifoBuffer(), 1 );
|
||||
final Buffer bounded = BoundedBuffer.decorate(new UnboundedFifoBuffer(), 1);
|
||||
bounded.add( "Hello" );
|
||||
try {
|
||||
bounded.addAll( Collections.singleton( "World" ) );
|
||||
bounded.addAll(Collections.singleton("World"));
|
||||
fail();
|
||||
}
|
||||
catch( BufferOverflowException e ) {
|
||||
} catch (BufferOverflowException e) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testAddToFullBufferRemoveViaIterator() {
|
||||
final Buffer bounded = BoundedBuffer.decorate( new UnboundedFifoBuffer(), 1, 500 );
|
||||
final Buffer bounded = BoundedBuffer.decorate(new UnboundedFifoBuffer(), 1, 500);
|
||||
bounded.add( "Hello" );
|
||||
new DelayedIteratorRemove( bounded, 200 ).start();
|
||||
bounded.add( "World" );
|
||||
|
@ -74,7 +72,7 @@ public class TestBoundedBuffer extends AbstractTestObject {
|
|||
}
|
||||
|
||||
public void testAddAllToFullBufferRemoveViaIterator() {
|
||||
final Buffer bounded = BoundedBuffer.decorate( new UnboundedFifoBuffer(), 2, 500 );
|
||||
final Buffer bounded = BoundedBuffer.decorate(new UnboundedFifoBuffer(), 2, 500);
|
||||
bounded.add( "Hello" );
|
||||
bounded.add( "World" );
|
||||
new DelayedIteratorRemove( bounded, 200, 2 ).start();
|
||||
|
@ -85,7 +83,7 @@ public class TestBoundedBuffer extends AbstractTestObject {
|
|||
}
|
||||
|
||||
public void testAddToFullBufferWithTimeout() {
|
||||
final Buffer bounded = BoundedBuffer.decorate( new UnboundedFifoBuffer(), 1, 500 );
|
||||
final Buffer bounded = BoundedBuffer.decorate(new UnboundedFifoBuffer(), 1, 500);
|
||||
bounded.add( "Hello" );
|
||||
new DelayedRemove( bounded, 200 ).start();
|
||||
bounded.add( "World" );
|
||||
|
@ -100,7 +98,7 @@ public class TestBoundedBuffer extends AbstractTestObject {
|
|||
}
|
||||
|
||||
public void testAddAllToFullBufferWithTimeout() {
|
||||
final Buffer bounded = BoundedBuffer.decorate( new UnboundedFifoBuffer(), 2, 500 );
|
||||
final Buffer bounded = BoundedBuffer.decorate(new UnboundedFifoBuffer(), 2, 500);
|
||||
bounded.add( "Hello" );
|
||||
bounded.add( "World" );
|
||||
new DelayedRemove( bounded, 200, 2 ).start();
|
||||
|
@ -124,27 +122,26 @@ public class TestBoundedBuffer extends AbstractTestObject {
|
|||
|
||||
private final int nToRemove;
|
||||
|
||||
public DelayedIteratorRemove( Buffer buffer, long delay, int nToRemove ) {
|
||||
public DelayedIteratorRemove(Buffer buffer, long delay, int nToRemove) {
|
||||
this.buffer = buffer;
|
||||
this.delay = delay;
|
||||
this.nToRemove = nToRemove;
|
||||
}
|
||||
|
||||
public DelayedIteratorRemove( Buffer buffer, long delay ) {
|
||||
this( buffer, delay, 1 );
|
||||
public DelayedIteratorRemove(Buffer buffer, long delay) {
|
||||
this(buffer, delay, 1);
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep( delay );
|
||||
Thread.sleep(delay);
|
||||
Iterator iter = buffer.iterator();
|
||||
for( int i = 0; i < nToRemove; ++i ) {
|
||||
for (int i = 0; i < nToRemove; ++i) {
|
||||
iter.next();
|
||||
iter.remove();
|
||||
}
|
||||
|
||||
}
|
||||
catch( InterruptedException e ) {
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -157,24 +154,23 @@ public class TestBoundedBuffer extends AbstractTestObject {
|
|||
|
||||
private final int nToRemove;
|
||||
|
||||
public DelayedRemove( Buffer buffer, long delay, int nToRemove ) {
|
||||
public DelayedRemove(Buffer buffer, long delay, int nToRemove) {
|
||||
this.buffer = buffer;
|
||||
this.delay = delay;
|
||||
this.nToRemove = nToRemove;
|
||||
}
|
||||
|
||||
public DelayedRemove( Buffer buffer, long delay ) {
|
||||
this( buffer, delay, 1 );
|
||||
public DelayedRemove(Buffer buffer, long delay) {
|
||||
this(buffer, delay, 1);
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep( delay );
|
||||
for( int i = 0; i < nToRemove; ++i ) {
|
||||
Thread.sleep(delay);
|
||||
for (int i = 0; i < nToRemove; ++i) {
|
||||
buffer.remove();
|
||||
}
|
||||
}
|
||||
catch( InterruptedException e ) {
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue