src/share/classes/com/sun/tools/javac/util/List.java

Mon, 23 Sep 2013 10:42:38 +0200

author
alundblad
date
Mon, 23 Sep 2013 10:42:38 +0200
changeset 2047
5f915a0c9615
parent 2000
4a6acc42c3a1
child 2525
2eb010b6cb22
permissions
-rw-r--r--

6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
Summary: Static factory method ListBuffer.lb removed. Replaced by constructor calls.
Reviewed-by: jfranck, jjg

duke@1 1 /*
jjg@1755 2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.util;
duke@1 27
duke@1 28 import java.lang.reflect.Array;
duke@1 29 import java.util.ArrayList;
duke@1 30 import java.util.Collection;
duke@1 31 import java.util.Collections;
duke@1 32 import java.util.Iterator;
duke@1 33 import java.util.AbstractCollection;
duke@1 34 import java.util.ListIterator;
duke@1 35 import java.util.NoSuchElementException;
duke@1 36
duke@1 37 /** A class for generic linked lists. Links are supposed to be
duke@1 38 * immutable, the only exception being the incremental construction of
duke@1 39 * lists via ListBuffers. List is the main container class in
jjg@1755 40 * GJC. Most data structures and algorithms in GJC use lists rather
duke@1 41 * than arrays.
duke@1 42 *
duke@1 43 * <p>Lists are always trailed by a sentinel element, whose head and tail
duke@1 44 * are both null.
duke@1 45 *
jjg@581 46 * <p><b>This is NOT part of any supported API.
jjg@581 47 * If you write code that depends on this, you do so at your own risk.
duke@1 48 * This code and its internal interfaces are subject to change or
duke@1 49 * deletion without notice.</b>
duke@1 50 */
duke@1 51 public class List<A> extends AbstractCollection<A> implements java.util.List<A> {
duke@1 52
duke@1 53 /** The first element of the list, supposed to be immutable.
duke@1 54 */
duke@1 55 public A head;
duke@1 56
duke@1 57 /** The remainder of the list except for its first element, supposed
duke@1 58 * to be immutable.
duke@1 59 */
duke@1 60 //@Deprecated
duke@1 61 public List<A> tail;
duke@1 62
duke@1 63 /** Construct a list given its head and tail.
duke@1 64 */
duke@1 65 List(A head, List<A> tail) {
duke@1 66 this.tail = tail;
duke@1 67 this.head = head;
duke@1 68 }
duke@1 69
duke@1 70 /** Construct an empty list.
duke@1 71 */
duke@1 72 @SuppressWarnings("unchecked")
duke@1 73 public static <A> List<A> nil() {
mcimadamore@184 74 return (List<A>)EMPTY_LIST;
duke@1 75 }
mcimadamore@184 76
vromero@1442 77 private static final List<?> EMPTY_LIST = new List<Object>(null,null) {
duke@1 78 public List<Object> setTail(List<Object> tail) {
duke@1 79 throw new UnsupportedOperationException();
duke@1 80 }
duke@1 81 public boolean isEmpty() {
duke@1 82 return true;
duke@1 83 }
duke@1 84 };
duke@1 85
mcimadamore@1347 86 /** Returns the list obtained from 'l' after removing all elements 'elem'
mcimadamore@1347 87 */
mcimadamore@1347 88 public static <A> List<A> filter(List<A> l, A elem) {
mcimadamore@1347 89 Assert.checkNonNull(elem);
mcimadamore@1347 90 List<A> res = List.nil();
mcimadamore@1347 91 for (A a : l) {
mcimadamore@1347 92 if (a != null && !a.equals(elem)) {
mcimadamore@1347 93 res = res.prepend(a);
mcimadamore@1347 94 }
mcimadamore@1347 95 }
mcimadamore@1347 96 return res.reverse();
mcimadamore@1347 97 }
mcimadamore@1347 98
mcimadamore@1550 99 public List<A> intersect(List<A> that) {
alundblad@2047 100 ListBuffer<A> buf = new ListBuffer<>();
mcimadamore@1550 101 for (A el : this) {
mcimadamore@1550 102 if (that.contains(el)) {
mcimadamore@1550 103 buf.append(el);
mcimadamore@1550 104 }
mcimadamore@1550 105 }
mcimadamore@1550 106 return buf.toList();
mcimadamore@1550 107 }
mcimadamore@1550 108
mcimadamore@1550 109 public List<A> diff(List<A> that) {
alundblad@2047 110 ListBuffer<A> buf = new ListBuffer<>();
mcimadamore@1550 111 for (A el : this) {
mcimadamore@1550 112 if (!that.contains(el)) {
mcimadamore@1550 113 buf.append(el);
mcimadamore@1550 114 }
mcimadamore@1550 115 }
mcimadamore@1550 116 return buf.toList();
mcimadamore@1550 117 }
mcimadamore@1550 118
vromero@2000 119 /**
vromero@2000 120 * Create a new list from the first {@code n} elements of this list
vromero@2000 121 */
vromero@2000 122 public List<A> take(int n) {
alundblad@2047 123 ListBuffer<A> buf = new ListBuffer<>();
vromero@2000 124 int count = 0;
vromero@2000 125 for (A el : this) {
vromero@2000 126 if (count++ == n) break;
vromero@2000 127 buf.append(el);
vromero@2000 128 }
vromero@2000 129 return buf.toList();
vromero@2000 130 }
vromero@2000 131
duke@1 132 /** Construct a list consisting of given element.
duke@1 133 */
duke@1 134 public static <A> List<A> of(A x1) {
duke@1 135 return new List<A>(x1, List.<A>nil());
duke@1 136 }
duke@1 137
duke@1 138 /** Construct a list consisting of given elements.
duke@1 139 */
duke@1 140 public static <A> List<A> of(A x1, A x2) {
duke@1 141 return new List<A>(x1, of(x2));
duke@1 142 }
duke@1 143
duke@1 144 /** Construct a list consisting of given elements.
duke@1 145 */
duke@1 146 public static <A> List<A> of(A x1, A x2, A x3) {
duke@1 147 return new List<A>(x1, of(x2, x3));
duke@1 148 }
duke@1 149
duke@1 150 /** Construct a list consisting of given elements.
duke@1 151 */
mcimadamore@795 152 @SuppressWarnings({"varargs", "unchecked"})
duke@1 153 public static <A> List<A> of(A x1, A x2, A x3, A... rest) {
duke@1 154 return new List<A>(x1, new List<A>(x2, new List<A>(x3, from(rest))));
duke@1 155 }
duke@1 156
duke@1 157 /**
duke@1 158 * Construct a list consisting all elements of given array.
duke@1 159 * @param array an array; if {@code null} return an empty list
duke@1 160 */
duke@1 161 public static <A> List<A> from(A[] array) {
duke@1 162 List<A> xs = nil();
duke@1 163 if (array != null)
duke@1 164 for (int i = array.length - 1; i >= 0; i--)
duke@1 165 xs = new List<A>(array[i], xs);
duke@1 166 return xs;
duke@1 167 }
duke@1 168
mcimadamore@1347 169 public static <A> List<A> from(Iterable<? extends A> coll) {
alundblad@2047 170 ListBuffer<A> xs = new ListBuffer<>();
mcimadamore@1347 171 for (A a : coll) {
mcimadamore@1759 172 xs.append(a);
mcimadamore@1347 173 }
mcimadamore@1759 174 return xs.toList();
mcimadamore@1347 175 }
mcimadamore@1347 176
duke@1 177 /** Construct a list consisting of a given number of identical elements.
duke@1 178 * @param len The number of elements in the list.
duke@1 179 * @param init The value of each element.
duke@1 180 */
duke@1 181 @Deprecated
duke@1 182 public static <A> List<A> fill(int len, A init) {
duke@1 183 List<A> l = nil();
duke@1 184 for (int i = 0; i < len; i++) l = new List<A>(init, l);
duke@1 185 return l;
duke@1 186 }
duke@1 187
duke@1 188 /** Does list have no elements?
duke@1 189 */
duke@1 190 @Override
duke@1 191 public boolean isEmpty() {
duke@1 192 return tail == null;
duke@1 193 }
duke@1 194
duke@1 195 /** Does list have elements?
duke@1 196 */
duke@1 197 //@Deprecated
duke@1 198 public boolean nonEmpty() {
duke@1 199 return tail != null;
duke@1 200 }
duke@1 201
duke@1 202 /** Return the number of elements in this list.
duke@1 203 */
duke@1 204 //@Deprecated
duke@1 205 public int length() {
duke@1 206 List<A> l = this;
duke@1 207 int len = 0;
duke@1 208 while (l.tail != null) {
duke@1 209 l = l.tail;
duke@1 210 len++;
duke@1 211 }
duke@1 212 return len;
duke@1 213 }
duke@1 214 @Override
duke@1 215 public int size() {
duke@1 216 return length();
duke@1 217 }
duke@1 218
duke@1 219 public List<A> setTail(List<A> tail) {
duke@1 220 this.tail = tail;
duke@1 221 return tail;
duke@1 222 }
duke@1 223
duke@1 224 /** Prepend given element to front of list, forming and returning
duke@1 225 * a new list.
duke@1 226 */
duke@1 227 public List<A> prepend(A x) {
duke@1 228 return new List<A>(x, this);
duke@1 229 }
duke@1 230
duke@1 231 /** Prepend given list of elements to front of list, forming and returning
duke@1 232 * a new list.
duke@1 233 */
duke@1 234 public List<A> prependList(List<A> xs) {
duke@1 235 if (this.isEmpty()) return xs;
duke@1 236 if (xs.isEmpty()) return this;
duke@1 237 if (xs.tail.isEmpty()) return prepend(xs.head);
duke@1 238 // return this.prependList(xs.tail).prepend(xs.head);
duke@1 239 List<A> result = this;
duke@1 240 List<A> rev = xs.reverse();
jjg@816 241 Assert.check(rev != xs);
duke@1 242 // since xs.reverse() returned a new list, we can reuse the
duke@1 243 // individual List objects, instead of allocating new ones.
duke@1 244 while (rev.nonEmpty()) {
duke@1 245 List<A> h = rev;
duke@1 246 rev = rev.tail;
duke@1 247 h.setTail(result);
duke@1 248 result = h;
duke@1 249 }
duke@1 250 return result;
duke@1 251 }
duke@1 252
duke@1 253 /** Reverse list.
duke@1 254 * If the list is empty or a singleton, then the same list is returned.
duke@1 255 * Otherwise a new list is formed.
duke@1 256 */
duke@1 257 public List<A> reverse() {
duke@1 258 // if it is empty or a singleton, return itself
duke@1 259 if (isEmpty() || tail.isEmpty())
duke@1 260 return this;
duke@1 261
duke@1 262 List<A> rev = nil();
duke@1 263 for (List<A> l = this; l.nonEmpty(); l = l.tail)
duke@1 264 rev = new List<A>(l.head, rev);
duke@1 265 return rev;
duke@1 266 }
duke@1 267
duke@1 268 /** Append given element at length, forming and returning
duke@1 269 * a new list.
duke@1 270 */
duke@1 271 public List<A> append(A x) {
duke@1 272 return of(x).prependList(this);
duke@1 273 }
duke@1 274
duke@1 275 /** Append given list at length, forming and returning
duke@1 276 * a new list.
duke@1 277 */
duke@1 278 public List<A> appendList(List<A> x) {
duke@1 279 return x.prependList(this);
duke@1 280 }
duke@1 281
duke@1 282 /**
duke@1 283 * Append given list buffer at length, forming and returning a new
duke@1 284 * list.
duke@1 285 */
duke@1 286 public List<A> appendList(ListBuffer<A> x) {
duke@1 287 return appendList(x.toList());
duke@1 288 }
duke@1 289
duke@1 290 /** Copy successive elements of this list into given vector until
duke@1 291 * list is exhausted or end of vector is reached.
duke@1 292 */
duke@1 293 @Override @SuppressWarnings("unchecked")
duke@1 294 public <T> T[] toArray(T[] vec) {
duke@1 295 int i = 0;
duke@1 296 List<A> l = this;
duke@1 297 Object[] dest = vec;
duke@1 298 while (l.nonEmpty() && i < vec.length) {
duke@1 299 dest[i] = l.head;
duke@1 300 l = l.tail;
duke@1 301 i++;
duke@1 302 }
duke@1 303 if (l.isEmpty()) {
duke@1 304 if (i < vec.length)
duke@1 305 vec[i] = null;
duke@1 306 return vec;
duke@1 307 }
duke@1 308
duke@1 309 vec = (T[])Array.newInstance(vec.getClass().getComponentType(), size());
duke@1 310 return toArray(vec);
duke@1 311 }
duke@1 312
duke@1 313 public Object[] toArray() {
duke@1 314 return toArray(new Object[size()]);
duke@1 315 }
duke@1 316
duke@1 317 /** Form a string listing all elements with given separator character.
duke@1 318 */
duke@1 319 public String toString(String sep) {
duke@1 320 if (isEmpty()) {
duke@1 321 return "";
duke@1 322 } else {
jjg@1362 323 StringBuilder buf = new StringBuilder();
duke@1 324 buf.append(head);
duke@1 325 for (List<A> l = tail; l.nonEmpty(); l = l.tail) {
duke@1 326 buf.append(sep);
duke@1 327 buf.append(l.head);
duke@1 328 }
duke@1 329 return buf.toString();
duke@1 330 }
duke@1 331 }
duke@1 332
duke@1 333 /** Form a string listing all elements with comma as the separator character.
duke@1 334 */
duke@1 335 @Override
duke@1 336 public String toString() {
duke@1 337 return toString(",");
duke@1 338 }
duke@1 339
duke@1 340 /** Compute a hash code, overrides Object
duke@1 341 * @see java.util.List#hashCode
duke@1 342 */
duke@1 343 @Override
duke@1 344 public int hashCode() {
duke@1 345 List<A> l = this;
duke@1 346 int h = 1;
duke@1 347 while (l.tail != null) {
duke@1 348 h = h * 31 + (l.head == null ? 0 : l.head.hashCode());
duke@1 349 l = l.tail;
duke@1 350 }
duke@1 351 return h;
duke@1 352 }
duke@1 353
duke@1 354 /** Is this list the same as other list?
duke@1 355 * @see java.util.List#equals
duke@1 356 */
duke@1 357 @Override
duke@1 358 public boolean equals(Object other) {
duke@1 359 if (other instanceof List<?>)
duke@1 360 return equals(this, (List<?>)other);
duke@1 361 if (other instanceof java.util.List<?>) {
duke@1 362 List<A> t = this;
duke@1 363 Iterator<?> oIter = ((java.util.List<?>) other).iterator();
duke@1 364 while (t.tail != null && oIter.hasNext()) {
duke@1 365 Object o = oIter.next();
duke@1 366 if ( !(t.head == null ? o == null : t.head.equals(o)))
duke@1 367 return false;
duke@1 368 t = t.tail;
duke@1 369 }
duke@1 370 return (t.isEmpty() && !oIter.hasNext());
duke@1 371 }
duke@1 372 return false;
duke@1 373 }
duke@1 374
duke@1 375 /** Are the two lists the same?
duke@1 376 */
mcimadamore@184 377 public static boolean equals(List<?> xs, List<?> ys) {
duke@1 378 while (xs.tail != null && ys.tail != null) {
duke@1 379 if (xs.head == null) {
duke@1 380 if (ys.head != null) return false;
duke@1 381 } else {
duke@1 382 if (!xs.head.equals(ys.head)) return false;
duke@1 383 }
duke@1 384 xs = xs.tail;
duke@1 385 ys = ys.tail;
duke@1 386 }
duke@1 387 return xs.tail == null && ys.tail == null;
duke@1 388 }
duke@1 389
duke@1 390 /** Does the list contain the specified element?
duke@1 391 */
duke@1 392 @Override
duke@1 393 public boolean contains(Object x) {
duke@1 394 List<A> l = this;
duke@1 395 while (l.tail != null) {
duke@1 396 if (x == null) {
duke@1 397 if (l.head == null) return true;
duke@1 398 } else {
duke@1 399 if (l.head.equals(x)) return true;
duke@1 400 }
duke@1 401 l = l.tail;
duke@1 402 }
duke@1 403 return false;
duke@1 404 }
duke@1 405
duke@1 406 /** The last element in the list, if any, or null.
duke@1 407 */
duke@1 408 public A last() {
duke@1 409 A last = null;
duke@1 410 List<A> t = this;
duke@1 411 while (t.tail != null) {
duke@1 412 last = t.head;
duke@1 413 t = t.tail;
duke@1 414 }
duke@1 415 return last;
duke@1 416 }
duke@1 417
duke@1 418 @SuppressWarnings("unchecked")
duke@1 419 public static <T> List<T> convert(Class<T> klass, List<?> list) {
duke@1 420 if (list == null)
duke@1 421 return null;
duke@1 422 for (Object o : list)
duke@1 423 klass.cast(o);
duke@1 424 return (List<T>)list;
duke@1 425 }
duke@1 426
vromero@1442 427 private static final Iterator<?> EMPTYITERATOR = new Iterator<Object>() {
duke@1 428 public boolean hasNext() {
duke@1 429 return false;
duke@1 430 }
duke@1 431 public Object next() {
duke@1 432 throw new java.util.NoSuchElementException();
duke@1 433 }
duke@1 434 public void remove() {
duke@1 435 throw new UnsupportedOperationException();
duke@1 436 }
duke@1 437 };
duke@1 438
duke@1 439 @SuppressWarnings("unchecked")
duke@1 440 private static <A> Iterator<A> emptyIterator() {
mcimadamore@184 441 return (Iterator<A>)EMPTYITERATOR;
duke@1 442 }
duke@1 443
duke@1 444 @Override
duke@1 445 public Iterator<A> iterator() {
duke@1 446 if (tail == null)
duke@1 447 return emptyIterator();
duke@1 448 return new Iterator<A>() {
duke@1 449 List<A> elems = List.this;
duke@1 450 public boolean hasNext() {
duke@1 451 return elems.tail != null;
duke@1 452 }
duke@1 453 public A next() {
duke@1 454 if (elems.tail == null)
duke@1 455 throw new NoSuchElementException();
duke@1 456 A result = elems.head;
duke@1 457 elems = elems.tail;
duke@1 458 return result;
duke@1 459 }
duke@1 460 public void remove() {
duke@1 461 throw new UnsupportedOperationException();
duke@1 462 }
duke@1 463 };
duke@1 464 }
duke@1 465
duke@1 466 public A get(int index) {
duke@1 467 if (index < 0)
duke@1 468 throw new IndexOutOfBoundsException(String.valueOf(index));
duke@1 469
duke@1 470 List<A> l = this;
duke@1 471 for (int i = index; i-- > 0 && !l.isEmpty(); l = l.tail)
duke@1 472 ;
duke@1 473
duke@1 474 if (l.isEmpty())
duke@1 475 throw new IndexOutOfBoundsException("Index: " + index + ", " +
duke@1 476 "Size: " + size());
duke@1 477 return l.head;
duke@1 478 }
duke@1 479
duke@1 480 public boolean addAll(int index, Collection<? extends A> c) {
duke@1 481 if (c.isEmpty())
duke@1 482 return false;
duke@1 483 throw new UnsupportedOperationException();
duke@1 484 }
duke@1 485
duke@1 486 public A set(int index, A element) {
duke@1 487 throw new UnsupportedOperationException();
duke@1 488 }
duke@1 489
duke@1 490 public void add(int index, A element) {
duke@1 491 throw new UnsupportedOperationException();
duke@1 492 }
duke@1 493
duke@1 494 public A remove(int index) {
duke@1 495 throw new UnsupportedOperationException();
duke@1 496 }
duke@1 497
duke@1 498 public int indexOf(Object o) {
duke@1 499 int i = 0;
duke@1 500 for (List<A> l = this; l.tail != null; l = l.tail, i++) {
duke@1 501 if (l.head == null ? o == null : l.head.equals(o))
duke@1 502 return i;
duke@1 503 }
duke@1 504 return -1;
duke@1 505 }
duke@1 506
duke@1 507 public int lastIndexOf(Object o) {
duke@1 508 int last = -1;
duke@1 509 int i = 0;
duke@1 510 for (List<A> l = this; l.tail != null; l = l.tail, i++) {
duke@1 511 if (l.head == null ? o == null : l.head.equals(o))
duke@1 512 last = i;
duke@1 513 }
duke@1 514 return last;
duke@1 515 }
duke@1 516
duke@1 517 public ListIterator<A> listIterator() {
duke@1 518 return Collections.unmodifiableList(new ArrayList<A>(this)).listIterator();
duke@1 519 }
duke@1 520
duke@1 521 public ListIterator<A> listIterator(int index) {
duke@1 522 return Collections.unmodifiableList(new ArrayList<A>(this)).listIterator(index);
duke@1 523 }
duke@1 524
duke@1 525 public java.util.List<A> subList(int fromIndex, int toIndex) {
duke@1 526 if (fromIndex < 0 || toIndex > size() || fromIndex > toIndex)
duke@1 527 throw new IllegalArgumentException();
duke@1 528
duke@1 529 ArrayList<A> a = new ArrayList<A>(toIndex - fromIndex);
duke@1 530 int i = 0;
duke@1 531 for (List<A> l = this; l.tail != null; l = l.tail, i++) {
duke@1 532 if (i == toIndex)
duke@1 533 break;
duke@1 534 if (i >= fromIndex)
duke@1 535 a.add(l.head);
duke@1 536 }
duke@1 537
duke@1 538 return Collections.unmodifiableList(a);
duke@1 539 }
duke@1 540 }

mercurial