0N/A/*
553N/A * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
553N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
553N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.tools.javac.util;
0N/A
69N/Aimport java.util.AbstractQueue;
0N/Aimport java.util.Collection;
0N/Aimport java.util.Iterator;
0N/Aimport java.util.NoSuchElementException;
0N/A
0N/A/** A class for constructing lists by appending elements. Modelled after
0N/A * java.lang.StringBuffer.
0N/A *
580N/A * <p><b>This is NOT part of any supported API.
580N/A * If you write code that depends on this, you do so at your own risk.
0N/A * This code and its internal interfaces are subject to change or
0N/A * deletion without notice.</b>
0N/A */
69N/Apublic class ListBuffer<A> extends AbstractQueue<A> {
0N/A
0N/A public static <T> ListBuffer<T> lb() {
0N/A return new ListBuffer<T>();
0N/A }
0N/A
69N/A public static <T> ListBuffer<T> of(T x) {
69N/A ListBuffer<T> lb = new ListBuffer<T>();
69N/A lb.add(x);
69N/A return lb;
69N/A }
69N/A
0N/A /** The list of elements of this buffer.
0N/A */
0N/A public List<A> elems;
0N/A
0N/A /** A pointer pointing to the last, sentinel element of `elems'.
0N/A */
0N/A public List<A> last;
0N/A
0N/A /** The number of element in this buffer.
0N/A */
0N/A public int count;
0N/A
0N/A /** Has a list been created from this buffer yet?
0N/A */
0N/A public boolean shared;
0N/A
0N/A /** Create a new initially empty list buffer.
0N/A */
0N/A public ListBuffer() {
0N/A clear();
0N/A }
0N/A
0N/A public final void clear() {
0N/A this.elems = new List<A>(null,null);
0N/A this.last = this.elems;
0N/A count = 0;
0N/A shared = false;
0N/A }
0N/A
0N/A /** Return the number of elements in this buffer.
0N/A */
0N/A public int length() {
0N/A return count;
0N/A }
0N/A public int size() {
0N/A return count;
0N/A }
0N/A
0N/A /** Is buffer empty?
0N/A */
0N/A public boolean isEmpty() {
0N/A return count == 0;
0N/A }
0N/A
0N/A /** Is buffer not empty?
0N/A */
0N/A public boolean nonEmpty() {
0N/A return count != 0;
0N/A }
0N/A
0N/A /** Copy list and sets last.
0N/A */
0N/A private void copy() {
0N/A List<A> p = elems = new List<A>(elems.head, elems.tail);
0N/A while (true) {
0N/A List<A> tail = p.tail;
0N/A if (tail == null) break;
0N/A tail = new List<A>(tail.head, tail.tail);
0N/A p.setTail(tail);
0N/A p = tail;
0N/A }
0N/A last = p;
0N/A shared = false;
0N/A }
0N/A
0N/A /** Prepend an element to buffer.
0N/A */
0N/A public ListBuffer<A> prepend(A x) {
0N/A elems = elems.prepend(x);
0N/A count++;
0N/A return this;
0N/A }
0N/A
0N/A /** Append an element to buffer.
0N/A */
0N/A public ListBuffer<A> append(A x) {
69N/A x.getClass(); // null check
0N/A if (shared) copy();
0N/A last.head = x;
0N/A last.setTail(new List<A>(null,null));
0N/A last = last.tail;
0N/A count++;
0N/A return this;
0N/A }
0N/A
0N/A /** Append all elements in a list to buffer.
0N/A */
0N/A public ListBuffer<A> appendList(List<A> xs) {
0N/A while (xs.nonEmpty()) {
0N/A append(xs.head);
0N/A xs = xs.tail;
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A /** Append all elements in a list to buffer.
0N/A */
0N/A public ListBuffer<A> appendList(ListBuffer<A> xs) {
0N/A return appendList(xs.toList());
0N/A }
0N/A
0N/A /** Append all elements in an array to buffer.
0N/A */
0N/A public ListBuffer<A> appendArray(A[] xs) {
0N/A for (int i = 0; i < xs.length; i++) {
0N/A append(xs[i]);
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A /** Convert buffer to a list of all its elements.
0N/A */
0N/A public List<A> toList() {
0N/A shared = true;
0N/A return elems;
0N/A }
0N/A
0N/A /** Does the list contain the specified element?
0N/A */
0N/A public boolean contains(Object x) {
0N/A return elems.contains(x);
0N/A }
0N/A
0N/A /** Convert buffer to an array
0N/A */
0N/A public <T> T[] toArray(T[] vec) {
0N/A return elems.toArray(vec);
0N/A }
0N/A public Object[] toArray() {
0N/A return toArray(new Object[size()]);
0N/A }
0N/A
0N/A /** The first element in this buffer.
0N/A */
0N/A public A first() {
0N/A return elems.head;
0N/A }
0N/A
69N/A /** Return first element in this buffer and remove
0N/A */
69N/A public A next() {
69N/A A x = elems.head;
0N/A if (elems != last) {
0N/A elems = elems.tail;
0N/A count--;
0N/A }
0N/A return x;
0N/A }
0N/A
0N/A /** An enumeration of all elements in this buffer.
0N/A */
0N/A public Iterator<A> iterator() {
0N/A return new Iterator<A>() {
0N/A List<A> elems = ListBuffer.this.elems;
0N/A public boolean hasNext() {
0N/A return elems != last;
0N/A }
0N/A public A next() {
0N/A if (elems == last)
0N/A throw new NoSuchElementException();
0N/A A elem = elems.head;
0N/A elems = elems.tail;
0N/A return elem;
0N/A }
0N/A public void remove() {
0N/A throw new UnsupportedOperationException();
0N/A }
0N/A };
0N/A }
0N/A
0N/A public boolean add(A a) {
69N/A append(a);
69N/A return true;
0N/A }
69N/A
0N/A public boolean remove(Object o) {
0N/A throw new UnsupportedOperationException();
0N/A }
69N/A
0N/A public boolean containsAll(Collection<?> c) {
69N/A for (Object x: c) {
69N/A if (!contains(x))
69N/A return false;
69N/A }
69N/A return true;
0N/A }
69N/A
0N/A public boolean addAll(Collection<? extends A> c) {
69N/A for (A a: c)
69N/A append(a);
69N/A return true;
0N/A }
69N/A
0N/A public boolean removeAll(Collection<?> c) {
0N/A throw new UnsupportedOperationException();
0N/A }
69N/A
0N/A public boolean retainAll(Collection<?> c) {
0N/A throw new UnsupportedOperationException();
0N/A }
69N/A
69N/A public boolean offer(A a) {
69N/A append(a);
69N/A return true;
69N/A }
69N/A
69N/A public A poll() {
69N/A return next();
69N/A }
69N/A
69N/A public A peek() {
69N/A return first();
69N/A }
0N/A}