Sat, 01 Dec 2007 00:00:00 +0000
Initial load
duke@1 | 1 | /* |
duke@1 | 2 | * Copyright 1999-2006 Sun Microsystems, Inc. All Rights Reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@1 | 7 | * published by the Free Software Foundation. Sun designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
duke@1 | 9 | * by Sun in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
duke@1 | 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@1 | 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@1 | 23 | * have any questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.javac.util; |
duke@1 | 27 | |
duke@1 | 28 | import java.util.Collection; |
duke@1 | 29 | import java.util.Iterator; |
duke@1 | 30 | import java.util.NoSuchElementException; |
duke@1 | 31 | |
duke@1 | 32 | /** A class for constructing lists by appending elements. Modelled after |
duke@1 | 33 | * java.lang.StringBuffer. |
duke@1 | 34 | * |
duke@1 | 35 | * <p><b>This is NOT part of any API supported by Sun Microsystems. If |
duke@1 | 36 | * you write code that depends on this, you do so at your own risk. |
duke@1 | 37 | * This code and its internal interfaces are subject to change or |
duke@1 | 38 | * deletion without notice.</b> |
duke@1 | 39 | */ |
duke@1 | 40 | public class ListBuffer<A> implements Collection<A> { |
duke@1 | 41 | |
duke@1 | 42 | public static <T> ListBuffer<T> lb() { |
duke@1 | 43 | return new ListBuffer<T>(); |
duke@1 | 44 | } |
duke@1 | 45 | |
duke@1 | 46 | /** The list of elements of this buffer. |
duke@1 | 47 | */ |
duke@1 | 48 | public List<A> elems; |
duke@1 | 49 | |
duke@1 | 50 | /** A pointer pointing to the last, sentinel element of `elems'. |
duke@1 | 51 | */ |
duke@1 | 52 | public List<A> last; |
duke@1 | 53 | |
duke@1 | 54 | /** The number of element in this buffer. |
duke@1 | 55 | */ |
duke@1 | 56 | public int count; |
duke@1 | 57 | |
duke@1 | 58 | /** Has a list been created from this buffer yet? |
duke@1 | 59 | */ |
duke@1 | 60 | public boolean shared; |
duke@1 | 61 | |
duke@1 | 62 | /** Create a new initially empty list buffer. |
duke@1 | 63 | */ |
duke@1 | 64 | public ListBuffer() { |
duke@1 | 65 | clear(); |
duke@1 | 66 | } |
duke@1 | 67 | |
duke@1 | 68 | public final void clear() { |
duke@1 | 69 | this.elems = new List<A>(null,null); |
duke@1 | 70 | this.last = this.elems; |
duke@1 | 71 | count = 0; |
duke@1 | 72 | shared = false; |
duke@1 | 73 | } |
duke@1 | 74 | |
duke@1 | 75 | /** Return the number of elements in this buffer. |
duke@1 | 76 | */ |
duke@1 | 77 | public int length() { |
duke@1 | 78 | return count; |
duke@1 | 79 | } |
duke@1 | 80 | public int size() { |
duke@1 | 81 | return count; |
duke@1 | 82 | } |
duke@1 | 83 | |
duke@1 | 84 | /** Is buffer empty? |
duke@1 | 85 | */ |
duke@1 | 86 | public boolean isEmpty() { |
duke@1 | 87 | return count == 0; |
duke@1 | 88 | } |
duke@1 | 89 | |
duke@1 | 90 | /** Is buffer not empty? |
duke@1 | 91 | */ |
duke@1 | 92 | public boolean nonEmpty() { |
duke@1 | 93 | return count != 0; |
duke@1 | 94 | } |
duke@1 | 95 | |
duke@1 | 96 | /** Copy list and sets last. |
duke@1 | 97 | */ |
duke@1 | 98 | private void copy() { |
duke@1 | 99 | List<A> p = elems = new List<A>(elems.head, elems.tail); |
duke@1 | 100 | while (true) { |
duke@1 | 101 | List<A> tail = p.tail; |
duke@1 | 102 | if (tail == null) break; |
duke@1 | 103 | tail = new List<A>(tail.head, tail.tail); |
duke@1 | 104 | p.setTail(tail); |
duke@1 | 105 | p = tail; |
duke@1 | 106 | } |
duke@1 | 107 | last = p; |
duke@1 | 108 | shared = false; |
duke@1 | 109 | } |
duke@1 | 110 | |
duke@1 | 111 | /** Prepend an element to buffer. |
duke@1 | 112 | */ |
duke@1 | 113 | public ListBuffer<A> prepend(A x) { |
duke@1 | 114 | elems = elems.prepend(x); |
duke@1 | 115 | count++; |
duke@1 | 116 | return this; |
duke@1 | 117 | } |
duke@1 | 118 | |
duke@1 | 119 | /** Append an element to buffer. |
duke@1 | 120 | */ |
duke@1 | 121 | public ListBuffer<A> append(A x) { |
duke@1 | 122 | if (shared) copy(); |
duke@1 | 123 | last.head = x; |
duke@1 | 124 | last.setTail(new List<A>(null,null)); |
duke@1 | 125 | last = last.tail; |
duke@1 | 126 | count++; |
duke@1 | 127 | return this; |
duke@1 | 128 | } |
duke@1 | 129 | |
duke@1 | 130 | /** Append all elements in a list to buffer. |
duke@1 | 131 | */ |
duke@1 | 132 | public ListBuffer<A> appendList(List<A> xs) { |
duke@1 | 133 | while (xs.nonEmpty()) { |
duke@1 | 134 | append(xs.head); |
duke@1 | 135 | xs = xs.tail; |
duke@1 | 136 | } |
duke@1 | 137 | return this; |
duke@1 | 138 | } |
duke@1 | 139 | |
duke@1 | 140 | /** Append all elements in a list to buffer. |
duke@1 | 141 | */ |
duke@1 | 142 | public ListBuffer<A> appendList(ListBuffer<A> xs) { |
duke@1 | 143 | return appendList(xs.toList()); |
duke@1 | 144 | } |
duke@1 | 145 | |
duke@1 | 146 | /** Append all elements in an array to buffer. |
duke@1 | 147 | */ |
duke@1 | 148 | public ListBuffer<A> appendArray(A[] xs) { |
duke@1 | 149 | for (int i = 0; i < xs.length; i++) { |
duke@1 | 150 | append(xs[i]); |
duke@1 | 151 | } |
duke@1 | 152 | return this; |
duke@1 | 153 | } |
duke@1 | 154 | |
duke@1 | 155 | /** Convert buffer to a list of all its elements. |
duke@1 | 156 | */ |
duke@1 | 157 | public List<A> toList() { |
duke@1 | 158 | shared = true; |
duke@1 | 159 | return elems; |
duke@1 | 160 | } |
duke@1 | 161 | |
duke@1 | 162 | /** Does the list contain the specified element? |
duke@1 | 163 | */ |
duke@1 | 164 | public boolean contains(Object x) { |
duke@1 | 165 | return elems.contains(x); |
duke@1 | 166 | } |
duke@1 | 167 | |
duke@1 | 168 | /** Convert buffer to an array |
duke@1 | 169 | */ |
duke@1 | 170 | public <T> T[] toArray(T[] vec) { |
duke@1 | 171 | return elems.toArray(vec); |
duke@1 | 172 | } |
duke@1 | 173 | public Object[] toArray() { |
duke@1 | 174 | return toArray(new Object[size()]); |
duke@1 | 175 | } |
duke@1 | 176 | |
duke@1 | 177 | /** The first element in this buffer. |
duke@1 | 178 | */ |
duke@1 | 179 | public A first() { |
duke@1 | 180 | return elems.head; |
duke@1 | 181 | } |
duke@1 | 182 | |
duke@1 | 183 | /** Remove the first element in this buffer. |
duke@1 | 184 | */ |
duke@1 | 185 | public void remove() { |
duke@1 | 186 | if (elems != last) { |
duke@1 | 187 | elems = elems.tail; |
duke@1 | 188 | count--; |
duke@1 | 189 | } |
duke@1 | 190 | } |
duke@1 | 191 | |
duke@1 | 192 | /** Return first element in this buffer and remove |
duke@1 | 193 | */ |
duke@1 | 194 | public A next() { |
duke@1 | 195 | A x = elems.head; |
duke@1 | 196 | remove(); |
duke@1 | 197 | return x; |
duke@1 | 198 | } |
duke@1 | 199 | |
duke@1 | 200 | /** An enumeration of all elements in this buffer. |
duke@1 | 201 | */ |
duke@1 | 202 | public Iterator<A> iterator() { |
duke@1 | 203 | return new Iterator<A>() { |
duke@1 | 204 | List<A> elems = ListBuffer.this.elems; |
duke@1 | 205 | public boolean hasNext() { |
duke@1 | 206 | return elems != last; |
duke@1 | 207 | } |
duke@1 | 208 | public A next() { |
duke@1 | 209 | if (elems == last) |
duke@1 | 210 | throw new NoSuchElementException(); |
duke@1 | 211 | A elem = elems.head; |
duke@1 | 212 | elems = elems.tail; |
duke@1 | 213 | return elem; |
duke@1 | 214 | } |
duke@1 | 215 | public void remove() { |
duke@1 | 216 | throw new UnsupportedOperationException(); |
duke@1 | 217 | } |
duke@1 | 218 | }; |
duke@1 | 219 | } |
duke@1 | 220 | |
duke@1 | 221 | public boolean add(A a) { |
duke@1 | 222 | throw new UnsupportedOperationException(); |
duke@1 | 223 | } |
duke@1 | 224 | public boolean remove(Object o) { |
duke@1 | 225 | throw new UnsupportedOperationException(); |
duke@1 | 226 | } |
duke@1 | 227 | public boolean containsAll(Collection<?> c) { |
duke@1 | 228 | throw new UnsupportedOperationException(); |
duke@1 | 229 | } |
duke@1 | 230 | public boolean addAll(Collection<? extends A> c) { |
duke@1 | 231 | throw new UnsupportedOperationException(); |
duke@1 | 232 | } |
duke@1 | 233 | public boolean removeAll(Collection<?> c) { |
duke@1 | 234 | throw new UnsupportedOperationException(); |
duke@1 | 235 | } |
duke@1 | 236 | public boolean retainAll(Collection<?> c) { |
duke@1 | 237 | throw new UnsupportedOperationException(); |
duke@1 | 238 | } |
duke@1 | 239 | } |