From 5c2298877492934485e0f8ff67910f721eef591d Mon Sep 17 00:00:00 2001 From: "James W. Carman" Date: Wed, 23 Nov 2005 13:02:10 +0000 Subject: [PATCH] Removing TimeoutBuffer (BlockingBuffer will be enhanced instead). git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@348426 13f79535-47bb-0310-9956-ffa450edef68 --- ...meoutBuffer.emptyCollection.version3.2.obj | Bin 488 -> 0 bytes ...imeoutBuffer.fullCollection.version3.2.obj | Bin 978 -> 0 bytes .../collections/buffer/TimeoutBuffer.java | 111 -------- .../commons/collections/buffer/TestAll.java | 1 - .../collections/buffer/TestTimeoutBuffer.java | 256 ------------------ 5 files changed, 368 deletions(-) delete mode 100644 data/test/TimeoutBuffer.emptyCollection.version3.2.obj delete mode 100644 data/test/TimeoutBuffer.fullCollection.version3.2.obj delete mode 100644 src/java/org/apache/commons/collections/buffer/TimeoutBuffer.java delete mode 100644 src/test/org/apache/commons/collections/buffer/TestTimeoutBuffer.java diff --git a/data/test/TimeoutBuffer.emptyCollection.version3.2.obj b/data/test/TimeoutBuffer.emptyCollection.version3.2.obj deleted file mode 100644 index 543b4e804fc401135e38f8a3803cd42f2df63591..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 488 zcmZ4UmVvdnh`~6&C|xhHATc>3RWCU|H#a}87)a;jq$ZbSg4ju=X=$lNdLfy)srjWP zPGFw+9gp_e%NFisVqo-QU@w74RTMFp5NDQCPJVKBW?nkFVGKYcEQm8AxH2y}qbNTw zvnn+OV$6awbCT}P$S!7L02$*zz!-!N^^lEnhI?GGhOubBC$|z41CtK}7eaXngP2cN zVp*boX-Q^|K0>{<4+9IxTO|xa5c!|i^PB80?%7`uW1 z2^J<+M#RX%#KHm}fsGHq`!o_kEN!GGj^*d~Uhe$i7oBVy(seUV(pj4A=1Ephr*+lx zwJh_jEAV?X-`UBVv^|vcz5x~?dPxFzxyPpcZW`Q%SVfb%<*Pzlyx>Ks`3BB zh)0Cujo8|+vfZYxibwhOa?IPWuSY*$Ojk$Z*^%l zSvxrR_-ZhTo zD30bBj^#Lx=LAmdNJuUU$xR`-DkOJ>5Xb26VAvA=6Fc1d9Ko|%EVIT~IfiMsj!a`UG3t=HFgoUsW7Q#X} z2nXRH9E5{#5DvmYI0y&fAv}bK@DLutLwE=e;UPRkfCvx)B0vO)01+SpM1Tko5h6lF bhzJoOB1D9U5D_BA#eI50r}&-or}z60Y-I%L diff --git a/src/java/org/apache/commons/collections/buffer/TimeoutBuffer.java b/src/java/org/apache/commons/collections/buffer/TimeoutBuffer.java deleted file mode 100644 index 87f11db80..000000000 --- a/src/java/org/apache/commons/collections/buffer/TimeoutBuffer.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.collections.buffer; - -import org.apache.commons.collections.Buffer; -import org.apache.commons.collections.BufferUnderflowException; - -/** - * Decorates another Buffer to make {@link #get()} and - * {@link #remove()} block (until timeout expires) when the Buffer - * is empty. - *

- * If either get or remove is called on an empty - * Buffer, the calling thread waits (until timeout expires) for - * notification that an add or addAll operation - * has completed. - *

- * When one or more entries are added to an empty Buffer, all - * threads blocked in get or remove are notified. - * There is no guarantee that concurrent blocked get or - * remove requests will be "unblocked" and receive data in the - * order that they arrive. - * - * @author James Carman - * @version $Revision: $ $Date: $ - * @since Commons Collections 3.2 - */ -public class TimeoutBuffer extends BlockingBuffer { - - /** The serialization lock. */ - private static final long serialVersionUID = 1719328905017860541L; - - /** The timeout length. */ - private final long timeout; - - /** - * Decorates the specified buffer adding timeout behaviour. - * - * @param buffer the buffer to decorate, must not be null - * @param timeout the timeout value in milliseconds - * @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); - } - - //----------------------------------------------------------------------- - /** - * Constructor that wraps (not copies). - * - * @param buffer the buffer to decorate, must not be null - * @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; - } - - /** - * Gets the length of the timeout. - * - * @return the timeout value - */ - public long getTimeout() { - return timeout; - } - - //----------------------------------------------------------------------- - /** - * 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() { - 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() { - return remove(timeout); - } - -} diff --git a/src/test/org/apache/commons/collections/buffer/TestAll.java b/src/test/org/apache/commons/collections/buffer/TestAll.java index 0830ca1b9..97bc93b67 100644 --- a/src/test/org/apache/commons/collections/buffer/TestAll.java +++ b/src/test/org/apache/commons/collections/buffer/TestAll.java @@ -52,7 +52,6 @@ public class TestAll extends TestCase { suite.addTest(TestSynchronizedBuffer.suite()); suite.addTest(TestTransformedBuffer.suite()); suite.addTest(TestUnmodifiableBuffer.suite()); - suite.addTest(TestTimeoutBuffer.suite()); return suite; } diff --git a/src/test/org/apache/commons/collections/buffer/TestTimeoutBuffer.java b/src/test/org/apache/commons/collections/buffer/TestTimeoutBuffer.java deleted file mode 100644 index 9b19a40f0..000000000 --- a/src/test/org/apache/commons/collections/buffer/TestTimeoutBuffer.java +++ /dev/null @@ -1,256 +0,0 @@ -/* - * Copyright 2001-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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.collections.buffer; - -import junit.framework.Test; -import junit.framework.TestSuite; -import org.apache.commons.collections.AbstractTestObject; -import org.apache.commons.collections.ArrayStack; -import org.apache.commons.collections.Buffer; -import org.apache.commons.collections.BufferUnderflowException; -import org.apache.commons.collections.BufferUtils; - -/** - * @author James Carman - * @version 1.0 - */ -public class TestTimeoutBuffer extends AbstractTestObject { -//---------------------------------------------------------------------------------------------------------------------- -// Fields -//---------------------------------------------------------------------------------------------------------------------- - - private static final int FULL_SIZE = 100; - - private static final int TIMEOUT = 100; - -//---------------------------------------------------------------------------------------------------------------------- -// Static Methods -//---------------------------------------------------------------------------------------------------------------------- - - public static Test suite() { - return new TestSuite( TestTimeoutBuffer.class ); - } - -//---------------------------------------------------------------------------------------------------------------------- -// Constructors -//---------------------------------------------------------------------------------------------------------------------- - - public TestTimeoutBuffer( String testName ) { - super( testName ); - } - -//---------------------------------------------------------------------------------------------------------------------- -// 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() { - return "3.2"; - } - - public boolean isEqualsCheckable() { - return false; - } - - public Object makeObject() { - return BufferUtils.timeoutBuffer( new ArrayStack(), TIMEOUT ); - } - - public void testEmptySerialization() { - try { - final TimeoutBuffer b = ( TimeoutBuffer ) readExternalFormFromDisk( - getCanonicalEmptyCollectionName( makeObject() ) ); - assertTrue( b.isEmpty() ); - } - catch( Exception e ) { - fail( "Could not read object from disk." ); - } - } - - public void testFullSerialization() { - try { - final TimeoutBuffer b = ( TimeoutBuffer ) readExternalFormFromDisk( - getCanonicalFullCollectionName( makeObject() ) ); - assertEquals( FULL_SIZE, b.size() ); - } - catch( Exception e ) { - fail( "Could not read object from disk." ); - } - } - - public void testSuccessfulWaitOnGet() { - Buffer b = ( Buffer ) makeObject(); - executeAsynchronously( new Getter( b ) ); - executeAsynchronously( new Adder( b, "Hello" ) ); - } - - private static void executeAsynchronously( Runnable r ) { - new Thread( r ).start(); - } - - public void testSuccessfulWaitOnRemove() { - Buffer b = ( Buffer ) makeObject(); - executeAsynchronously( new Remover( b ) ); - executeAsynchronously( new Adder( b, "Hello" ) ); - } - - public void testTimeoutOnGet() { - final Buffer buffer = makeBuffer(); - try { - Getter remover = new Getter( buffer ); - executeAsynchronously( remover ); - executeAsynchronously( new Adder( buffer, "Howdy" ), TIMEOUT * 2 ); - assertFalse( remover.isSuccesful() ); - } - catch( BufferUnderflowException e ) { - } - } - - private TimeoutBuffer makeBuffer() { - return ( TimeoutBuffer ) makeObject(); - } - - private static void executeAsynchronously( Runnable r, long delay ) { - new Thread( new DelayedRunnable( r, delay ) ).start(); - } - - public void testTimeoutOnRemove() { - final Buffer buffer = makeBuffer(); - try { - Remover remover = new Remover( buffer ); - executeAsynchronously( remover ); - executeAsynchronously( new Adder( buffer, "Howdy" ), TIMEOUT * 2 ); - assertFalse( remover.isSuccesful() ); - } - catch( BufferUnderflowException e ) { - } - } - -//---------------------------------------------------------------------------------------------------------------------- -// Inner Classes -//---------------------------------------------------------------------------------------------------------------------- - - private static class DelayedRunnable implements Runnable { - private final Runnable r; - - private final long delay; - public DelayedRunnable( Runnable r, long delay ) { - this.r = r; - this.delay = delay; - } - - public void run() { - try { - Thread.sleep( delay ); - } - catch( InterruptedException e ) { - e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. - } - r.run(); - } - } - - private static class Adder implements Runnable { - private final Buffer b; - - private final Object o; - public Adder( Buffer b, Object o ) { - this.b = b; - this.o = o; - } - - public void run() { - b.add( o ); - } - } - - private static class Remover extends BufferReader { - public Remover( Buffer b ) { - super( b ); - } - - protected void performOperation() { - b.remove(); - } - } - - private static abstract class BufferReader implements Runnable { - - protected final Buffer b; - private Boolean succesful; - - protected BufferReader( Buffer b ) { - this.b = b; - } - - protected abstract void performOperation(); - - public final synchronized void run() { - try { - performOperation(); - succesful = Boolean.TRUE; - } - catch( BufferUnderflowException e ) { - succesful = Boolean.FALSE; - } - notifyAll(); - } - - public synchronized boolean isSuccesful() { - while( succesful == null ) { - try { - wait(); - } - catch( InterruptedException e ) { - } - } - return succesful.booleanValue(); - } - } - - private static class Getter extends BufferReader { - public Getter( Buffer b ) { - super( b ); - } - - protected void performOperation() { - b.get(); - } - } - -//---------------------------------------------------------------------------------------------------------------------- -// main() method -//---------------------------------------------------------------------------------------------------------------------- - - public static void main( String args[] ) { - String[] testCaseName = {TestTimeoutBuffer.class.getName()}; - junit.textui.TestRunner.main( testCaseName ); - } -} -