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

Tue, 05 Mar 2013 14:12:07 +0000

author
mcimadamore
date
Tue, 05 Mar 2013 14:12:07 +0000
changeset 1613
d2a98dde7ecc
parent 1444
170e486632d9
child 2047
5f915a0c9615
permissions
-rw-r--r--

8009227: Certain diagnostics should not be deferred
Summary: Add new diagnostic flag to mark non deferrable diagnostics
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 1999, 2012, Oracle and/or its affiliates. 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.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * 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 supported API.
    37  *  If 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     private List<A> elems;
    57     /** A pointer pointing to the last element of 'elems' containing data,
    58      *  or null if the list is empty.
    59      */
    60     private List<A> last;
    62     /** The number of element in this buffer.
    63      */
    64     private int count;
    66     /** Has a list been created from this buffer yet?
    67      */
    68     private boolean shared;
    70     /** Create a new initially empty list buffer.
    71      */
    72     public ListBuffer() {
    73         clear();
    74     }
    76     public final void clear() {
    77         this.elems = List.nil();
    78         this.last = null;
    79         count = 0;
    80         shared = false;
    81     }
    83     /** Return the number of elements in this buffer.
    84      */
    85     public int length() {
    86         return count;
    87     }
    88     public int size() {
    89         return count;
    90     }
    92     /** Is buffer empty?
    93      */
    94     public boolean isEmpty() {
    95         return count == 0;
    96     }
    98     /** Is buffer not empty?
    99      */
   100     public boolean nonEmpty() {
   101         return count != 0;
   102     }
   104     /** Copy list and sets last.
   105      */
   106     private void copy() {
   107         if (elems.nonEmpty()) {
   108             List<A> orig = elems;
   110             elems = last = List.<A>of(orig.head);
   112             while ((orig = orig.tail).nonEmpty()) {
   113                 last.tail = List.<A>of(orig.head);
   114                 last = last.tail;
   115             }
   116         }
   117     }
   119     /** Prepend an element to buffer.
   120      */
   121     public ListBuffer<A> prepend(A x) {
   122         elems = elems.prepend(x);
   123         if (last == null) last = elems;
   124         count++;
   125         return this;
   126     }
   128     /** Append an element to buffer.
   129      */
   130     public ListBuffer<A> append(A x) {
   131         x.getClass(); // null check
   132         if (shared) copy();
   133         List<A> newLast = List.<A>of(x);
   134         if (last != null) {
   135             last.tail = newLast;
   136             last = newLast;
   137         } else {
   138             elems = last = newLast;
   139         }
   140         count++;
   141         return this;
   142     }
   144     /** Append all elements in a list to buffer.
   145      */
   146     public ListBuffer<A> appendList(List<A> xs) {
   147         while (xs.nonEmpty()) {
   148             append(xs.head);
   149             xs = xs.tail;
   150         }
   151         return this;
   152     }
   154     /** Append all elements in a list to buffer.
   155      */
   156     public ListBuffer<A> appendList(ListBuffer<A> xs) {
   157         return appendList(xs.toList());
   158     }
   160     /** Append all elements in an array to buffer.
   161      */
   162     public ListBuffer<A> appendArray(A[] xs) {
   163         for (int i = 0; i < xs.length; i++) {
   164             append(xs[i]);
   165         }
   166         return this;
   167     }
   169     /** Convert buffer to a list of all its elements.
   170      */
   171     public List<A> toList() {
   172         shared = true;
   173         return elems;
   174     }
   176     /** Does the list contain the specified element?
   177      */
   178     public boolean contains(Object x) {
   179         return elems.contains(x);
   180     }
   182     /** Convert buffer to an array
   183      */
   184     public <T> T[] toArray(T[] vec) {
   185         return elems.toArray(vec);
   186     }
   187     public Object[] toArray() {
   188         return toArray(new Object[size()]);
   189     }
   191     /** The first element in this buffer.
   192      */
   193     public A first() {
   194         return elems.head;
   195     }
   197     /** Return first element in this buffer and remove
   198      */
   199     public A next() {
   200         A x = elems.head;
   201         if (!elems.isEmpty()) {
   202             elems = elems.tail;
   203             if (elems.isEmpty()) last = null;
   204             count--;
   205         }
   206         return x;
   207     }
   209     /** An enumeration of all elements in this buffer.
   210      */
   211     public Iterator<A> iterator() {
   212         return new Iterator<A>() {
   213             List<A> elems = ListBuffer.this.elems;
   214             public boolean hasNext() {
   215                 return !elems.isEmpty();
   216             }
   217             public A next() {
   218                 if (elems.isEmpty())
   219                     throw new NoSuchElementException();
   220                 A elem = elems.head;
   221                 elems = elems.tail;
   222                 return elem;
   223             }
   224             public void remove() {
   225                 throw new UnsupportedOperationException();
   226             }
   227         };
   228     }
   230     public boolean add(A a) {
   231         append(a);
   232         return true;
   233     }
   235     public boolean remove(Object o) {
   236         throw new UnsupportedOperationException();
   237     }
   239     public boolean containsAll(Collection<?> c) {
   240         for (Object x: c) {
   241             if (!contains(x))
   242                 return false;
   243         }
   244         return true;
   245     }
   247     public boolean addAll(Collection<? extends A> c) {
   248         for (A a: c)
   249             append(a);
   250         return true;
   251     }
   253     public boolean removeAll(Collection<?> c) {
   254         throw new UnsupportedOperationException();
   255     }
   257     public boolean retainAll(Collection<?> c) {
   258         throw new UnsupportedOperationException();
   259     }
   261     public boolean offer(A a) {
   262         append(a);
   263         return true;
   264     }
   266     public A poll() {
   267         return next();
   268     }
   270     public A peek() {
   271         return first();
   272     }
   274     public A last() {
   275         return last != null ? last.head : null;
   276     }
   277 }

mercurial