1 11 package org.eclipse.jface.viewers.deferred; 12 13 import java.util.Iterator ; 14 import java.util.LinkedList ; 15 16 20 final class ChangeQueue { 21 25 public static final int ADD = 0; 26 30 public static final int REMOVE = 1; 31 35 public static final int SET = 2; 36 40 public static final int UPDATE = 3; 41 42 46 public static final class Change { 47 private int type; 48 private Object [] elements; 49 50 58 public Change(int type, Object [] elements) { 59 this.type = type; 60 this.elements = elements; 61 } 62 63 69 public int getType() { 70 return type; 71 } 72 73 79 public Object [] getElements() { 80 return elements; 81 } 82 } 83 84 private LinkedList queue = new LinkedList (); 85 private int workload = 0; 86 87 93 public synchronized void enqueue(int type, Object [] elements) { 94 enqueue(new Change(type, elements)); 95 } 96 97 101 public synchronized void enqueue(Change toQueue) { 102 if (toQueue.type == SET) { 105 workload = 0; 106 LinkedList newQueue = new LinkedList (); 107 for (Iterator 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 128 public synchronized Change dequeue() { 129 Change result = (Change)queue.removeFirst(); 130 131 workload -= result.elements.length; 132 return result; 133 } 134 135 139 public synchronized boolean isEmpty() { 140 return queue.isEmpty(); 141 } 142 } 143 | Popular Tags |