author | mcimadamore |
Fri, 16 Dec 2016 15:27:47 +0000 | |
changeset 42828 | cce89649f958 |
parent 29291 | 076c277565f7 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
14803
diff
changeset
|
2 |
* Copyright (c) 1999, 2013, 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 |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
43 |
public static <T> ListBuffer<T> of(T x) { |
22163 | 44 |
ListBuffer<T> lb = new ListBuffer<>(); |
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
45 |
lb.add(x); |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
46 |
return lb; |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
47 |
} |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
48 |
|
10 | 49 |
/** The list of elements of this buffer. |
50 |
*/ |
|
14803
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
51 |
private List<A> elems; |
10 | 52 |
|
14803
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
53 |
/** A pointer pointing to the last element of 'elems' containing data, |
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
54 |
* or null if the list is empty. |
10 | 55 |
*/ |
14803
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
56 |
private List<A> last; |
10 | 57 |
|
58 |
/** The number of element in this buffer. |
|
59 |
*/ |
|
14803
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
60 |
private int count; |
10 | 61 |
|
62 |
/** Has a list been created from this buffer yet? |
|
63 |
*/ |
|
14803
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
64 |
private boolean shared; |
10 | 65 |
|
66 |
/** Create a new initially empty list buffer. |
|
67 |
*/ |
|
68 |
public ListBuffer() { |
|
69 |
clear(); |
|
70 |
} |
|
71 |
||
72 |
public final void clear() { |
|
14803
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
73 |
this.elems = List.nil(); |
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
74 |
this.last = null; |
10 | 75 |
count = 0; |
76 |
shared = false; |
|
77 |
} |
|
78 |
||
79 |
/** Return the number of elements in this buffer. |
|
80 |
*/ |
|
81 |
public int length() { |
|
82 |
return count; |
|
83 |
} |
|
84 |
public int size() { |
|
85 |
return count; |
|
86 |
} |
|
87 |
||
88 |
/** Is buffer empty? |
|
89 |
*/ |
|
90 |
public boolean isEmpty() { |
|
91 |
return count == 0; |
|
92 |
} |
|
93 |
||
94 |
/** Is buffer not empty? |
|
95 |
*/ |
|
96 |
public boolean nonEmpty() { |
|
97 |
return count != 0; |
|
98 |
} |
|
99 |
||
100 |
/** Copy list and sets last. |
|
101 |
*/ |
|
102 |
private void copy() { |
|
14803
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
103 |
if (elems.nonEmpty()) { |
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
104 |
List<A> orig = elems; |
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
105 |
|
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
29291
diff
changeset
|
106 |
elems = last = List.of(orig.head); |
14803
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
107 |
|
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
108 |
while ((orig = orig.tail).nonEmpty()) { |
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
29291
diff
changeset
|
109 |
last.tail = List.of(orig.head); |
14803
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
110 |
last = last.tail; |
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
111 |
} |
10 | 112 |
} |
113 |
} |
|
114 |
||
115 |
/** Prepend an element to buffer. |
|
116 |
*/ |
|
117 |
public ListBuffer<A> prepend(A x) { |
|
118 |
elems = elems.prepend(x); |
|
14803
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
119 |
if (last == null) last = elems; |
10 | 120 |
count++; |
121 |
return this; |
|
122 |
} |
|
123 |
||
124 |
/** Append an element to buffer. |
|
125 |
*/ |
|
126 |
public ListBuffer<A> append(A x) { |
|
29291
076c277565f7
8073550: java* tools: replace obj.getClass hacks with Assert.checkNonNull or Objects.requireNonNull
mcimadamore
parents:
25874
diff
changeset
|
127 |
Assert.checkNonNull(x); |
10 | 128 |
if (shared) copy(); |
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
29291
diff
changeset
|
129 |
List<A> newLast = List.of(x); |
14803
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
130 |
if (last != null) { |
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
131 |
last.tail = newLast; |
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
132 |
last = newLast; |
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
133 |
} else { |
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
134 |
elems = last = newLast; |
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
135 |
} |
10 | 136 |
count++; |
137 |
return this; |
|
138 |
} |
|
139 |
||
140 |
/** Append all elements in a list to buffer. |
|
141 |
*/ |
|
142 |
public ListBuffer<A> appendList(List<A> xs) { |
|
143 |
while (xs.nonEmpty()) { |
|
144 |
append(xs.head); |
|
145 |
xs = xs.tail; |
|
146 |
} |
|
147 |
return this; |
|
148 |
} |
|
149 |
||
150 |
/** Append all elements in a list to buffer. |
|
151 |
*/ |
|
152 |
public ListBuffer<A> appendList(ListBuffer<A> xs) { |
|
153 |
return appendList(xs.toList()); |
|
154 |
} |
|
155 |
||
156 |
/** Append all elements in an array to buffer. |
|
157 |
*/ |
|
158 |
public ListBuffer<A> appendArray(A[] xs) { |
|
22159
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
20249
diff
changeset
|
159 |
for (A x : xs) { |
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
20249
diff
changeset
|
160 |
append(x); |
10 | 161 |
} |
162 |
return this; |
|
163 |
} |
|
164 |
||
165 |
/** Convert buffer to a list of all its elements. |
|
166 |
*/ |
|
167 |
public List<A> toList() { |
|
168 |
shared = true; |
|
169 |
return elems; |
|
170 |
} |
|
171 |
||
172 |
/** Does the list contain the specified element? |
|
173 |
*/ |
|
174 |
public boolean contains(Object x) { |
|
175 |
return elems.contains(x); |
|
176 |
} |
|
177 |
||
178 |
/** Convert buffer to an array |
|
179 |
*/ |
|
180 |
public <T> T[] toArray(T[] vec) { |
|
181 |
return elems.toArray(vec); |
|
182 |
} |
|
183 |
public Object[] toArray() { |
|
184 |
return toArray(new Object[size()]); |
|
185 |
} |
|
186 |
||
187 |
/** The first element in this buffer. |
|
188 |
*/ |
|
189 |
public A first() { |
|
190 |
return elems.head; |
|
191 |
} |
|
192 |
||
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
193 |
/** Return first element in this buffer and remove |
10 | 194 |
*/ |
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
195 |
public A next() { |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
196 |
A x = elems.head; |
14803
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
197 |
if (!elems.isEmpty()) { |
10 | 198 |
elems = elems.tail; |
14803
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
199 |
if (elems.isEmpty()) last = null; |
10 | 200 |
count--; |
201 |
} |
|
202 |
return x; |
|
203 |
} |
|
204 |
||
205 |
/** An enumeration of all elements in this buffer. |
|
206 |
*/ |
|
207 |
public Iterator<A> iterator() { |
|
208 |
return new Iterator<A>() { |
|
209 |
List<A> elems = ListBuffer.this.elems; |
|
210 |
public boolean hasNext() { |
|
14803
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
211 |
return !elems.isEmpty(); |
10 | 212 |
} |
213 |
public A next() { |
|
14803
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
214 |
if (elems.isEmpty()) |
10 | 215 |
throw new NoSuchElementException(); |
216 |
A elem = elems.head; |
|
217 |
elems = elems.tail; |
|
218 |
return elem; |
|
219 |
} |
|
220 |
public void remove() { |
|
221 |
throw new UnsupportedOperationException(); |
|
222 |
} |
|
223 |
}; |
|
224 |
} |
|
225 |
||
226 |
public boolean add(A a) { |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
227 |
append(a); |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
228 |
return true; |
10 | 229 |
} |
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
230 |
|
10 | 231 |
public boolean remove(Object o) { |
232 |
throw new UnsupportedOperationException(); |
|
233 |
} |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
234 |
|
10 | 235 |
public boolean containsAll(Collection<?> c) { |
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
236 |
for (Object x: c) { |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
237 |
if (!contains(x)) |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
238 |
return false; |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
239 |
} |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
240 |
return true; |
10 | 241 |
} |
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
242 |
|
10 | 243 |
public boolean addAll(Collection<? extends A> c) { |
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
244 |
for (A a: c) |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
245 |
append(a); |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
246 |
return true; |
10 | 247 |
} |
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
248 |
|
10 | 249 |
public boolean removeAll(Collection<?> c) { |
250 |
throw new UnsupportedOperationException(); |
|
251 |
} |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
252 |
|
10 | 253 |
public boolean retainAll(Collection<?> c) { |
254 |
throw new UnsupportedOperationException(); |
|
255 |
} |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
256 |
|
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
257 |
public boolean offer(A a) { |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
258 |
append(a); |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
259 |
return true; |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
260 |
} |
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 |
public A poll() { |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
263 |
return next(); |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
264 |
} |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
265 |
|
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
266 |
public A peek() { |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
267 |
return first(); |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
268 |
} |
14803
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
269 |
|
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
270 |
public A last() { |
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
271 |
return last != null ? last.head : null; |
88347e495d34
8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents:
5847
diff
changeset
|
272 |
} |
10 | 273 |
} |