Author: rwhitcomb Date: Thu Jan 11 20:44:26 2018 New Revision: 1820931 URL: http://svn.apache.org/viewvc?rev=1820931&view=rev Log: PIVOT-1022: Make similar changes to Queue and all its implementations: add a "maxLength" property and implement checking this length before queuing a new item. Add a test in QueueTest.java.
Update Javadoc in Queue.java and Stack.java about what the max length/depth means for the enqueue/push process. Modified: pivot/trunk/core/src/org/apache/pivot/collections/ArrayQueue.java pivot/trunk/core/src/org/apache/pivot/collections/LinkedQueue.java pivot/trunk/core/src/org/apache/pivot/collections/Queue.java pivot/trunk/core/src/org/apache/pivot/collections/Stack.java pivot/trunk/core/src/org/apache/pivot/collections/concurrent/SynchronizedQueue.java pivot/trunk/core/test/org/apache/pivot/collections/test/QueueTest.java pivot/trunk/core/test/org/apache/pivot/collections/test/StackTest.java Modified: pivot/trunk/core/src/org/apache/pivot/collections/ArrayQueue.java URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/collections/ArrayQueue.java?rev=1820931&r1=1820930&r2=1820931&view=diff ============================================================================== --- pivot/trunk/core/src/org/apache/pivot/collections/ArrayQueue.java (original) +++ pivot/trunk/core/src/org/apache/pivot/collections/ArrayQueue.java Thu Jan 11 20:44:26 2018 @@ -22,6 +22,7 @@ import java.util.Iterator; import org.apache.pivot.util.ImmutableIterator; import org.apache.pivot.util.ListenerList; +import org.apache.pivot.util.Utils; /** * Implementation of the {@link Queue} interface that is backed by an array. @@ -30,6 +31,7 @@ public class ArrayQueue<T> implements Qu private static final long serialVersionUID = -3856732506886968324L; private ArrayList<T> arrayList = new ArrayList<>(); + private int maxLength = 0; private transient QueueListenerList<T> queueListeners = new QueueListenerList<>(); public ArrayQueue() { @@ -44,15 +46,28 @@ public class ArrayQueue<T> implements Qu ensureCapacity(capacity); } + public ArrayQueue(int capacity, int maxLength) { + ensureCapacity(capacity); + setMaxLength(maxLength); + } + + public ArrayQueue(int capacity, int maxLength, Comparator<T> comparator) { + ensureCapacity(capacity); + setMaxLength(maxLength); + setComparator(comparator); + } + @Override public void enqueue(T item) { - if (getComparator() == null) { - arrayList.insert(item, 0); - } else { - arrayList.add(item); - } + if (maxLength == 0 || arrayList.getLength() < maxLength) { + if (getComparator() == null) { + arrayList.insert(item, 0); + } else { + arrayList.add(item); + } - queueListeners.itemEnqueued(this, item); + queueListeners.itemEnqueued(this, item); + } } @Override @@ -97,6 +112,17 @@ public class ArrayQueue<T> implements Qu return arrayList.getLength(); } + @Override + public int getMaxLength() { + return maxLength; + } + + @Override + public void setMaxLength(int maxLength) { + Utils.checkNonNegative(maxLength, "maxLength"); + this.maxLength = maxLength; + } + public void ensureCapacity(int capacity) { arrayList.ensureCapacity(capacity); } Modified: pivot/trunk/core/src/org/apache/pivot/collections/LinkedQueue.java URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/collections/LinkedQueue.java?rev=1820931&r1=1820930&r2=1820931&view=diff ============================================================================== --- pivot/trunk/core/src/org/apache/pivot/collections/LinkedQueue.java (original) +++ pivot/trunk/core/src/org/apache/pivot/collections/LinkedQueue.java Thu Jan 11 20:44:26 2018 @@ -22,6 +22,7 @@ import java.util.Iterator; import org.apache.pivot.util.ImmutableIterator; import org.apache.pivot.util.ListenerList; +import org.apache.pivot.util.Utils; /** * Implementation of the {@link Queue} interface that is backed by a linked @@ -31,6 +32,7 @@ public class LinkedQueue<T> implements Q private static final long serialVersionUID = 1598074020226109253L; private LinkedList<T> linkedList = new LinkedList<>(); + private int maxLength = 0; private transient QueueListenerList<T> queueListeners = new QueueListenerList<>(); public LinkedQueue() { @@ -41,15 +43,26 @@ public class LinkedQueue<T> implements Q setComparator(comparator); } + public LinkedQueue(int maxLength) { + setMaxLength(maxLength); + } + + public LinkedQueue(int maxLength, Comparator<T> comparator) { + setMaxLength(maxLength); + setComparator(comparator); + } + @Override public void enqueue(T item) { - if (getComparator() == null) { - linkedList.insert(item, 0); - } else { - linkedList.add(item); - } + if (maxLength == 0 || linkedList.getLength() < maxLength) { + if (getComparator() == null) { + linkedList.insert(item, 0); + } else { + linkedList.add(item); + } - queueListeners.itemEnqueued(this, item); + queueListeners.itemEnqueued(this, item); + } } @Override @@ -95,6 +108,17 @@ public class LinkedQueue<T> implements Q } @Override + public int getMaxLength() { + return maxLength; + } + + @Override + public void setMaxLength(int maxLength) { + Utils.checkNonNegative(maxLength, "maxLength"); + this.maxLength = maxLength; + } + + @Override public Comparator<T> getComparator() { return linkedList.getComparator(); } Modified: pivot/trunk/core/src/org/apache/pivot/collections/Queue.java URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/collections/Queue.java?rev=1820931&r1=1820930&r2=1820931&view=diff ============================================================================== --- pivot/trunk/core/src/org/apache/pivot/collections/Queue.java (original) +++ pivot/trunk/core/src/org/apache/pivot/collections/Queue.java Thu Jan 11 20:44:26 2018 @@ -55,6 +55,8 @@ public interface Queue<T> extends Collec * Enqueues an item. If the queue is unsorted, the item is added at the tail * of the queue (index <tt>0</tt>). Otherwise, it is inserted at the * appropriate index. + * <p> If there is a maximum queue length defined and the queue is already at + * the maximum length this new item will not be queued. * * @param item The item to add to the queue. */ @@ -92,6 +94,18 @@ public interface Queue<T> extends Collec public int getLength(); /** + * @return The maximum queue length allowed (0 means unlimited). + */ + public int getMaxLength(); + + /** + * Set the maximum allowed queue length (0 means unlimited). + * + * @param maxLength The maximum allowed length. + */ + public void setMaxLength(int maxLength); + + /** * @return The queue listener list. */ public ListenerList<QueueListener<T>> getQueueListeners(); Modified: pivot/trunk/core/src/org/apache/pivot/collections/Stack.java URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/collections/Stack.java?rev=1820931&r1=1820930&r2=1820931&view=diff ============================================================================== --- pivot/trunk/core/src/org/apache/pivot/collections/Stack.java (original) +++ pivot/trunk/core/src/org/apache/pivot/collections/Stack.java Thu Jan 11 20:44:26 2018 @@ -55,6 +55,9 @@ public interface Stack<T> extends Collec * "Pushes" an item onto the stack. If the stack is unsorted, the item is * added at the top of the stack (<tt>getLength()</tt>). Otherwise, it is * inserted at the appropriate index. + * <p> If there is a maximum stack depth defined and the stack goes past + * this maximum depth, the deepest item (which could be this new item, + * depending on the comparator) will be removed. * * @param item The item to push onto the stack. */ Modified: pivot/trunk/core/src/org/apache/pivot/collections/concurrent/SynchronizedQueue.java URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/collections/concurrent/SynchronizedQueue.java?rev=1820931&r1=1820930&r2=1820931&view=diff ============================================================================== --- pivot/trunk/core/src/org/apache/pivot/collections/concurrent/SynchronizedQueue.java (original) +++ pivot/trunk/core/src/org/apache/pivot/collections/concurrent/SynchronizedQueue.java Thu Jan 11 20:44:26 2018 @@ -109,6 +109,16 @@ public class SynchronizedQueue<T> implem } @Override + public int getMaxLength() { + return queue.getMaxLength(); + } + + @Override + public void setMaxLength(int maxLength) { + queue.setMaxLength(maxLength); + } + + @Override public synchronized Comparator<T> getComparator() { return queue.getComparator(); } Modified: pivot/trunk/core/test/org/apache/pivot/collections/test/QueueTest.java URL: http://svn.apache.org/viewvc/pivot/trunk/core/test/org/apache/pivot/collections/test/QueueTest.java?rev=1820931&r1=1820930&r2=1820931&view=diff ============================================================================== --- pivot/trunk/core/test/org/apache/pivot/collections/test/QueueTest.java (original) +++ pivot/trunk/core/test/org/apache/pivot/collections/test/QueueTest.java Thu Jan 11 20:44:26 2018 @@ -16,6 +16,7 @@ */ package org.apache.pivot.collections.test; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.util.Comparator; @@ -23,6 +24,7 @@ import java.util.Comparator; import org.apache.pivot.collections.ArrayQueue; import org.apache.pivot.collections.LinkedQueue; import org.apache.pivot.collections.Queue; +import org.apache.pivot.collections.QueueListener; import org.junit.Test; public class QueueTest { @@ -32,6 +34,8 @@ public class QueueTest { testQueue(new LinkedQueue<String>()); testSortedQueue(new ArrayQueue<String>(5)); testSortedQueue(new LinkedQueue<String>()); + testMaxLengthQueue(new ArrayQueue<String>(5, 5)); + testMaxLengthQueue(new LinkedQueue<String>(5)); } private static void testQueue(Queue<String> queue) { @@ -78,4 +82,24 @@ public class QueueTest { i--; } } + + private static class Listener extends QueueListener.Adapter<String> { + public int queueCount = 0; + + @Override + public void itemEnqueued(Queue<String> queue, String item) { + queueCount++; + } + } + + private static void testMaxLengthQueue(Queue<String> queue) { + Listener listener = new Listener(); + queue.getQueueListeners().add(listener); + + for (int i = 0; i < queue.getMaxLength() + 2; i++) { + queue.enqueue("This is a test!"); + } + + assertEquals(listener.queueCount, queue.getMaxLength()); + } } Modified: pivot/trunk/core/test/org/apache/pivot/collections/test/StackTest.java URL: http://svn.apache.org/viewvc/pivot/trunk/core/test/org/apache/pivot/collections/test/StackTest.java?rev=1820931&r1=1820930&r2=1820931&view=diff ============================================================================== --- pivot/trunk/core/test/org/apache/pivot/collections/test/StackTest.java (original) +++ pivot/trunk/core/test/org/apache/pivot/collections/test/StackTest.java Thu Jan 11 20:44:26 2018 @@ -16,8 +16,8 @@ */ package org.apache.pivot.collections.test; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import org.apache.pivot.collections.ArrayStack; import org.apache.pivot.collections.LinkedStack;