1 11 package org.eclipse.core.internal.jobs; 12 13 import java.util.*; 14 15 18 public class Queue { 19 protected Object [] elements; 20 protected int head; 21 protected boolean reuse; 22 protected int tail; 23 24 public Queue() { 25 this(20, false); 26 } 27 28 36 public Queue(int size, boolean reuse) { 37 elements = new Object [size]; 38 head = tail = 0; 39 this.reuse = reuse; 40 } 41 42 45 public void enqueue(Object element) { 46 int newTail = increment(tail); 47 if (newTail == head) { 48 grow(); 49 newTail = tail + 1; 50 } 51 elements[tail] = element; 52 tail = newTail; 53 } 54 55 59 public int decrement(int index) { 60 return (index == 0) ? (elements.length - 1) : index - 1; 61 } 62 63 public Iterator elements() { 64 65 if (isEmpty()) 66 return new ArrayList(0).iterator(); 67 68 69 if (head <= tail) 70 return Arrays.asList(elements).iterator(); 71 72 73 Object [] newElements = new Object [size()]; 74 int end = (elements.length - head); 75 System.arraycopy(elements, head, newElements, 0, end); 76 System.arraycopy(elements, 0, newElements, end, tail); 77 return Arrays.asList(newElements).iterator(); 78 } 79 80 public Object get(Object o) { 81 int index = head; 82 while (index != tail) { 83 if (elements[index].equals(o)) 84 return elements[index]; 85 index = increment(index); 86 } 87 return null; 88 } 89 90 93 public boolean remove(Object o) { 94 int index = head; 95 while (index != tail) { 97 if (elements[index].equals(o)) 98 break; 99 index = increment(index); 100 } 101 if (index == tail) 103 return false; 104 Object toRemove = elements[index]; 106 int nextIndex = -1; 107 while (index != tail) { 108 nextIndex = increment(index); 109 if (nextIndex != tail) 110 elements[index] = elements[nextIndex]; 111 112 index = nextIndex; 113 } 114 tail = decrement(tail); 116 117 elements[tail] = reuse ? toRemove : null; 120 return true; 121 } 122 123 protected void grow() { 124 int newSize = (int) (elements.length * 1.5); 125 Object [] newElements = new Object [newSize]; 126 if (tail >= head) 127 System.arraycopy(elements, head, newElements, head, size()); 128 else { 129 int newHead = newSize - (elements.length - head); 130 System.arraycopy(elements, 0, newElements, 0, tail + 1); 131 System.arraycopy(elements, head, newElements, newHead, (newSize - newHead)); 132 head = newHead; 133 } 134 elements = newElements; 135 } 136 137 141 public int increment(int index) { 142 return (index == (elements.length - 1)) ? 0 : index + 1; 143 } 144 145 public boolean isEmpty() { 146 return tail == head; 147 } 148 149 public Object peek() { 150 return elements[head]; 151 } 152 153 156 public Object dequeue() { 157 if (isEmpty()) 158 return null; 159 Object result = peek(); 160 if (!reuse) 161 elements[head] = null; 162 head = increment(head); 163 return result; 164 } 165 166 public int size() { 167 return tail > head ? (tail - head) : ((elements.length - head) + tail); 168 } 169 170 public String toString() { 171 StringBuffer sb = new StringBuffer (); 172 sb.append("["); if (!isEmpty()) { 174 Iterator it = elements(); 175 while (true) { 176 sb.append(it.next()); 177 if (it.hasNext()) 178 sb.append(", "); else 180 break; 181 } 182 } 183 sb.append("]"); return sb.toString(); 185 } 186 } 187 | Popular Tags |