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

Mon, 23 Sep 2013 10:42:38 +0200

author
alundblad
date
Mon, 23 Sep 2013 10:42:38 +0200
changeset 2047
5f915a0c9615
parent 1444
170e486632d9
child 2525
2eb010b6cb22
permissions
-rw-r--r--

6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
Summary: Static factory method ListBuffer.lb removed. Replaced by constructor calls.
Reviewed-by: jfranck, jjg

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

mercurial