aoqi@0: /* aoqi@0: * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.javac.util; aoqi@0: aoqi@0: import java.util.AbstractQueue; aoqi@0: import java.util.Collection; aoqi@0: import java.util.Iterator; aoqi@0: import java.util.NoSuchElementException; aoqi@0: aoqi@0: /** A class for constructing lists by appending elements. Modelled after aoqi@0: * java.lang.StringBuffer. aoqi@0: * aoqi@0: *

This is NOT part of any supported API. aoqi@0: * If you write code that depends on this, you do so at your own risk. aoqi@0: * This code and its internal interfaces are subject to change or aoqi@0: * deletion without notice. aoqi@0: */ aoqi@0: public class ListBuffer extends AbstractQueue { aoqi@0: aoqi@0: public static ListBuffer of(T x) { aoqi@0: ListBuffer lb = new ListBuffer(); aoqi@0: lb.add(x); aoqi@0: return lb; aoqi@0: } aoqi@0: aoqi@0: /** The list of elements of this buffer. aoqi@0: */ aoqi@0: private List elems; aoqi@0: aoqi@0: /** A pointer pointing to the last element of 'elems' containing data, aoqi@0: * or null if the list is empty. aoqi@0: */ aoqi@0: private List last; aoqi@0: aoqi@0: /** The number of element in this buffer. aoqi@0: */ aoqi@0: private int count; aoqi@0: aoqi@0: /** Has a list been created from this buffer yet? aoqi@0: */ aoqi@0: private boolean shared; aoqi@0: aoqi@0: /** Create a new initially empty list buffer. aoqi@0: */ aoqi@0: public ListBuffer() { aoqi@0: clear(); aoqi@0: } aoqi@0: aoqi@0: public final void clear() { aoqi@0: this.elems = List.nil(); aoqi@0: this.last = null; aoqi@0: count = 0; aoqi@0: shared = false; aoqi@0: } aoqi@0: aoqi@0: /** Return the number of elements in this buffer. aoqi@0: */ aoqi@0: public int length() { aoqi@0: return count; aoqi@0: } aoqi@0: public int size() { aoqi@0: return count; aoqi@0: } aoqi@0: aoqi@0: /** Is buffer empty? aoqi@0: */ aoqi@0: public boolean isEmpty() { aoqi@0: return count == 0; aoqi@0: } aoqi@0: aoqi@0: /** Is buffer not empty? aoqi@0: */ aoqi@0: public boolean nonEmpty() { aoqi@0: return count != 0; aoqi@0: } aoqi@0: aoqi@0: /** Copy list and sets last. aoqi@0: */ aoqi@0: private void copy() { aoqi@0: if (elems.nonEmpty()) { aoqi@0: List orig = elems; aoqi@0: aoqi@0: elems = last = List.of(orig.head); aoqi@0: aoqi@0: while ((orig = orig.tail).nonEmpty()) { aoqi@0: last.tail = List.of(orig.head); aoqi@0: last = last.tail; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** Prepend an element to buffer. aoqi@0: */ aoqi@0: public ListBuffer prepend(A x) { aoqi@0: elems = elems.prepend(x); aoqi@0: if (last == null) last = elems; aoqi@0: count++; aoqi@0: return this; aoqi@0: } aoqi@0: aoqi@0: /** Append an element to buffer. aoqi@0: */ aoqi@0: public ListBuffer append(A x) { aoqi@0: x.getClass(); // null check aoqi@0: if (shared) copy(); aoqi@0: List newLast = List.of(x); aoqi@0: if (last != null) { aoqi@0: last.tail = newLast; aoqi@0: last = newLast; aoqi@0: } else { aoqi@0: elems = last = newLast; aoqi@0: } aoqi@0: count++; aoqi@0: return this; aoqi@0: } aoqi@0: aoqi@0: /** Append all elements in a list to buffer. aoqi@0: */ aoqi@0: public ListBuffer appendList(List xs) { aoqi@0: while (xs.nonEmpty()) { aoqi@0: append(xs.head); aoqi@0: xs = xs.tail; aoqi@0: } aoqi@0: return this; aoqi@0: } aoqi@0: aoqi@0: /** Append all elements in a list to buffer. aoqi@0: */ aoqi@0: public ListBuffer appendList(ListBuffer xs) { aoqi@0: return appendList(xs.toList()); aoqi@0: } aoqi@0: aoqi@0: /** Append all elements in an array to buffer. aoqi@0: */ aoqi@0: public ListBuffer appendArray(A[] xs) { aoqi@0: for (int i = 0; i < xs.length; i++) { aoqi@0: append(xs[i]); aoqi@0: } aoqi@0: return this; aoqi@0: } aoqi@0: aoqi@0: /** Convert buffer to a list of all its elements. aoqi@0: */ aoqi@0: public List toList() { aoqi@0: shared = true; aoqi@0: return elems; aoqi@0: } aoqi@0: aoqi@0: /** Does the list contain the specified element? aoqi@0: */ aoqi@0: public boolean contains(Object x) { aoqi@0: return elems.contains(x); aoqi@0: } aoqi@0: aoqi@0: /** Convert buffer to an array aoqi@0: */ aoqi@0: public T[] toArray(T[] vec) { aoqi@0: return elems.toArray(vec); aoqi@0: } aoqi@0: public Object[] toArray() { aoqi@0: return toArray(new Object[size()]); aoqi@0: } aoqi@0: aoqi@0: /** The first element in this buffer. aoqi@0: */ aoqi@0: public A first() { aoqi@0: return elems.head; aoqi@0: } aoqi@0: aoqi@0: /** Return first element in this buffer and remove aoqi@0: */ aoqi@0: public A next() { aoqi@0: A x = elems.head; aoqi@0: if (!elems.isEmpty()) { aoqi@0: elems = elems.tail; aoqi@0: if (elems.isEmpty()) last = null; aoqi@0: count--; aoqi@0: } aoqi@0: return x; aoqi@0: } aoqi@0: aoqi@0: /** An enumeration of all elements in this buffer. aoqi@0: */ aoqi@0: public Iterator iterator() { aoqi@0: return new Iterator() { aoqi@0: List elems = ListBuffer.this.elems; aoqi@0: public boolean hasNext() { aoqi@0: return !elems.isEmpty(); aoqi@0: } aoqi@0: public A next() { aoqi@0: if (elems.isEmpty()) aoqi@0: throw new NoSuchElementException(); aoqi@0: A elem = elems.head; aoqi@0: elems = elems.tail; aoqi@0: return elem; aoqi@0: } aoqi@0: public void remove() { aoqi@0: throw new UnsupportedOperationException(); aoqi@0: } aoqi@0: }; aoqi@0: } aoqi@0: aoqi@0: public boolean add(A a) { aoqi@0: append(a); aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: public boolean remove(Object o) { aoqi@0: throw new UnsupportedOperationException(); aoqi@0: } aoqi@0: aoqi@0: public boolean containsAll(Collection c) { aoqi@0: for (Object x: c) { aoqi@0: if (!contains(x)) aoqi@0: return false; aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: public boolean addAll(Collection c) { aoqi@0: for (A a: c) aoqi@0: append(a); aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: public boolean removeAll(Collection c) { aoqi@0: throw new UnsupportedOperationException(); aoqi@0: } aoqi@0: aoqi@0: public boolean retainAll(Collection c) { aoqi@0: throw new UnsupportedOperationException(); aoqi@0: } aoqi@0: aoqi@0: public boolean offer(A a) { aoqi@0: append(a); aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: public A poll() { aoqi@0: return next(); aoqi@0: } aoqi@0: aoqi@0: public A peek() { aoqi@0: return first(); aoqi@0: } aoqi@0: aoqi@0: public A last() { aoqi@0: return last != null ? last.head : null; aoqi@0: } aoqi@0: }