Merging from -r468106:814127 of collections_jdk5_branch - namely where this code was generified; mostly in r738956.

Also see the following revisions:

    ------------------------------------------------------------------------
    r471579 | scolebourne | 2006-11-05 16:14:58 -0800 (Sun, 05 Nov 2006) | 1 line
    
    Generify, remove getBuffer() - use covariant decorated()
    ------------------------------------------------------------------------


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@815029 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2009-09-15 05:54:29 +00:00
parent 94fa451d95
commit c02d4dfcbc
1 changed files with 18 additions and 18 deletions

View File

@ -45,7 +45,7 @@ import org.apache.commons.collections.iterators.AbstractIteratorDecorator;
* @version $Revision$ $Date$ * @version $Revision$ $Date$
* @since Commons Collections 3.2 * @since Commons Collections 3.2
*/ */
public class BoundedBuffer extends SynchronizedBuffer implements BoundedCollection { public class BoundedBuffer<E> extends SynchronizedBuffer<E> implements BoundedCollection<E> {
/** The serialization version. */ /** The serialization version. */
private static final long serialVersionUID = 1536432911093974264L; private static final long serialVersionUID = 1536432911093974264L;
@ -67,8 +67,8 @@ public class BoundedBuffer extends SynchronizedBuffer implements BoundedCollecti
* @throws IllegalArgumentException if the buffer is null * @throws IllegalArgumentException if the buffer is null
* @throws IllegalArgumentException if the maximum size is zero or less * @throws IllegalArgumentException if the maximum size is zero or less
*/ */
public static BoundedBuffer decorate(Buffer buffer, int maximumSize) { public static <E> BoundedBuffer<E> decorate(Buffer<E> buffer, int maximumSize) {
return new BoundedBuffer(buffer, maximumSize, 0L); return new BoundedBuffer<E>(buffer, maximumSize, 0L);
} }
/** /**
@ -82,8 +82,8 @@ public class BoundedBuffer extends SynchronizedBuffer implements BoundedCollecti
* @throws IllegalArgumentException if the buffer is null * @throws IllegalArgumentException if the buffer is null
* @throws IllegalArgumentException if the maximum size is zero or less * @throws IllegalArgumentException if the maximum size is zero or less
*/ */
public static BoundedBuffer decorate(Buffer buffer, int maximumSize, long timeout) { public static <E> BoundedBuffer<E> decorate(Buffer<E> buffer, int maximumSize, long timeout) {
return new BoundedBuffer(buffer, maximumSize, timeout); return new BoundedBuffer<E>(buffer, maximumSize, timeout);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@ -97,7 +97,7 @@ public class BoundedBuffer extends SynchronizedBuffer implements BoundedCollecti
* @throws IllegalArgumentException if the buffer is null * @throws IllegalArgumentException if the buffer is null
* @throws IllegalArgumentException if the maximum size is zero or less * @throws IllegalArgumentException if the maximum size is zero or less
*/ */
protected BoundedBuffer(Buffer buffer, int maximumSize, long timeout) { protected BoundedBuffer(Buffer<E> buffer, int maximumSize, long timeout) {
super(buffer); super(buffer);
if (maximumSize < 1) { if (maximumSize < 1) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
@ -107,29 +107,29 @@ public class BoundedBuffer extends SynchronizedBuffer implements BoundedCollecti
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
public Object remove() { public E remove() {
synchronized (lock) { synchronized (lock) {
Object returnValue = getBuffer().remove(); E returnValue = decorated().remove();
lock.notifyAll(); lock.notifyAll();
return returnValue; return returnValue;
} }
} }
public boolean add(Object o) { public boolean add(E o) {
synchronized (lock) { synchronized (lock) {
timeoutWait(1); timeoutWait(1);
return getBuffer().add(o); return decorated().add(o);
} }
} }
public boolean addAll(final Collection c) { public boolean addAll(final Collection<? extends E> c) {
synchronized (lock) { synchronized (lock) {
timeoutWait(c.size()); timeoutWait(c.size());
return getBuffer().addAll(c); return decorated().addAll(c);
} }
} }
public Iterator iterator() { public Iterator<E> iterator() {
return new NotifyingIterator(collection.iterator()); return new NotifyingIterator(collection.iterator());
} }
@ -141,7 +141,7 @@ public class BoundedBuffer extends SynchronizedBuffer implements BoundedCollecti
} }
if (timeout <= 0) { if (timeout <= 0) {
// no wait period (immediate timeout) // no wait period (immediate timeout)
if (getBuffer().size() + nAdditions > maximumSize) { if (decorated().size() + nAdditions > maximumSize) {
throw new BufferOverflowException( throw new BufferOverflowException(
"Buffer size cannot exceed " + maximumSize); "Buffer size cannot exceed " + maximumSize);
} }
@ -149,7 +149,7 @@ public class BoundedBuffer extends SynchronizedBuffer implements BoundedCollecti
} }
final long expiration = System.currentTimeMillis() + timeout; final long expiration = System.currentTimeMillis() + timeout;
long timeLeft = expiration - System.currentTimeMillis(); long timeLeft = expiration - System.currentTimeMillis();
while (timeLeft > 0 && getBuffer().size() + nAdditions > maximumSize) { while (timeLeft > 0 && decorated().size() + nAdditions > maximumSize) {
try { try {
lock.wait(timeLeft); lock.wait(timeLeft);
timeLeft = expiration - System.currentTimeMillis(); timeLeft = expiration - System.currentTimeMillis();
@ -160,7 +160,7 @@ public class BoundedBuffer extends SynchronizedBuffer implements BoundedCollecti
"Caused by InterruptedException: " + out.toString()); "Caused by InterruptedException: " + out.toString());
} }
} }
if (getBuffer().size() + nAdditions > maximumSize) { if (decorated().size() + nAdditions > maximumSize) {
throw new BufferOverflowException("Timeout expired"); throw new BufferOverflowException("Timeout expired");
} }
} }
@ -178,9 +178,9 @@ public class BoundedBuffer extends SynchronizedBuffer implements BoundedCollecti
/** /**
* BoundedBuffer iterator. * BoundedBuffer iterator.
*/ */
private class NotifyingIterator extends AbstractIteratorDecorator { private class NotifyingIterator extends AbstractIteratorDecorator<E> {
public NotifyingIterator(Iterator it) { public NotifyingIterator(Iterator<E> it) {
super(it); super(it);
} }