KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > deferred > ChangeQueue


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  *******************************************************************************/

11 package org.eclipse.jface.viewers.deferred;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.LinkedList JavaDoc;
15
16 /**
17  * Holds a queue of additions, removals, updates, and SET calls for a
18  * BackgroundContentProvider
19  */

20 final class ChangeQueue {
21     /**
22      * Represents the addition of an item
23      * @since 3.1
24      */

25     public static final int ADD = 0;
26     /**
27      * Represents the removal of an item
28      * @since 3.1
29      */

30     public static final int REMOVE = 1;
31     /**
32      * Represents a reset of all the items
33      * @since 3.1
34      */

35     public static final int SET = 2;
36     /**
37      * Represents an update of an item
38      * @since 3.1
39      */

40     public static final int UPDATE = 3;
41     
42     /**
43      *
44      * @since 3.1
45      */

46     public static final class Change {
47         private int type;
48         private Object JavaDoc[] elements;
49         
50         /**
51          * Create a change of the specified type that affects the given elements.
52          *
53          * @param type one of <code>ADD</code>, <code>REMOVE</code>, <code>SET</code>, or <code>UPDATE</code>.
54          * @param elements the elements affected by the change.
55          *
56          * @since 3.1
57          */

58         public Change(int type, Object JavaDoc[] elements) {
59             this.type = type;
60             this.elements = elements;
61         }
62         
63         /**
64          * Get the type of change.
65          * @return one of <code>ADD</code>, <code>REMOVE</code>, <code>SET</code>, or <code>UPDATE</code>.
66          *
67          * @since 3.1
68          */

69         public int getType() {
70             return type;
71         }
72         
73         /**
74          * Return the elements associated with the change.
75          * @return the elements affected by the change.
76          *
77          * @since 3.1
78          */

79         public Object JavaDoc[] getElements() {
80             return elements;
81         }
82     }
83     
84     private LinkedList JavaDoc queue = new LinkedList JavaDoc();
85     private int workload = 0;
86     
87     /**
88      * Create a change of the given type and elements and enqueue it.
89      *
90      * @param type the type of change to be created
91      * @param elements the elements affected by the change
92      */

93     public synchronized void enqueue(int type, Object JavaDoc[] elements) {
94         enqueue(new Change(type, elements));
95     }
96     
97     /**
98      * Add the specified change to the queue
99      * @param toQueue the change to be added
100      */

101     public synchronized void enqueue(Change toQueue) {
102         // A SET event makes all previous adds, removes, and sets redundant... so remove
103
// them from the queue
104
if (toQueue.type == SET) {
105             workload = 0;
106             LinkedList JavaDoc newQueue = new LinkedList JavaDoc();
107             for (Iterator JavaDoc iter = queue.iterator(); iter.hasNext();) {
108                 Change next = (Change) iter.next();
109                 
110                 if (next.getType() == ADD || next.getType() == REMOVE || next.getType() == SET) {
111                     continue;
112                 }
113                 
114                 newQueue.add(next);
115                 workload += next.elements.length;
116             }
117             queue = newQueue;
118         }
119         
120         queue.add(toQueue);
121         workload += toQueue.elements.length;
122     }
123     
124     /**
125      * Remove the first change from the queue.
126      * @return the first change
127      */

128     public synchronized Change dequeue() {
129         Change result = (Change)queue.removeFirst();
130         
131         workload -= result.elements.length;
132         return result;
133     }
134     
135     /**
136      * Return whether the queue is empty
137      * @return <code>true</code> if empty, <code>false</code> otherwise
138      */

139     public synchronized boolean isEmpty() {
140         return queue.isEmpty();
141     }
142 }
143
Popular Tags