Mon, 11 Apr 2011 16:31:22 -0700
Merge
duke@1 | 1 | /* |
ohair@554 | 2 | * Copyright (c) 1999, 2008, Oracle and/or its affiliates. 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 |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle 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 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.javac.util; |
duke@1 | 27 | |
jjg@70 | 28 | import java.util.AbstractQueue; |
duke@1 | 29 | import java.util.Collection; |
duke@1 | 30 | import java.util.Iterator; |
duke@1 | 31 | import java.util.NoSuchElementException; |
duke@1 | 32 | |
duke@1 | 33 | /** A class for constructing lists by appending elements. Modelled after |
duke@1 | 34 | * java.lang.StringBuffer. |
duke@1 | 35 | * |
jjg@581 | 36 | * <p><b>This is NOT part of any supported API. |
jjg@581 | 37 | * If you write code that depends on this, you do so at your own risk. |
duke@1 | 38 | * This code and its internal interfaces are subject to change or |
duke@1 | 39 | * deletion without notice.</b> |
duke@1 | 40 | */ |
jjg@70 | 41 | public class ListBuffer<A> extends AbstractQueue<A> { |
duke@1 | 42 | |
duke@1 | 43 | public static <T> ListBuffer<T> lb() { |
duke@1 | 44 | return new ListBuffer<T>(); |
duke@1 | 45 | } |
duke@1 | 46 | |
jjg@70 | 47 | public static <T> ListBuffer<T> of(T x) { |
jjg@70 | 48 | ListBuffer<T> lb = new ListBuffer<T>(); |
jjg@70 | 49 | lb.add(x); |
jjg@70 | 50 | return lb; |
jjg@70 | 51 | } |
jjg@70 | 52 | |
duke@1 | 53 | /** The list of elements of this buffer. |
duke@1 | 54 | */ |
duke@1 | 55 | public List<A> elems; |
duke@1 | 56 | |
duke@1 | 57 | /** A pointer pointing to the last, sentinel element of `elems'. |
duke@1 | 58 | */ |
duke@1 | 59 | public List<A> last; |
duke@1 | 60 | |
duke@1 | 61 | /** The number of element in this buffer. |
duke@1 | 62 | */ |
duke@1 | 63 | public int count; |
duke@1 | 64 | |
duke@1 | 65 | /** Has a list been created from this buffer yet? |
duke@1 | 66 | */ |
duke@1 | 67 | public boolean shared; |
duke@1 | 68 | |
duke@1 | 69 | /** Create a new initially empty list buffer. |
duke@1 | 70 | */ |
duke@1 | 71 | public ListBuffer() { |
duke@1 | 72 | clear(); |
duke@1 | 73 | } |
duke@1 | 74 | |
duke@1 | 75 | public final void clear() { |
duke@1 | 76 | this.elems = new List<A>(null,null); |
duke@1 | 77 | this.last = this.elems; |
duke@1 | 78 | count = 0; |
duke@1 | 79 | shared = false; |
duke@1 | 80 | } |
duke@1 | 81 | |
duke@1 | 82 | /** Return the number of elements in this buffer. |
duke@1 | 83 | */ |
duke@1 | 84 | public int length() { |
duke@1 | 85 | return count; |
duke@1 | 86 | } |
duke@1 | 87 | public int size() { |
duke@1 | 88 | return count; |
duke@1 | 89 | } |
duke@1 | 90 | |
duke@1 | 91 | /** Is buffer empty? |
duke@1 | 92 | */ |
duke@1 | 93 | public boolean isEmpty() { |
duke@1 | 94 | return count == 0; |
duke@1 | 95 | } |
duke@1 | 96 | |
duke@1 | 97 | /** Is buffer not empty? |
duke@1 | 98 | */ |
duke@1 | 99 | public boolean nonEmpty() { |
duke@1 | 100 | return count != 0; |
duke@1 | 101 | } |
duke@1 | 102 | |
duke@1 | 103 | /** Copy list and sets last. |
duke@1 | 104 | */ |
duke@1 | 105 | private void copy() { |
duke@1 | 106 | List<A> p = elems = new List<A>(elems.head, elems.tail); |
duke@1 | 107 | while (true) { |
duke@1 | 108 | List<A> tail = p.tail; |
duke@1 | 109 | if (tail == null) break; |
duke@1 | 110 | tail = new List<A>(tail.head, tail.tail); |
duke@1 | 111 | p.setTail(tail); |
duke@1 | 112 | p = tail; |
duke@1 | 113 | } |
duke@1 | 114 | last = p; |
duke@1 | 115 | shared = false; |
duke@1 | 116 | } |
duke@1 | 117 | |
duke@1 | 118 | /** Prepend an element to buffer. |
duke@1 | 119 | */ |
duke@1 | 120 | public ListBuffer<A> prepend(A x) { |
duke@1 | 121 | elems = elems.prepend(x); |
duke@1 | 122 | count++; |
duke@1 | 123 | return this; |
duke@1 | 124 | } |
duke@1 | 125 | |
duke@1 | 126 | /** Append an element to buffer. |
duke@1 | 127 | */ |
duke@1 | 128 | public ListBuffer<A> append(A x) { |
jjg@70 | 129 | x.getClass(); // null check |
duke@1 | 130 | if (shared) copy(); |
duke@1 | 131 | last.head = x; |
duke@1 | 132 | last.setTail(new List<A>(null,null)); |
duke@1 | 133 | last = last.tail; |
duke@1 | 134 | count++; |
duke@1 | 135 | return this; |
duke@1 | 136 | } |
duke@1 | 137 | |
duke@1 | 138 | /** Append all elements in a list to buffer. |
duke@1 | 139 | */ |
duke@1 | 140 | public ListBuffer<A> appendList(List<A> xs) { |
duke@1 | 141 | while (xs.nonEmpty()) { |
duke@1 | 142 | append(xs.head); |
duke@1 | 143 | xs = xs.tail; |
duke@1 | 144 | } |
duke@1 | 145 | return this; |
duke@1 | 146 | } |
duke@1 | 147 | |
duke@1 | 148 | /** Append all elements in a list to buffer. |
duke@1 | 149 | */ |
duke@1 | 150 | public ListBuffer<A> appendList(ListBuffer<A> xs) { |
duke@1 | 151 | return appendList(xs.toList()); |
duke@1 | 152 | } |
duke@1 | 153 | |
duke@1 | 154 | /** Append all elements in an array to buffer. |
duke@1 | 155 | */ |
duke@1 | 156 | public ListBuffer<A> appendArray(A[] xs) { |
duke@1 | 157 | for (int i = 0; i < xs.length; i++) { |
duke@1 | 158 | append(xs[i]); |
duke@1 | 159 | } |
duke@1 | 160 | return this; |
duke@1 | 161 | } |
duke@1 | 162 | |
duke@1 | 163 | /** Convert buffer to a list of all its elements. |
duke@1 | 164 | */ |
duke@1 | 165 | public List<A> toList() { |
duke@1 | 166 | shared = true; |
duke@1 | 167 | return elems; |
duke@1 | 168 | } |
duke@1 | 169 | |
duke@1 | 170 | /** Does the list contain the specified element? |
duke@1 | 171 | */ |
duke@1 | 172 | public boolean contains(Object x) { |
duke@1 | 173 | return elems.contains(x); |
duke@1 | 174 | } |
duke@1 | 175 | |
duke@1 | 176 | /** Convert buffer to an array |
duke@1 | 177 | */ |
duke@1 | 178 | public <T> T[] toArray(T[] vec) { |
duke@1 | 179 | return elems.toArray(vec); |
duke@1 | 180 | } |
duke@1 | 181 | public Object[] toArray() { |
duke@1 | 182 | return toArray(new Object[size()]); |
duke@1 | 183 | } |
duke@1 | 184 | |
duke@1 | 185 | /** The first element in this buffer. |
duke@1 | 186 | */ |
duke@1 | 187 | public A first() { |
duke@1 | 188 | return elems.head; |
duke@1 | 189 | } |
duke@1 | 190 | |
jjg@70 | 191 | /** Return first element in this buffer and remove |
duke@1 | 192 | */ |
jjg@70 | 193 | public A next() { |
jjg@70 | 194 | A x = elems.head; |
duke@1 | 195 | if (elems != last) { |
duke@1 | 196 | elems = elems.tail; |
duke@1 | 197 | count--; |
duke@1 | 198 | } |
duke@1 | 199 | return x; |
duke@1 | 200 | } |
duke@1 | 201 | |
duke@1 | 202 | /** An enumeration of all elements in this buffer. |
duke@1 | 203 | */ |
duke@1 | 204 | public Iterator<A> iterator() { |
duke@1 | 205 | return new Iterator<A>() { |
duke@1 | 206 | List<A> elems = ListBuffer.this.elems; |
duke@1 | 207 | public boolean hasNext() { |
duke@1 | 208 | return elems != last; |
duke@1 | 209 | } |
duke@1 | 210 | public A next() { |
duke@1 | 211 | if (elems == last) |
duke@1 | 212 | throw new NoSuchElementException(); |
duke@1 | 213 | A elem = elems.head; |
duke@1 | 214 | elems = elems.tail; |
duke@1 | 215 | return elem; |
duke@1 | 216 | } |
duke@1 | 217 | public void remove() { |
duke@1 | 218 | throw new UnsupportedOperationException(); |
duke@1 | 219 | } |
duke@1 | 220 | }; |
duke@1 | 221 | } |
duke@1 | 222 | |
duke@1 | 223 | public boolean add(A a) { |
jjg@70 | 224 | append(a); |
jjg@70 | 225 | return true; |
duke@1 | 226 | } |
jjg@70 | 227 | |
duke@1 | 228 | public boolean remove(Object o) { |
duke@1 | 229 | throw new UnsupportedOperationException(); |
duke@1 | 230 | } |
jjg@70 | 231 | |
duke@1 | 232 | public boolean containsAll(Collection<?> c) { |
jjg@70 | 233 | for (Object x: c) { |
jjg@70 | 234 | if (!contains(x)) |
jjg@70 | 235 | return false; |
jjg@70 | 236 | } |
jjg@70 | 237 | return true; |
duke@1 | 238 | } |
jjg@70 | 239 | |
duke@1 | 240 | public boolean addAll(Collection<? extends A> c) { |
jjg@70 | 241 | for (A a: c) |
jjg@70 | 242 | append(a); |
jjg@70 | 243 | return true; |
duke@1 | 244 | } |
jjg@70 | 245 | |
duke@1 | 246 | public boolean removeAll(Collection<?> c) { |
duke@1 | 247 | throw new UnsupportedOperationException(); |
duke@1 | 248 | } |
jjg@70 | 249 | |
duke@1 | 250 | public boolean retainAll(Collection<?> c) { |
duke@1 | 251 | throw new UnsupportedOperationException(); |
duke@1 | 252 | } |
jjg@70 | 253 | |
jjg@70 | 254 | public boolean offer(A a) { |
jjg@70 | 255 | append(a); |
jjg@70 | 256 | return true; |
jjg@70 | 257 | } |
jjg@70 | 258 | |
jjg@70 | 259 | public A poll() { |
jjg@70 | 260 | return next(); |
jjg@70 | 261 | } |
jjg@70 | 262 | |
jjg@70 | 263 | public A peek() { |
jjg@70 | 264 | return first(); |
jjg@70 | 265 | } |
duke@1 | 266 | } |