author | jjg |
Thu, 10 Jun 2010 16:08:01 -0700 | |
changeset 5847 | 1908176fd6e3 |
parent 5520 | 86e4b9a9da40 |
child 14803 | 88347e495d34 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
5520 | 2 |
* Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved. |
10 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5520 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.util; |
|
27 |
||
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
28 |
import java.util.AbstractQueue; |
10 | 29 |
import java.util.Collection; |
30 |
import java.util.Iterator; |
|
31 |
import java.util.NoSuchElementException; |
|
32 |
||
33 |
/** A class for constructing lists by appending elements. Modelled after |
|
34 |
* java.lang.StringBuffer. |
|
35 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
36 |
* <p><b>This is NOT part of any supported API. |
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
37 |
* If you write code that depends on this, you do so at your own risk. |
10 | 38 |
* This code and its internal interfaces are subject to change or |
39 |
* deletion without notice.</b> |
|
40 |
*/ |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
41 |
public class ListBuffer<A> extends AbstractQueue<A> { |
10 | 42 |
|
43 |
public static <T> ListBuffer<T> lb() { |
|
44 |
return new ListBuffer<T>(); |
|
45 |
} |
|
46 |
||
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
47 |
public static <T> ListBuffer<T> of(T x) { |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
48 |
ListBuffer<T> lb = new ListBuffer<T>(); |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
49 |
lb.add(x); |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
50 |
return lb; |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
51 |
} |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
52 |
|
10 | 53 |
/** The list of elements of this buffer. |
54 |
*/ |
|
55 |
public List<A> elems; |
|
56 |
||
57 |
/** A pointer pointing to the last, sentinel element of `elems'. |
|
58 |
*/ |
|
59 |
public List<A> last; |
|
60 |
||
61 |
/** The number of element in this buffer. |
|
62 |
*/ |
|
63 |
public int count; |
|
64 |
||
65 |
/** Has a list been created from this buffer yet? |
|
66 |
*/ |
|
67 |
public boolean shared; |
|
68 |
||
69 |
/** Create a new initially empty list buffer. |
|
70 |
*/ |
|
71 |
public ListBuffer() { |
|
72 |
clear(); |
|
73 |
} |
|
74 |
||
75 |
public final void clear() { |
|
76 |
this.elems = new List<A>(null,null); |
|
77 |
this.last = this.elems; |
|
78 |
count = 0; |
|
79 |
shared = false; |
|
80 |
} |
|
81 |
||
82 |
/** Return the number of elements in this buffer. |
|
83 |
*/ |
|
84 |
public int length() { |
|
85 |
return count; |
|
86 |
} |
|
87 |
public int size() { |
|
88 |
return count; |
|
89 |
} |
|
90 |
||
91 |
/** Is buffer empty? |
|
92 |
*/ |
|
93 |
public boolean isEmpty() { |
|
94 |
return count == 0; |
|
95 |
} |
|
96 |
||
97 |
/** Is buffer not empty? |
|
98 |
*/ |
|
99 |
public boolean nonEmpty() { |
|
100 |
return count != 0; |
|
101 |
} |
|
102 |
||
103 |
/** Copy list and sets last. |
|
104 |
*/ |
|
105 |
private void copy() { |
|
106 |
List<A> p = elems = new List<A>(elems.head, elems.tail); |
|
107 |
while (true) { |
|
108 |
List<A> tail = p.tail; |
|
109 |
if (tail == null) break; |
|
110 |
tail = new List<A>(tail.head, tail.tail); |
|
111 |
p.setTail(tail); |
|
112 |
p = tail; |
|
113 |
} |
|
114 |
last = p; |
|
115 |
shared = false; |
|
116 |
} |
|
117 |
||
118 |
/** Prepend an element to buffer. |
|
119 |
*/ |
|
120 |
public ListBuffer<A> prepend(A x) { |
|
121 |
elems = elems.prepend(x); |
|
122 |
count++; |
|
123 |
return this; |
|
124 |
} |
|
125 |
||
126 |
/** Append an element to buffer. |
|
127 |
*/ |
|
128 |
public ListBuffer<A> append(A x) { |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
129 |
x.getClass(); // null check |
10 | 130 |
if (shared) copy(); |
131 |
last.head = x; |
|
132 |
last.setTail(new List<A>(null,null)); |
|
133 |
last = last.tail; |
|
134 |
count++; |
|
135 |
return this; |
|
136 |
} |
|
137 |
||
138 |
/** Append all elements in a list to buffer. |
|
139 |
*/ |
|
140 |
public ListBuffer<A> appendList(List<A> xs) { |
|
141 |
while (xs.nonEmpty()) { |
|
142 |
append(xs.head); |
|
143 |
xs = xs.tail; |
|
144 |
} |
|
145 |
return this; |
|
146 |
} |
|
147 |
||
148 |
/** Append all elements in a list to buffer. |
|
149 |
*/ |
|
150 |
public ListBuffer<A> appendList(ListBuffer<A> xs) { |
|
151 |
return appendList(xs.toList()); |
|
152 |
} |
|
153 |
||
154 |
/** Append all elements in an array to buffer. |
|
155 |
*/ |
|
156 |
public ListBuffer<A> appendArray(A[] xs) { |
|
157 |
for (int i = 0; i < xs.length; i++) { |
|
158 |
append(xs[i]); |
|
159 |
} |
|
160 |
return this; |
|
161 |
} |
|
162 |
||
163 |
/** Convert buffer to a list of all its elements. |
|
164 |
*/ |
|
165 |
public List<A> toList() { |
|
166 |
shared = true; |
|
167 |
return elems; |
|
168 |
} |
|
169 |
||
170 |
/** Does the list contain the specified element? |
|
171 |
*/ |
|
172 |
public boolean contains(Object x) { |
|
173 |
return elems.contains(x); |
|
174 |
} |
|
175 |
||
176 |
/** Convert buffer to an array |
|
177 |
*/ |
|
178 |
public <T> T[] toArray(T[] vec) { |
|
179 |
return elems.toArray(vec); |
|
180 |
} |
|
181 |
public Object[] toArray() { |
|
182 |
return toArray(new Object[size()]); |
|
183 |
} |
|
184 |
||
185 |
/** The first element in this buffer. |
|
186 |
*/ |
|
187 |
public A first() { |
|
188 |
return elems.head; |
|
189 |
} |
|
190 |
||
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
191 |
/** Return first element in this buffer and remove |
10 | 192 |
*/ |
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
193 |
public A next() { |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
194 |
A x = elems.head; |
10 | 195 |
if (elems != last) { |
196 |
elems = elems.tail; |
|
197 |
count--; |
|
198 |
} |
|
199 |
return x; |
|
200 |
} |
|
201 |
||
202 |
/** An enumeration of all elements in this buffer. |
|
203 |
*/ |
|
204 |
public Iterator<A> iterator() { |
|
205 |
return new Iterator<A>() { |
|
206 |
List<A> elems = ListBuffer.this.elems; |
|
207 |
public boolean hasNext() { |
|
208 |
return elems != last; |
|
209 |
} |
|
210 |
public A next() { |
|
211 |
if (elems == last) |
|
212 |
throw new NoSuchElementException(); |
|
213 |
A elem = elems.head; |
|
214 |
elems = elems.tail; |
|
215 |
return elem; |
|
216 |
} |
|
217 |
public void remove() { |
|
218 |
throw new UnsupportedOperationException(); |
|
219 |
} |
|
220 |
}; |
|
221 |
} |
|
222 |
||
223 |
public boolean add(A a) { |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
224 |
append(a); |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
225 |
return true; |
10 | 226 |
} |
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
227 |
|
10 | 228 |
public boolean remove(Object o) { |
229 |
throw new UnsupportedOperationException(); |
|
230 |
} |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
231 |
|
10 | 232 |
public boolean containsAll(Collection<?> c) { |
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
233 |
for (Object x: c) { |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
234 |
if (!contains(x)) |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
235 |
return false; |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
236 |
} |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
237 |
return true; |
10 | 238 |
} |
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
239 |
|
10 | 240 |
public boolean addAll(Collection<? extends A> c) { |
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
241 |
for (A a: c) |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
242 |
append(a); |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
243 |
return true; |
10 | 244 |
} |
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
245 |
|
10 | 246 |
public boolean removeAll(Collection<?> c) { |
247 |
throw new UnsupportedOperationException(); |
|
248 |
} |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
249 |
|
10 | 250 |
public boolean retainAll(Collection<?> c) { |
251 |
throw new UnsupportedOperationException(); |
|
252 |
} |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
253 |
|
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
254 |
public boolean offer(A a) { |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
255 |
append(a); |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
256 |
return true; |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
257 |
} |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
258 |
|
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
259 |
public A poll() { |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
260 |
return next(); |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
261 |
} |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
262 |
|
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
263 |
public A peek() { |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
264 |
return first(); |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
265 |
} |
10 | 266 |
} |