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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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

mercurial