diff dwtx/jface/viewers/deferred/ChangeQueue.d @ 10:b6c35faf97c8

Viewers
author Frank Benoit <benoit@tionex.de>
date Mon, 31 Mar 2008 00:47:19 +0200
parents
children 04b47443bb01
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dwtx/jface/viewers/deferred/ChangeQueue.d	Mon Mar 31 00:47:19 2008 +0200
@@ -0,0 +1,149 @@
+/*******************************************************************************
+ * Copyright (c) 2004, 2005 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *     Frank Benoit <benoit@tionex.de>
+ *******************************************************************************/
+module dwtx.jface.viewers.deferred.ChangeQueue;
+
+import tango.util.collection.LinkSeq;
+
+import dwt.dwthelper.utils;
+
+/**
+ * Holds a queue of additions, removals, updates, and SET calls for a
+ * BackgroundContentProvider
+ */
+final class ChangeQueue {
+    /**
+     * Represents the addition of an item
+     * @since 3.1
+     */
+    public static const int ADD = 0;
+    /**
+     * Represents the removal of an item
+     * @since 3.1
+     */
+    public static const int REMOVE = 1;
+    /**
+     * Represents a reset of all the items
+     * @since 3.1
+     */
+    public static const int SET = 2;
+    /**
+     * Represents an update of an item
+     * @since 3.1
+     */
+    public static const int UPDATE = 3;
+
+    /**
+     *
+     * @since 3.1
+     */
+    public static final class Change {
+        private int type;
+        private Object[] elements;
+
+        /**
+         * Create a change of the specified type that affects the given elements.
+         *
+         * @param type one of <code>ADD</code>, <code>REMOVE</code>, <code>SET</code>, or <code>UPDATE</code>.
+         * @param elements the elements affected by the change.
+         *
+         * @since 3.1
+         */
+        public this(int type, Object[] elements) {
+            this.type = type;
+            this.elements = elements;
+        }
+
+        /**
+         * Get the type of change.
+         * @return one of <code>ADD</code>, <code>REMOVE</code>, <code>SET</code>, or <code>UPDATE</code>.
+         *
+         * @since 3.1
+         */
+        public int getType() {
+            return type;
+        }
+
+        /**
+         * Return the elements associated with the change.
+         * @return the elements affected by the change.
+         *
+         * @since 3.1
+         */
+        public Object[] getElements() {
+            return elements;
+        }
+    }
+
+    private LinkSeq!(Change) queue;
+    private int workload = 0;
+
+    public this(){
+        queue = new LinkSeq!(Change);
+    }
+
+    /**
+     * Create a change of the given type and elements and enqueue it.
+     *
+     * @param type the type of change to be created
+     * @param elements the elements affected by the change
+     */
+    public synchronized void enqueue(int type, Object[] elements) {
+        enqueue(new Change(type, elements));
+    }
+
+    /**
+     * Add the specified change to the queue
+     * @param toQueue the change to be added
+     */
+    public synchronized void enqueue(Change toQueue) {
+        // A SET event makes all previous adds, removes, and sets redundant... so remove
+        // them from the queue
+        if (toQueue.type is SET) {
+            workload = 0;
+            LinkSeq!(Change) newQueue = new LinkSeq!(Change);
+            foreach( next; queue ){
+
+                if (next.getType() is ADD || next.getType() is REMOVE || next.getType() is SET) {
+                    continue;
+                }
+
+                newQueue.append(next);
+                workload += next.elements.length;
+            }
+            queue = newQueue;
+        }
+
+        queue.append(toQueue);
+        workload += toQueue.elements.length;
+    }
+
+    /**
+     * Remove the first change from the queue.
+     * @return the first change
+     */
+    public synchronized Change dequeue() {
+        Change result = queue.head;
+        queue.removeHead();
+
+        workload -= result.elements.length;
+        return result;
+    }
+
+    /**
+     * Return whether the queue is empty
+     * @return <code>true</code> if empty, <code>false</code> otherwise
+     */
+    public synchronized bool isEmpty() {
+        return queue.drained();
+    }
+}