Tidy up formatting and tests afetr initial checkin

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@333048 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2005-11-13 16:23:35 +00:00
parent ea2548e01e
commit 0d0a25fdfb
2 changed files with 75 additions and 53 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,88 +16,96 @@
package org.apache.commons.collections.buffer; package org.apache.commons.collections.buffer;
import org.apache.commons.collections.Buffer; import org.apache.commons.collections.Buffer;
import org.apache.commons.collections.BufferUnderflowException;
/** /**
* Decorates another <code>Buffer</code> to make {@link #get()} and * Decorates another <code>Buffer</code> to make {@link #get()} and
* {@link #remove()} block (until timeout expires) when the <code>Buffer</code> is empty. * {@link #remove()} block (until timeout expires) when the <code>Buffer</code>
* is empty.
* <p> * <p>
* If either <code>get</code> or <code>remove</code> is called on an empty * If either <code>get</code> or <code>remove</code> is called on an empty
* <code>Buffer</code>, the calling thread waits (until timeout expires) for notification that * <code>Buffer</code>, the calling thread waits (until timeout expires) for
* an <code>add</code> or <code>addAll</code> operation has completed. * notification that an <code>add</code> or <code>addAll</code> operation
* has completed.
* <p> * <p>
* When one or more entries are added to an empty <code>Buffer</code>, * When one or more entries are added to an empty <code>Buffer</code>, all
* all threads blocked in <code>get</code> or <code>remove</code> are notified. * threads blocked in <code>get</code> or <code>remove</code> are notified.
* There is no guarantee that concurrent blocked <code>get</code> or * There is no guarantee that concurrent blocked <code>get</code> or
* <code>remove</code> requests will be "unblocked" and receive data in the * <code>remove</code> requests will be "unblocked" and receive data in the
* order that they arrive. * order that they arrive.
* <p>
* This class is Serializable from Commons Collections 3.2.
* *
* @author James Carman * @author James Carman
* @version $Revision$ $Date$ * @version $Revision: $ $Date: $
* @since Commons Collections 3.2 * @since Commons Collections 3.2
*/ */
public class TimeoutBuffer extends BlockingBuffer { public class TimeoutBuffer extends BlockingBuffer {
//----------------------------------------------------------------------------------------------------------------------
// Fields /** The serialization lock. */
//----------------------------------------------------------------------------------------------------------------------
private static final long serialVersionUID = 1719328905017860541L; private static final long serialVersionUID = 1719328905017860541L;
/** The timeout length. */
private final long timeout; private final long timeout;
//---------------------------------------------------------------------------------------------------------------------- /**
// Static Methods * Decorates the specified buffer adding timeout behaviour.
//---------------------------------------------------------------------------------------------------------------------- *
* @param buffer the buffer to decorate, must not be null
public static Buffer decorate( Buffer buffer, long timeout ) { * @param timeout the timeout value in milliseconds
return new TimeoutBuffer( buffer, timeout ); * @return the decorated buffer
* @throws IllegalArgumentException if the buffer is null
* @throws IllegalArgumentException if the timeout is negative
*/
public static Buffer decorate(Buffer buffer, long timeout) {
return new TimeoutBuffer(buffer, timeout);
} }
//---------------------------------------------------------------------------------------------------------------------- //-----------------------------------------------------------------------
// Constructors /**
//---------------------------------------------------------------------------------------------------------------------- * Constructor that wraps (not copies).
*
public TimeoutBuffer( Buffer buffer, long timeout ) { * @param buffer the buffer to decorate, must not be null
super( buffer ); * @param timeout the timeout value in milliseconds
* @throws IllegalArgumentException if the buffer is null
* @throws IllegalArgumentException if the timeout is negative
*/
protected TimeoutBuffer(Buffer buffer, long timeout) {
super(buffer);
if (timeout < 0) {
throw new IllegalArgumentException("The timeout cannot be negative");
}
this.timeout = timeout; this.timeout = timeout;
} }
/**
* Gets the length of the timeout.
*
* @return the timeout value
*/
public long getTimeout() { public long getTimeout() {
return timeout; return timeout;
} }
//----------------------------------------------------------------------------------------------------------------------
// Buffer Implementation
//----------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------
/**
* 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.
*
* @throws BufferUnderflowException if an interrupt is received
* @throws BufferUnderflowException if the timeout expires
*/
public Object get() { public Object get() {
return get( timeout ); return get(timeout);
} }
/**
* 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.
*
* @throws BufferUnderflowException if an interrupt is received
* @throws BufferUnderflowException if the timeout expires
*/
public Object remove() { public Object remove() {
return remove( timeout ); return remove(timeout);
} }
public boolean equals( Object o ) {
if( this == o ) {
return true;
}
if( o == null || getClass() != o.getClass() ) {
return false;
}
if( !super.equals( o ) ) {
return false;
}
final TimeoutBuffer that = ( TimeoutBuffer ) o;
if( timeout != that.timeout ) {
return false;
}
return true;
}
public int hashCode() {
int result = super.hashCode();
result = 29 * result + ( int ) ( timeout ^ ( timeout >>> 32 ) );
return result;
}
} }

View File

@ -55,6 +55,20 @@ public class TestTimeoutBuffer extends AbstractTestObject {
//---------------------------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------------------------
// Other Methods // Other Methods
//---------------------------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------------------------
public void testDecorationExceptions() {
try {
TimeoutBuffer.decorate((Buffer) null, 1);
fail();
} catch (IllegalArgumentException ex) {
// expected
}
try {
TimeoutBuffer.decorate(new CircularFifoBuffer(4), -1);
fail();
} catch (IllegalArgumentException ex) {
// expected
}
}
public String getCompatibilityVersion() { public String getCompatibilityVersion() {
return "3.2"; return "3.2";