diff --git a/src/main/java/org/apache/commons/collections/Buffer.java b/src/main/java/org/apache/commons/collections/Buffer.java
deleted file mode 100644
index 937e6d8e7..000000000
--- a/src/main/java/org/apache/commons/collections/Buffer.java
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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;
-
-import java.util.Collection;
-
-/**
- * Defines a collection that allows objects to be removed in some well-defined order.
- *
- * The removal order can be based on insertion order (eg, a FIFO queue or a
- * LIFO stack), on access order (eg, an LRU cache), on some arbitrary comparator
- * (eg, a priority queue) or on any other well-defined ordering.
- *
- * Note that the removal order is not necessarily the same as the iteration
- * order. A Buffer
implementation may have equivalent removal
- * and iteration orders, but this is not required.
- *
- * This interface does not specify any behavior for
- * {@link Object#equals(Object)} and {@link Object#hashCode} methods. It
- * is therefore possible for a Buffer
implementation to also
- * also implement {@link java.util.List}, {@link java.util.Set} or
- * {@link Bag}.
- *
- * @param the type of the elements in the buffer
- * @since 2.1
- * @version $Id$
- */
-public interface Buffer extends Collection {
-
- /**
- * Gets and removes the next object from the buffer.
- *
- * @return the next object in the buffer, which is also removed
- * @throws BufferUnderflowException if the buffer is already empty
- */
- E remove();
-
- /**
- * Gets the next object from the buffer without removing it.
- *
- * @return the next object in the buffer, which is not removed
- * @throws BufferUnderflowException if the buffer is empty
- */
- E get();
-
-}
diff --git a/src/main/java/org/apache/commons/collections/BufferOverflowException.java b/src/main/java/org/apache/commons/collections/BufferOverflowException.java
deleted file mode 100644
index 1f73b900d..000000000
--- a/src/main/java/org/apache/commons/collections/BufferOverflowException.java
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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;
-
-/**
- * The BufferOverflowException is used when the buffer's capacity has been
- * exceeded.
- *
- * @since 2.1
- * @version $Id$
- */
-public class BufferOverflowException extends RuntimeException {
-
- /** Serialization version */
- private static final long serialVersionUID = -3992254982265755876L;
-
- /**
- * Constructs a new BufferOverflowException
.
- */
- public BufferOverflowException() {
- super();
- }
-
- /**
- * Construct a new BufferOverflowException
.
- *
- * @param message the detail message for this exception
- */
- public BufferOverflowException(final String message) {
- this(message, null);
- }
-
- /**
- * Construct a new BufferOverflowException
.
- *
- * @param message the detail message for this exception
- * @param exception the root cause of the exception
- */
- public BufferOverflowException(final String message, final Throwable exception) {
- super(message, exception);
- }
-
-}
diff --git a/src/main/java/org/apache/commons/collections/BufferUnderflowException.java b/src/main/java/org/apache/commons/collections/BufferUnderflowException.java
deleted file mode 100644
index 86954351a..000000000
--- a/src/main/java/org/apache/commons/collections/BufferUnderflowException.java
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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;
-
-import java.util.NoSuchElementException;
-
-/**
- * The BufferUnderflowException is used when the buffer is already empty.
- *
- * NOTE: From version 3.0, this exception extends NoSuchElementException.
- *
- * @since 2.1
- * @version $Id$
- */
-public class BufferUnderflowException extends NoSuchElementException {
-
- /** Serialization version */
- private static final long serialVersionUID = 7106567570467436893L;
-
- /**
- * Constructs a new BufferUnderflowException
.
- */
- public BufferUnderflowException() {
- super();
- }
-
- /**
- * Construct a new BufferUnderflowException
.
- *
- * @param message the detail message for this exception
- */
- public BufferUnderflowException(final String message) {
- super(message);
- }
-
- /**
- * Construct a new BufferUnderflowException
.
- *
- * @param message the detail message for this exception
- * @param exception the root cause of the exception
- */
- public BufferUnderflowException(final String message, final Throwable exception) {
- super(message);
- initCause(exception);
- }
-
-}
diff --git a/src/main/java/org/apache/commons/collections/BufferUtils.java b/src/main/java/org/apache/commons/collections/BufferUtils.java
deleted file mode 100644
index fcd0e667d..000000000
--- a/src/main/java/org/apache/commons/collections/BufferUtils.java
+++ /dev/null
@@ -1,206 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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;
-
-import org.apache.commons.collections.buffer.BlockingBuffer;
-import org.apache.commons.collections.buffer.BoundedBuffer;
-import org.apache.commons.collections.buffer.CircularFifoBuffer;
-import org.apache.commons.collections.buffer.PredicatedBuffer;
-import org.apache.commons.collections.buffer.SynchronizedBuffer;
-import org.apache.commons.collections.buffer.TransformedBuffer;
-import org.apache.commons.collections.buffer.UnmodifiableBuffer;
-
-/**
- * Provides utility methods and decorators for {@link Buffer} instances.
- *
- * @since 2.1
- * @version $Id$
- */
-public class BufferUtils {
-
- /**
- * An empty unmodifiable buffer.
- */
- public static final Buffer EMPTY_BUFFER = UnmodifiableBuffer.unmodifiableBuffer(new CircularFifoBuffer(1));
-
- /**
- * BufferUtils
should not normally be instantiated.
- */
- public BufferUtils() {
- }
-
- //-----------------------------------------------------------------------
- /**
- * Returns a synchronized buffer backed by the given buffer.
- * Much like the synchronized collections returned by
- * {@link java.util.Collections}, you must manually synchronize on
- * the returned buffer's iterator to avoid non-deterministic behavior:
- *
- *
- * Buffer b = BufferUtils.synchronizedBuffer(myBuffer);
- * synchronized (b) {
- * Iterator i = b.iterator();
- * while (i.hasNext()) {
- * process (i.next());
- * }
- * }
- *
- *
- * @param the type of the elements in the buffer
- * @param buffer the buffer to synchronize, must not be null
- * @return a synchronized buffer backed by that buffer
- * @throws IllegalArgumentException if the Buffer is null
- */
- public static Buffer synchronizedBuffer(final Buffer buffer) {
- return SynchronizedBuffer.synchronizedBuffer(buffer);
- }
-
- /**
- * Returns a synchronized buffer backed by the given buffer that will
- * block on {@link Buffer#get()} and {@link Buffer#remove()} operations.
- * If the buffer is empty, then the {@link Buffer#get()} and
- * {@link Buffer#remove()} operations will block until new elements
- * are added to the buffer, rather than immediately throwing a
- * BufferUnderflowException
.
- *
- * @param the type of the elements in the buffer
- * @param buffer the buffer to synchronize, must not be null
- * @return a blocking buffer backed by that buffer
- * @throws IllegalArgumentException if the Buffer is null
- */
- public static Buffer blockingBuffer(final Buffer buffer) {
- return BlockingBuffer.blockingBuffer(buffer);
- }
-
- /**
- * Returns a synchronized buffer backed by the given buffer that will
- * block on {@link Buffer#get()} and {@link Buffer#remove()} operations
- * until timeout
expires. If the buffer is empty, then the
- * {@link Buffer#get()} and {@link Buffer#remove()} operations will block
- * until new elements are added to the buffer, rather than immediately
- * throwing a BufferUnderflowException
.
- *
- * @param the type of the elements in the buffer
- * @param buffer the buffer to synchronize, must not be null
- * @param timeoutMillis the timeout value in milliseconds, zero or less for no timeout
- * @return a blocking buffer backed by that buffer
- * @throws IllegalArgumentException if the Buffer is null
- * @since 3.2
- */
- public static Buffer blockingBuffer(final Buffer buffer, final long timeoutMillis) {
- return BlockingBuffer.blockingBuffer(buffer, timeoutMillis);
- }
-
- /**
- * Returns a synchronized buffer backed by the given buffer that will
- * block on {@link Buffer#add(Object)} and
- * {@link Buffer#addAll(java.util.Collection)} until enough object(s) are
- * removed from the buffer to allow the object(s) to be added and still
- * maintain the maximum size.
- *
- * @param the type of the elements in the buffer
- * @param buffer the buffer to make bounded, must not be null
- * @param maximumSize the maximum size
- * @return a bounded buffer backed by the given buffer
- * @throws IllegalArgumentException if the given buffer is null
- * @since 3.2
- */
- public static Buffer boundedBuffer(final Buffer buffer, final int maximumSize) {
- return BoundedBuffer.boundedBuffer(buffer, maximumSize);
- }
-
- /**
- * Returns a synchronized buffer backed by the given buffer that will
- * block on {@link Buffer#add(Object)} and
- * {@link Buffer#addAll(java.util.Collection)} until enough object(s) are
- * removed from the buffer to allow the object(s) to be added and still
- * maintain the maximum size or the timeout expires.
- *
- * @param the type of the elements in the buffer
- * @param buffer the buffer to make bounded, must not be null
- * @param maximumSize the maximum size
- * @param timeoutMillis the timeout value in milliseconds, zero or less for no timeout
- * @return a bounded buffer backed by the given buffer
- * @throws IllegalArgumentException if the given buffer is null
- * @since 3.2
- */
- public static Buffer boundedBuffer(final Buffer buffer, final int maximumSize, final long timeoutMillis) {
- return BoundedBuffer.boundedBuffer(buffer, maximumSize, timeoutMillis);
- }
-
- /**
- * Returns an unmodifiable buffer backed by the given buffer.
- *
- * @param the type of the elements in the buffer
- * @param buffer the buffer to make unmodifiable, must not be null
- * @return an unmodifiable buffer backed by that buffer
- * @throws IllegalArgumentException if the Buffer is null
- */
- public static Buffer unmodifiableBuffer(final Buffer buffer) {
- return UnmodifiableBuffer.unmodifiableBuffer(buffer);
- }
-
- /**
- * Returns a predicated (validating) buffer backed by the given buffer.
- *
- * Only objects that pass the test in the given predicate can be added to the buffer.
- * Trying to add an invalid object results in an IllegalArgumentException.
- * It is important not to use the original buffer after invoking this method,
- * as it is a backdoor for adding invalid objects.
- *
- * @param the type of the elements in the buffer
- * @param buffer the buffer to predicate, must not be null
- * @param predicate the predicate used to evaluate new elements, must not be null
- * @return a predicated buffer
- * @throws IllegalArgumentException if the Buffer or Predicate is null
- */
- public static Buffer predicatedBuffer(final Buffer buffer, final Predicate super E> predicate) {
- return PredicatedBuffer.predicatedBuffer(buffer, predicate);
- }
-
- /**
- * Returns a transformed buffer backed by the given buffer.
- *
- * Each object is passed through the transformer as it is added to the
- * Buffer. It is important not to use the original buffer after invoking this
- * method, as it is a backdoor for adding untransformed objects.
- *
- * Existing entries in the specified buffer will not be transformed.
- * If you want that behaviour, see {@link TransformedBuffer#transformedBuffer}.
- *
- * @param the type of the elements in the buffer
- * @param buffer the buffer to predicate, must not be null
- * @param transformer the transformer for the buffer, must not be null
- * @return a transformed buffer backed by the given buffer
- * @throws IllegalArgumentException if the Buffer or Transformer is null
- */
- public static Buffer transformingBuffer(final Buffer buffer,
- final Transformer super E, ? extends E> transformer) {
- return TransformedBuffer.transformingBuffer(buffer, transformer);
- }
-
- /**
- * Get an empty Buffer
.
- *
- * @param the type of the elements in the buffer
- * @return an empty {@link Buffer}
- */
- @SuppressWarnings("unchecked") // OK, empty buffer is compatible with any type
- public static Buffer emptyBuffer() {
- return (Buffer) EMPTY_BUFFER;
- }
-}
diff --git a/src/main/java/org/apache/commons/collections/buffer/AbstractBufferDecorator.java b/src/main/java/org/apache/commons/collections/buffer/AbstractBufferDecorator.java
deleted file mode 100644
index 8d9d536e7..000000000
--- a/src/main/java/org/apache/commons/collections/buffer/AbstractBufferDecorator.java
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.collection.AbstractCollectionDecorator;
-
-/**
- * Decorates another {@link Buffer} to provide additional behaviour.
- *
- * Methods are forwarded directly to the decorated buffer.
- *
- * @param the type of the elements in the buffer
- * @since 3.0
- * @version $Id$
- */
-public abstract class AbstractBufferDecorator extends AbstractCollectionDecorator
- implements Buffer {
-
- /** Serialization version */
- private static final long serialVersionUID = -2629815475789577029L;
-
- /**
- * Constructor only used in deserialization, do not use otherwise.
- * @since 3.1
- */
- protected AbstractBufferDecorator() {
- super();
- }
-
- /**
- * Constructor that wraps (not copies).
- *
- * @param buffer the buffer to decorate, must not be null
- * @throws IllegalArgumentException if list is null
- */
- protected AbstractBufferDecorator(final Buffer buffer) {
- super(buffer);
- }
-
- /**
- * Gets the buffer being decorated.
- *
- * @return the decorated buffer
- */
- @Override
- protected Buffer decorated() {
- return (Buffer) super.decorated();
- }
-
- //-----------------------------------------------------------------------
-
- public E get() {
- return decorated().get();
- }
-
- public E remove() {
- return decorated().remove();
- }
-
-}
diff --git a/src/main/java/org/apache/commons/collections/buffer/BlockingBuffer.java b/src/main/java/org/apache/commons/collections/buffer/BlockingBuffer.java
deleted file mode 100644
index 878bb1689..000000000
--- a/src/main/java/org/apache/commons/collections/buffer/BlockingBuffer.java
+++ /dev/null
@@ -1,243 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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 java.io.PrintWriter;
-import java.io.StringWriter;
-import java.util.Collection;
-
-import org.apache.commons.collections.Buffer;
-import org.apache.commons.collections.BufferUnderflowException;
-
-/**
- * Decorates another {@link Buffer} to make {@link #get()} and
- * {@link #remove()} block when the Buffer
is empty.
- *
- * If either get
or remove
is called on an empty
- * {@link Buffer}, the calling thread waits for notification that
- * an add
or addAll
operation has completed.
- *
- * When one or more entries are added to an empty {@link 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.
- *
- * This class is Serializable from Commons Collections 3.1.
- * This class contains an extra field in 3.2, however the serialization
- * specification will handle this gracefully.
- *
- * @param the type of the elements in the buffer
- * @version $Id$
- * @since 3.0
- */
-public class BlockingBuffer extends SynchronizedBuffer {
-
- /** Serialization version. */
- private static final long serialVersionUID = 1719328905017860541L;
- /** The timeout value in milliseconds. */
- private final long timeout;
-
- /**
- * Factory method to create a blocking buffer.
- *
- * @param 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 BlockingBuffer blockingBuffer(final Buffer buffer) {
- return new BlockingBuffer(buffer);
- }
-
- /**
- * Factory method to create a blocking buffer with a timeout value.
- *
- * @param 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 3.2
- */
- public static BlockingBuffer blockingBuffer(final Buffer buffer, final long timeoutMillis) {
- return new BlockingBuffer(buffer, timeoutMillis);
- }
-
- //-----------------------------------------------------------------------
- /**
- * Constructor that wraps (not copies).
- *
- * @param buffer the buffer to decorate, must not be null
- * @throws IllegalArgumentException if the buffer is null
- */
- protected BlockingBuffer(final Buffer buffer) {
- super(buffer);
- this.timeout = 0;
- }
-
- /**
- * Constructor that wraps (not copies).
- *
- * @param buffer the buffer to decorate, must not be null
- * @param timeoutMillis the timeout value in milliseconds, zero or less for no timeout
- * @throws IllegalArgumentException if the buffer is null
- * @since 3.2
- */
- protected BlockingBuffer(final Buffer buffer, final long timeoutMillis) {
- super(buffer);
- this.timeout = timeoutMillis < 0 ? 0 : timeoutMillis;
- }
-
- //-----------------------------------------------------------------------
- @Override
- public boolean add(final E o) {
- synchronized (lock) {
- final boolean result = collection.add(o);
- lock.notifyAll();
- return result;
- }
- }
-
- @Override
- public boolean addAll(final Collection extends E> c) {
- synchronized (lock) {
- final boolean result = collection.addAll(c);
- lock.notifyAll();
- return result;
- }
- }
-
- /**
- * Gets the next value from the buffer, waiting until an object is
- * added if the buffer is empty. This method uses the default timeout
- * set in the constructor.
- *
- * @throws BufferUnderflowException if an interrupt is received
- * {@inheritDoc}
- */
- @Override
- public E get() {
- synchronized (lock) {
- while (collection.isEmpty()) {
- try {
- if (timeout <= 0) {
- lock.wait();
- } else {
- return get(timeout);
- }
- } catch (final InterruptedException e) {
- final PrintWriter out = new PrintWriter(new StringWriter());
- e.printStackTrace(out);
- throw new BufferUnderflowException("Caused by InterruptedException: " + out.toString());
- }
- }
- return decorated().get();
- }
- }
-
- /**
- * 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.
- *
- * @param timeout the timeout value in milliseconds
- * @return the next object in the buffer
- * @throws BufferUnderflowException if an interrupt is received
- * @throws BufferUnderflowException if the timeout expires
- * @since 3.2
- */
- public E get(final long timeout) {
- synchronized (lock) {
- final long expiration = System.currentTimeMillis() + timeout;
- long timeLeft = expiration - System.currentTimeMillis();
- while (timeLeft > 0 && collection.isEmpty()) {
- try {
- lock.wait(timeLeft);
- timeLeft = expiration - System.currentTimeMillis();
- } catch(final InterruptedException e) {
- final PrintWriter out = new PrintWriter(new StringWriter());
- e.printStackTrace(out);
- throw new BufferUnderflowException("Caused by InterruptedException: " + out.toString());
- }
- }
- if (collection.isEmpty()) {
- throw new BufferUnderflowException("Timeout expired");
- }
- return decorated().get();
- }
- }
-
- /**
- * Removes the next value from the buffer, waiting until an object is
- * added if the buffer is empty. This method uses the default timeout
- * set in the constructor.
- *
- * @throws BufferUnderflowException if an interrupt is received
- * {@inheritDoc}
- */
- @Override
- public E remove() {
- synchronized (lock) {
- while (collection.isEmpty()) {
- try {
- if (timeout <= 0) {
- lock.wait();
- } else {
- return remove(timeout);
- }
- } catch (final InterruptedException e) {
- final PrintWriter out = new PrintWriter(new StringWriter());
- e.printStackTrace(out);
- throw new BufferUnderflowException("Caused by InterruptedException: " + out.toString());
- }
- }
- return decorated().remove();
- }
- }
-
- /**
- * 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.
- *
- * @param timeout the timeout value in milliseconds
- * @return the next object in the buffer, which is also removed
- * @throws BufferUnderflowException if an interrupt is received
- * @throws BufferUnderflowException if the timeout expires
- * @since 3.2
- */
- public E remove(final long timeout) {
- synchronized (lock) {
- final long expiration = System.currentTimeMillis() + timeout;
- long timeLeft = expiration - System.currentTimeMillis();
- while (timeLeft > 0 && collection.isEmpty()) {
- try {
- lock.wait(timeLeft);
- timeLeft = expiration - System.currentTimeMillis();
- } catch(final InterruptedException e) {
- final PrintWriter out = new PrintWriter(new StringWriter());
- e.printStackTrace(out);
- throw new BufferUnderflowException("Caused by InterruptedException: " + out.toString());
- }
- }
- if (collection.isEmpty()) {
- throw new BufferUnderflowException("Timeout expired");
- }
- return decorated().remove();
- }
- }
-
-}
diff --git a/src/main/java/org/apache/commons/collections/buffer/BoundedBuffer.java b/src/main/java/org/apache/commons/collections/buffer/BoundedBuffer.java
deleted file mode 100644
index fe1b5059c..000000000
--- a/src/main/java/org/apache/commons/collections/buffer/BoundedBuffer.java
+++ /dev/null
@@ -1,213 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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 java.io.PrintWriter;
-import java.io.StringWriter;
-import java.util.Collection;
-import java.util.Iterator;
-
-import org.apache.commons.collections.BoundedCollection;
-import org.apache.commons.collections.Buffer;
-import org.apache.commons.collections.BufferOverflowException;
-import org.apache.commons.collections.BufferUnderflowException;
-import org.apache.commons.collections.iterators.AbstractIteratorDecorator;
-
-/**
- * Decorates another {@link Buffer} to ensure a fixed maximum size.
- *
- * Note: This class should only be used if you need to add bounded
- * behaviour to another buffer. If you just want a bounded buffer then
- * you should use {@link BoundedFifoBuffer} or {@link CircularFifoBuffer}.
- *
- * The decoration methods allow you to specify a timeout value.
- * This alters the behaviour of the add methods when the buffer is full.
- * Normally, when the buffer is full, the add method will throw an exception.
- * With a timeout, the add methods will wait for up to the timeout period
- * to try and add the elements.
- *
- * @since 3.2
- * @version $Id$
- */
-public class BoundedBuffer extends SynchronizedBuffer implements BoundedCollection {
-
- /** The serialization version. */
- private static final long serialVersionUID = 1536432911093974264L;
-
- /** The maximum size. */
- private final int maximumSize;
- /** The timeout milliseconds. */
- private final long timeout;
-
- /**
- * Factory method to create a bounded buffer.
- *
- * When the buffer is full, it will immediately throw a
- * BufferOverflowException
on calling {@link #add(Object)}.
- *
- * @param the type of the elements in the buffer
- * @param buffer the buffer to decorate, must not be null
- * @param maximumSize the maximum size, must be size one or greater
- * @return a new bounded buffer
- * @throws IllegalArgumentException if the buffer is null
- * @throws IllegalArgumentException if the maximum size is zero or less
- */
- public static BoundedBuffer boundedBuffer(final Buffer buffer, final int maximumSize) {
- return new BoundedBuffer(buffer, maximumSize, 0L);
- }
-
- /**
- * Factory method to create a bounded buffer that blocks for a maximum
- * amount of time.
- *
- * @param the type of the elements in the buffer
- * @param buffer the buffer to decorate, must not be null
- * @param maximumSize the maximum size, must be size one or greater
- * @param timeout the maximum amount of time to wait in milliseconds
- * @return a new bounded buffer
- * @throws IllegalArgumentException if the buffer is null
- * @throws IllegalArgumentException if the maximum size is zero or less
- */
- public static BoundedBuffer boundedBuffer(final Buffer buffer, final int maximumSize,
- final long timeout) {
- return new BoundedBuffer(buffer, maximumSize, timeout);
- }
-
- //-----------------------------------------------------------------------
- /**
- * Constructor that wraps (not copies) another buffer, making it bounded
- * waiting only up to a maximum amount of time.
- *
- * @param buffer the buffer to wrap, must not be null
- * @param maximumSize the maximum size, must be size one or greater
- * @param timeout the maximum amount of time to wait
- * @throws IllegalArgumentException if the buffer is null
- * @throws IllegalArgumentException if the maximum size is zero or less
- */
- protected BoundedBuffer(final Buffer buffer, final int maximumSize, final long timeout) {
- super(buffer);
- if (maximumSize < 1) {
- throw new IllegalArgumentException();
- }
- this.maximumSize = maximumSize;
- this.timeout = timeout;
- }
-
- //-----------------------------------------------------------------------
- @Override
- public E remove() {
- synchronized (lock) {
- final E returnValue = decorated().remove();
- lock.notifyAll();
- return returnValue;
- }
- }
-
- @Override
- public boolean add(final E o) {
- synchronized (lock) {
- timeoutWait(1);
- return decorated().add(o);
- }
- }
-
- @Override
- public boolean addAll(final Collection extends E> c) {
- synchronized (lock) {
- timeoutWait(c.size());
- return decorated().addAll(c);
- }
- }
-
- @Override
- public Iterator iterator() {
- return new NotifyingIterator(collection.iterator());
- }
-
- /**
- * Waits up to the specified timeout period that the given number of additions
- * can be made to the buffer.
- *
- * @param nAdditions the number of additions
- * @throws BufferOverflowException if the number of additions would overflow the buffer,
- * or the timeout has expired
- */
- private void timeoutWait(final int nAdditions) {
- // method synchronized by callers
- if (nAdditions > maximumSize) {
- throw new BufferOverflowException(
- "Buffer size cannot exceed " + maximumSize);
- }
- if (timeout <= 0) {
- // no wait period (immediate timeout)
- if (decorated().size() + nAdditions > maximumSize) {
- throw new BufferOverflowException(
- "Buffer size cannot exceed " + maximumSize);
- }
- return;
- }
- final long expiration = System.currentTimeMillis() + timeout;
- long timeLeft = expiration - System.currentTimeMillis();
- while (timeLeft > 0 && decorated().size() + nAdditions > maximumSize) {
- try {
- lock.wait(timeLeft);
- timeLeft = expiration - System.currentTimeMillis();
- } catch (final InterruptedException ex) {
- final PrintWriter out = new PrintWriter(new StringWriter());
- ex.printStackTrace(out);
- throw new BufferUnderflowException(
- "Caused by InterruptedException: " + out.toString());
- }
- }
- if (decorated().size() + nAdditions > maximumSize) {
- throw new BufferOverflowException("Timeout expired");
- }
- }
-
- public boolean isFull() {
- // size() is synchronized
- return size() == maxSize();
- }
-
- public int maxSize() {
- return maximumSize;
- }
-
- //-----------------------------------------------------------------------
- /**
- * BoundedBuffer iterator.
- */
- private class NotifyingIterator extends AbstractIteratorDecorator {
-
- /**
- * Create a new {@link NotifyingIterator}.
- *
- * @param it the decorated {@link Iterator}
- */
- public NotifyingIterator(final Iterator it) {
- super(it);
- }
-
- @Override
- public void remove() {
- synchronized (lock) {
- iterator.remove();
- lock.notifyAll();
- }
- }
- }
-}
diff --git a/src/main/java/org/apache/commons/collections/buffer/BoundedFifoBuffer.java b/src/main/java/org/apache/commons/collections/buffer/BoundedFifoBuffer.java
deleted file mode 100644
index 499154f52..000000000
--- a/src/main/java/org/apache/commons/collections/buffer/BoundedFifoBuffer.java
+++ /dev/null
@@ -1,404 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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 java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.io.Serializable;
-import java.util.AbstractCollection;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.NoSuchElementException;
-
-import org.apache.commons.collections.BoundedCollection;
-import org.apache.commons.collections.Buffer;
-import org.apache.commons.collections.BufferOverflowException;
-import org.apache.commons.collections.BufferUnderflowException;
-
-/**
- * The {@link BoundedFifoBuffer} is a very efficient implementation of a
- * {@link Buffer} with a fixed size.
- *
- * The removal order of a {@link BoundedFifoBuffer} is based on the
- * insertion order; elements are removed in the same order in which they
- * were added. The iteration order is the same as the removal order.
- *
- * The {@link #add(Object)}, {@link #remove()} and {@link #get()} operations
- * all perform in constant time. All other operations perform in linear
- * time or worse.
- *
- * Note that this implementation is not synchronized. The following can be
- * used to provide synchronized access to your BoundedFifoBuffer
:
- *
- * Buffer fifo = BufferUtils.synchronizedBuffer(new BoundedFifoBuffer());
- *
- *
- * This buffer prevents null objects from being added.
- *
- * This class is Serializable from Commons Collections 3.1.
- *
- * @since 3.0 (previously in main package v2.1)
- * @version $Id$
- */
-public class BoundedFifoBuffer extends AbstractCollection
- implements Buffer, BoundedCollection, Serializable {
-
- /** Serialization version */
- private static final long serialVersionUID = 5603722811189451017L;
-
- /** Underlying storage array */
- private transient E[] elements;
-
- /** Array index of first (oldest) buffer element */
- private transient int start = 0;
-
- /**
- * Index mod maxElements of the array position following the last buffer
- * element. Buffer elements start at elements[start] and "wrap around"
- * elements[maxElements-1], ending at elements[decrement(end)].
- * For example, elements = {c,a,b}, start=1, end=1 corresponds to
- * the buffer [a,b,c].
- */
- private transient int end = 0;
-
- /** Flag to indicate if the buffer is currently full. */
- private transient boolean full = false;
-
- /** Capacity of the buffer */
- private final int maxElements;
-
- /**
- * Constructs a new BoundedFifoBuffer
big enough to hold
- * 32 elements.
- */
- public BoundedFifoBuffer() {
- this(32);
- }
-
- /**
- * Constructs a new BoundedFifoBuffer
big enough to hold
- * the specified number of elements.
- *
- * @param size the maximum number of elements for this fifo
- * @throws IllegalArgumentException if the size is less than 1
- */
- @SuppressWarnings("unchecked")
- public BoundedFifoBuffer(final int size) {
- if (size <= 0) {
- throw new IllegalArgumentException("The size must be greater than 0");
- }
- elements = (E[]) new Object[size];
- maxElements = elements.length;
- }
-
- /**
- * Constructs a new BoundedFifoBuffer
big enough to hold all
- * of the elements in the specified collection. That collection's
- * elements will also be added to the buffer.
- *
- * @param coll the collection whose elements to add, may not be null
- * @throws NullPointerException if the collection is null
- */
- public BoundedFifoBuffer(final Collection extends E> coll) {
- this(coll.size());
- addAll(coll);
- }
-
- //-----------------------------------------------------------------------
- /**
- * Write the buffer out using a custom routine.
- *
- * @param out the output stream
- * @throws IOException if an I/O error occurs while writing to the output stream
- */
- private void writeObject(final ObjectOutputStream out) throws IOException {
- out.defaultWriteObject();
- out.writeInt(size());
- for (final E e : this) {
- out.writeObject(e);
- }
- }
-
- /**
- * Read the buffer in using a custom routine.
- *
- * @param in the input stream
- * @throws IOException if an I/O error occurs while writing to the output stream
- * @throws ClassNotFoundException if the class of a serialized object can not be found
- */
- @SuppressWarnings("unchecked")
- private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
- in.defaultReadObject();
- elements = (E[]) new Object[maxElements];
- final int size = in.readInt();
- for (int i = 0; i < size; i++) {
- elements[i] = (E) in.readObject();
- }
- start = 0;
- full = size == maxElements;
- if (full) {
- end = 0;
- } else {
- end = size;
- }
- }
-
- //-----------------------------------------------------------------------
- /**
- * Returns the number of elements stored in the buffer.
- *
- * @return this buffer's size
- */
- @Override
- public int size() {
- int size = 0;
-
- if (end < start) {
- size = maxElements - start + end;
- } else if (end == start) {
- size = full ? maxElements : 0;
- } else {
- size = end - start;
- }
-
- return size;
- }
-
- /**
- * Returns true if this buffer is empty; false otherwise.
- *
- * @return true if this buffer is empty
- */
- @Override
- public boolean isEmpty() {
- return size() == 0;
- }
-
- /**
- * Returns true if this collection is full and no new elements can be added.
- *
- * @return true
if the collection is full
- */
- public boolean isFull() {
- return size() == maxElements;
- }
-
- /**
- * Gets the maximum size of the collection (the bound).
- *
- * @return the maximum number of elements the collection can hold
- */
- public int maxSize() {
- return maxElements;
- }
-
- /**
- * Clears this buffer.
- */
- @Override
- public void clear() {
- full = false;
- start = 0;
- end = 0;
- Arrays.fill(elements, null);
- }
-
- /**
- * Adds the given element to this buffer.
- *
- * @param element the element to add
- * @return true, always
- * @throws NullPointerException if the given element is null
- * @throws BufferOverflowException if this buffer is full
- */
- @Override
- public boolean add(final E element) {
- if (null == element) {
- throw new NullPointerException("Attempted to add null object to buffer");
- }
-
- if (full) {
- throw new BufferOverflowException("The buffer cannot hold more than " + maxElements + " objects.");
- }
-
- elements[end++] = element;
-
- if (end >= maxElements) {
- end = 0;
- }
-
- if (end == start) {
- full = true;
- }
-
- return true;
- }
-
- /**
- * Returns the least recently inserted element in this buffer.
- *
- * @return the least recently inserted element
- * @throws BufferUnderflowException if the buffer is empty
- */
- public E get() {
- if (isEmpty()) {
- throw new BufferUnderflowException("The buffer is already empty");
- }
- return elements[start];
- }
-
- /**
- * Returns the element at the specified position in this buffer.
- *
- * @param index the position of the element in the buffer
- * @return the element at position {@code index}
- * @throws NoSuchElementException if the requested position is outside the range [0, size)
- */
- public E get(final int index) {
- final int sz = size();
- if (index < 0 || index >= sz) {
- throw new NoSuchElementException(
- String.format("The specified index (%1$d) is outside the available range [0, %2$d)",
- index, sz));
- }
-
- final int idx = (start + index) % maxElements;
- return elements[idx];
- }
-
- /**
- * Removes the least recently inserted element from this buffer.
- *
- * @return the least recently inserted element
- * @throws BufferUnderflowException if the buffer is empty
- */
- public E remove() {
- if (isEmpty()) {
- throw new BufferUnderflowException("The buffer is already empty");
- }
-
- final E element = elements[start];
-
- if (null != element) {
- elements[start++] = null;
-
- if (start >= maxElements) {
- start = 0;
- }
- full = false;
- }
- return element;
- }
-
- /**
- * Increments the internal index.
- *
- * @param index the index to increment
- * @return the updated index
- */
- private int increment(int index) {
- index++;
- if (index >= maxElements) {
- index = 0;
- }
- return index;
- }
-
- /**
- * Decrements the internal index.
- *
- * @param index the index to decrement
- * @return the updated index
- */
- private int decrement(int index) {
- index--;
- if (index < 0) {
- index = maxElements - 1;
- }
- return index;
- }
-
- /**
- * Returns an iterator over this buffer's elements.
- *
- * @return an iterator over this buffer's elements
- */
- @Override
- public Iterator iterator() {
- return new Iterator() {
-
- private int index = start;
- private int lastReturnedIndex = -1;
- private boolean isFirst = full;
-
- public boolean hasNext() {
- return isFirst || index != end;
- }
-
- public E next() {
- if (!hasNext()) {
- throw new NoSuchElementException();
- }
- isFirst = false;
- lastReturnedIndex = index;
- index = increment(index);
- return elements[lastReturnedIndex];
- }
-
- public void remove() {
- if (lastReturnedIndex == -1) {
- throw new IllegalStateException();
- }
-
- // First element can be removed quickly
- if (lastReturnedIndex == start) {
- BoundedFifoBuffer.this.remove();
- lastReturnedIndex = -1;
- return;
- }
-
- int pos = lastReturnedIndex + 1;
- if (start < lastReturnedIndex && pos < end) {
- // shift in one part
- System.arraycopy(elements, pos, elements,
- lastReturnedIndex, end - pos);
- } else {
- // Other elements require us to shift the subsequent elements
- while (pos != end) {
- if (pos >= maxElements) {
- elements[pos - 1] = elements[0];
- pos = 0;
- } else {
- elements[decrement(pos)] = elements[pos];
- pos = increment(pos);
- }
- }
- }
-
- lastReturnedIndex = -1;
- end = decrement(end);
- elements[end] = null;
- full = false;
- index = decrement(index);
- }
-
- };
- }
-
-}
diff --git a/src/main/java/org/apache/commons/collections/buffer/CircularFifoBuffer.java b/src/main/java/org/apache/commons/collections/buffer/CircularFifoBuffer.java
deleted file mode 100644
index 7f993f3d0..000000000
--- a/src/main/java/org/apache/commons/collections/buffer/CircularFifoBuffer.java
+++ /dev/null
@@ -1,94 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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 java.util.Collection;
-
-/**
- * CircularFifoBuffer is a first in first out buffer with a fixed size that
- * replaces its oldest element if full.
- *
- * The removal order of a {@link CircularFifoBuffer} is based on the
- * insertion order; elements are removed in the same order in which they
- * were added. The iteration order is the same as the removal order.
- *
- * The {@link #add(Object)}, {@link #remove()} and {@link #get()} operations
- * all perform in constant time. All other operations perform in linear
- * time or worse.
- *
- * Note that this implementation is not synchronized. The following can be
- * used to provide synchronized access to your CircularFifoBuffer
:
- *
- * Buffer fifo = BufferUtils.synchronizedBuffer(new CircularFifoBuffer());
- *
- *
- * This buffer prevents null objects from being added.
- *
- * This class is Serializable from Commons Collections 3.1.
- *
- * @since 3.0
- * @version $Id$
- */
-public class CircularFifoBuffer extends BoundedFifoBuffer {
-
- /** Serialization version */
- private static final long serialVersionUID = -8423413834657610406L;
-
- /**
- * Constructor that creates a buffer with the default size of 32.
- */
- public CircularFifoBuffer() {
- super(32);
- }
-
- /**
- * Constructor that creates a buffer with the specified size.
- *
- * @param size the size of the buffer (cannot be changed)
- * @throws IllegalArgumentException if the size is less than 1
- */
- public CircularFifoBuffer(final int size) {
- super(size);
- }
-
- /**
- * Constructor that creates a buffer from the specified collection.
- * The collection size also sets the buffer size
- *
- * @param coll the collection to copy into the buffer, may not be null
- * @throws NullPointerException if the collection is null
- */
- public CircularFifoBuffer(final Collection coll) {
- super(coll);
- }
-
- /**
- * If the buffer is full, the least recently added element is discarded so
- * that a new element can be inserted.
- *
- * @param element the element to add
- * @return true, always
- */
- @Override
- public boolean add(final E element) {
- if (isFull()) {
- remove();
- }
- return super.add(element);
- }
-
-}
diff --git a/src/main/java/org/apache/commons/collections/buffer/PredicatedBuffer.java b/src/main/java/org/apache/commons/collections/buffer/PredicatedBuffer.java
deleted file mode 100644
index 6a82d55db..000000000
--- a/src/main/java/org/apache/commons/collections/buffer/PredicatedBuffer.java
+++ /dev/null
@@ -1,98 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.Predicate;
-import org.apache.commons.collections.collection.PredicatedCollection;
-
-/**
- * Decorates another {@link Buffer} to validate that additions
- * match a specified predicate.
- *
- * This buffer exists to provide validation for the decorated buffer.
- * It is normally created to decorate an empty buffer.
- * If an object cannot be added to the buffer, an IllegalArgumentException is thrown.
- *
- * One usage would be to ensure that no null entries are added to the buffer.
- *
Buffer buffer = PredicatedBuffer.decorate(new UnboundedFifoBuffer(), NotNullPredicate.INSTANCE);
- *
- * This class is Serializable from Commons Collections 3.1.
- *
- * @since 3.0
- * @version $Id$
- */
-public class PredicatedBuffer extends PredicatedCollection implements Buffer {
-
- /** Serialization version */
- private static final long serialVersionUID = 2307609000539943581L;
-
- /**
- * Factory method to create a predicated (validating) buffer.
- *
- * If there are any elements already in the buffer being decorated, they
- * are validated.
- *
- * @param the type of the elements in the buffer
- * @param buffer the buffer to decorate, must not be null
- * @param predicate the predicate to use for validation, must not be null
- * @return a new predicated Buffer
- * @throws IllegalArgumentException if buffer or predicate is null
- * @throws IllegalArgumentException if the buffer contains invalid elements
- */
- public static PredicatedBuffer predicatedBuffer(final Buffer buffer,
- final Predicate super E> predicate) {
- return new PredicatedBuffer(buffer, predicate);
- }
-
- //-----------------------------------------------------------------------
- /**
- * Constructor that wraps (not copies).
- *
- * If there are any elements already in the collection being decorated, they
- * are validated.
- *
- * @param buffer the buffer to decorate, must not be null
- * @param predicate the predicate to use for validation, must not be null
- * @throws IllegalArgumentException if buffer or predicate is null
- * @throws IllegalArgumentException if the buffer contains invalid elements
- */
- protected PredicatedBuffer(final Buffer buffer, final Predicate super E> predicate) {
- super(buffer, predicate);
- }
-
- /**
- * Gets the buffer being decorated.
- *
- * @return the decorated buffer
- */
- @Override
- protected Buffer decorated() {
- return (Buffer) super.decorated();
- }
-
- //-----------------------------------------------------------------------
-
- public E get() {
- return decorated().get();
- }
-
- public E remove() {
- return decorated().remove();
- }
-
-}
diff --git a/src/main/java/org/apache/commons/collections/buffer/PriorityBuffer.java b/src/main/java/org/apache/commons/collections/buffer/PriorityBuffer.java
deleted file mode 100644
index 6271b9c8d..000000000
--- a/src/main/java/org/apache/commons/collections/buffer/PriorityBuffer.java
+++ /dev/null
@@ -1,544 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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 java.io.Serializable;
-import java.util.AbstractCollection;
-import java.util.Comparator;
-import java.util.Iterator;
-import java.util.NoSuchElementException;
-
-import org.apache.commons.collections.Buffer;
-import org.apache.commons.collections.BufferUnderflowException;
-import org.apache.commons.collections.comparators.ComparableComparator;
-
-/**
- * Binary heap implementation of {@link Buffer} that provides for
- * removal based on Comparator
ordering.
- *
- * The removal order of a binary heap is based on either the natural sort
- * order of its elements or a specified {@link Comparator}. The
- * {@link #remove()} method always returns the first element as determined
- * by the sort order. (The ascendingOrder
flag in the constructors
- * can be used to reverse the sort order, in which case {@link #remove()}
- * will always remove the last element.) The removal order is
- * not the same as the order of iteration; elements are
- * returned by the iterator in no particular order.
- *
- * The {@link #add(Object)} and {@link #remove()} operations perform
- * in logarithmic time. The {@link #get()} operation performs in constant
- * time. All other operations perform in linear time or worse.
- *
- * Note that this implementation is not synchronized. Use
- * {@link org.apache.commons.collections.BufferUtils#synchronizedBuffer(Buffer)} or
- * {@link org.apache.commons.collections.buffer.SynchronizedBuffer#synchronizedBuffer(Buffer)}
- * to provide synchronized access to a PriorityBuffer
:
- *
- * Buffer heap = SynchronizedBuffer.decorate(new PriorityBuffer());
- *
- *
- * This class is Serializable from Commons Collections 3.2.
- *
- * @since 3.0 (previously BinaryHeap v1.0)
- * @version $Id$
- */
-public class PriorityBuffer extends AbstractCollection implements Buffer, Serializable {
-
- /** Serialization lock. */
- private static final long serialVersionUID = 6891186490470027896L;
-
- /**
- * The default capacity for the buffer.
- */
- private static final int DEFAULT_CAPACITY = 13;
-
- /**
- * The elements in this buffer.
- */
- protected E[] elements;
-
- /**
- * The number of elements currently in this buffer.
- */
- protected int size;
-
- /**
- * If true, the first element as determined by the sort order will
- * be returned. If false, the last element as determined by the
- * sort order will be returned.
- */
- protected boolean ascendingOrder;
-
- /**
- * The comparator used to order the elements
- */
- protected Comparator super E> comparator;
-
- //-----------------------------------------------------------------------
- /**
- * Constructs a new empty buffer that sorts in ascending order by the
- * natural order of the objects added.
- */
- public PriorityBuffer() {
- this(DEFAULT_CAPACITY, true, null);
- }
-
- /**
- * Constructs a new empty buffer that sorts in ascending order using the
- * specified comparator.
- *
- * @param comparator the comparator used to order the elements,
- * null means use natural order
- */
- public PriorityBuffer(final Comparator super E> comparator) {
- this(DEFAULT_CAPACITY, true, comparator);
- }
-
- /**
- * Constructs a new empty buffer specifying the sort order and using the
- * natural order of the objects added.
- *
- * @param ascendingOrder if true
the heap is created as a
- * minimum heap; otherwise, the heap is created as a maximum heap
- */
- public PriorityBuffer(final boolean ascendingOrder) {
- this(DEFAULT_CAPACITY, ascendingOrder, null);
- }
-
- /**
- * Constructs a new empty buffer specifying the sort order and comparator.
- *
- * @param ascendingOrder true to use the order imposed by the given
- * comparator; false to reverse that order
- * @param comparator the comparator used to order the elements,
- * null means use natural order
- */
- public PriorityBuffer(final boolean ascendingOrder, final Comparator super E> comparator) {
- this(DEFAULT_CAPACITY, ascendingOrder, comparator);
- }
-
- /**
- * Constructs a new empty buffer that sorts in ascending order by the
- * natural order of the objects added, specifying an initial capacity.
- *
- * @param capacity the initial capacity for the buffer, greater than zero
- * @throws IllegalArgumentException if capacity
is <= 0
- */
- public PriorityBuffer(final int capacity) {
- this(capacity, true, null);
- }
-
- /**
- * Constructs a new empty buffer that sorts in ascending order using the
- * specified comparator and initial capacity.
- *
- * @param capacity the initial capacity for the buffer, greater than zero
- * @param comparator the comparator used to order the elements,
- * null means use natural order
- * @throws IllegalArgumentException if capacity
is <= 0
- */
- public PriorityBuffer(final int capacity, final Comparator super E> comparator) {
- this(capacity, true, comparator);
- }
-
- /**
- * Constructs a new empty buffer that specifying initial capacity and
- * sort order, using the natural order of the objects added.
- *
- * @param capacity the initial capacity for the buffer, greater than zero
- * @param ascendingOrder if true
the heap is created as a
- * minimum heap; otherwise, the heap is created as a maximum heap.
- * @throws IllegalArgumentException if capacity
is <= 0
- */
- public PriorityBuffer(final int capacity, final boolean ascendingOrder) {
- this(capacity, ascendingOrder, null);
- }
-
- /**
- * Constructs a new empty buffer that specifying initial capacity,
- * sort order and comparator.
- *
- * @param capacity the initial capacity for the buffer, greater than zero
- * @param ascendingOrder true to use the order imposed by the given
- * comparator; false to reverse that order
- * @param comparator the comparator used to order the elements,
- * null means use natural order
- * @throws IllegalArgumentException if capacity
is <= 0
- */
- @SuppressWarnings("unchecked")
- public PriorityBuffer(final int capacity, final boolean ascendingOrder, final Comparator super E> comparator) {
- super();
- if (capacity <= 0) {
- throw new IllegalArgumentException("invalid capacity");
- }
- this.ascendingOrder = ascendingOrder;
-
- //+1 as 0 is noop
- this.elements = (E[]) new Object[capacity + 1];
- this.comparator = (Comparator super E>) (comparator == null ? ComparableComparator.INSTANCE : comparator);
- }
-
- //-----------------------------------------------------------------------
- /**
- * Checks whether the heap is ascending or descending order.
- *
- * @return true if ascending order (a min heap)
- */
- public boolean isAscendingOrder() {
- return ascendingOrder;
- }
-
- /**
- * Gets the comparator being used for this buffer, null is natural order.
- *
- * @return the comparator in use, null is natural order
- */
- public Comparator super E> comparator() {
- return comparator;
- }
-
- //-----------------------------------------------------------------------
- /**
- * Returns the number of elements in this buffer.
- *
- * @return the number of elements in this buffer
- */
- @Override
- public int size() {
- return size;
- }
-
- /**
- * Clears all elements from the buffer.
- */
- @Override
- @SuppressWarnings("unchecked")
- public void clear() {
- elements = (E[]) new Object[elements.length]; // for gc
- size = 0;
- }
-
- /**
- * Adds an element to the buffer.
- *
- * The element added will be sorted according to the comparator in use.
- *
- * @param element the element to be added
- * @return true always
- */
- @Override
- public boolean add(final E element) {
- if (isAtCapacity()) {
- grow();
- }
- // percolate element to its place in tree
- if (ascendingOrder) {
- percolateUpMinHeap(element);
- } else {
- percolateUpMaxHeap(element);
- }
- return true;
- }
-
- /**
- * Gets the next element to be removed without actually removing it (peek).
- *
- * @return the next element
- * @throws BufferUnderflowException if the buffer is empty
- */
- public E get() {
- if (isEmpty()) {
- throw new BufferUnderflowException();
- }
- return elements[1];
- }
-
- /**
- * Gets and removes the next element (pop).
- *
- * @return the next element
- * @throws BufferUnderflowException if the buffer is empty
- */
- public E remove() {
- final E result = get();
- elements[1] = elements[size--];
-
- // set the unused element to 'null' so that the garbage collector
- // can free the object if not used anywhere else.(remove reference)
- elements[size + 1] = null;
-
- if (size != 0) {
- // percolate top element to it's place in tree
- if (ascendingOrder) {
- percolateDownMinHeap(1);
- } else {
- percolateDownMaxHeap(1);
- }
- }
-
- return result;
- }
-
- //-----------------------------------------------------------------------
- /**
- * Tests if the buffer is at capacity.
- *
- * @return true
if buffer is full; false
otherwise.
- */
- protected boolean isAtCapacity() {
- //+1 as element 0 is noop
- return elements.length == size + 1;
- }
-
-
- /**
- * Percolates element down heap from the position given by the index.
- *
- * Assumes it is a minimum heap.
- *
- * @param index the index for the element
- */
- protected void percolateDownMinHeap(final int index) {
- final E element = elements[index];
- int hole = index;
-
- while (hole * 2 <= size) {
- int child = hole * 2;
-
- // if we have a right child and that child can not be percolated
- // up then move onto other child
- if (child != size && compare(elements[child + 1], elements[child]) < 0) {
- child++;
- }
-
- // if we found resting place of bubble then terminate search
- if (compare(elements[child], element) >= 0) {
- break;
- }
-
- elements[hole] = elements[child];
- hole = child;
- }
-
- elements[hole] = element;
- }
-
- /**
- * Percolates element down heap from the position given by the index.
- *
- * Assumes it is a maximum heap.
- *
- * @param index the index of the element
- */
- protected void percolateDownMaxHeap(final int index) {
- final E element = elements[index];
- int hole = index;
-
- while (hole * 2 <= size) {
- int child = hole * 2;
-
- // if we have a right child and that child can not be percolated
- // up then move onto other child
- if (child != size && compare(elements[child + 1], elements[child]) > 0) {
- child++;
- }
-
- // if we found resting place of bubble then terminate search
- if (compare(elements[child], element) <= 0) {
- break;
- }
-
- elements[hole] = elements[child];
- hole = child;
- }
-
- elements[hole] = element;
- }
-
- /**
- * Percolates element up heap from the position given by the index.
- *
- * Assumes it is a minimum heap.
- *
- * @param index the index of the element to be percolated up
- */
- protected void percolateUpMinHeap(final int index) {
- int hole = index;
- final E element = elements[hole];
- while (hole > 1 && compare(element, elements[hole / 2]) < 0) {
- // save element that is being pushed down
- // as the element "bubble" is percolated up
- final int next = hole / 2;
- elements[hole] = elements[next];
- hole = next;
- }
- elements[hole] = element;
- }
-
- /**
- * Percolates a new element up heap from the bottom.
- *
- * Assumes it is a minimum heap.
- *
- * @param element the element
- */
- protected void percolateUpMinHeap(final E element) {
- elements[++size] = element;
- percolateUpMinHeap(size);
- }
-
- /**
- * Percolates element up heap from from the position given by the index.
- *
- * Assume it is a maximum heap.
- *
- * @param index the index of the element to be percolated up
- */
- protected void percolateUpMaxHeap(final int index) {
- int hole = index;
- final E element = elements[hole];
-
- while (hole > 1 && compare(element, elements[hole / 2]) > 0) {
- // save element that is being pushed down
- // as the element "bubble" is percolated up
- final int next = hole / 2;
- elements[hole] = elements[next];
- hole = next;
- }
-
- elements[hole] = element;
- }
-
- /**
- * Percolates a new element up heap from the bottom.
- *
- * Assume it is a maximum heap.
- *
- * @param element the element
- */
- protected void percolateUpMaxHeap(final E element) {
- elements[++size] = element;
- percolateUpMaxHeap(size);
- }
-
- /**
- * Compares two objects using the comparator if specified, or the
- * natural order otherwise.
- *
- * @param a the first object
- * @param b the second object
- * @return -ve if a less than b, 0 if they are equal, +ve if a greater than b
- */
- protected int compare(final E a, final E b) {
- return comparator.compare(a, b);
- }
-
- /**
- * Increases the size of the heap to support additional elements
- */
- @SuppressWarnings("unchecked")
- protected void grow() {
- final E[] array = (E[]) new Object[elements.length * 2];
- System.arraycopy(elements, 0, array, 0, elements.length);
- elements = array;
- }
-
- //-----------------------------------------------------------------------
- /**
- * Returns an iterator over this heap's elements.
- *
- * @return an iterator over this heap's elements
- */
- @Override
- public Iterator iterator() {
- return new Iterator() {
-
- private int index = 1;
- private int lastReturnedIndex = -1;
-
- public boolean hasNext() {
- return index <= size;
- }
-
- public E next() {
- if (!hasNext()) {
- throw new NoSuchElementException();
- }
- lastReturnedIndex = index;
- index++;
- return elements[lastReturnedIndex];
- }
-
- public void remove() {
- if (lastReturnedIndex == -1) {
- throw new IllegalStateException();
- }
- elements[ lastReturnedIndex ] = elements[ size ];
- elements[ size ] = null;
- size--;
- if( size != 0 && lastReturnedIndex <= size) {
- int compareToParent = 0;
- if (lastReturnedIndex > 1) {
- compareToParent = compare(elements[lastReturnedIndex],
- elements[lastReturnedIndex / 2]);
- }
- if (ascendingOrder) {
- if (lastReturnedIndex > 1 && compareToParent < 0) {
- percolateUpMinHeap(lastReturnedIndex);
- } else {
- percolateDownMinHeap(lastReturnedIndex);
- }
- } else { // max heap
- if (lastReturnedIndex > 1 && compareToParent > 0) {
- percolateUpMaxHeap(lastReturnedIndex);
- } else {
- percolateDownMaxHeap(lastReturnedIndex);
- }
- }
- }
- index--;
- lastReturnedIndex = -1;
- }
-
- };
- }
-
- /**
- * Returns a string representation of this heap. The returned string
- * is similar to those produced by standard JDK collections.
- *
- * @return a string representation of this heap
- */
- @Override
- public String toString() {
- final StringBuilder sb = new StringBuilder();
-
- sb.append("[ ");
-
- for (int i = 1; i < size + 1; i++) {
- if (i != 1) {
- sb.append(", ");
- }
- sb.append(elements[i]);
- }
-
- sb.append(" ]");
-
- return sb.toString();
- }
-
-}
diff --git a/src/main/java/org/apache/commons/collections/buffer/SynchronizedBuffer.java b/src/main/java/org/apache/commons/collections/buffer/SynchronizedBuffer.java
deleted file mode 100644
index 92580d81b..000000000
--- a/src/main/java/org/apache/commons/collections/buffer/SynchronizedBuffer.java
+++ /dev/null
@@ -1,99 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.collection.SynchronizedCollection;
-
-/**
- * Decorates another {@link Buffer} to synchronize its behaviour
- * for a multi-threaded environment.
- *
- * Methods are synchronized, then forwarded to the decorated buffer.
- *
- * This class is Serializable from Commons Collections 3.1.
- *
- * @param the type of the elements in the buffer
- * @since 3.0
- * @version $Id$
- */
-public class SynchronizedBuffer
- extends SynchronizedCollection
- implements Buffer {
-
- /** Serialization version */
- private static final long serialVersionUID = -6859936183953626253L;
-
- /**
- * Factory method to create a synchronized buffer.
- *
- * @param 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 SynchronizedBuffer synchronizedBuffer(final Buffer buffer) {
- return new SynchronizedBuffer(buffer);
- }
-
- //-----------------------------------------------------------------------
- /**
- * Constructor that wraps (not copies).
- *
- * @param buffer the buffer to decorate, must not be null
- * @throws IllegalArgumentException if the buffer is null
- */
- protected SynchronizedBuffer(final Buffer buffer) {
- super(buffer);
- }
-
- /**
- * Constructor that wraps (not copies).
- *
- * @param buffer the buffer to decorate, must not be null
- * @param lock the lock object to use, must not be null
- * @throws IllegalArgumentException if the buffer is null
- */
- protected SynchronizedBuffer(final Buffer buffer, final Object lock) {
- super(buffer, lock);
- }
-
- /**
- * Gets the buffer being decorated.
- *
- * @return the decorated buffer
- */
- @Override
- protected Buffer decorated() {
- return (Buffer) super.decorated();
- }
-
- //-----------------------------------------------------------------------
-
- public E get() {
- synchronized (lock) {
- return decorated().get();
- }
- }
-
- public E remove() {
- synchronized (lock) {
- return decorated().remove();
- }
- }
-
-}
diff --git a/src/main/java/org/apache/commons/collections/buffer/TransformedBuffer.java b/src/main/java/org/apache/commons/collections/buffer/TransformedBuffer.java
deleted file mode 100644
index d3fd6c475..000000000
--- a/src/main/java/org/apache/commons/collections/buffer/TransformedBuffer.java
+++ /dev/null
@@ -1,123 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.Transformer;
-import org.apache.commons.collections.collection.TransformedCollection;
-
-/**
- * Decorates another {@link Buffer} to transform objects that are added.
- *
- * The add methods are affected by this class.
- * Thus objects must be removed or searched for using their transformed form.
- * For example, if the transformation converts Strings to Integers, you must
- * use the Integer form to remove objects.
- *
- * This class is Serializable from Commons Collections 3.1.
- *
- * @since 3.0
- * @version $Id$
- */
-public class TransformedBuffer extends TransformedCollection implements Buffer {
-
- /** Serialization version */
- private static final long serialVersionUID = -7901091318986132033L;
-
- /**
- * Factory method to create a transforming buffer.
- *
- * If there are any elements already in the buffer being decorated, they
- * are NOT transformed.
- * Contrast this with {@link #transformedBuffer(Buffer, Transformer)}.
- *
- * @param the type of the elements in the buffer
- * @param buffer the buffer to decorate, must not be null
- * @param transformer the transformer to use for conversion, must not be null
- * @return a new transformed Buffer
- * @throws IllegalArgumentException if buffer or transformer is null
- */
- public static TransformedBuffer transformingBuffer(final Buffer buffer,
- final Transformer super E, ? extends E> transformer) {
- return new TransformedBuffer(buffer, transformer);
- }
-
- /**
- * Factory method to create a transforming buffer that will transform
- * existing contents of the specified buffer.
- *
- * If there are any elements already in the buffer being decorated, they
- * will be transformed by this method.
- * Contrast this with {@link #transformingBuffer(Buffer, Transformer)}.
- *
- * @param the type of the elements in the buffer
- * @param buffer the buffer to decorate, must not be null
- * @param transformer the transformer to use for conversion, must not be null
- * @return a new transformed Buffer
- * @throws IllegalArgumentException if buffer or transformer is null
- * @since 4.0
- */
- public static TransformedBuffer transformedBuffer(final Buffer buffer,
- final Transformer super E, ? extends E> transformer) {
- // throws IAE if buffer or transformer is null
- final TransformedBuffer decorated = new TransformedBuffer(buffer, transformer);
- if (buffer.size() > 0) {
- @SuppressWarnings("unchecked") // buffer is type
- final E[] values = (E[]) buffer.toArray();
- buffer.clear();
- for (final E value : values) {
- decorated.decorated().add(transformer.transform(value));
- }
- }
- return decorated;
- }
-
- //-----------------------------------------------------------------------
- /**
- * Constructor that wraps (not copies).
- *
- * If there are any elements already in the buffer being decorated, they
- * are NOT transformed.
- *
- * @param buffer the buffer to decorate, must not be null
- * @param transformer the transformer to use for conversion, must not be null
- * @throws IllegalArgumentException if buffer or transformer is null
- */
- protected TransformedBuffer(final Buffer buffer, final Transformer super E, ? extends E> transformer) {
- super(buffer, transformer);
- }
-
- /**
- * Gets the decorated buffer.
- *
- * @return the decorated buffer
- */
- protected Buffer getBuffer() {
- return (Buffer) collection;
- }
-
- //-----------------------------------------------------------------------
-
- public E get() {
- return getBuffer().get();
- }
-
- public E remove() {
- return getBuffer().remove();
- }
-
-}
diff --git a/src/main/java/org/apache/commons/collections/buffer/UnboundedFifoBuffer.java b/src/main/java/org/apache/commons/collections/buffer/UnboundedFifoBuffer.java
deleted file mode 100644
index 87c529a59..000000000
--- a/src/main/java/org/apache/commons/collections/buffer/UnboundedFifoBuffer.java
+++ /dev/null
@@ -1,318 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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 java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.io.Serializable;
-import java.util.AbstractCollection;
-import java.util.Iterator;
-import java.util.NoSuchElementException;
-
-import org.apache.commons.collections.Buffer;
-import org.apache.commons.collections.BufferUnderflowException;
-
-/**
- * UnboundedFifoBuffer is a very efficient implementation of
- * {@link Buffer} that can grow to any size.
- * According to performance testing, it exhibits a constant access time, but it
- * also outperforms ArrayList when used for the same purpose.
- *
- * The removal order of an {@link UnboundedFifoBuffer} is based on the insertion
- * order; elements are removed in the same order in which they were added.
- * The iteration order is the same as the removal order.
- *
- * The {@link #remove()} and {@link #get()} operations perform in constant time.
- * The {@link #add(Object)} operation performs in amortized constant time. All
- * other operations perform in linear time or worse.
- *
- * Note that this implementation is not synchronized. The following can be
- * used to provide synchronized access to your UnboundedFifoBuffer
:
- *
- * Buffer fifo = BufferUtils.synchronizedBuffer(new UnboundedFifoBuffer());
- *
- *
- * This buffer prevents null objects from being added.
- *
- * This class is Serializable from Commons Collections 3.1.
- *
- * @since 3.0 (previously in main package v2.1)
- * @version $Id$
- */
-public class UnboundedFifoBuffer extends AbstractCollection implements Buffer, Serializable {
- // invariant: buffer.length > size()
- // ie.buffer always has at least one empty entry
-
- /** Serialization version */
- private static final long serialVersionUID = -3482960336579541419L;
-
- /** The array of objects in the buffer. */
- protected transient E[] buffer;
-
- /** The current head index. */
- protected transient int head;
-
- /** The current tail index. */
- protected transient int tail;
-
- /**
- * Constructs an UnboundedFifoBuffer with the default number of elements.
- * It is exactly the same as performing the following:
- *
- *
- * new UnboundedFifoBuffer(32);
- *
- */
- public UnboundedFifoBuffer() {
- this(32);
- }
-
- /**
- * Constructs an UnboundedFifoBuffer with the specified number of elements.
- * The integer must be a positive integer.
- *
- * @param initialSize the initial size of the buffer
- * @throws IllegalArgumentException if the size is less than 1
- */
- @SuppressWarnings("unchecked")
- public UnboundedFifoBuffer(final int initialSize) {
- if (initialSize <= 0) {
- throw new IllegalArgumentException("The size must be greater than 0");
- }
- buffer = (E[]) new Object[initialSize + 1];
- head = 0;
- tail = 0;
- }
-
- //-----------------------------------------------------------------------
- /**
- * Write the buffer out using a custom routine.
- *
- * @param out the output stream
- * @throws IOException if an I/O error occurs while writing to the output stream
- */
- private void writeObject(final ObjectOutputStream out) throws IOException {
- out.defaultWriteObject();
- out.writeInt(size());
- out.writeInt(buffer.length);
- for (final E e : this) {
- out.writeObject(e);
- }
- }
-
- /**
- * Read the buffer in using a custom routine.
- *
- * @param in the input stream
- * @throws IOException if an I/O error occurs while reading from the input stream
- * @throws ClassNotFoundException if the class of a serialized object can not be found
- */
- @SuppressWarnings("unchecked")
- private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
- in.defaultReadObject();
- final int size = in.readInt();
- final int length = in.readInt();
- buffer = (E[]) new Object[length];
- for (int i = 0; i < size; i++) {
- buffer[i] = (E) in.readObject();
- }
- head = 0;
- tail = size;
- }
-
- //-----------------------------------------------------------------------
- /**
- * Returns the number of elements stored in the buffer.
- *
- * @return this buffer's size
- */
- @Override
- public int size() {
- int size = 0;
-
- if (tail < head) {
- size = buffer.length - head + tail;
- } else {
- size = tail - head;
- }
-
- return size;
- }
-
- /**
- * Returns true if this buffer is empty; false otherwise.
- *
- * @return true if this buffer is empty
- */
- @Override
- public boolean isEmpty() {
- return size() == 0;
- }
-
- /**
- * Adds the given element to this buffer.
- *
- * @param obj the element to add
- * @return true, always
- * @throws NullPointerException if the given element is null
- */
- @Override
- @SuppressWarnings("unchecked")
- public boolean add(final E obj) {
- if (obj == null) {
- throw new NullPointerException("Attempted to add null object to buffer");
- }
-
- if (size() + 1 >= buffer.length) {
- // copy contents to a new buffer array
- final E[] tmp = (E[]) new Object[(buffer.length - 1) * 2 + 1];
- int j = 0;
- // move head to element zero in the new array
- for (int i = head; i != tail;) {
- tmp[j] = buffer[i];
- buffer[i] = null;
-
- j++;
- i = increment(i);
- }
- buffer = tmp;
- head = 0;
- tail = j;
- }
-
- buffer[tail] = obj;
- tail = increment(tail);
- return true;
- }
-
- /**
- * Returns the next object in the buffer.
- *
- * @return the next object in the buffer
- * @throws BufferUnderflowException if this buffer is empty
- */
- public E get() {
- if (isEmpty()) {
- throw new BufferUnderflowException("The buffer is already empty");
- }
-
- return buffer[head];
- }
-
- /**
- * Removes the next object from the buffer
- *
- * @return the removed object
- * @throws BufferUnderflowException if this buffer is empty
- */
- public E remove() {
- if (isEmpty()) {
- throw new BufferUnderflowException("The buffer is already empty");
- }
-
- final E element = buffer[head];
- if (element != null) {
- buffer[head] = null;
- head = increment(head);
- }
- return element;
- }
-
- /**
- * Increments the internal index.
- *
- * @param index the index to increment
- * @return the updated index
- */
- private int increment(int index) {
- index++;
- if (index >= buffer.length) {
- index = 0;
- }
- return index;
- }
-
- /**
- * Decrements the internal index.
- *
- * @param index the index to decrement
- * @return the updated index
- */
- private int decrement(int index) {
- index--;
- if (index < 0) {
- index = buffer.length - 1;
- }
- return index;
- }
-
- /**
- * Returns an iterator over this buffer's elements.
- *
- * @return an iterator over this buffer's elements
- */
- @Override
- public Iterator iterator() {
- return new Iterator() {
-
- private int index = head;
- private int lastReturnedIndex = -1;
-
- public boolean hasNext() {
- return index != tail;
-
- }
-
- public E next() {
- if (!hasNext()) {
- throw new NoSuchElementException();
- }
- lastReturnedIndex = index;
- index = increment(index);
- return buffer[lastReturnedIndex];
- }
-
- public void remove() {
- if (lastReturnedIndex == -1) {
- throw new IllegalStateException();
- }
-
- // First element can be removed quickly
- if (lastReturnedIndex == head) {
- UnboundedFifoBuffer.this.remove();
- lastReturnedIndex = -1;
- return;
- }
-
- // Other elements require us to shift the subsequent elements
- int i = increment(lastReturnedIndex);
- while (i != tail) {
- buffer[decrement(i)] = buffer[i];
- i = increment(i);
- }
-
- lastReturnedIndex = -1;
- tail = decrement(tail);
- buffer[tail] = null;
- index = decrement(index);
- }
-
- };
- }
-
-}
diff --git a/src/main/java/org/apache/commons/collections/buffer/UnmodifiableBuffer.java b/src/main/java/org/apache/commons/collections/buffer/UnmodifiableBuffer.java
deleted file mode 100644
index 5ffad87c7..000000000
--- a/src/main/java/org/apache/commons/collections/buffer/UnmodifiableBuffer.java
+++ /dev/null
@@ -1,142 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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 java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.io.Serializable;
-import java.util.Collection;
-import java.util.Iterator;
-
-import org.apache.commons.collections.Buffer;
-import org.apache.commons.collections.Unmodifiable;
-import org.apache.commons.collections.iterators.UnmodifiableIterator;
-
-/**
- * Decorates another {@link Buffer} to ensure it can't be altered.
- *
- * This class is Serializable from Commons Collections 3.1.
- *
- * Attempts to modify it will result in an UnsupportedOperationException.
- *
- * @since 3.0
- * @version $Id$
- */
-public final class UnmodifiableBuffer
- extends AbstractBufferDecorator
- implements Unmodifiable, Serializable {
-
- /** Serialization version */
- private static final long serialVersionUID = 1832948656215393357L;
-
- /**
- * Factory method to create an unmodifiable buffer.
- *
- * If the buffer passed in is already unmodifiable, it is returned.
- *
- * @param the type of the elements in the buffer
- * @param buffer the buffer to decorate, must not be null
- * @return an unmodifiable Buffer
- * @throws IllegalArgumentException if buffer is null
- */
- public static Buffer unmodifiableBuffer(final Buffer buffer) {
- if (buffer instanceof Unmodifiable) {
- return buffer;
- }
- return new UnmodifiableBuffer(buffer);
- }
-
- //-----------------------------------------------------------------------
- /**
- * Constructor that wraps (not copies).
- *
- * @param buffer the buffer to decorate, must not be null
- * @throws IllegalArgumentException if buffer is null
- */
- private UnmodifiableBuffer(final Buffer buffer) {
- super(buffer);
- }
-
- //-----------------------------------------------------------------------
- /**
- * Write the collection out using a custom routine.
- *
- * @param out the output stream
- * @throws IOException if an I/O error occurs while writing to the output stream
- */
- private void writeObject(final ObjectOutputStream out) throws IOException {
- out.defaultWriteObject();
- out.writeObject(collection);
- }
-
- /**
- * Read the collection in using a custom routine.
- *
- * @param in the input stream
- * @throws IOException if an I/O error occurs while reading from the input stream
- * @throws ClassNotFoundException if the class of a serialized object can not be found
- */
- @SuppressWarnings("unchecked")
- private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
- in.defaultReadObject();
- collection = (Collection) in.readObject();
- }
-
- //-----------------------------------------------------------------------
- @Override
- public Iterator iterator() {
- return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
- }
-
- @Override
- public boolean add(final Object object) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public boolean addAll(final Collection extends E> coll) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void clear() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public boolean remove(final Object object) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public boolean removeAll(final Collection> coll) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public boolean retainAll(final Collection> coll) {
- throw new UnsupportedOperationException();
- }
-
- //-----------------------------------------------------------------------
- @Override
- public E remove() {
- throw new UnsupportedOperationException();
- }
-
-}
diff --git a/src/main/java/org/apache/commons/collections/buffer/package-info.java b/src/main/java/org/apache/commons/collections/buffer/package-info.java
deleted file mode 100644
index da1031260..000000000
--- a/src/main/java/org/apache/commons/collections/buffer/package-info.java
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.
- */
-/**
- * This package contains implementations of the
- * {@link org.apache.commons.collections.Buffer Buffer} interface.
- *
- * The following implementations are provided in the package:
- *
- * PriorityBuffer - provides for removal based on a comparator ordering
- * BoundedFifoBuffer - implements a buffer with a fixed size that throws exceptions when full
- * CircularFifoBuffer - implements a buffer with a fixed size that discards oldest when full
- * UnboundedFifoBuffer - implements a buffer that grows in size if necessary
- *
- *
- * The following decorators are provided in the package:
- *
- * Synchronized - synchronizes method access for multi-threaded environments
- * Unmodifiable - ensures the collection cannot be altered
- * Predicated - ensures that only elements that are valid according to a predicate can be added
- * Transformed - transforms elements added to the buffer
- * Blocking - blocks on get and remove until an element is available
- *
- *
- * @version $Id$
- */
-package org.apache.commons.collections.buffer;
diff --git a/src/test/java/org/apache/commons/collections/BufferUtilsTest.java b/src/test/java/org/apache/commons/collections/BufferUtilsTest.java
deleted file mode 100644
index 273f22a15..000000000
--- a/src/test/java/org/apache/commons/collections/BufferUtilsTest.java
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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;
-
-import junit.framework.Test;
-
-import org.apache.commons.collections.buffer.PredicatedBuffer;
-import org.apache.commons.collections.ArrayStack;
-
-/**
- * Tests for BufferUtils.
- *
- * @version $Revision$
- *
- * @author Unknown
- */
-public class BufferUtilsTest extends BulkTest {
-
- public BufferUtilsTest(final String name) {
- super(name);
- }
-
-
- public static Test suite() {
- return BulkTest.makeSuite(BufferUtilsTest.class);
- }
-
- public void testNothing() {
- }
-
- public void testpredicatedBuffer() {
-// final Predicate predicate = new Predicate() {
-// public boolean evaluate(final Object o) {
-// return o instanceof String;
-// }
-// };
-// Buffer buffer = BufferUtils.predicatedBuffer(new ArrayStack(), predicate);
-// assertTrue("returned object should be a PredicatedBuffer",
-// buffer instanceof PredicatedBuffer);
-// try {
-// buffer = BufferUtils.predicatedBuffer(new ArrayStack(), null);
-// fail("Expecting IllegalArgumentException for null predicate.");
-// } catch (final IllegalArgumentException ex) {
-// // expected
-// }
-// try {
-// buffer = BufferUtils.predicatedBuffer(null, predicate);
-// fail("Expecting IllegalArgumentException for null buffer.");
-// } catch (final IllegalArgumentException ex) {
-// // expected
-// }
- }
-
-}
diff --git a/src/test/java/org/apache/commons/collections/buffer/BlockingBufferTest.java b/src/test/java/org/apache/commons/collections/buffer/BlockingBufferTest.java
deleted file mode 100644
index d0d8a4d97..000000000
--- a/src/test/java/org/apache/commons/collections/buffer/BlockingBufferTest.java
+++ /dev/null
@@ -1,576 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.AbstractObjectTest;
-import org.apache.commons.collections.Buffer;
-import org.apache.commons.collections.BufferUnderflowException;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.Set;
-
-/**
- * Extension of {@link AbstractObjectTest} for exercising the
- * {@link BlockingBuffer} implementation.
- *
- * @since 3.0
- * @version $Id$
- */
-public class BlockingBufferTest extends AbstractObjectTest {
-
- public BlockingBufferTest(final String testName) {
- super(testName);
- }
-
- @Override
- public Buffer makeObject() {
- return BlockingBuffer.blockingBuffer(new MyBuffer());
- }
-
- @Override
- public boolean isEqualsCheckable() {
- return false;
- }
-
- //-----------------------------------------------------------------------
-
- @SuppressWarnings("unchecked")
- protected E makeElement() {
- return (E) new Object();
- }
-
- /**
- * Tests {@link BlockingBuffer#get()} in combination with
- * {@link BlockingBuffer#add(Object)}.
- */
- public void testGetWithAdd() {
- final Buffer blockingBuffer = makeObject();
- final E obj = makeElement();
- new DelayedAdd(blockingBuffer, obj).start();
-
- // verify does not throw BufferUnderflowException; should block until other thread has added to the buffer .
- assertSame(obj, blockingBuffer.get());
- }
-
- public void testGetWithAddTimeout() {
- final Buffer blockingBuffer = BlockingBuffer.blockingBuffer(new MyBuffer(), 500);
- final E obj = makeElement();
- new DelayedAdd(blockingBuffer, obj, 100).start();
-
- // verify does not throw BufferUnderflowException; should block until other thread has added to the buffer .
- assertSame(obj, blockingBuffer.get());
- }
-
- //-----------------------------------------------------------------------
-
- /**
- * Tests {@link BlockingBuffer#get()} in combination with
- * {@link BlockingBuffer#addAll(java.util.Collection)}.
- */
- public void testGetWithAddAll() {
- final Buffer blockingBuffer = makeObject();
- final E obj = makeElement();
- new DelayedAddAll(blockingBuffer, obj).start();
-
- // verify does not throw BufferUnderflowException; should block until other thread has added to the buffer .
- assertSame(obj, blockingBuffer.get());
- }
-
- public void testGetWithAddAllTimeout() {
- final Buffer blockingBuffer = BlockingBuffer.blockingBuffer(new MyBuffer(), 500);
- final E obj = makeElement();
- new DelayedAddAll(blockingBuffer, obj, 100).start();
-
- // verify does not throw BufferUnderflowException; should block until other thread has added to the buffer .
- assertSame(obj, blockingBuffer.get());
- }
-
- //-----------------------------------------------------------------------
-
- /**
- * Tests {@link BlockingBuffer#remove()} in combination with
- * {@link BlockingBuffer#add(Object)}.
- */
- public void testRemoveWithAdd() {
- final Buffer blockingBuffer = makeObject();
- final E obj = makeElement();
- new DelayedAdd(blockingBuffer, obj).start();
-
- // verify does not throw BufferUnderflowException; should block until other thread has added to the buffer .
- assertSame(obj, blockingBuffer.remove());
- }
-
- public void testRemoveWithAddTimeout() {
- final Buffer blockingBuffer = BlockingBuffer.blockingBuffer(new MyBuffer(), 100);
- final E obj = makeElement();
- new DelayedAdd(blockingBuffer, obj, 500).start();
- try {
- blockingBuffer.remove();
- } catch (final BufferUnderflowException e) {
- }
- }
-
- //-----------------------------------------------------------------------
-
- /**
- * Tests {@link BlockingBuffer#remove()} in combination with
- * {@link BlockingBuffer#addAll(java.util.Collection)}.
- */
- public void testRemoveWithAddAll() {
- final Buffer blockingBuffer = makeObject();
- final E obj = makeElement();
- new DelayedAddAll(blockingBuffer, obj).start();
-
- // verify does not throw BufferUnderflowException; should block until other thread has added to the buffer .
- assertSame(obj, blockingBuffer.remove());
- }
-
- public void testRemoveWithAddAllTimeout() {
- final Buffer blockingBuffer = BlockingBuffer.blockingBuffer(new MyBuffer(), 100);
- final E obj = makeElement();
- new DelayedAddAll(blockingBuffer, obj, 500).start();
- try {
- blockingBuffer.remove();
- } catch (final BufferUnderflowException e) {
- }
- }
-
- //-----------------------------------------------------------------------
-
- /**
- * Tests {@link BlockingBuffer#get()} in combination with
- * {@link BlockingBuffer#add(Object)} using multiple read threads.
Two
- * read threads should block on an empty buffer until one object is added
- * then both threads should complete.
- */
- public void testBlockedGetWithAdd() {
- final Buffer blockingBuffer = makeObject();
- final E obj = makeElement();
-
- // run methods will get and compare -- must wait for add
- final Thread thread1 = new ReadThread(blockingBuffer, obj);
- final Thread thread2 = new ReadThread(blockingBuffer, obj);
- thread1.start();
- thread2.start();
-
- // give hungry read threads ample time to hang
- delay();
-
- // notifyAll should allow both read threads to complete
- blockingBuffer.add(obj);
-
- // allow notified threads to complete
- delay();
-
- // There should not be any threads waiting.
- if (thread1.isAlive() || thread2.isAlive()) {
- fail("Live thread(s) when both should be dead.");
- }
- }
-
- //-----------------------------------------------------------------------
-
- /**
- * Tests {@link BlockingBuffer#get()} in combination with
- * {@link BlockingBuffer#addAll(java.util.Collection)} using multiple read
- * threads.
Two read threads should block on an empty buffer until a
- * singleton is added then both threads should complete.
- */
- public void testBlockedGetWithAddAll() {
- final Buffer blockingBuffer = makeObject();
- final E obj = makeElement();
-
- // run methods will get and compare -- must wait for addAll
- final Thread thread1 = new ReadThread(blockingBuffer, obj);
- final Thread thread2 = new ReadThread(blockingBuffer, obj);
- thread1.start();
- thread2.start();
-
- // give hungry read threads ample time to hang
- delay();
-
- // notifyAll should allow both read threads to complete
- blockingBuffer.addAll(Collections.singleton(obj));
-
- // allow notified threads to complete
- delay();
-
- // There should not be any threads waiting.
- if (thread1.isAlive() || thread2.isAlive()) {
- fail("Live thread(s) when both should be dead.");
- }
- }
-
- //-----------------------------------------------------------------------
-
- /**
- * Tests interrupted {@link BlockingBuffer#get()}.
- */
- public void testInterruptedGet() {
- final Buffer blockingBuffer = makeObject();
- final E obj = makeElement();
-
- // spawn a read thread to wait on the empty buffer
- final ArrayList exceptionList = new ArrayList();
- final Thread thread = new ReadThread(blockingBuffer, obj, exceptionList);
- thread.start();
-
- // Interrupting the thread should cause it to throw BufferUnderflowException
- thread.interrupt();
-
- // Chill, so thread can throw and add message to exceptionList
- delay();
- assertTrue("Thread interrupt should have led to underflow", exceptionList
- .contains("BufferUnderFlow"));
- if (thread.isAlive()) {
- fail("Read thread has hung.");
- }
-
- }
-
- //-----------------------------------------------------------------------
-
- /**
- * Tests {@link BlockingBuffer#remove()} in combination with
- * {@link BlockingBuffer#add(Object)} using multiple read threads.
Two
- * read threads should block on an empty buffer until one object is added
- * then one thread should complete. The remaining thread should complete
- * after the addition of a second object.
- */
- public void testBlockedRemoveWithAdd() {
- final Buffer blockingBuffer = makeObject();
- final E obj = makeElement();
-
- // run methods will remove and compare -- must wait for add
- final Thread thread1 = new ReadThread(blockingBuffer, obj, null, "remove");
- final Thread thread2 = new ReadThread(blockingBuffer, obj, null, "remove");
- thread1.start();
- thread2.start();
-
- // give hungry read threads ample time to hang
- delay();
- blockingBuffer.add(obj);
-
- // allow notified threads to complete
- delay();
-
- // There should be one thread waiting.
- assertTrue("There is one thread waiting", thread1.isAlive() ^ thread2.isAlive());
- blockingBuffer.add(obj);
-
- // allow notified thread to complete
- delay();
-
- // There should not be any threads waiting.
- if (thread1.isAlive() || thread2.isAlive()) {
- fail("Live thread(s) when both should be dead.");
- }
- }
-
- //-----------------------------------------------------------------------
-
- /**
- * Tests {@link BlockingBuffer#remove()} in combination with
- * {@link BlockingBuffer#addAll(java.util.Collection)} using multiple read
- * threads.
Two read threads should block on an empty buffer until a
- * singleton collection is added then one thread should complete. The
- * remaining thread should complete after the addition of a second
- * singleton.
- */
- public void testBlockedRemoveWithAddAll1() {
- final Buffer blockingBuffer = makeObject();
- final E obj = makeElement();
-
- // run methods will remove and compare -- must wait for addAll
- final Thread thread1 = new ReadThread(blockingBuffer, obj, null, "remove");
- final Thread thread2 = new ReadThread(blockingBuffer, obj, null, "remove");
- thread1.start();
- thread2.start();
-
- // give hungry read threads ample time to hang
- delay();
- blockingBuffer.addAll(Collections.singleton(obj));
-
- // allow notified threads to complete
- delay();
-
- // There should be one thread waiting.
- assertTrue("There is one thread waiting", thread1.isAlive() ^ thread2.isAlive());
- blockingBuffer.addAll(Collections.singleton(obj));
-
- // allow notified thread to complete
- delay();
-
- // There should not be any threads waiting.
- if (thread1.isAlive() || thread2.isAlive()) {
- fail("Live thread(s) when both should be dead.");
- }
- }
-
- //-----------------------------------------------------------------------
-
- /**
- * Tests {@link BlockingBuffer#remove()} in combination with
- * {@link BlockingBuffer#addAll(java.util.Collection)} using multiple read
- * threads.
Two read threads should block on an empty buffer until a
- * collection with two distinct objects is added then both threads should
- * complete. Each thread should have read a different object.
- */
- public void testBlockedRemoveWithAddAll2() {
- final Buffer blockingBuffer = makeObject();
- final E obj1 = makeElement();
- final E obj2 = makeElement();
- final Set objs = Collections.synchronizedSet(new HashSet());
- objs.add(obj1);
- objs.add(obj2);
-
- // run methods will remove and compare -- must wait for addAll
- final Thread thread1 = new ReadThread(blockingBuffer, objs, "remove");
- final Thread thread2 = new ReadThread(blockingBuffer, objs, "remove");
- thread1.start();
- thread2.start();
-
- // give hungry read threads ample time to hang
- delay();
- blockingBuffer.addAll(objs);
-
- // allow notified threads to complete
- delay();
- assertEquals("Both objects were removed", 0, objs.size());
-
- // There should not be any threads waiting.
- if (thread1.isAlive() || thread2.isAlive()) {
- fail("Live thread(s) when both should be dead.");
- }
- }
-
- //-----------------------------------------------------------------------
-
- /**
- * Tests interrupted remove.
- */
- public void testInterruptedRemove() {
- final Buffer blockingBuffer = makeObject();
- final E obj = makeElement();
-
- // spawn a read thread to wait on the empty buffer
- final ArrayList exceptionList = new ArrayList();
- final Thread thread = new ReadThread(blockingBuffer, obj, exceptionList, "remove");
- thread.start();
-
- // Interrupting the thread should cause it to throw BufferUnderflowException
- thread.interrupt();
-
- // Chill, so thread can throw and add message to exceptionList
- delay();
- assertTrue("Thread interrupt should have led to underflow", exceptionList
- .contains("BufferUnderFlow"));
- if (thread.isAlive()) {
- fail("Read thread has hung.");
- }
-
- }
-
- public void testTimeoutGet() {
- final BlockingBuffer buffer = new BlockingBuffer(new MyBuffer());
- try {
- buffer.get(100);
- fail("Get should have timed out.");
- } catch (final BufferUnderflowException e) {
- }
- }
-
- public void testTimeoutRemove() {
- final BlockingBuffer buffer = new BlockingBuffer(new MyBuffer());
- try {
- buffer.remove(100);
- fail("Get should have timed out.");
- } catch (final BufferUnderflowException e) {
- }
- }
-
- protected static class DelayedAdd extends Thread {
-
- Buffer buffer;
-
- E obj;
-
- long delay = 1000;
-
- public DelayedAdd(final Buffer buffer, final E obj, final long delay) {
- this.buffer = buffer;
- this.obj = obj;
- this.delay = delay;
- }
-
- DelayedAdd(final Buffer buffer, final E obj) {
- super();
- this.buffer = buffer;
- this.obj = obj;
- }
-
- @Override
- public void run() {
- try {
- // wait for other thread to block on get() or remove()
- Thread.sleep(delay);
- } catch (final InterruptedException e) {
- }
- buffer.add(obj);
- }
- }
-
- protected static class DelayedAddAll extends Thread {
-
- Buffer buffer;
-
- E obj;
-
- long delay = 100;
-
- public DelayedAddAll(final Buffer buffer, final E obj, final long delay) {
- this.buffer = buffer;
- this.obj = obj;
- this.delay = delay;
- }
-
- DelayedAddAll(final Buffer buffer, final E obj) {
- super();
- this.buffer = buffer;
- this.obj = obj;
- }
-
- @Override
- public void run() {
- try {
- // wait for other thread to block on get() or remove()
- Thread.sleep(delay);
- } catch (final InterruptedException e) {
- }
- buffer.addAll(Collections.singleton(obj));
- }
- }
-
- protected static class ReadThread extends Thread {
-
- Buffer buffer;
-
- Object obj;
-
- ArrayList exceptionList = null;
-
- String action = "get";
-
- Set objs;
-
- ReadThread(final Buffer buffer, final Object obj) {
- super();
- this.buffer = buffer;
- this.obj = obj;
- }
-
- ReadThread(final Buffer buffer, final Object obj, final ArrayList exceptionList) {
- super();
- this.buffer = buffer;
- this.obj = obj;
- this.exceptionList = exceptionList;
- }
-
- ReadThread(final Buffer buffer, final Object obj, final ArrayList exceptionList, final String action) {
- super();
- this.buffer = buffer;
- this.obj = obj;
- this.exceptionList = exceptionList;
- this.action = action;
- }
-
- ReadThread(final Buffer buffer, final Set objs, final String action) {
- super();
- this.buffer = buffer;
- this.objs = objs;
- this.action = action;
- }
-
- @Override
- public void run() {
- try {
- if (action == "get") {
- assertSame(obj, buffer.get());
- } else {
- if (null != obj) {
- assertSame(obj, buffer.remove());
- } else {
- assertTrue(objs.remove(buffer.remove()));
- }
- }
- } catch (final BufferUnderflowException ex) {
- exceptionList.add("BufferUnderFlow");
- }
- }
- }
-
- @SuppressWarnings("serial")
- protected static class MyBuffer extends LinkedList