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

Mon, 04 Feb 2013 18:08:53 -0500

author
dholmes
date
Mon, 04 Feb 2013 18:08:53 -0500
changeset 1570
f91144b7da75
parent 1444
170e486632d9
child 2047
5f915a0c9615
permissions
-rw-r--r--

Merge

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

mercurial