Generify, remove getBuffer() - use covariant decorated()

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/branches/collections_jdk5_branch@471579 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2006-11-06 00:14:58 +00:00
parent 52674fa23d
commit 898be70eaa
4 changed files with 52 additions and 52 deletions

View File

@ -24,12 +24,15 @@ import org.apache.commons.collections.collection.AbstractCollectionDecorator;
* <p> * <p>
* Methods are forwarded directly to the decorated buffer. * Methods are forwarded directly to the decorated buffer.
* *
* @param <E> the type of the elements in the buffer
* @since Commons Collections 3.0 * @since Commons Collections 3.0
* @version $Revision$ $Date$ * @version $Revision$ $Date$
* *
* @author Stephen Colebourne * @author Stephen Colebourne
*/ */
public abstract class AbstractBufferDecorator extends AbstractCollectionDecorator implements Buffer { public abstract class AbstractBufferDecorator<E>
extends AbstractCollectionDecorator<E>
implements Buffer<E> {
/** /**
* Constructor only used in deserialization, do not use otherwise. * Constructor only used in deserialization, do not use otherwise.
@ -45,7 +48,7 @@ public abstract class AbstractBufferDecorator extends AbstractCollectionDecorato
* @param buffer the buffer to decorate, must not be null * @param buffer the buffer to decorate, must not be null
* @throws IllegalArgumentException if list is null * @throws IllegalArgumentException if list is null
*/ */
protected AbstractBufferDecorator(Buffer buffer) { protected AbstractBufferDecorator(Buffer<E> buffer) {
super(buffer); super(buffer);
} }
@ -53,27 +56,17 @@ public abstract class AbstractBufferDecorator extends AbstractCollectionDecorato
* Gets the buffer being decorated. * Gets the buffer being decorated.
* *
* @return the decorated buffer * @return the decorated buffer
* @deprecated use decorated()
*/ */
protected Buffer getBuffer() { protected Buffer<E> decorated() {
return decorated(); return (Buffer<E>) super.decorated();
}
/**
* Gets the buffer being decorated.
*
* @return the decorated buffer
*/
protected Buffer decorated() {
return (Buffer) super.decorated();
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
public Object get() { public E get() {
return decorated().get(); return decorated().get();
} }
public Object remove() { public E remove() {
return decorated().remove(); return decorated().remove();
} }

View File

@ -45,10 +45,11 @@ import org.apache.commons.collections.BufferUnderflowException;
* @author Janek Bogucki * @author Janek Bogucki
* @author Phil Steitz * @author Phil Steitz
* @author James Carman * @author James Carman
* @param <E> the type of the elements in the buffer
* @version $Revision$ $Date$ * @version $Revision$ $Date$
* @since Commons Collections 3.0 * @since Commons Collections 3.0
*/ */
public class BlockingBuffer extends SynchronizedBuffer { public class BlockingBuffer<E> extends SynchronizedBuffer<E> {
/** Serialization version. */ /** Serialization version. */
private static final long serialVersionUID = 1719328905017860541L; private static final long serialVersionUID = 1719328905017860541L;
@ -58,25 +59,27 @@ public class BlockingBuffer extends SynchronizedBuffer {
/** /**
* Factory method to create a blocking buffer. * Factory method to create a blocking buffer.
* *
* @param <t> the type of the elements in the 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 * @return a new blocking Buffer
* @throws IllegalArgumentException if buffer is null * @throws IllegalArgumentException if buffer is null
*/ */
public static Buffer decorate(Buffer buffer) { public static <T> Buffer<T> decorate(Buffer<T> buffer) {
return new BlockingBuffer(buffer); return new BlockingBuffer<T>(buffer);
} }
/** /**
* Factory method to create a blocking buffer with a timeout value. * Factory method to create a blocking buffer with a timeout value.
* *
* @param <t> the type of the elements in the buffer
* @param buffer the buffer to decorate, must not be null * @param buffer the buffer to decorate, must not be null
* @param timeoutMillis the timeout value in milliseconds, zero or less for no timeout * @param timeoutMillis the timeout value in milliseconds, zero or less for no timeout
* @return a new blocking buffer * @return a new blocking buffer
* @throws IllegalArgumentException if the buffer is null * @throws IllegalArgumentException if the buffer is null
* @since Commons Collections 3.2 * @since Commons Collections 3.2
*/ */
public static Buffer decorate(Buffer buffer, long timeoutMillis) { public static <T> Buffer<T> decorate(Buffer<T> buffer, long timeoutMillis) {
return new BlockingBuffer(buffer, timeoutMillis); return new BlockingBuffer<T>(buffer, timeoutMillis);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@ -86,7 +89,7 @@ public class BlockingBuffer extends SynchronizedBuffer {
* @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 * @throws IllegalArgumentException if the buffer is null
*/ */
protected BlockingBuffer(Buffer buffer) { protected BlockingBuffer(Buffer<E> buffer) {
super(buffer); super(buffer);
this.timeout = 0; this.timeout = 0;
} }
@ -99,13 +102,13 @@ public class BlockingBuffer extends SynchronizedBuffer {
* @throws IllegalArgumentException if the buffer is null * @throws IllegalArgumentException if the buffer is null
* @since Commons Collections 3.2 * @since Commons Collections 3.2
*/ */
protected BlockingBuffer(Buffer buffer, long timeoutMillis) { protected BlockingBuffer(Buffer<E> buffer, long timeoutMillis) {
super(buffer); super(buffer);
this.timeout = (timeoutMillis < 0 ? 0 : timeoutMillis); this.timeout = (timeoutMillis < 0 ? 0 : timeoutMillis);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
public boolean add(Object o) { public boolean add(E o) {
synchronized (lock) { synchronized (lock) {
boolean result = collection.add(o); boolean result = collection.add(o);
lock.notifyAll(); lock.notifyAll();
@ -113,7 +116,7 @@ public class BlockingBuffer extends SynchronizedBuffer {
} }
} }
public boolean addAll(Collection c) { public boolean addAll(Collection<? extends E> c) {
synchronized (lock) { synchronized (lock) {
boolean result = collection.addAll(c); boolean result = collection.addAll(c);
lock.notifyAll(); lock.notifyAll();
@ -128,7 +131,7 @@ public class BlockingBuffer extends SynchronizedBuffer {
* *
* @throws BufferUnderflowException if an interrupt is received * @throws BufferUnderflowException if an interrupt is received
*/ */
public Object get() { public E get() {
synchronized (lock) { synchronized (lock) {
while (collection.isEmpty()) { while (collection.isEmpty()) {
try { try {
@ -143,7 +146,7 @@ public class BlockingBuffer extends SynchronizedBuffer {
throw new BufferUnderflowException("Caused by InterruptedException: " + out.toString()); throw new BufferUnderflowException("Caused by InterruptedException: " + out.toString());
} }
} }
return getBuffer().get(); return decorated().get();
} }
} }
@ -156,7 +159,7 @@ public class BlockingBuffer extends SynchronizedBuffer {
* @throws BufferUnderflowException if the timeout expires * @throws BufferUnderflowException if the timeout expires
* @since Commons Collections 3.2 * @since Commons Collections 3.2
*/ */
public Object get(final long timeout) { public E get(final long timeout) {
synchronized (lock) { synchronized (lock) {
final long expiration = System.currentTimeMillis() + timeout; final long expiration = System.currentTimeMillis() + timeout;
long timeLeft = expiration - System.currentTimeMillis(); long timeLeft = expiration - System.currentTimeMillis();
@ -173,7 +176,7 @@ public class BlockingBuffer extends SynchronizedBuffer {
if (collection.isEmpty()) { if (collection.isEmpty()) {
throw new BufferUnderflowException("Timeout expired"); throw new BufferUnderflowException("Timeout expired");
} }
return getBuffer().get(); return decorated().get();
} }
} }
@ -184,7 +187,7 @@ public class BlockingBuffer extends SynchronizedBuffer {
* *
* @throws BufferUnderflowException if an interrupt is received * @throws BufferUnderflowException if an interrupt is received
*/ */
public Object remove() { public E remove() {
synchronized (lock) { synchronized (lock) {
while (collection.isEmpty()) { while (collection.isEmpty()) {
try { try {
@ -199,7 +202,7 @@ public class BlockingBuffer extends SynchronizedBuffer {
throw new BufferUnderflowException("Caused by InterruptedException: " + out.toString()); throw new BufferUnderflowException("Caused by InterruptedException: " + out.toString());
} }
} }
return getBuffer().remove(); return decorated().remove();
} }
} }
@ -212,7 +215,7 @@ public class BlockingBuffer extends SynchronizedBuffer {
* @throws BufferUnderflowException if the timeout expires * @throws BufferUnderflowException if the timeout expires
* @since Commons Collections 3.2 * @since Commons Collections 3.2
*/ */
public Object remove(final long timeout) { public E remove(final long timeout) {
synchronized (lock) { synchronized (lock) {
final long expiration = System.currentTimeMillis() + timeout; final long expiration = System.currentTimeMillis() + timeout;
long timeLeft = expiration - System.currentTimeMillis(); long timeLeft = expiration - System.currentTimeMillis();
@ -229,7 +232,7 @@ public class BlockingBuffer extends SynchronizedBuffer {
if (collection.isEmpty()) { if (collection.isEmpty()) {
throw new BufferUnderflowException("Timeout expired"); throw new BufferUnderflowException("Timeout expired");
} }
return getBuffer().remove(); return decorated().remove();
} }
} }

View File

@ -109,7 +109,7 @@ public class BoundedBuffer extends SynchronizedBuffer implements BoundedCollecti
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
public Object remove() { public Object remove() {
synchronized (lock) { synchronized (lock) {
Object returnValue = getBuffer().remove(); Object returnValue = decorated().remove();
lock.notifyAll(); lock.notifyAll();
return returnValue; return returnValue;
} }
@ -118,14 +118,14 @@ public class BoundedBuffer extends SynchronizedBuffer implements BoundedCollecti
public boolean add(Object o) { public boolean add(Object 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 c) {
synchronized (lock) { synchronized (lock) {
timeoutWait(c.size()); timeoutWait(c.size());
return getBuffer().addAll(c); return decorated().addAll(c);
} }
} }
@ -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");
} }
} }

View File

@ -27,12 +27,15 @@ import org.apache.commons.collections.collection.SynchronizedCollection;
* <p> * <p>
* This class is Serializable from Commons Collections 3.1. * This class is Serializable from Commons Collections 3.1.
* *
* @param <E> the type of the elements in the buffer
* @since Commons Collections 3.0 * @since Commons Collections 3.0
* @version $Revision$ $Date$ * @version $Revision$ $Date$
* *
* @author Stephen Colebourne * @author Stephen Colebourne
*/ */
public class SynchronizedBuffer extends SynchronizedCollection implements Buffer { public class SynchronizedBuffer<E>
extends SynchronizedCollection<E>
implements Buffer<E> {
/** Serialization version */ /** Serialization version */
private static final long serialVersionUID = -6859936183953626253L; private static final long serialVersionUID = -6859936183953626253L;
@ -40,14 +43,15 @@ public class SynchronizedBuffer extends SynchronizedCollection implements Buffer
/** /**
* Factory method to create a synchronized buffer. * Factory method to create a synchronized buffer.
* *
* @param <T> the type of the elements in the buffer
* @param buffer the buffer to decorate, must not be null * @param buffer the buffer to decorate, must not be null
* @return a new synchronized Buffer * @return a new synchronized Buffer
* @throws IllegalArgumentException if buffer is null * @throws IllegalArgumentException if buffer is null
*/ */
public static Buffer decorate(Buffer buffer) { public static <T> Buffer<T> decorate(Buffer<T> buffer) {
return new SynchronizedBuffer(buffer); return new SynchronizedBuffer<T>(buffer);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
* Constructor that wraps (not copies). * Constructor that wraps (not copies).
@ -55,7 +59,7 @@ public class SynchronizedBuffer extends SynchronizedCollection implements Buffer
* @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 * @throws IllegalArgumentException if the buffer is null
*/ */
protected SynchronizedBuffer(Buffer buffer) { protected SynchronizedBuffer(Buffer<E> buffer) {
super(buffer); super(buffer);
} }
@ -66,7 +70,7 @@ public class SynchronizedBuffer extends SynchronizedCollection implements Buffer
* @param lock the lock object to use, must not be null * @param lock the lock object to use, must not be null
* @throws IllegalArgumentException if the buffer is null * @throws IllegalArgumentException if the buffer is null
*/ */
protected SynchronizedBuffer(Buffer buffer, Object lock) { protected SynchronizedBuffer(Buffer<E> buffer, Object lock) {
super(buffer, lock); super(buffer, lock);
} }
@ -75,21 +79,21 @@ public class SynchronizedBuffer extends SynchronizedCollection implements Buffer
* *
* @return the decorated buffer * @return the decorated buffer
*/ */
protected Buffer getBuffer() { protected Buffer<E> decorated() {
return (Buffer) collection; return (Buffer<E>) super.decorated();
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
public Object get() { public E get() {
synchronized (lock) { synchronized (lock) {
return getBuffer().get(); return decorated().get();
} }
} }
public Object remove() { public E remove() {
synchronized (lock) { synchronized (lock) {
return getBuffer().remove(); return decorated().remove();
} }
} }
} }