[COLLECTION-432] Remove Buffer interface from ArrayStack.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1468579 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d2280c28ec
commit
b8eee79cce
|
@ -29,10 +29,12 @@ import java.util.EmptyStackException;
|
|||
* The removal order of an <code>ArrayStack</code> is based on insertion
|
||||
* order: The most recently added element is removed first. The iteration
|
||||
* order is <i>not</i> the same as the removal order. The iterator returns
|
||||
* elements from the bottom up, whereas the {@link #remove()} method removes
|
||||
* them from the top down.
|
||||
* elements from the bottom up.
|
||||
* <p>
|
||||
* Unlike <code>Stack</code>, <code>ArrayStack</code> accepts null entries.
|
||||
* <p>
|
||||
* <b>Note:</b> From version 4.0 onwards, this class does not implement the
|
||||
* removed {@code Buffer} interface anymore.
|
||||
*
|
||||
* @see java.util.Stack
|
||||
* @since 1.0
|
||||
|
@ -40,7 +42,7 @@ import java.util.EmptyStackException;
|
|||
* @deprecated use {@link java.util.ArrayDeque} instead (available from Java 1.6)
|
||||
*/
|
||||
@Deprecated
|
||||
public class ArrayStack<E> extends ArrayList<E> implements Buffer<E> {
|
||||
public class ArrayStack<E> extends ArrayList<E> {
|
||||
|
||||
/** Ensure serialization compatibility */
|
||||
private static final long serialVersionUID = 2130079159931574599L;
|
||||
|
@ -162,32 +164,4 @@ public class ArrayStack<E> extends ArrayList<E> implements Buffer<E> {
|
|||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the element on the top of the stack.
|
||||
*
|
||||
* @return the element on the top of the stack
|
||||
* @throws BufferUnderflowException if the stack is empty
|
||||
*/
|
||||
public E get() {
|
||||
final int size = size();
|
||||
if (size == 0) {
|
||||
throw new BufferUnderflowException();
|
||||
}
|
||||
return get(size - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the element on the top of the stack.
|
||||
*
|
||||
* @return the removed element
|
||||
* @throws BufferUnderflowException if the stack is empty
|
||||
*/
|
||||
public E remove() {
|
||||
final int size = size();
|
||||
if (size == 0) {
|
||||
throw new BufferUnderflowException();
|
||||
}
|
||||
return remove(size - 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ 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;
|
||||
|
@ -34,7 +35,7 @@ public class BufferUtils {
|
|||
/**
|
||||
* An empty unmodifiable buffer.
|
||||
*/
|
||||
public static final Buffer<Object> EMPTY_BUFFER = UnmodifiableBuffer.unmodifiableBuffer(new ArrayStack<Object>(1));
|
||||
public static final Buffer<Object> EMPTY_BUFFER = UnmodifiableBuffer.unmodifiableBuffer(new CircularFifoBuffer<Object>(1));
|
||||
|
||||
/**
|
||||
* <code>BufferUtils</code> should not normally be instantiated.
|
||||
|
|
|
@ -19,8 +19,8 @@ package org.apache.commons.collections.iterators;
|
|||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import org.apache.commons.collections.ArrayStack;
|
||||
import org.apache.commons.collections.Transformer;
|
||||
import org.apache.commons.collections.ArrayStack;
|
||||
|
||||
/**
|
||||
* An Iterator that can traverse multiple iterators down an object graph.
|
||||
|
@ -73,6 +73,7 @@ import org.apache.commons.collections.Transformer;
|
|||
* @since 3.1
|
||||
* @version $Id$
|
||||
*/
|
||||
@SuppressWarnings("deprecation") // we use the deprecated ArrayStack - change to ArrayDeque (Java 1.6)
|
||||
public class ObjectGraphIterator<E> implements Iterator<E> {
|
||||
|
||||
/** The stack of iterators */
|
||||
|
|
|
@ -19,6 +19,7 @@ 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.
|
||||
|
@ -42,26 +43,26 @@ public class BufferUtilsTest extends BulkTest {
|
|||
}
|
||||
|
||||
public void testpredicatedBuffer() {
|
||||
final Predicate<Object> predicate = new Predicate<Object>() {
|
||||
public boolean evaluate(final Object o) {
|
||||
return o instanceof String;
|
||||
}
|
||||
};
|
||||
Buffer<Object> buffer = BufferUtils.predicatedBuffer(new ArrayStack<Object>(), predicate);
|
||||
assertTrue("returned object should be a PredicatedBuffer",
|
||||
buffer instanceof PredicatedBuffer);
|
||||
try {
|
||||
buffer = BufferUtils.predicatedBuffer(new ArrayStack<Object>(), 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
|
||||
}
|
||||
// final Predicate<Object> predicate = new Predicate<Object>() {
|
||||
// public boolean evaluate(final Object o) {
|
||||
// return o instanceof String;
|
||||
// }
|
||||
// };
|
||||
// Buffer<Object> buffer = BufferUtils.predicatedBuffer(new ArrayStack<Object>(), predicate);
|
||||
// assertTrue("returned object should be a PredicatedBuffer",
|
||||
// buffer instanceof PredicatedBuffer);
|
||||
// try {
|
||||
// buffer = BufferUtils.predicatedBuffer(new ArrayStack<Object>(), 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
|
||||
// }
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -17,12 +17,13 @@
|
|||
package org.apache.commons.collections.buffer;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.EmptyStackException;
|
||||
|
||||
import org.apache.commons.collections.ArrayStack;
|
||||
import org.apache.commons.collections.Buffer;
|
||||
import org.apache.commons.collections.BufferUnderflowException;
|
||||
import org.apache.commons.collections.Predicate;
|
||||
import org.apache.commons.collections.collection.PredicatedCollectionTest;
|
||||
import org.apache.commons.collections.ArrayStack;
|
||||
|
||||
/**
|
||||
* Extension of {@link PredicatedCollectionTest} for exercising the
|
||||
|
@ -45,7 +46,7 @@ public class PredicatedBufferTest<E> extends PredicatedCollectionTest<E> {
|
|||
|
||||
@Override
|
||||
public Buffer<E> makeObject() {
|
||||
return decorateCollection(new ArrayStack<E>(), truePredicate);
|
||||
return decorateCollection(new LifoStackAsBuffer<E>(), truePredicate);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -63,7 +64,7 @@ public class PredicatedBufferTest<E> extends PredicatedCollectionTest<E> {
|
|||
//------------------------------------------------------------
|
||||
|
||||
public Buffer<E> makeTestBuffer() {
|
||||
return decorateCollection(new ArrayStack<E>(), testPredicate);
|
||||
return decorateCollection(new LifoStackAsBuffer<E>(), testPredicate);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
@ -105,5 +106,25 @@ public class PredicatedBufferTest<E> extends PredicatedCollectionTest<E> {
|
|||
// resetFull();
|
||||
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/PredicatedBuffer.fullCollection.version3.1.obj");
|
||||
// }
|
||||
|
||||
private static class LifoStackAsBuffer<E> extends ArrayStack<E> implements Buffer<E> {
|
||||
|
||||
public E get() {
|
||||
try {
|
||||
return peek();
|
||||
} catch (EmptyStackException e) {
|
||||
throw new BufferUnderflowException();
|
||||
}
|
||||
}
|
||||
|
||||
public E remove() {
|
||||
try {
|
||||
return pop();
|
||||
} catch (EmptyStackException e) {
|
||||
throw new BufferUnderflowException();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
package org.apache.commons.collections.buffer;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.apache.commons.collections.ArrayStack;
|
||||
import org.apache.commons.collections.Buffer;
|
||||
import org.apache.commons.collections.collection.TransformedCollectionTest;
|
||||
|
||||
|
@ -35,7 +34,7 @@ public class TransformedBufferTest extends TestCase {
|
|||
}
|
||||
|
||||
public void testTransformedBuffer() {
|
||||
final Buffer<Object> buffer = TransformedBuffer.transformingBuffer(new ArrayStack<Object>(), TransformedCollectionTest.STRING_TO_INTEGER_TRANSFORMER);
|
||||
final Buffer<Object> buffer = TransformedBuffer.transformingBuffer(new CircularFifoBuffer<Object>(), TransformedCollectionTest.STRING_TO_INTEGER_TRANSFORMER);
|
||||
assertEquals(0, buffer.size());
|
||||
final Object[] els = new Object[] { "1", "3", "5", "7", "2", "4", "6" };
|
||||
for (int i = 0; i < els.length; i++) {
|
||||
|
@ -51,7 +50,7 @@ public class TransformedBufferTest extends TestCase {
|
|||
}
|
||||
|
||||
public void testTransformedBuffer_decorateTransform() {
|
||||
final Buffer originalBuffer = new ArrayStack();
|
||||
final Buffer originalBuffer = new CircularFifoBuffer();
|
||||
final Object[] els = new Object[] {"1", "3", "5", "7", "2", "4", "6"};
|
||||
for (final Object el : els) {
|
||||
originalBuffer.add(el);
|
||||
|
|
|
@ -20,10 +20,8 @@ import java.util.ArrayList;
|
|||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.apache.commons.collections.ArrayStack;
|
||||
import org.apache.commons.collections.Buffer;
|
||||
import org.apache.commons.collections.BufferUtils;
|
||||
import org.apache.commons.collections.buffer.BoundedBuffer;
|
||||
import org.apache.commons.collections.BoundedCollection;
|
||||
import org.apache.commons.collections.list.FixedSizeList;
|
||||
|
||||
/**
|
||||
* Extension of {@link AbstractCollectionTest} for exercising the
|
||||
|
@ -40,16 +38,15 @@ public class UnmodifiableBoundedCollectionTest<E> extends AbstractCollectionTest
|
|||
//-----------------------------------------------------------------------
|
||||
@Override
|
||||
public Collection<E> makeObject() {
|
||||
final BoundedBuffer<E> buffer = BoundedBuffer.<E>boundedBuffer(new ArrayStack<E>(), 10);
|
||||
return UnmodifiableBoundedCollection.unmodifiableBoundedCollection(buffer);
|
||||
final BoundedCollection<E> coll = FixedSizeList.<E>fixedSizeList(new ArrayList<E>());
|
||||
return UnmodifiableBoundedCollection.unmodifiableBoundedCollection(coll);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<E> makeFullCollection() {
|
||||
final E[] allElements = getFullElements();
|
||||
final Buffer<E> buffer = BufferUtils.boundedBuffer(new ArrayStack<E>(), allElements.length);
|
||||
buffer.addAll(Arrays.asList(allElements));
|
||||
return UnmodifiableBoundedCollection.unmodifiableBoundedCollection(buffer);
|
||||
final BoundedCollection<E> coll = FixedSizeList.<E>fixedSizeList(new ArrayList<E>(Arrays.asList(allElements)));
|
||||
return UnmodifiableBoundedCollection.unmodifiableBoundedCollection(coll);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue