10
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2004, 2005 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.viewers.deferred.ChangeQueue;
|
|
14
|
|
15 import tango.util.collection.LinkSeq;
|
|
16
|
|
17 import dwt.dwthelper.utils;
|
|
18
|
|
19 /**
|
|
20 * Holds a queue of additions, removals, updates, and SET calls for a
|
|
21 * BackgroundContentProvider
|
|
22 */
|
|
23 final class ChangeQueue {
|
|
24 /**
|
|
25 * Represents the addition of an item
|
|
26 * @since 3.1
|
|
27 */
|
|
28 public static const int ADD = 0;
|
|
29 /**
|
|
30 * Represents the removal of an item
|
|
31 * @since 3.1
|
|
32 */
|
|
33 public static const int REMOVE = 1;
|
|
34 /**
|
|
35 * Represents a reset of all the items
|
|
36 * @since 3.1
|
|
37 */
|
|
38 public static const int SET = 2;
|
|
39 /**
|
|
40 * Represents an update of an item
|
|
41 * @since 3.1
|
|
42 */
|
|
43 public static const int UPDATE = 3;
|
|
44
|
|
45 /**
|
|
46 *
|
|
47 * @since 3.1
|
|
48 */
|
|
49 public static final class Change {
|
|
50 private int type;
|
|
51 private Object[] elements;
|
|
52
|
|
53 /**
|
|
54 * Create a change of the specified type that affects the given elements.
|
|
55 *
|
|
56 * @param type one of <code>ADD</code>, <code>REMOVE</code>, <code>SET</code>, or <code>UPDATE</code>.
|
|
57 * @param elements the elements affected by the change.
|
|
58 *
|
|
59 * @since 3.1
|
|
60 */
|
|
61 public this(int type, Object[] elements) {
|
|
62 this.type = type;
|
|
63 this.elements = elements;
|
|
64 }
|
|
65
|
|
66 /**
|
|
67 * Get the type of change.
|
|
68 * @return one of <code>ADD</code>, <code>REMOVE</code>, <code>SET</code>, or <code>UPDATE</code>.
|
|
69 *
|
|
70 * @since 3.1
|
|
71 */
|
|
72 public int getType() {
|
|
73 return type;
|
|
74 }
|
|
75
|
|
76 /**
|
|
77 * Return the elements associated with the change.
|
|
78 * @return the elements affected by the change.
|
|
79 *
|
|
80 * @since 3.1
|
|
81 */
|
|
82 public Object[] getElements() {
|
|
83 return elements;
|
|
84 }
|
|
85 }
|
|
86
|
|
87 private LinkSeq!(Change) queue;
|
|
88 private int workload = 0;
|
|
89
|
|
90 public this(){
|
|
91 queue = new LinkSeq!(Change);
|
|
92 }
|
|
93
|
|
94 /**
|
|
95 * Create a change of the given type and elements and enqueue it.
|
|
96 *
|
|
97 * @param type the type of change to be created
|
|
98 * @param elements the elements affected by the change
|
|
99 */
|
|
100 public synchronized void enqueue(int type, Object[] elements) {
|
|
101 enqueue(new Change(type, elements));
|
|
102 }
|
|
103
|
|
104 /**
|
|
105 * Add the specified change to the queue
|
|
106 * @param toQueue the change to be added
|
|
107 */
|
|
108 public synchronized void enqueue(Change toQueue) {
|
|
109 // A SET event makes all previous adds, removes, and sets redundant... so remove
|
|
110 // them from the queue
|
|
111 if (toQueue.type is SET) {
|
|
112 workload = 0;
|
|
113 LinkSeq!(Change) newQueue = new LinkSeq!(Change);
|
|
114 foreach( next; queue ){
|
|
115
|
|
116 if (next.getType() is ADD || next.getType() is REMOVE || next.getType() is SET) {
|
|
117 continue;
|
|
118 }
|
|
119
|
|
120 newQueue.append(next);
|
|
121 workload += next.elements.length;
|
|
122 }
|
|
123 queue = newQueue;
|
|
124 }
|
|
125
|
|
126 queue.append(toQueue);
|
|
127 workload += toQueue.elements.length;
|
|
128 }
|
|
129
|
|
130 /**
|
|
131 * Remove the first change from the queue.
|
|
132 * @return the first change
|
|
133 */
|
|
134 public synchronized Change dequeue() {
|
|
135 Change result = queue.head;
|
|
136 queue.removeHead();
|
|
137
|
|
138 workload -= result.elements.length;
|
|
139 return result;
|
|
140 }
|
|
141
|
|
142 /**
|
|
143 * Return whether the queue is empty
|
|
144 * @return <code>true</code> if empty, <code>false</code> otherwise
|
|
145 */
|
|
146 public synchronized bool isEmpty() {
|
|
147 return queue.drained();
|
|
148 }
|
|
149 }
|