diff -r cfde9737131e -r 170e486632d9 src/share/classes/com/sun/tools/javac/util/ListBuffer.java --- a/src/share/classes/com/sun/tools/javac/util/ListBuffer.java Tue Dec 11 15:05:55 2012 -0800 +++ b/src/share/classes/com/sun/tools/javac/util/ListBuffer.java Wed Dec 12 20:26:56 2012 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -52,19 +52,20 @@ /** The list of elements of this buffer. */ - public List elems; + private List elems; - /** A pointer pointing to the last, sentinel element of `elems'. + /** A pointer pointing to the last element of 'elems' containing data, + * or null if the list is empty. */ - public List last; + private List last; /** The number of element in this buffer. */ - public int count; + private int count; /** Has a list been created from this buffer yet? */ - public boolean shared; + private boolean shared; /** Create a new initially empty list buffer. */ @@ -73,8 +74,8 @@ } public final void clear() { - this.elems = new List(null,null); - this.last = this.elems; + this.elems = List.nil(); + this.last = null; count = 0; shared = false; } @@ -103,22 +104,23 @@ /** Copy list and sets last. */ private void copy() { - List p = elems = new List(elems.head, elems.tail); - while (true) { - List tail = p.tail; - if (tail == null) break; - tail = new List(tail.head, tail.tail); - p.setTail(tail); - p = tail; + if (elems.nonEmpty()) { + List orig = elems; + + elems = last = List.of(orig.head); + + while ((orig = orig.tail).nonEmpty()) { + last.tail = List.of(orig.head); + last = last.tail; + } } - last = p; - shared = false; } /** Prepend an element to buffer. */ public ListBuffer prepend(A x) { elems = elems.prepend(x); + if (last == null) last = elems; count++; return this; } @@ -128,9 +130,13 @@ public ListBuffer append(A x) { x.getClass(); // null check if (shared) copy(); - last.head = x; - last.setTail(new List(null,null)); - last = last.tail; + List newLast = List.of(x); + if (last != null) { + last.tail = newLast; + last = newLast; + } else { + elems = last = newLast; + } count++; return this; } @@ -192,8 +198,9 @@ */ public A next() { A x = elems.head; - if (elems != last) { + if (!elems.isEmpty()) { elems = elems.tail; + if (elems.isEmpty()) last = null; count--; } return x; @@ -205,10 +212,10 @@ return new Iterator() { List elems = ListBuffer.this.elems; public boolean hasNext() { - return elems != last; + return !elems.isEmpty(); } public A next() { - if (elems == last) + if (elems.isEmpty()) throw new NoSuchElementException(); A elem = elems.head; elems = elems.tail; @@ -263,4 +270,8 @@ public A peek() { return first(); } + + public A last() { + return last != null ? last.head : null; + } }