001: /*
002: * Copyright 1999-2006 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.Collection;
029: import java.util.Iterator;
030: import java.util.NoSuchElementException;
031:
032: /** A class for constructing lists by appending elements. Modelled after
033: * java.lang.StringBuffer.
034: *
035: * <p><b>This is NOT part of any API supported by Sun Microsystems. If
036: * you write code that depends on this, you do so at your own risk.
037: * This code and its internal interfaces are subject to change or
038: * deletion without notice.</b>
039: */
040: @Version("@(#)ListBuffer.java 1.34 07/05/05")
041: public class ListBuffer<A> implements Collection<A> {
042:
043: public static <T> ListBuffer<T> lb() {
044: return new ListBuffer<T>();
045: }
046:
047: /** The list of elements of this buffer.
048: */
049: public List<A> elems;
050:
051: /** A pointer pointing to the last, sentinel element of `elems'.
052: */
053: public List<A> last;
054:
055: /** The number of element in this buffer.
056: */
057: public int count;
058:
059: /** Has a list been created from this buffer yet?
060: */
061: public boolean shared;
062:
063: /** Create a new initially empty list buffer.
064: */
065: public ListBuffer() {
066: clear();
067: }
068:
069: public final void clear() {
070: this .elems = new List<A>(null, null);
071: this .last = this .elems;
072: count = 0;
073: shared = false;
074: }
075:
076: /** Return the number of elements in this buffer.
077: */
078: public int length() {
079: return count;
080: }
081:
082: public int size() {
083: return count;
084: }
085:
086: /** Is buffer empty?
087: */
088: public boolean isEmpty() {
089: return count == 0;
090: }
091:
092: /** Is buffer not empty?
093: */
094: public boolean nonEmpty() {
095: return count != 0;
096: }
097:
098: /** Copy list and sets last.
099: */
100: private void copy() {
101: List<A> p = elems = new List<A>(elems.head, elems.tail);
102: while (true) {
103: List<A> tail = p.tail;
104: if (tail == null)
105: break;
106: tail = new List<A>(tail.head, tail.tail);
107: p.setTail(tail);
108: p = tail;
109: }
110: last = p;
111: shared = false;
112: }
113:
114: /** Prepend an element to buffer.
115: */
116: public ListBuffer<A> prepend(A x) {
117: elems = elems.prepend(x);
118: count++;
119: return this ;
120: }
121:
122: /** Append an element to buffer.
123: */
124: public ListBuffer<A> append(A x) {
125: if (shared)
126: copy();
127: last.head = x;
128: last.setTail(new List<A>(null, null));
129: last = last.tail;
130: count++;
131: return this ;
132: }
133:
134: /** Append all elements in a list to buffer.
135: */
136: public ListBuffer<A> appendList(List<A> xs) {
137: while (xs.nonEmpty()) {
138: append(xs.head);
139: xs = xs.tail;
140: }
141: return this ;
142: }
143:
144: /** Append all elements in a list to buffer.
145: */
146: public ListBuffer<A> appendList(ListBuffer<A> xs) {
147: return appendList(xs.toList());
148: }
149:
150: /** Append all elements in an array to buffer.
151: */
152: public ListBuffer<A> appendArray(A[] xs) {
153: for (int i = 0; i < xs.length; i++) {
154: append(xs[i]);
155: }
156: return this ;
157: }
158:
159: /** Convert buffer to a list of all its elements.
160: */
161: public List<A> toList() {
162: shared = true;
163: return elems;
164: }
165:
166: /** Does the list contain the specified element?
167: */
168: public boolean contains(Object x) {
169: return elems.contains(x);
170: }
171:
172: /** Convert buffer to an array
173: */
174: public <T> T[] toArray(T[] vec) {
175: return elems.toArray(vec);
176: }
177:
178: public Object[] toArray() {
179: return toArray(new Object[size()]);
180: }
181:
182: /** The first element in this buffer.
183: */
184: public A first() {
185: return elems.head;
186: }
187:
188: /** Remove the first element in this buffer.
189: */
190: public void remove() {
191: if (elems != last) {
192: elems = elems.tail;
193: count--;
194: }
195: }
196:
197: /** Return first element in this buffer and remove
198: */
199: public A next() {
200: A x = elems.head;
201: remove();
202: return x;
203: }
204:
205: /** An enumeration of all elements in this buffer.
206: */
207: public Iterator<A> iterator() {
208: return new Iterator<A>() {
209: List<A> elems = ListBuffer.this .elems;
210:
211: public boolean hasNext() {
212: return elems != last;
213: }
214:
215: public A next() {
216: if (elems == last)
217: throw new NoSuchElementException();
218: A elem = elems.head;
219: elems = elems.tail;
220: return elem;
221: }
222:
223: public void remove() {
224: throw new UnsupportedOperationException();
225: }
226: };
227: }
228:
229: public boolean add(A a) {
230: throw new UnsupportedOperationException();
231: }
232:
233: public boolean remove(Object o) {
234: throw new UnsupportedOperationException();
235: }
236:
237: public boolean containsAll(Collection<?> c) {
238: throw new UnsupportedOperationException();
239: }
240:
241: public boolean addAll(Collection<? extends A> c) {
242: throw new UnsupportedOperationException();
243: }
244:
245: public boolean removeAll(Collection<?> c) {
246: throw new UnsupportedOperationException();
247: }
248:
249: public boolean retainAll(Collection<?> c) {
250: throw new UnsupportedOperationException();
251: }
252: }
|