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:
parent
52674fa23d
commit
898be70eaa
|
@ -24,12 +24,15 @@ import org.apache.commons.collections.collection.AbstractCollectionDecorator;
|
|||
* <p>
|
||||
* Methods are forwarded directly to the decorated buffer.
|
||||
*
|
||||
* @param <E> the type of the elements in the buffer
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision$ $Date$
|
||||
*
|
||||
* @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.
|
||||
|
@ -45,7 +48,7 @@ public abstract class AbstractBufferDecorator extends AbstractCollectionDecorato
|
|||
* @param buffer the buffer to decorate, must not be null
|
||||
* @throws IllegalArgumentException if list is null
|
||||
*/
|
||||
protected AbstractBufferDecorator(Buffer buffer) {
|
||||
protected AbstractBufferDecorator(Buffer<E> buffer) {
|
||||
super(buffer);
|
||||
}
|
||||
|
||||
|
@ -53,27 +56,17 @@ public abstract class AbstractBufferDecorator extends AbstractCollectionDecorato
|
|||
* Gets the buffer being decorated.
|
||||
*
|
||||
* @return the decorated buffer
|
||||
* @deprecated use decorated()
|
||||
*/
|
||||
protected Buffer getBuffer() {
|
||||
return decorated();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the buffer being decorated.
|
||||
*
|
||||
* @return the decorated buffer
|
||||
*/
|
||||
protected Buffer decorated() {
|
||||
return (Buffer) super.decorated();
|
||||
protected Buffer<E> decorated() {
|
||||
return (Buffer<E>) super.decorated();
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public Object get() {
|
||||
public E get() {
|
||||
return decorated().get();
|
||||
}
|
||||
|
||||
public Object remove() {
|
||||
public E remove() {
|
||||
return decorated().remove();
|
||||
}
|
||||
|
||||
|
|
|
@ -45,10 +45,11 @@ import org.apache.commons.collections.BufferUnderflowException;
|
|||
* @author Janek Bogucki
|
||||
* @author Phil Steitz
|
||||
* @author James Carman
|
||||
* @param <E> the type of the elements in the buffer
|
||||
* @version $Revision$ $Date$
|
||||
* @since Commons Collections 3.0
|
||||
*/
|
||||
public class BlockingBuffer extends SynchronizedBuffer {
|
||||
public class BlockingBuffer<E> extends SynchronizedBuffer<E> {
|
||||
|
||||
/** Serialization version. */
|
||||
private static final long serialVersionUID = 1719328905017860541L;
|
||||
|
@ -58,25 +59,27 @@ public class BlockingBuffer extends SynchronizedBuffer {
|
|||
/**
|
||||
* 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
|
||||
* @return a new blocking Buffer
|
||||
* @throws IllegalArgumentException if buffer is null
|
||||
*/
|
||||
public static Buffer decorate(Buffer buffer) {
|
||||
return new BlockingBuffer(buffer);
|
||||
public static <T> Buffer<T> decorate(Buffer<T> buffer) {
|
||||
return new BlockingBuffer<T>(buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 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 timeoutMillis) {
|
||||
return new BlockingBuffer(buffer, timeoutMillis);
|
||||
public static <T> Buffer<T> decorate(Buffer<T> buffer, long 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
|
||||
* @throws IllegalArgumentException if the buffer is null
|
||||
*/
|
||||
protected BlockingBuffer(Buffer buffer) {
|
||||
protected BlockingBuffer(Buffer<E> buffer) {
|
||||
super(buffer);
|
||||
this.timeout = 0;
|
||||
}
|
||||
|
@ -99,13 +102,13 @@ public class BlockingBuffer extends SynchronizedBuffer {
|
|||
* @throws IllegalArgumentException if the buffer is null
|
||||
* @since Commons Collections 3.2
|
||||
*/
|
||||
protected BlockingBuffer(Buffer buffer, long timeoutMillis) {
|
||||
protected BlockingBuffer(Buffer<E> buffer, long timeoutMillis) {
|
||||
super(buffer);
|
||||
this.timeout = (timeoutMillis < 0 ? 0 : timeoutMillis);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public boolean add(Object o) {
|
||||
public boolean add(E o) {
|
||||
synchronized (lock) {
|
||||
boolean result = collection.add(o);
|
||||
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) {
|
||||
boolean result = collection.addAll(c);
|
||||
lock.notifyAll();
|
||||
|
@ -128,7 +131,7 @@ public class BlockingBuffer extends SynchronizedBuffer {
|
|||
*
|
||||
* @throws BufferUnderflowException if an interrupt is received
|
||||
*/
|
||||
public Object get() {
|
||||
public E get() {
|
||||
synchronized (lock) {
|
||||
while (collection.isEmpty()) {
|
||||
try {
|
||||
|
@ -143,7 +146,7 @@ public class BlockingBuffer extends SynchronizedBuffer {
|
|||
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
|
||||
* @since Commons Collections 3.2
|
||||
*/
|
||||
public Object get(final long timeout) {
|
||||
public E get(final long timeout) {
|
||||
synchronized (lock) {
|
||||
final long expiration = System.currentTimeMillis() + timeout;
|
||||
long timeLeft = expiration - System.currentTimeMillis();
|
||||
|
@ -173,7 +176,7 @@ public class BlockingBuffer extends SynchronizedBuffer {
|
|||
if (collection.isEmpty()) {
|
||||
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
|
||||
*/
|
||||
public Object remove() {
|
||||
public E remove() {
|
||||
synchronized (lock) {
|
||||
while (collection.isEmpty()) {
|
||||
try {
|
||||
|
@ -199,7 +202,7 @@ public class BlockingBuffer extends SynchronizedBuffer {
|
|||
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
|
||||
* @since Commons Collections 3.2
|
||||
*/
|
||||
public Object remove(final long timeout) {
|
||||
public E remove(final long timeout) {
|
||||
synchronized (lock) {
|
||||
final long expiration = System.currentTimeMillis() + timeout;
|
||||
long timeLeft = expiration - System.currentTimeMillis();
|
||||
|
@ -229,7 +232,7 @@ public class BlockingBuffer extends SynchronizedBuffer {
|
|||
if (collection.isEmpty()) {
|
||||
throw new BufferUnderflowException("Timeout expired");
|
||||
}
|
||||
return getBuffer().remove();
|
||||
return decorated().remove();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -109,7 +109,7 @@ public class BoundedBuffer extends SynchronizedBuffer implements BoundedCollecti
|
|||
//-----------------------------------------------------------------------
|
||||
public Object remove() {
|
||||
synchronized (lock) {
|
||||
Object returnValue = getBuffer().remove();
|
||||
Object returnValue = decorated().remove();
|
||||
lock.notifyAll();
|
||||
return returnValue;
|
||||
}
|
||||
|
@ -118,14 +118,14 @@ public class BoundedBuffer extends SynchronizedBuffer implements BoundedCollecti
|
|||
public boolean add(Object o) {
|
||||
synchronized (lock) {
|
||||
timeoutWait(1);
|
||||
return getBuffer().add(o);
|
||||
return decorated().add(o);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean addAll(final Collection c) {
|
||||
synchronized (lock) {
|
||||
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) {
|
||||
// no wait period (immediate timeout)
|
||||
if (getBuffer().size() + nAdditions > maximumSize) {
|
||||
if (decorated().size() + nAdditions > maximumSize) {
|
||||
throw new BufferOverflowException(
|
||||
"Buffer size cannot exceed " + maximumSize);
|
||||
}
|
||||
|
@ -149,7 +149,7 @@ public class BoundedBuffer extends SynchronizedBuffer implements BoundedCollecti
|
|||
}
|
||||
final long expiration = System.currentTimeMillis() + timeout;
|
||||
long timeLeft = expiration - System.currentTimeMillis();
|
||||
while (timeLeft > 0 && getBuffer().size() + nAdditions > maximumSize) {
|
||||
while (timeLeft > 0 && decorated().size() + nAdditions > maximumSize) {
|
||||
try {
|
||||
lock.wait(timeLeft);
|
||||
timeLeft = expiration - System.currentTimeMillis();
|
||||
|
@ -160,7 +160,7 @@ public class BoundedBuffer extends SynchronizedBuffer implements BoundedCollecti
|
|||
"Caused by InterruptedException: " + out.toString());
|
||||
}
|
||||
}
|
||||
if (getBuffer().size() + nAdditions > maximumSize) {
|
||||
if (decorated().size() + nAdditions > maximumSize) {
|
||||
throw new BufferOverflowException("Timeout expired");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,12 +27,15 @@ import org.apache.commons.collections.collection.SynchronizedCollection;
|
|||
* <p>
|
||||
* This class is Serializable from Commons Collections 3.1.
|
||||
*
|
||||
* @param <E> the type of the elements in the buffer
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision$ $Date$
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
*/
|
||||
public class SynchronizedBuffer extends SynchronizedCollection implements Buffer {
|
||||
public class SynchronizedBuffer<E>
|
||||
extends SynchronizedCollection<E>
|
||||
implements Buffer<E> {
|
||||
|
||||
/** Serialization version */
|
||||
private static final long serialVersionUID = -6859936183953626253L;
|
||||
|
@ -40,14 +43,15 @@ public class SynchronizedBuffer extends SynchronizedCollection implements 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
|
||||
* @return a new synchronized Buffer
|
||||
* @throws IllegalArgumentException if buffer is null
|
||||
*/
|
||||
public static Buffer decorate(Buffer buffer) {
|
||||
return new SynchronizedBuffer(buffer);
|
||||
public static <T> Buffer<T> decorate(Buffer<T> buffer) {
|
||||
return new SynchronizedBuffer<T>(buffer);
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* 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
|
||||
* @throws IllegalArgumentException if the buffer is null
|
||||
*/
|
||||
protected SynchronizedBuffer(Buffer buffer) {
|
||||
protected SynchronizedBuffer(Buffer<E> 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
|
||||
* @throws IllegalArgumentException if the buffer is null
|
||||
*/
|
||||
protected SynchronizedBuffer(Buffer buffer, Object lock) {
|
||||
protected SynchronizedBuffer(Buffer<E> buffer, Object lock) {
|
||||
super(buffer, lock);
|
||||
}
|
||||
|
||||
|
@ -75,21 +79,21 @@ public class SynchronizedBuffer extends SynchronizedCollection implements Buffer
|
|||
*
|
||||
* @return the decorated buffer
|
||||
*/
|
||||
protected Buffer getBuffer() {
|
||||
return (Buffer) collection;
|
||||
protected Buffer<E> decorated() {
|
||||
return (Buffer<E>) super.decorated();
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public Object get() {
|
||||
public E get() {
|
||||
synchronized (lock) {
|
||||
return getBuffer().get();
|
||||
return decorated().get();
|
||||
}
|
||||
}
|
||||
|
||||
public Object remove() {
|
||||
public E remove() {
|
||||
synchronized (lock) {
|
||||
return getBuffer().remove();
|
||||
return decorated().remove();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue