001    /*
002     * Copyright 1999-2008 Sun Microsystems, Inc.  All Rights Reserved.
003     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004     *
005     * This code is free software; you can redistribute it and/or modify it
006     * under the terms of the GNU General Public License version 2 only, as
007     * published by the Free Software Foundation.  Sun designates this
008     * particular file as subject to the "Classpath" exception as provided
009     * by Sun in the LICENSE file that accompanied this code.
010     *
011     * This code is distributed in the hope that it will be useful, but WITHOUT
012     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014     * version 2 for more details (a copy is included in the LICENSE file that
015     * accompanied this code).
016     *
017     * You should have received a copy of the GNU General Public License version
018     * 2 along with this work; if not, write to the Free Software Foundation,
019     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020     *
021     * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022     * CA 95054 USA or visit www.sun.com if you need additional information or
023     * have any questions.
024     */
025    
026    package com.sun.tools.javac.util;
027    
028    import java.util.AbstractQueue;
029    import java.util.Collection;
030    import java.util.Iterator;
031    import java.util.NoSuchElementException;
032    
033    /** A class for constructing lists by appending elements. Modelled after
034     *  java.lang.StringBuffer.
035     *
036     *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
037     *  you write code that depends on this, you do so at your own risk.
038     *  This code and its internal interfaces are subject to change or
039     *  deletion without notice.</b>
040     */
041    public class ListBuffer<A> extends AbstractQueue<A> {
042    
043        public static <T> ListBuffer<T> lb() {
044            return new ListBuffer<T>();
045        }
046    
047        public static <T> ListBuffer<T> of(T x) {
048            ListBuffer<T> lb = new ListBuffer<T>();
049            lb.add(x);
050            return lb;
051        }
052    
053        /** The list of elements of this buffer.
054         */
055        public List<A> elems;
056    
057        /** A pointer pointing to the last, sentinel element of `elems'.
058         */
059        public List<A> last;
060    
061        /** The number of element in this buffer.
062         */
063        public int count;
064    
065        /** Has a list been created from this buffer yet?
066         */
067        public boolean shared;
068    
069        /** Create a new initially empty list buffer.
070         */
071        public ListBuffer() {
072            clear();
073        }
074    
075        public final void clear() {
076            this.elems = new List<A>(null,null);
077            this.last = this.elems;
078            count = 0;
079            shared = false;
080        }
081    
082        /** Return the number of elements in this buffer.
083         */
084        public int length() {
085            return count;
086        }
087        public int size() {
088            return count;
089        }
090    
091        /** Is buffer empty?
092         */
093        public boolean isEmpty() {
094            return count == 0;
095        }
096    
097        /** Is buffer not empty?
098         */
099        public boolean nonEmpty() {
100            return count != 0;
101        }
102    
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        }
117    
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        }
125    
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        }
137    
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        }
147    
148        /** Append all elements in a list to buffer.
149         */
150        public ListBuffer<A> appendList(ListBuffer<A> xs) {
151            return appendList(xs.toList());
152        }
153    
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        }
162    
163        /** Convert buffer to a list of all its elements.
164         */
165        public List<A> toList() {
166            shared = true;
167            return elems;
168        }
169    
170        /** Does the list contain the specified element?
171         */
172        public boolean contains(Object x) {
173            return elems.contains(x);
174        }
175    
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        }
184    
185        /** The first element in this buffer.
186         */
187        public A first() {
188            return elems.head;
189        }
190    
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        }
201    
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        }
222    
223        public boolean add(A a) {
224            append(a);
225            return true;
226        }
227    
228        public boolean remove(Object o) {
229            throw new UnsupportedOperationException();
230        }
231    
232        public boolean containsAll(Collection<?> c) {
233            for (Object x: c) {
234                if (!contains(x))
235                    return false;
236            }
237            return true;
238        }
239    
240        public boolean addAll(Collection<? extends A> c) {
241            for (A a: c)
242                append(a);
243            return true;
244        }
245    
246        public boolean removeAll(Collection<?> c) {
247            throw new UnsupportedOperationException();
248        }
249    
250        public boolean retainAll(Collection<?> c) {
251            throw new UnsupportedOperationException();
252        }
253    
254        public boolean offer(A a) {
255            append(a);
256            return true;
257        }
258    
259        public A poll() {
260            return next();
261        }
262    
263        public A peek() {
264            return first();
265        }
266    }