You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2009/06/01 16:50:41 UTC

svn commit: r780667 - in /incubator/pivot/trunk/core/src/pivot/collections: ./ concurrent/

Author: gbrown
Date: Mon Jun  1 14:50:41 2009
New Revision: 780667

URL: http://svn.apache.org/viewvc?rev=780667&view=rev
Log:
Split Queue and Stack into their own interfaces (no longer extend List).

Modified:
    incubator/pivot/trunk/core/src/pivot/collections/ArrayQueue.java
    incubator/pivot/trunk/core/src/pivot/collections/ArrayStack.java
    incubator/pivot/trunk/core/src/pivot/collections/LinkedList.java
    incubator/pivot/trunk/core/src/pivot/collections/LinkedQueue.java
    incubator/pivot/trunk/core/src/pivot/collections/LinkedStack.java
    incubator/pivot/trunk/core/src/pivot/collections/Queue.java
    incubator/pivot/trunk/core/src/pivot/collections/Stack.java
    incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedCollection.java
    incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedList.java
    incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedMap.java
    incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedQueue.java
    incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedSet.java
    incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedStack.java

Modified: incubator/pivot/trunk/core/src/pivot/collections/ArrayQueue.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/collections/ArrayQueue.java?rev=780667&r1=780666&r2=780667&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/collections/ArrayQueue.java (original)
+++ incubator/pivot/trunk/core/src/pivot/collections/ArrayQueue.java Mon Jun  1 14:50:41 2009
@@ -16,6 +16,8 @@
  */
 package pivot.collections;
 
+import java.util.Comparator;
+
 /**
  * Implementation of the {@link Queue} interface that is backed by an
  * array.
@@ -25,18 +27,19 @@
 public class ArrayQueue<T> extends ArrayList<T> implements Queue<T> {
     private static final long serialVersionUID = 0;
 
+    private QueueListenerList<T> queueListeners = new QueueListenerList<T>();
+
     public ArrayQueue() {
-        super();
+        this(null);
     }
 
-    public ArrayQueue(List<T> items) {
-        super(items);
+    public ArrayQueue(Comparator<T> comparator) {
+        super(comparator);
     }
 
     public void enqueue(T item) {
         add(item);
-
-        // TODO Fire enqueue event
+        queueListeners.itemEnqueued(this, item);
     }
 
     public T dequeue() {
@@ -46,8 +49,7 @@
         }
 
         T item = remove(length - 1, 1).get(0);
-
-        // TODO Fire dequeue event
+        queueListeners.itemDequeued(this, item);
 
         return item;
     }
@@ -61,4 +63,8 @@
 
         return item;
     }
+
+    public boolean isEmpty() {
+        return (getLength() == 0);
+    }
 }

Modified: incubator/pivot/trunk/core/src/pivot/collections/ArrayStack.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/collections/ArrayStack.java?rev=780667&r1=780666&r2=780667&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/collections/ArrayStack.java (original)
+++ incubator/pivot/trunk/core/src/pivot/collections/ArrayStack.java Mon Jun  1 14:50:41 2009
@@ -16,6 +16,8 @@
  */
 package pivot.collections;
 
+import java.util.Comparator;
+
 /**
  * Implementation of the {@link Stack} interface that is backed by an
  * array.
@@ -25,18 +27,19 @@
 public class ArrayStack<T> extends ArrayList<T> implements Stack<T> {
     private static final long serialVersionUID = 0;
 
+    private StackListenerList<T> stackListeners = new StackListenerList<T>();
+
     public ArrayStack() {
-        super();
+        this(null);
     }
 
-    public ArrayStack(List<T> items) {
-        super(items);
+    public ArrayStack(Comparator<T> comparator) {
+        super(comparator);
     }
 
     public void push(T item) {
         add(item);
-
-        // TODO Fire push event
+        stackListeners.itemPushed(this, item);
     }
 
     public T pop() {
@@ -46,8 +49,7 @@
         }
 
         T item = remove(length - 1, 1).get(0);
-
-        // TODO Fire pop event
+        stackListeners.itemPopped(this, item);
 
         return item;
     }
@@ -70,4 +72,8 @@
 
         return update(length - 1, item);
     }
+
+    public boolean isEmpty() {
+        return (getLength() == 0);
+    }
 }

Modified: incubator/pivot/trunk/core/src/pivot/collections/LinkedList.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/collections/LinkedList.java?rev=780667&r1=780666&r2=780667&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/collections/LinkedList.java (original)
+++ incubator/pivot/trunk/core/src/pivot/collections/LinkedList.java Mon Jun  1 14:50:41 2009
@@ -89,6 +89,13 @@
     private Comparator<T> comparator = null;
     private transient ListListenerList<T> listListeners = new ListListenerList<T>();
 
+    public LinkedList() {
+    }
+
+    public LinkedList(Comparator<T> comparator) {
+        this.comparator = comparator;
+    }
+
     public int add(T item) {
         int index;
         if (comparator == null) {

Modified: incubator/pivot/trunk/core/src/pivot/collections/LinkedQueue.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/collections/LinkedQueue.java?rev=780667&r1=780666&r2=780667&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/collections/LinkedQueue.java (original)
+++ incubator/pivot/trunk/core/src/pivot/collections/LinkedQueue.java Mon Jun  1 14:50:41 2009
@@ -16,28 +16,53 @@
  */
 package pivot.collections;
 
+import java.util.Comparator;
+
 /**
  * Implementation of the {@link Queue} interface that is backed by a linked
  * list.
- * <p>
- * TODO This class is currently incomplete.
  */
 public class LinkedQueue<T> extends LinkedList<T> implements Queue<T> {
     private static final long serialVersionUID = 0;
 
-    public void enqueue(T item) {
-        // TODO Auto-generated method stub
+    private QueueListenerList<T> queueListeners = new QueueListenerList<T>();
+
+    public LinkedQueue() {
+        this(null);
+    }
+
+    public LinkedQueue(Comparator<T> comparator) {
+        super(comparator);
+    }
 
+    public void enqueue(T item) {
+        add(item);
+        queueListeners.itemEnqueued(this, item);
     }
 
     public T dequeue() {
-        // TODO Auto-generated method stub
-        // TODO Throw if empty
-        return null;
+        int length = getLength();
+        if (length == 0) {
+            throw new IllegalStateException();
+        }
+
+        T item = remove(length - 1, 1).get(0);
+        queueListeners.itemDequeued(this, item);
+
+        return item;
     }
 
     public T peek() {
-        // TODO Auto-generated method stub
-        return null;
+        T item = null;
+        int length = getLength();
+        if (length > 0) {
+            item = get(length - 1);
+        }
+
+        return item;
+    }
+
+    public boolean isEmpty() {
+        return (getLength() == 0);
     }
 }

Modified: incubator/pivot/trunk/core/src/pivot/collections/LinkedStack.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/collections/LinkedStack.java?rev=780667&r1=780666&r2=780667&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/collections/LinkedStack.java (original)
+++ incubator/pivot/trunk/core/src/pivot/collections/LinkedStack.java Mon Jun  1 14:50:41 2009
@@ -16,33 +16,62 @@
  */
 package pivot.collections;
 
+import java.util.Comparator;
+
 /**
  * Implementation of the {@link Stack} interface that is backed by a linked
  * list.
- * <p>
- * TODO This class is currently incomplete.
  */
 public class LinkedStack<T> extends LinkedList<T> implements Stack<T> {
     private static final long serialVersionUID = 0;
 
-    public void push(T item) {
-        // TODO Auto-generated method stub
+    private StackListenerList<T> stackListeners = new StackListenerList<T>();
+
+    public LinkedStack() {
+        this(null);
+    }
 
+    public LinkedStack(Comparator<T> comparator) {
+        super(comparator);
+    }
+
+    public void push(T item) {
+        add(item);
+        stackListeners.itemPushed(this, item);
     }
 
     public T pop() {
-        // TODO Auto-generated method stub
-        // TODO Throw if empty
-        return null;
+        int length = getLength();
+        if (length == 0) {
+            throw new IllegalStateException();
+        }
+
+        T item = remove(length - 1, 1).get(0);
+        stackListeners.itemPopped(this, item);
+
+        return item;
     }
 
     public T peek() {
-        // TODO Auto-generated method stub
-        return null;
+        T item = null;
+        int length = getLength();
+        if (length > 0) {
+            item = get(length - 1);
+        }
+
+        return item;
     }
 
     public T poke(T item) {
-        // TODO Auto-generated method stub
-        return null;
+        int length = getLength();
+        if (length == 0) {
+            throw new IllegalStateException();
+        }
+
+        return update(length - 1, item);
+    }
+
+    public boolean isEmpty() {
+        return (getLength() == 0);
     }
 }

Modified: incubator/pivot/trunk/core/src/pivot/collections/Queue.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/collections/Queue.java?rev=780667&r1=780666&r2=780667&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/collections/Queue.java (original)
+++ incubator/pivot/trunk/core/src/pivot/collections/Queue.java Mon Jun  1 14:50:41 2009
@@ -16,13 +16,35 @@
  */
 package pivot.collections;
 
+import pivot.util.ListenerList;
+
 /**
  * Interface representing a first-in, first-out (FIFO) queue when unsorted, and
  * a priority queue when sorted.
  *
  * @author gbrown
  */
-public interface Queue<T> extends List<T> {
+public interface Queue<T> extends Collection<T> {
+    /**
+     * Queue listener list.
+     *
+     * @author gbrown
+     */
+    public static class QueueListenerList<T> extends ListenerList<QueueListener<T>>
+        implements QueueListener<T> {
+        public void itemEnqueued(Queue<T> queue, T item) {
+            for (QueueListener<T> listener : this) {
+                listener.itemEnqueued(queue, item);
+            }
+        }
+
+        public void itemDequeued(Queue<T> queue, T item) {
+            for (QueueListener<T> listener : this) {
+                listener.itemDequeued(queue, item);
+            }
+        }
+    }
+
     /**
      * 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
@@ -44,8 +66,17 @@
     /**
      * Returns the item at the head of the queue without removing it from the
      * queue. Returns null if the queue contains no items. Will also return null
-     * if the head item in the queue is null. <tt>getLength()</tt> can be used
+     * if the head item in the queue is null. <tt>isEmpty()</tt> can be used
      * to distinguish between these two cases.
      */
     public T peek();
+
+    /**
+     * Tests the emptiness of the queue.
+     *
+     * @return
+     * <tt>true</tt> if the queue contains no items; <tt>false</tt>,
+     * otherwise.
+     */
+    public boolean isEmpty();
 }

Modified: incubator/pivot/trunk/core/src/pivot/collections/Stack.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/collections/Stack.java?rev=780667&r1=780666&r2=780667&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/collections/Stack.java (original)
+++ incubator/pivot/trunk/core/src/pivot/collections/Stack.java Mon Jun  1 14:50:41 2009
@@ -16,13 +16,35 @@
  */
 package pivot.collections;
 
+import pivot.util.ListenerList;
+
 /**
  * Interface representing a last-in, first-out (LIFO) stack when unsorted, and
  * a priority stack when sorted.
  *
  * @author gbrown
  */
-public interface Stack<T> extends List<T> {
+public interface Stack<T> extends Collection<T> {
+    /**
+     * Stack listener list.
+     *
+     * @author gbrown
+     */
+    public static class StackListenerList<T> extends ListenerList<StackListener<T>>
+        implements StackListener<T> {
+        public void itemPushed(Stack<T> stack, T item) {
+            for (StackListener<T> listener : this) {
+                listener.itemPushed(stack, item);
+            }
+        }
+
+        public void itemPopped(Stack<T> stack, T item) {
+            for (StackListener<T> listener : this) {
+                listener.itemPopped(stack, item);
+            }
+        }
+    }
+
     /**
      * "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
@@ -56,4 +78,13 @@
      * If the stack contains no items.
      */
     public T poke(T item);
+
+    /**
+     * Tests the emptiness of the stack.
+     *
+     * @return
+     * <tt>true</tt> if the stack contains no items; <tt>false</tt>,
+     * otherwise.
+     */
+    public boolean isEmpty();
 }

Modified: incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedCollection.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedCollection.java?rev=780667&r1=780666&r2=780667&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedCollection.java (original)
+++ incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedCollection.java Mon Jun  1 14:50:41 2009
@@ -41,6 +41,10 @@
         this.collection = collection;
     }
 
+    public synchronized void clear() {
+        collection.clear();
+    }
+
     public synchronized Comparator<T> getComparator() {
         return collection.getComparator();
     }

Modified: incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedList.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedList.java?rev=780667&r1=780666&r2=780667&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedList.java (original)
+++ incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedList.java Mon Jun  1 14:50:41 2009
@@ -98,10 +98,6 @@
         return ((List<T>)collection).remove(index, count);
     }
 
-    public synchronized void clear() {
-        ((List<T>)collection).clear();
-    }
-
     public synchronized T get(int index) {
         return ((List<T>)collection).get(index);
     }

Modified: incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedMap.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedMap.java?rev=780667&r1=780666&r2=780667&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedMap.java (original)
+++ incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedMap.java Mon Jun  1 14:50:41 2009
@@ -94,11 +94,6 @@
     }
 
     @SuppressWarnings("unchecked")
-    public synchronized void clear() {
-        ((Map<K, V>)collection).clear();
-    }
-
-    @SuppressWarnings("unchecked")
     public synchronized boolean isEmpty() {
         return ((Map<K, V>)collection).isEmpty();
     }

Modified: incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedQueue.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedQueue.java?rev=780667&r1=780666&r2=780667&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedQueue.java (original)
+++ incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedQueue.java Mon Jun  1 14:50:41 2009
@@ -23,7 +23,7 @@
  *
  * @author gbrown
  */
-public class SynchronizedQueue<T> extends SynchronizedList<T>
+public class SynchronizedQueue<T> extends SynchronizedCollection<T>
     implements Queue<T> {
     public SynchronizedQueue(Queue<T> queue) {
         super(queue);
@@ -47,7 +47,7 @@
         T item = null;
 
         try {
-            while (getLength() == 0) {
+            while (isEmpty()) {
                 wait();
             }
 
@@ -62,19 +62,7 @@
         return ((Queue<T>)collection).peek();
     }
 
-    @Override
-    public synchronized int add(T item) {
-        int index = super.add(item);
-
-        notify();
-
-        return index;
-    }
-
-    @Override
-    public synchronized void insert(T item, int index) {
-        super.insert(item, index);
-
-        notify();
+    public synchronized boolean isEmpty() {
+        return ((Queue<T>)collection).isEmpty();
     }
 }

Modified: incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedSet.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedSet.java?rev=780667&r1=780666&r2=780667&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedSet.java (original)
+++ incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedSet.java Mon Jun  1 14:50:41 2009
@@ -80,10 +80,6 @@
         ((Set<E>)collection).remove(element);
     }
 
-    public synchronized void clear() {
-        ((Set<E>)collection).clear();
-    }
-
     public synchronized boolean contains(E element) {
         return ((Set<E>)collection).contains(element);
     }

Modified: incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedStack.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedStack.java?rev=780667&r1=780666&r2=780667&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedStack.java (original)
+++ incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedStack.java Mon Jun  1 14:50:41 2009
@@ -23,7 +23,7 @@
  *
  * @author gbrown
  */
-public class SynchronizedStack<T> extends SynchronizedList<T>
+public class SynchronizedStack<T> extends SynchronizedCollection<T>
     implements Stack<T> {
     public SynchronizedStack(Stack<T> stack) {
         super(stack);
@@ -45,7 +45,7 @@
         T item = null;
 
         try {
-            while (getLength() == 0) {
+            while (isEmpty()) {
                 wait();
             }
 
@@ -64,4 +64,8 @@
     public T poke(T item) {
         return ((Stack<T>)collection).poke(item);
     }
+
+    public synchronized boolean isEmpty() {
+        return ((Stack<T>)collection).isEmpty();
+    }
 }