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

Thu, 02 Oct 2008 19:58:40 -0700

author
xdono
date
Thu, 02 Oct 2008 19:58:40 -0700
changeset 117
24a47c3062fe
parent 70
62fcf8d73dc5
child 554
9d9f26857129
permissions
-rw-r--r--

6754988: Update copyright year
Summary: Update for files that have been modified starting July 2008
Reviewed-by: ohair, tbell

     1 /*
     2  * Copyright 1999-2008 Sun Microsystems, Inc.  All Rights Reserved.
     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
     7  * published by the Free Software Foundation.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun in the LICENSE file that accompanied this code.
    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  *
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    26 package com.sun.tools.javac.util;
    28 import java.util.AbstractQueue;
    29 import java.util.Collection;
    30 import java.util.Iterator;
    31 import java.util.NoSuchElementException;
    33 /** A class for constructing lists by appending elements. Modelled after
    34  *  java.lang.StringBuffer.
    35  *
    36  *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    37  *  you write code that depends on this, you do so at your own risk.
    38  *  This code and its internal interfaces are subject to change or
    39  *  deletion without notice.</b>
    40  */
    41 public class ListBuffer<A> extends AbstractQueue<A> {
    43     public static <T> ListBuffer<T> lb() {
    44         return new ListBuffer<T>();
    45     }
    47     public static <T> ListBuffer<T> of(T x) {
    48         ListBuffer<T> lb = new ListBuffer<T>();
    49         lb.add(x);
    50         return lb;
    51     }
    53     /** The list of elements of this buffer.
    54      */
    55     public List<A> elems;
    57     /** A pointer pointing to the last, sentinel element of `elems'.
    58      */
    59     public List<A> last;
    61     /** The number of element in this buffer.
    62      */
    63     public int count;
    65     /** Has a list been created from this buffer yet?
    66      */
    67     public boolean shared;
    69     /** Create a new initially empty list buffer.
    70      */
    71     public ListBuffer() {
    72         clear();
    73     }
    75     public final void clear() {
    76         this.elems = new List<A>(null,null);
    77         this.last = this.elems;
    78         count = 0;
    79         shared = false;
    80     }
    82     /** Return the number of elements in this buffer.
    83      */
    84     public int length() {
    85         return count;
    86     }
    87     public int size() {
    88         return count;
    89     }
    91     /** Is buffer empty?
    92      */
    93     public boolean isEmpty() {
    94         return count == 0;
    95     }
    97     /** Is buffer not empty?
    98      */
    99     public boolean nonEmpty() {
   100         return count != 0;
   101     }
   103     /** Copy list and sets last.
   104      */
   105     private void copy() {
   106         List<A> p = elems = new List<A>(elems.head, elems.tail);
   107         while (true) {
   108             List<A> tail = p.tail;
   109             if (tail == null) break;
   110             tail = new List<A>(tail.head, tail.tail);
   111             p.setTail(tail);
   112             p = tail;
   113         }
   114         last = p;
   115         shared = false;
   116     }
   118     /** Prepend an element to buffer.
   119      */
   120     public ListBuffer<A> prepend(A x) {
   121         elems = elems.prepend(x);
   122         count++;
   123         return this;
   124     }
   126     /** Append an element to buffer.
   127      */
   128     public ListBuffer<A> append(A x) {
   129         x.getClass(); // null check
   130         if (shared) copy();
   131         last.head = x;
   132         last.setTail(new List<A>(null,null));
   133         last = last.tail;
   134         count++;
   135         return this;
   136     }
   138     /** Append all elements in a list to buffer.
   139      */
   140     public ListBuffer<A> appendList(List<A> xs) {
   141         while (xs.nonEmpty()) {
   142             append(xs.head);
   143             xs = xs.tail;
   144         }
   145         return this;
   146     }
   148     /** Append all elements in a list to buffer.
   149      */
   150     public ListBuffer<A> appendList(ListBuffer<A> xs) {
   151         return appendList(xs.toList());
   152     }
   154     /** Append all elements in an array to buffer.
   155      */
   156     public ListBuffer<A> appendArray(A[] xs) {
   157         for (int i = 0; i < xs.length; i++) {
   158             append(xs[i]);
   159         }
   160         return this;
   161     }
   163     /** Convert buffer to a list of all its elements.
   164      */
   165     public List<A> toList() {
   166         shared = true;
   167         return elems;
   168     }
   170     /** Does the list contain the specified element?
   171      */
   172     public boolean contains(Object x) {
   173         return elems.contains(x);
   174     }
   176     /** Convert buffer to an array
   177      */
   178     public <T> T[] toArray(T[] vec) {
   179         return elems.toArray(vec);
   180     }
   181     public Object[] toArray() {
   182         return toArray(new Object[size()]);
   183     }
   185     /** The first element in this buffer.
   186      */
   187     public A first() {
   188         return elems.head;
   189     }
   191     /** Return first element in this buffer and remove
   192      */
   193     public A next() {
   194         A x = elems.head;
   195         if (elems != last) {
   196             elems = elems.tail;
   197             count--;
   198         }
   199         return x;
   200     }
   202     /** An enumeration of all elements in this buffer.
   203      */
   204     public Iterator<A> iterator() {
   205         return new Iterator<A>() {
   206             List<A> elems = ListBuffer.this.elems;
   207             public boolean hasNext() {
   208                 return elems != last;
   209             }
   210             public A next() {
   211                 if (elems == last)
   212                     throw new NoSuchElementException();
   213                 A elem = elems.head;
   214                 elems = elems.tail;
   215                 return elem;
   216             }
   217             public void remove() {
   218                 throw new UnsupportedOperationException();
   219             }
   220         };
   221     }
   223     public boolean add(A a) {
   224         append(a);
   225         return true;
   226     }
   228     public boolean remove(Object o) {
   229         throw new UnsupportedOperationException();
   230     }
   232     public boolean containsAll(Collection<?> c) {
   233         for (Object x: c) {
   234             if (!contains(x))
   235                 return false;
   236         }
   237         return true;
   238     }
   240     public boolean addAll(Collection<? extends A> c) {
   241         for (A a: c)
   242             append(a);
   243         return true;
   244     }
   246     public boolean removeAll(Collection<?> c) {
   247         throw new UnsupportedOperationException();
   248     }
   250     public boolean retainAll(Collection<?> c) {
   251         throw new UnsupportedOperationException();
   252     }
   254     public boolean offer(A a) {
   255         append(a);
   256         return true;
   257     }
   259     public A poll() {
   260         return next();
   261     }
   263     public A peek() {
   264         return first();
   265     }
   266 }

mercurial