author | mcimadamore |
Wed, 15 May 2013 14:00:31 +0100 | |
changeset 17582 | 4079713129dd |
parent 17578 | 46ac954e4a84 |
child 19914 | d86271bd430a |
permissions | -rw-r--r-- |
10 | 1 |
/* |
17578 | 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 |
||
28 |
import java.lang.reflect.Array; |
|
29 |
import java.util.ArrayList; |
|
30 |
import java.util.Collection; |
|
31 |
import java.util.Collections; |
|
32 |
import java.util.Iterator; |
|
33 |
import java.util.AbstractCollection; |
|
34 |
import java.util.ListIterator; |
|
35 |
import java.util.NoSuchElementException; |
|
36 |
||
37 |
/** A class for generic linked lists. Links are supposed to be |
|
38 |
* immutable, the only exception being the incremental construction of |
|
39 |
* lists via ListBuffers. List is the main container class in |
|
17578 | 40 |
* GJC. Most data structures and algorithms in GJC use lists rather |
10 | 41 |
* than arrays. |
42 |
* |
|
43 |
* <p>Lists are always trailed by a sentinel element, whose head and tail |
|
44 |
* are both null. |
|
45 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5846
diff
changeset
|
46 |
* <p><b>This is NOT part of any supported API. |
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5846
diff
changeset
|
47 |
* If you write code that depends on this, you do so at your own risk. |
10 | 48 |
* This code and its internal interfaces are subject to change or |
49 |
* deletion without notice.</b> |
|
50 |
*/ |
|
51 |
public class List<A> extends AbstractCollection<A> implements java.util.List<A> { |
|
52 |
||
53 |
/** The first element of the list, supposed to be immutable. |
|
54 |
*/ |
|
55 |
public A head; |
|
56 |
||
57 |
/** The remainder of the list except for its first element, supposed |
|
58 |
* to be immutable. |
|
59 |
*/ |
|
60 |
//@Deprecated |
|
61 |
public List<A> tail; |
|
62 |
||
63 |
/** Construct a list given its head and tail. |
|
64 |
*/ |
|
65 |
List(A head, List<A> tail) { |
|
66 |
this.tail = tail; |
|
67 |
this.head = head; |
|
68 |
} |
|
69 |
||
70 |
/** Construct an empty list. |
|
71 |
*/ |
|
72 |
@SuppressWarnings("unchecked") |
|
73 |
public static <A> List<A> nil() { |
|
1789
7ac8c0815000
6765045: Remove rawtypes warnings from langtools
mcimadamore
parents:
10
diff
changeset
|
74 |
return (List<A>)EMPTY_LIST; |
10 | 75 |
} |
1789
7ac8c0815000
6765045: Remove rawtypes warnings from langtools
mcimadamore
parents:
10
diff
changeset
|
76 |
|
14801
d66cab4ef397
8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
14263
diff
changeset
|
77 |
private static final List<?> EMPTY_LIST = new List<Object>(null,null) { |
10 | 78 |
public List<Object> setTail(List<Object> tail) { |
79 |
throw new UnsupportedOperationException(); |
|
80 |
} |
|
81 |
public boolean isEmpty() { |
|
82 |
return true; |
|
83 |
} |
|
84 |
}; |
|
85 |
||
14057
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
8032
diff
changeset
|
86 |
/** Returns the list obtained from 'l' after removing all elements 'elem' |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
8032
diff
changeset
|
87 |
*/ |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
8032
diff
changeset
|
88 |
public static <A> List<A> filter(List<A> l, A elem) { |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
8032
diff
changeset
|
89 |
Assert.checkNonNull(elem); |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
8032
diff
changeset
|
90 |
List<A> res = List.nil(); |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
8032
diff
changeset
|
91 |
for (A a : l) { |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
8032
diff
changeset
|
92 |
if (a != null && !a.equals(elem)) { |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
8032
diff
changeset
|
93 |
res = res.prepend(a); |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
8032
diff
changeset
|
94 |
} |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
8032
diff
changeset
|
95 |
} |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
8032
diff
changeset
|
96 |
return res.reverse(); |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
8032
diff
changeset
|
97 |
} |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
8032
diff
changeset
|
98 |
|
15705 | 99 |
public List<A> intersect(List<A> that) { |
100 |
ListBuffer<A> buf = ListBuffer.lb(); |
|
101 |
for (A el : this) { |
|
102 |
if (that.contains(el)) { |
|
103 |
buf.append(el); |
|
104 |
} |
|
105 |
} |
|
106 |
return buf.toList(); |
|
107 |
} |
|
108 |
||
109 |
public List<A> diff(List<A> that) { |
|
110 |
ListBuffer<A> buf = ListBuffer.lb(); |
|
111 |
for (A el : this) { |
|
112 |
if (!that.contains(el)) { |
|
113 |
buf.append(el); |
|
114 |
} |
|
115 |
} |
|
116 |
return buf.toList(); |
|
117 |
} |
|
118 |
||
10 | 119 |
/** Construct a list consisting of given element. |
120 |
*/ |
|
121 |
public static <A> List<A> of(A x1) { |
|
122 |
return new List<A>(x1, List.<A>nil()); |
|
123 |
} |
|
124 |
||
125 |
/** Construct a list consisting of given elements. |
|
126 |
*/ |
|
127 |
public static <A> List<A> of(A x1, A x2) { |
|
128 |
return new List<A>(x1, of(x2)); |
|
129 |
} |
|
130 |
||
131 |
/** Construct a list consisting of given elements. |
|
132 |
*/ |
|
133 |
public static <A> List<A> of(A x1, A x2, A x3) { |
|
134 |
return new List<A>(x1, of(x2, x3)); |
|
135 |
} |
|
136 |
||
137 |
/** Construct a list consisting of given elements. |
|
138 |
*/ |
|
7643
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
5847
diff
changeset
|
139 |
@SuppressWarnings({"varargs", "unchecked"}) |
10 | 140 |
public static <A> List<A> of(A x1, A x2, A x3, A... rest) { |
141 |
return new List<A>(x1, new List<A>(x2, new List<A>(x3, from(rest)))); |
|
142 |
} |
|
143 |
||
144 |
/** |
|
145 |
* Construct a list consisting all elements of given array. |
|
146 |
* @param array an array; if {@code null} return an empty list |
|
147 |
*/ |
|
148 |
public static <A> List<A> from(A[] array) { |
|
149 |
List<A> xs = nil(); |
|
150 |
if (array != null) |
|
151 |
for (int i = array.length - 1; i >= 0; i--) |
|
152 |
xs = new List<A>(array[i], xs); |
|
153 |
return xs; |
|
154 |
} |
|
155 |
||
14057
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
8032
diff
changeset
|
156 |
public static <A> List<A> from(Iterable<? extends A> coll) { |
17582
4079713129dd
8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents:
17578
diff
changeset
|
157 |
ListBuffer<A> xs = ListBuffer.lb(); |
14057
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
8032
diff
changeset
|
158 |
for (A a : coll) { |
17582
4079713129dd
8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents:
17578
diff
changeset
|
159 |
xs.append(a); |
14057
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
8032
diff
changeset
|
160 |
} |
17582
4079713129dd
8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents:
17578
diff
changeset
|
161 |
return xs.toList(); |
14057
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
8032
diff
changeset
|
162 |
} |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
8032
diff
changeset
|
163 |
|
10 | 164 |
/** Construct a list consisting of a given number of identical elements. |
165 |
* @param len The number of elements in the list. |
|
166 |
* @param init The value of each element. |
|
167 |
*/ |
|
168 |
@Deprecated |
|
169 |
public static <A> List<A> fill(int len, A init) { |
|
170 |
List<A> l = nil(); |
|
171 |
for (int i = 0; i < len; i++) l = new List<A>(init, l); |
|
172 |
return l; |
|
173 |
} |
|
174 |
||
175 |
/** Does list have no elements? |
|
176 |
*/ |
|
177 |
@Override |
|
178 |
public boolean isEmpty() { |
|
179 |
return tail == null; |
|
180 |
} |
|
181 |
||
182 |
/** Does list have elements? |
|
183 |
*/ |
|
184 |
//@Deprecated |
|
185 |
public boolean nonEmpty() { |
|
186 |
return tail != null; |
|
187 |
} |
|
188 |
||
189 |
/** Return the number of elements in this list. |
|
190 |
*/ |
|
191 |
//@Deprecated |
|
192 |
public int length() { |
|
193 |
List<A> l = this; |
|
194 |
int len = 0; |
|
195 |
while (l.tail != null) { |
|
196 |
l = l.tail; |
|
197 |
len++; |
|
198 |
} |
|
199 |
return len; |
|
200 |
} |
|
201 |
@Override |
|
202 |
public int size() { |
|
203 |
return length(); |
|
204 |
} |
|
205 |
||
206 |
public List<A> setTail(List<A> tail) { |
|
207 |
this.tail = tail; |
|
208 |
return tail; |
|
209 |
} |
|
210 |
||
211 |
/** Prepend given element to front of list, forming and returning |
|
212 |
* a new list. |
|
213 |
*/ |
|
214 |
public List<A> prepend(A x) { |
|
215 |
return new List<A>(x, this); |
|
216 |
} |
|
217 |
||
218 |
/** Prepend given list of elements to front of list, forming and returning |
|
219 |
* a new list. |
|
220 |
*/ |
|
221 |
public List<A> prependList(List<A> xs) { |
|
222 |
if (this.isEmpty()) return xs; |
|
223 |
if (xs.isEmpty()) return this; |
|
224 |
if (xs.tail.isEmpty()) return prepend(xs.head); |
|
225 |
// return this.prependList(xs.tail).prepend(xs.head); |
|
226 |
List<A> result = this; |
|
227 |
List<A> rev = xs.reverse(); |
|
8032 | 228 |
Assert.check(rev != xs); |
10 | 229 |
// since xs.reverse() returned a new list, we can reuse the |
230 |
// individual List objects, instead of allocating new ones. |
|
231 |
while (rev.nonEmpty()) { |
|
232 |
List<A> h = rev; |
|
233 |
rev = rev.tail; |
|
234 |
h.setTail(result); |
|
235 |
result = h; |
|
236 |
} |
|
237 |
return result; |
|
238 |
} |
|
239 |
||
240 |
/** Reverse list. |
|
241 |
* If the list is empty or a singleton, then the same list is returned. |
|
242 |
* Otherwise a new list is formed. |
|
243 |
*/ |
|
244 |
public List<A> reverse() { |
|
245 |
// if it is empty or a singleton, return itself |
|
246 |
if (isEmpty() || tail.isEmpty()) |
|
247 |
return this; |
|
248 |
||
249 |
List<A> rev = nil(); |
|
250 |
for (List<A> l = this; l.nonEmpty(); l = l.tail) |
|
251 |
rev = new List<A>(l.head, rev); |
|
252 |
return rev; |
|
253 |
} |
|
254 |
||
255 |
/** Append given element at length, forming and returning |
|
256 |
* a new list. |
|
257 |
*/ |
|
258 |
public List<A> append(A x) { |
|
259 |
return of(x).prependList(this); |
|
260 |
} |
|
261 |
||
262 |
/** Append given list at length, forming and returning |
|
263 |
* a new list. |
|
264 |
*/ |
|
265 |
public List<A> appendList(List<A> x) { |
|
266 |
return x.prependList(this); |
|
267 |
} |
|
268 |
||
269 |
/** |
|
270 |
* Append given list buffer at length, forming and returning a new |
|
271 |
* list. |
|
272 |
*/ |
|
273 |
public List<A> appendList(ListBuffer<A> x) { |
|
274 |
return appendList(x.toList()); |
|
275 |
} |
|
276 |
||
277 |
/** Copy successive elements of this list into given vector until |
|
278 |
* list is exhausted or end of vector is reached. |
|
279 |
*/ |
|
280 |
@Override @SuppressWarnings("unchecked") |
|
281 |
public <T> T[] toArray(T[] vec) { |
|
282 |
int i = 0; |
|
283 |
List<A> l = this; |
|
284 |
Object[] dest = vec; |
|
285 |
while (l.nonEmpty() && i < vec.length) { |
|
286 |
dest[i] = l.head; |
|
287 |
l = l.tail; |
|
288 |
i++; |
|
289 |
} |
|
290 |
if (l.isEmpty()) { |
|
291 |
if (i < vec.length) |
|
292 |
vec[i] = null; |
|
293 |
return vec; |
|
294 |
} |
|
295 |
||
296 |
vec = (T[])Array.newInstance(vec.getClass().getComponentType(), size()); |
|
297 |
return toArray(vec); |
|
298 |
} |
|
299 |
||
300 |
public Object[] toArray() { |
|
301 |
return toArray(new Object[size()]); |
|
302 |
} |
|
303 |
||
304 |
/** Form a string listing all elements with given separator character. |
|
305 |
*/ |
|
306 |
public String toString(String sep) { |
|
307 |
if (isEmpty()) { |
|
308 |
return ""; |
|
309 |
} else { |
|
14263 | 310 |
StringBuilder buf = new StringBuilder(); |
10 | 311 |
buf.append(head); |
312 |
for (List<A> l = tail; l.nonEmpty(); l = l.tail) { |
|
313 |
buf.append(sep); |
|
314 |
buf.append(l.head); |
|
315 |
} |
|
316 |
return buf.toString(); |
|
317 |
} |
|
318 |
} |
|
319 |
||
320 |
/** Form a string listing all elements with comma as the separator character. |
|
321 |
*/ |
|
322 |
@Override |
|
323 |
public String toString() { |
|
324 |
return toString(","); |
|
325 |
} |
|
326 |
||
327 |
/** Compute a hash code, overrides Object |
|
328 |
* @see java.util.List#hashCode |
|
329 |
*/ |
|
330 |
@Override |
|
331 |
public int hashCode() { |
|
332 |
List<A> l = this; |
|
333 |
int h = 1; |
|
334 |
while (l.tail != null) { |
|
335 |
h = h * 31 + (l.head == null ? 0 : l.head.hashCode()); |
|
336 |
l = l.tail; |
|
337 |
} |
|
338 |
return h; |
|
339 |
} |
|
340 |
||
341 |
/** Is this list the same as other list? |
|
342 |
* @see java.util.List#equals |
|
343 |
*/ |
|
344 |
@Override |
|
345 |
public boolean equals(Object other) { |
|
346 |
if (other instanceof List<?>) |
|
347 |
return equals(this, (List<?>)other); |
|
348 |
if (other instanceof java.util.List<?>) { |
|
349 |
List<A> t = this; |
|
350 |
Iterator<?> oIter = ((java.util.List<?>) other).iterator(); |
|
351 |
while (t.tail != null && oIter.hasNext()) { |
|
352 |
Object o = oIter.next(); |
|
353 |
if ( !(t.head == null ? o == null : t.head.equals(o))) |
|
354 |
return false; |
|
355 |
t = t.tail; |
|
356 |
} |
|
357 |
return (t.isEmpty() && !oIter.hasNext()); |
|
358 |
} |
|
359 |
return false; |
|
360 |
} |
|
361 |
||
362 |
/** Are the two lists the same? |
|
363 |
*/ |
|
1789
7ac8c0815000
6765045: Remove rawtypes warnings from langtools
mcimadamore
parents:
10
diff
changeset
|
364 |
public static boolean equals(List<?> xs, List<?> ys) { |
10 | 365 |
while (xs.tail != null && ys.tail != null) { |
366 |
if (xs.head == null) { |
|
367 |
if (ys.head != null) return false; |
|
368 |
} else { |
|
369 |
if (!xs.head.equals(ys.head)) return false; |
|
370 |
} |
|
371 |
xs = xs.tail; |
|
372 |
ys = ys.tail; |
|
373 |
} |
|
374 |
return xs.tail == null && ys.tail == null; |
|
375 |
} |
|
376 |
||
377 |
/** Does the list contain the specified element? |
|
378 |
*/ |
|
379 |
@Override |
|
380 |
public boolean contains(Object x) { |
|
381 |
List<A> l = this; |
|
382 |
while (l.tail != null) { |
|
383 |
if (x == null) { |
|
384 |
if (l.head == null) return true; |
|
385 |
} else { |
|
386 |
if (l.head.equals(x)) return true; |
|
387 |
} |
|
388 |
l = l.tail; |
|
389 |
} |
|
390 |
return false; |
|
391 |
} |
|
392 |
||
393 |
/** The last element in the list, if any, or null. |
|
394 |
*/ |
|
395 |
public A last() { |
|
396 |
A last = null; |
|
397 |
List<A> t = this; |
|
398 |
while (t.tail != null) { |
|
399 |
last = t.head; |
|
400 |
t = t.tail; |
|
401 |
} |
|
402 |
return last; |
|
403 |
} |
|
404 |
||
405 |
@SuppressWarnings("unchecked") |
|
406 |
public static <T> List<T> convert(Class<T> klass, List<?> list) { |
|
407 |
if (list == null) |
|
408 |
return null; |
|
409 |
for (Object o : list) |
|
410 |
klass.cast(o); |
|
411 |
return (List<T>)list; |
|
412 |
} |
|
413 |
||
14801
d66cab4ef397
8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
14263
diff
changeset
|
414 |
private static final Iterator<?> EMPTYITERATOR = new Iterator<Object>() { |
10 | 415 |
public boolean hasNext() { |
416 |
return false; |
|
417 |
} |
|
418 |
public Object next() { |
|
419 |
throw new java.util.NoSuchElementException(); |
|
420 |
} |
|
421 |
public void remove() { |
|
422 |
throw new UnsupportedOperationException(); |
|
423 |
} |
|
424 |
}; |
|
425 |
||
426 |
@SuppressWarnings("unchecked") |
|
427 |
private static <A> Iterator<A> emptyIterator() { |
|
1789
7ac8c0815000
6765045: Remove rawtypes warnings from langtools
mcimadamore
parents:
10
diff
changeset
|
428 |
return (Iterator<A>)EMPTYITERATOR; |
10 | 429 |
} |
430 |
||
431 |
@Override |
|
432 |
public Iterator<A> iterator() { |
|
433 |
if (tail == null) |
|
434 |
return emptyIterator(); |
|
435 |
return new Iterator<A>() { |
|
436 |
List<A> elems = List.this; |
|
437 |
public boolean hasNext() { |
|
438 |
return elems.tail != null; |
|
439 |
} |
|
440 |
public A next() { |
|
441 |
if (elems.tail == null) |
|
442 |
throw new NoSuchElementException(); |
|
443 |
A result = elems.head; |
|
444 |
elems = elems.tail; |
|
445 |
return result; |
|
446 |
} |
|
447 |
public void remove() { |
|
448 |
throw new UnsupportedOperationException(); |
|
449 |
} |
|
450 |
}; |
|
451 |
} |
|
452 |
||
453 |
public A get(int index) { |
|
454 |
if (index < 0) |
|
455 |
throw new IndexOutOfBoundsException(String.valueOf(index)); |
|
456 |
||
457 |
List<A> l = this; |
|
458 |
for (int i = index; i-- > 0 && !l.isEmpty(); l = l.tail) |
|
459 |
; |
|
460 |
||
461 |
if (l.isEmpty()) |
|
462 |
throw new IndexOutOfBoundsException("Index: " + index + ", " + |
|
463 |
"Size: " + size()); |
|
464 |
return l.head; |
|
465 |
} |
|
466 |
||
467 |
public boolean addAll(int index, Collection<? extends A> c) { |
|
468 |
if (c.isEmpty()) |
|
469 |
return false; |
|
470 |
throw new UnsupportedOperationException(); |
|
471 |
} |
|
472 |
||
473 |
public A set(int index, A element) { |
|
474 |
throw new UnsupportedOperationException(); |
|
475 |
} |
|
476 |
||
477 |
public void add(int index, A element) { |
|
478 |
throw new UnsupportedOperationException(); |
|
479 |
} |
|
480 |
||
481 |
public A remove(int index) { |
|
482 |
throw new UnsupportedOperationException(); |
|
483 |
} |
|
484 |
||
485 |
public int indexOf(Object o) { |
|
486 |
int i = 0; |
|
487 |
for (List<A> l = this; l.tail != null; l = l.tail, i++) { |
|
488 |
if (l.head == null ? o == null : l.head.equals(o)) |
|
489 |
return i; |
|
490 |
} |
|
491 |
return -1; |
|
492 |
} |
|
493 |
||
494 |
public int lastIndexOf(Object o) { |
|
495 |
int last = -1; |
|
496 |
int i = 0; |
|
497 |
for (List<A> l = this; l.tail != null; l = l.tail, i++) { |
|
498 |
if (l.head == null ? o == null : l.head.equals(o)) |
|
499 |
last = i; |
|
500 |
} |
|
501 |
return last; |
|
502 |
} |
|
503 |
||
504 |
public ListIterator<A> listIterator() { |
|
505 |
return Collections.unmodifiableList(new ArrayList<A>(this)).listIterator(); |
|
506 |
} |
|
507 |
||
508 |
public ListIterator<A> listIterator(int index) { |
|
509 |
return Collections.unmodifiableList(new ArrayList<A>(this)).listIterator(index); |
|
510 |
} |
|
511 |
||
512 |
public java.util.List<A> subList(int fromIndex, int toIndex) { |
|
513 |
if (fromIndex < 0 || toIndex > size() || fromIndex > toIndex) |
|
514 |
throw new IllegalArgumentException(); |
|
515 |
||
516 |
ArrayList<A> a = new ArrayList<A>(toIndex - fromIndex); |
|
517 |
int i = 0; |
|
518 |
for (List<A> l = this; l.tail != null; l = l.tail, i++) { |
|
519 |
if (i == toIndex) |
|
520 |
break; |
|
521 |
if (i >= fromIndex) |
|
522 |
a.add(l.head); |
|
523 |
} |
|
524 |
||
525 |
return Collections.unmodifiableList(a); |
|
526 |
} |
|
527 |
} |