equal
deleted
inserted
replaced
23 * have any questions. |
23 * have any questions. |
24 */ |
24 */ |
25 |
25 |
26 package com.sun.tools.javac.util; |
26 package com.sun.tools.javac.util; |
27 |
27 |
|
28 import java.util.AbstractQueue; |
28 import java.util.Collection; |
29 import java.util.Collection; |
29 import java.util.Iterator; |
30 import java.util.Iterator; |
30 import java.util.NoSuchElementException; |
31 import java.util.NoSuchElementException; |
31 |
32 |
32 /** A class for constructing lists by appending elements. Modelled after |
33 /** A class for constructing lists by appending elements. Modelled after |
35 * <p><b>This is NOT part of any API supported by Sun Microsystems. If |
36 * <p><b>This is NOT part of any API supported by Sun Microsystems. If |
36 * you write code that depends on this, you do so at your own risk. |
37 * you write code that depends on this, you do so at your own risk. |
37 * This code and its internal interfaces are subject to change or |
38 * This code and its internal interfaces are subject to change or |
38 * deletion without notice.</b> |
39 * deletion without notice.</b> |
39 */ |
40 */ |
40 public class ListBuffer<A> implements Collection<A> { |
41 public class ListBuffer<A> extends AbstractQueue<A> { |
41 |
42 |
42 public static <T> ListBuffer<T> lb() { |
43 public static <T> ListBuffer<T> lb() { |
43 return new ListBuffer<T>(); |
44 return new ListBuffer<T>(); |
|
45 } |
|
46 |
|
47 public static <T> ListBuffer<T> of(T x) { |
|
48 ListBuffer<T> lb = new ListBuffer<T>(); |
|
49 lb.add(x); |
|
50 return lb; |
44 } |
51 } |
45 |
52 |
46 /** The list of elements of this buffer. |
53 /** The list of elements of this buffer. |
47 */ |
54 */ |
48 public List<A> elems; |
55 public List<A> elems; |
117 } |
124 } |
118 |
125 |
119 /** Append an element to buffer. |
126 /** Append an element to buffer. |
120 */ |
127 */ |
121 public ListBuffer<A> append(A x) { |
128 public ListBuffer<A> append(A x) { |
|
129 x.getClass(); // null check |
122 if (shared) copy(); |
130 if (shared) copy(); |
123 last.head = x; |
131 last.head = x; |
124 last.setTail(new List<A>(null,null)); |
132 last.setTail(new List<A>(null,null)); |
125 last = last.tail; |
133 last = last.tail; |
126 count++; |
134 count++; |
178 */ |
186 */ |
179 public A first() { |
187 public A first() { |
180 return elems.head; |
188 return elems.head; |
181 } |
189 } |
182 |
190 |
183 /** Remove the first element in this buffer. |
191 /** Return first element in this buffer and remove |
184 */ |
192 */ |
185 public void remove() { |
193 public A next() { |
|
194 A x = elems.head; |
186 if (elems != last) { |
195 if (elems != last) { |
187 elems = elems.tail; |
196 elems = elems.tail; |
188 count--; |
197 count--; |
189 } |
198 } |
190 } |
|
191 |
|
192 /** Return first element in this buffer and remove |
|
193 */ |
|
194 public A next() { |
|
195 A x = elems.head; |
|
196 remove(); |
|
197 return x; |
199 return x; |
198 } |
200 } |
199 |
201 |
200 /** An enumeration of all elements in this buffer. |
202 /** An enumeration of all elements in this buffer. |
201 */ |
203 */ |
217 } |
219 } |
218 }; |
220 }; |
219 } |
221 } |
220 |
222 |
221 public boolean add(A a) { |
223 public boolean add(A a) { |
222 throw new UnsupportedOperationException(); |
224 append(a); |
223 } |
225 return true; |
|
226 } |
|
227 |
224 public boolean remove(Object o) { |
228 public boolean remove(Object o) { |
225 throw new UnsupportedOperationException(); |
229 throw new UnsupportedOperationException(); |
226 } |
230 } |
|
231 |
227 public boolean containsAll(Collection<?> c) { |
232 public boolean containsAll(Collection<?> c) { |
228 throw new UnsupportedOperationException(); |
233 for (Object x: c) { |
229 } |
234 if (!contains(x)) |
|
235 return false; |
|
236 } |
|
237 return true; |
|
238 } |
|
239 |
230 public boolean addAll(Collection<? extends A> c) { |
240 public boolean addAll(Collection<? extends A> c) { |
231 throw new UnsupportedOperationException(); |
241 for (A a: c) |
232 } |
242 append(a); |
|
243 return true; |
|
244 } |
|
245 |
233 public boolean removeAll(Collection<?> c) { |
246 public boolean removeAll(Collection<?> c) { |
234 throw new UnsupportedOperationException(); |
247 throw new UnsupportedOperationException(); |
235 } |
248 } |
|
249 |
236 public boolean retainAll(Collection<?> c) { |
250 public boolean retainAll(Collection<?> c) { |
237 throw new UnsupportedOperationException(); |
251 throw new UnsupportedOperationException(); |
238 } |
252 } |
|
253 |
|
254 public boolean offer(A a) { |
|
255 append(a); |
|
256 return true; |
|
257 } |
|
258 |
|
259 public A poll() { |
|
260 return next(); |
|
261 } |
|
262 |
|
263 public A peek() { |
|
264 return first(); |
|
265 } |
239 } |
266 } |