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

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1
9a66ca7c79fa
child 70
62fcf8d73dc5
permissions
-rw-r--r--

Initial load

     1 /*
     2  * Copyright 1999-2006 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.Collection;
    29 import java.util.Iterator;
    30 import java.util.NoSuchElementException;
    32 /** A class for constructing lists by appending elements. Modelled after
    33  *  java.lang.StringBuffer.
    34  *
    35  *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    36  *  you write code that depends on this, you do so at your own risk.
    37  *  This code and its internal interfaces are subject to change or
    38  *  deletion without notice.</b>
    39  */
    40 public class ListBuffer<A> implements Collection<A> {
    42     public static <T> ListBuffer<T> lb() {
    43         return new ListBuffer<T>();
    44     }
    46     /** The list of elements of this buffer.
    47      */
    48     public List<A> elems;
    50     /** A pointer pointing to the last, sentinel element of `elems'.
    51      */
    52     public List<A> last;
    54     /** The number of element in this buffer.
    55      */
    56     public int count;
    58     /** Has a list been created from this buffer yet?
    59      */
    60     public boolean shared;
    62     /** Create a new initially empty list buffer.
    63      */
    64     public ListBuffer() {
    65         clear();
    66     }
    68     public final void clear() {
    69         this.elems = new List<A>(null,null);
    70         this.last = this.elems;
    71         count = 0;
    72         shared = false;
    73     }
    75     /** Return the number of elements in this buffer.
    76      */
    77     public int length() {
    78         return count;
    79     }
    80     public int size() {
    81         return count;
    82     }
    84     /** Is buffer empty?
    85      */
    86     public boolean isEmpty() {
    87         return count == 0;
    88     }
    90     /** Is buffer not empty?
    91      */
    92     public boolean nonEmpty() {
    93         return count != 0;
    94     }
    96     /** Copy list and sets last.
    97      */
    98     private void copy() {
    99         List<A> p = elems = new List<A>(elems.head, elems.tail);
   100         while (true) {
   101             List<A> tail = p.tail;
   102             if (tail == null) break;
   103             tail = new List<A>(tail.head, tail.tail);
   104             p.setTail(tail);
   105             p = tail;
   106         }
   107         last = p;
   108         shared = false;
   109     }
   111     /** Prepend an element to buffer.
   112      */
   113     public ListBuffer<A> prepend(A x) {
   114         elems = elems.prepend(x);
   115         count++;
   116         return this;
   117     }
   119     /** Append an element to buffer.
   120      */
   121     public ListBuffer<A> append(A x) {
   122         if (shared) copy();
   123         last.head = x;
   124         last.setTail(new List<A>(null,null));
   125         last = last.tail;
   126         count++;
   127         return this;
   128     }
   130     /** Append all elements in a list to buffer.
   131      */
   132     public ListBuffer<A> appendList(List<A> xs) {
   133         while (xs.nonEmpty()) {
   134             append(xs.head);
   135             xs = xs.tail;
   136         }
   137         return this;
   138     }
   140     /** Append all elements in a list to buffer.
   141      */
   142     public ListBuffer<A> appendList(ListBuffer<A> xs) {
   143         return appendList(xs.toList());
   144     }
   146     /** Append all elements in an array to buffer.
   147      */
   148     public ListBuffer<A> appendArray(A[] xs) {
   149         for (int i = 0; i < xs.length; i++) {
   150             append(xs[i]);
   151         }
   152         return this;
   153     }
   155     /** Convert buffer to a list of all its elements.
   156      */
   157     public List<A> toList() {
   158         shared = true;
   159         return elems;
   160     }
   162     /** Does the list contain the specified element?
   163      */
   164     public boolean contains(Object x) {
   165         return elems.contains(x);
   166     }
   168     /** Convert buffer to an array
   169      */
   170     public <T> T[] toArray(T[] vec) {
   171         return elems.toArray(vec);
   172     }
   173     public Object[] toArray() {
   174         return toArray(new Object[size()]);
   175     }
   177     /** The first element in this buffer.
   178      */
   179     public A first() {
   180         return elems.head;
   181     }
   183     /** Remove the first element in this buffer.
   184      */
   185     public void remove() {
   186         if (elems != last) {
   187             elems = elems.tail;
   188             count--;
   189         }
   190     }
   192     /** Return first element in this buffer and remove
   193      */
   194     public A next() {
   195         A x = elems.head;
   196         remove();
   197         return x;
   198     }
   200     /** An enumeration of all elements in this buffer.
   201      */
   202     public Iterator<A> iterator() {
   203         return new Iterator<A>() {
   204             List<A> elems = ListBuffer.this.elems;
   205             public boolean hasNext() {
   206                 return elems != last;
   207             }
   208             public A next() {
   209                 if (elems == last)
   210                     throw new NoSuchElementException();
   211                 A elem = elems.head;
   212                 elems = elems.tail;
   213                 return elem;
   214             }
   215             public void remove() {
   216                 throw new UnsupportedOperationException();
   217             }
   218         };
   219     }
   221     public boolean add(A a) {
   222         throw new UnsupportedOperationException();
   223     }
   224     public boolean remove(Object o) {
   225         throw new UnsupportedOperationException();
   226     }
   227     public boolean containsAll(Collection<?> c) {
   228         throw new UnsupportedOperationException();
   229     }
   230     public boolean addAll(Collection<? extends A> c) {
   231         throw new UnsupportedOperationException();
   232     }
   233     public boolean removeAll(Collection<?> c) {
   234         throw new UnsupportedOperationException();
   235     }
   236     public boolean retainAll(Collection<?> c) {
   237         throw new UnsupportedOperationException();
   238     }
   239 }

mercurial