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

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/util/List.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,540 @@
     1.4 +/*
     1.5 + * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javac.util;
    1.30 +
    1.31 +import java.lang.reflect.Array;
    1.32 +import java.util.ArrayList;
    1.33 +import java.util.Collection;
    1.34 +import java.util.Collections;
    1.35 +import java.util.Iterator;
    1.36 +import java.util.AbstractCollection;
    1.37 +import java.util.ListIterator;
    1.38 +import java.util.NoSuchElementException;
    1.39 +
    1.40 +/** A class for generic linked lists. Links are supposed to be
    1.41 + *  immutable, the only exception being the incremental construction of
    1.42 + *  lists via ListBuffers.  List is the main container class in
    1.43 + *  GJC. Most data structures and algorithms in GJC use lists rather
    1.44 + *  than arrays.
    1.45 + *
    1.46 + *  <p>Lists are always trailed by a sentinel element, whose head and tail
    1.47 + *  are both null.
    1.48 + *
    1.49 + *  <p><b>This is NOT part of any supported API.
    1.50 + *  If you write code that depends on this, you do so at your own risk.
    1.51 + *  This code and its internal interfaces are subject to change or
    1.52 + *  deletion without notice.</b>
    1.53 + */
    1.54 +public class List<A> extends AbstractCollection<A> implements java.util.List<A> {
    1.55 +
    1.56 +    /** The first element of the list, supposed to be immutable.
    1.57 +     */
    1.58 +    public A head;
    1.59 +
    1.60 +    /** The remainder of the list except for its first element, supposed
    1.61 +     *  to be immutable.
    1.62 +     */
    1.63 +    //@Deprecated
    1.64 +    public List<A> tail;
    1.65 +
    1.66 +    /** Construct a list given its head and tail.
    1.67 +     */
    1.68 +    List(A head, List<A> tail) {
    1.69 +        this.tail = tail;
    1.70 +        this.head = head;
    1.71 +    }
    1.72 +
    1.73 +    /** Construct an empty list.
    1.74 +     */
    1.75 +    @SuppressWarnings("unchecked")
    1.76 +    public static <A> List<A> nil() {
    1.77 +        return (List<A>)EMPTY_LIST;
    1.78 +    }
    1.79 +
    1.80 +    private static final List<?> EMPTY_LIST = new List<Object>(null,null) {
    1.81 +        public List<Object> setTail(List<Object> tail) {
    1.82 +            throw new UnsupportedOperationException();
    1.83 +        }
    1.84 +        public boolean isEmpty() {
    1.85 +            return true;
    1.86 +        }
    1.87 +    };
    1.88 +
    1.89 +    /** Returns the list obtained from 'l' after removing all elements 'elem'
    1.90 +     */
    1.91 +    public static <A> List<A> filter(List<A> l, A elem) {
    1.92 +        Assert.checkNonNull(elem);
    1.93 +        List<A> res = List.nil();
    1.94 +        for (A a : l) {
    1.95 +            if (a != null && !a.equals(elem)) {
    1.96 +                res = res.prepend(a);
    1.97 +            }
    1.98 +        }
    1.99 +        return res.reverse();
   1.100 +    }
   1.101 +
   1.102 +    public List<A> intersect(List<A> that) {
   1.103 +        ListBuffer<A> buf = new ListBuffer<>();
   1.104 +        for (A el : this) {
   1.105 +            if (that.contains(el)) {
   1.106 +                buf.append(el);
   1.107 +            }
   1.108 +        }
   1.109 +        return buf.toList();
   1.110 +    }
   1.111 +
   1.112 +    public List<A> diff(List<A> that) {
   1.113 +        ListBuffer<A> buf = new ListBuffer<>();
   1.114 +        for (A el : this) {
   1.115 +            if (!that.contains(el)) {
   1.116 +                buf.append(el);
   1.117 +            }
   1.118 +        }
   1.119 +        return buf.toList();
   1.120 +    }
   1.121 +
   1.122 +    /**
   1.123 +     * Create a new list from the first {@code n} elements of this list
   1.124 +     */
   1.125 +    public List<A> take(int n) {
   1.126 +        ListBuffer<A> buf = new ListBuffer<>();
   1.127 +        int count = 0;
   1.128 +        for (A el : this) {
   1.129 +            if (count++ == n) break;
   1.130 +            buf.append(el);
   1.131 +        }
   1.132 +        return buf.toList();
   1.133 +    }
   1.134 +
   1.135 +    /** Construct a list consisting of given element.
   1.136 +     */
   1.137 +    public static <A> List<A> of(A x1) {
   1.138 +        return new List<A>(x1, List.<A>nil());
   1.139 +    }
   1.140 +
   1.141 +    /** Construct a list consisting of given elements.
   1.142 +     */
   1.143 +    public static <A> List<A> of(A x1, A x2) {
   1.144 +        return new List<A>(x1, of(x2));
   1.145 +    }
   1.146 +
   1.147 +    /** Construct a list consisting of given elements.
   1.148 +     */
   1.149 +    public static <A> List<A> of(A x1, A x2, A x3) {
   1.150 +        return new List<A>(x1, of(x2, x3));
   1.151 +    }
   1.152 +
   1.153 +    /** Construct a list consisting of given elements.
   1.154 +     */
   1.155 +    @SuppressWarnings({"varargs", "unchecked"})
   1.156 +    public static <A> List<A> of(A x1, A x2, A x3, A... rest) {
   1.157 +        return new List<A>(x1, new List<A>(x2, new List<A>(x3, from(rest))));
   1.158 +    }
   1.159 +
   1.160 +    /**
   1.161 +     * Construct a list consisting all elements of given array.
   1.162 +     * @param array an array; if {@code null} return an empty list
   1.163 +     */
   1.164 +    public static <A> List<A> from(A[] array) {
   1.165 +        List<A> xs = nil();
   1.166 +        if (array != null)
   1.167 +            for (int i = array.length - 1; i >= 0; i--)
   1.168 +                xs = new List<A>(array[i], xs);
   1.169 +        return xs;
   1.170 +    }
   1.171 +
   1.172 +    public static <A> List<A> from(Iterable<? extends A> coll) {
   1.173 +        ListBuffer<A> xs = new ListBuffer<>();
   1.174 +        for (A a : coll) {
   1.175 +            xs.append(a);
   1.176 +        }
   1.177 +        return xs.toList();
   1.178 +    }
   1.179 +
   1.180 +    /** Construct a list consisting of a given number of identical elements.
   1.181 +     *  @param len    The number of elements in the list.
   1.182 +     *  @param init   The value of each element.
   1.183 +     */
   1.184 +    @Deprecated
   1.185 +    public static <A> List<A> fill(int len, A init) {
   1.186 +        List<A> l = nil();
   1.187 +        for (int i = 0; i < len; i++) l = new List<A>(init, l);
   1.188 +        return l;
   1.189 +    }
   1.190 +
   1.191 +    /** Does list have no elements?
   1.192 +     */
   1.193 +    @Override
   1.194 +    public boolean isEmpty() {
   1.195 +        return tail == null;
   1.196 +    }
   1.197 +
   1.198 +    /** Does list have elements?
   1.199 +     */
   1.200 +    //@Deprecated
   1.201 +    public boolean nonEmpty() {
   1.202 +        return tail != null;
   1.203 +    }
   1.204 +
   1.205 +    /** Return the number of elements in this list.
   1.206 +     */
   1.207 +    //@Deprecated
   1.208 +    public int length() {
   1.209 +        List<A> l = this;
   1.210 +        int len = 0;
   1.211 +        while (l.tail != null) {
   1.212 +            l = l.tail;
   1.213 +            len++;
   1.214 +        }
   1.215 +        return len;
   1.216 +    }
   1.217 +    @Override
   1.218 +    public int size() {
   1.219 +        return length();
   1.220 +    }
   1.221 +
   1.222 +    public List<A> setTail(List<A> tail) {
   1.223 +        this.tail = tail;
   1.224 +        return tail;
   1.225 +    }
   1.226 +
   1.227 +    /** Prepend given element to front of list, forming and returning
   1.228 +     *  a new list.
   1.229 +     */
   1.230 +    public List<A> prepend(A x) {
   1.231 +        return new List<A>(x, this);
   1.232 +    }
   1.233 +
   1.234 +    /** Prepend given list of elements to front of list, forming and returning
   1.235 +     *  a new list.
   1.236 +     */
   1.237 +    public List<A> prependList(List<A> xs) {
   1.238 +        if (this.isEmpty()) return xs;
   1.239 +        if (xs.isEmpty()) return this;
   1.240 +        if (xs.tail.isEmpty()) return prepend(xs.head);
   1.241 +        // return this.prependList(xs.tail).prepend(xs.head);
   1.242 +        List<A> result = this;
   1.243 +        List<A> rev = xs.reverse();
   1.244 +        Assert.check(rev != xs);
   1.245 +        // since xs.reverse() returned a new list, we can reuse the
   1.246 +        // individual List objects, instead of allocating new ones.
   1.247 +        while (rev.nonEmpty()) {
   1.248 +            List<A> h = rev;
   1.249 +            rev = rev.tail;
   1.250 +            h.setTail(result);
   1.251 +            result = h;
   1.252 +        }
   1.253 +        return result;
   1.254 +    }
   1.255 +
   1.256 +    /** Reverse list.
   1.257 +     * If the list is empty or a singleton, then the same list is returned.
   1.258 +     * Otherwise a new list is formed.
   1.259 +     */
   1.260 +    public List<A> reverse() {
   1.261 +        // if it is empty or a singleton, return itself
   1.262 +        if (isEmpty() || tail.isEmpty())
   1.263 +            return this;
   1.264 +
   1.265 +        List<A> rev = nil();
   1.266 +        for (List<A> l = this; l.nonEmpty(); l = l.tail)
   1.267 +            rev = new List<A>(l.head, rev);
   1.268 +        return rev;
   1.269 +    }
   1.270 +
   1.271 +    /** Append given element at length, forming and returning
   1.272 +     *  a new list.
   1.273 +     */
   1.274 +    public List<A> append(A x) {
   1.275 +        return of(x).prependList(this);
   1.276 +    }
   1.277 +
   1.278 +    /** Append given list at length, forming and returning
   1.279 +     *  a new list.
   1.280 +     */
   1.281 +    public List<A> appendList(List<A> x) {
   1.282 +        return x.prependList(this);
   1.283 +    }
   1.284 +
   1.285 +    /**
   1.286 +     * Append given list buffer at length, forming and returning a new
   1.287 +     * list.
   1.288 +     */
   1.289 +    public List<A> appendList(ListBuffer<A> x) {
   1.290 +        return appendList(x.toList());
   1.291 +    }
   1.292 +
   1.293 +    /** Copy successive elements of this list into given vector until
   1.294 +     *  list is exhausted or end of vector is reached.
   1.295 +     */
   1.296 +    @Override @SuppressWarnings("unchecked")
   1.297 +    public <T> T[] toArray(T[] vec) {
   1.298 +        int i = 0;
   1.299 +        List<A> l = this;
   1.300 +        Object[] dest = vec;
   1.301 +        while (l.nonEmpty() && i < vec.length) {
   1.302 +            dest[i] = l.head;
   1.303 +            l = l.tail;
   1.304 +            i++;
   1.305 +        }
   1.306 +        if (l.isEmpty()) {
   1.307 +            if (i < vec.length)
   1.308 +                vec[i] = null;
   1.309 +            return vec;
   1.310 +        }
   1.311 +
   1.312 +        vec = (T[])Array.newInstance(vec.getClass().getComponentType(), size());
   1.313 +        return toArray(vec);
   1.314 +    }
   1.315 +
   1.316 +    public Object[] toArray() {
   1.317 +        return toArray(new Object[size()]);
   1.318 +    }
   1.319 +
   1.320 +    /** Form a string listing all elements with given separator character.
   1.321 +     */
   1.322 +    public String toString(String sep) {
   1.323 +        if (isEmpty()) {
   1.324 +            return "";
   1.325 +        } else {
   1.326 +            StringBuilder buf = new StringBuilder();
   1.327 +            buf.append(head);
   1.328 +            for (List<A> l = tail; l.nonEmpty(); l = l.tail) {
   1.329 +                buf.append(sep);
   1.330 +                buf.append(l.head);
   1.331 +            }
   1.332 +            return buf.toString();
   1.333 +        }
   1.334 +    }
   1.335 +
   1.336 +    /** Form a string listing all elements with comma as the separator character.
   1.337 +     */
   1.338 +    @Override
   1.339 +    public String toString() {
   1.340 +        return toString(",");
   1.341 +    }
   1.342 +
   1.343 +    /** Compute a hash code, overrides Object
   1.344 +     *  @see java.util.List#hashCode
   1.345 +     */
   1.346 +    @Override
   1.347 +    public int hashCode() {
   1.348 +        List<A> l = this;
   1.349 +        int h = 1;
   1.350 +        while (l.tail != null) {
   1.351 +            h = h * 31 + (l.head == null ? 0 : l.head.hashCode());
   1.352 +            l = l.tail;
   1.353 +        }
   1.354 +        return h;
   1.355 +    }
   1.356 +
   1.357 +    /** Is this list the same as other list?
   1.358 +     *  @see java.util.List#equals
   1.359 +     */
   1.360 +    @Override
   1.361 +    public boolean equals(Object other) {
   1.362 +        if (other instanceof List<?>)
   1.363 +            return equals(this, (List<?>)other);
   1.364 +        if (other instanceof java.util.List<?>) {
   1.365 +            List<A> t = this;
   1.366 +            Iterator<?> oIter = ((java.util.List<?>) other).iterator();
   1.367 +            while (t.tail != null && oIter.hasNext()) {
   1.368 +                Object o = oIter.next();
   1.369 +                if ( !(t.head == null ? o == null : t.head.equals(o)))
   1.370 +                    return false;
   1.371 +                t = t.tail;
   1.372 +            }
   1.373 +            return (t.isEmpty() && !oIter.hasNext());
   1.374 +        }
   1.375 +        return false;
   1.376 +    }
   1.377 +
   1.378 +    /** Are the two lists the same?
   1.379 +     */
   1.380 +    public static boolean equals(List<?> xs, List<?> ys) {
   1.381 +        while (xs.tail != null && ys.tail != null) {
   1.382 +            if (xs.head == null) {
   1.383 +                if (ys.head != null) return false;
   1.384 +            } else {
   1.385 +                if (!xs.head.equals(ys.head)) return false;
   1.386 +            }
   1.387 +            xs = xs.tail;
   1.388 +            ys = ys.tail;
   1.389 +        }
   1.390 +        return xs.tail == null && ys.tail == null;
   1.391 +    }
   1.392 +
   1.393 +    /** Does the list contain the specified element?
   1.394 +     */
   1.395 +    @Override
   1.396 +    public boolean contains(Object x) {
   1.397 +        List<A> l = this;
   1.398 +        while (l.tail != null) {
   1.399 +            if (x == null) {
   1.400 +                if (l.head == null) return true;
   1.401 +            } else {
   1.402 +                if (l.head.equals(x)) return true;
   1.403 +            }
   1.404 +            l = l.tail;
   1.405 +        }
   1.406 +        return false;
   1.407 +    }
   1.408 +
   1.409 +    /** The last element in the list, if any, or null.
   1.410 +     */
   1.411 +    public A last() {
   1.412 +        A last = null;
   1.413 +        List<A> t = this;
   1.414 +        while (t.tail != null) {
   1.415 +            last = t.head;
   1.416 +            t = t.tail;
   1.417 +        }
   1.418 +        return last;
   1.419 +    }
   1.420 +
   1.421 +    @SuppressWarnings("unchecked")
   1.422 +    public static <T> List<T> convert(Class<T> klass, List<?> list) {
   1.423 +        if (list == null)
   1.424 +            return null;
   1.425 +        for (Object o : list)
   1.426 +            klass.cast(o);
   1.427 +        return (List<T>)list;
   1.428 +    }
   1.429 +
   1.430 +    private static final Iterator<?> EMPTYITERATOR = new Iterator<Object>() {
   1.431 +            public boolean hasNext() {
   1.432 +                return false;
   1.433 +            }
   1.434 +            public Object next() {
   1.435 +                throw new java.util.NoSuchElementException();
   1.436 +            }
   1.437 +            public void remove() {
   1.438 +                throw new UnsupportedOperationException();
   1.439 +            }
   1.440 +        };
   1.441 +
   1.442 +    @SuppressWarnings("unchecked")
   1.443 +    private static <A> Iterator<A> emptyIterator() {
   1.444 +        return (Iterator<A>)EMPTYITERATOR;
   1.445 +    }
   1.446 +
   1.447 +    @Override
   1.448 +    public Iterator<A> iterator() {
   1.449 +        if (tail == null)
   1.450 +            return emptyIterator();
   1.451 +        return new Iterator<A>() {
   1.452 +            List<A> elems = List.this;
   1.453 +            public boolean hasNext() {
   1.454 +                return elems.tail != null;
   1.455 +            }
   1.456 +            public A next() {
   1.457 +                if (elems.tail == null)
   1.458 +                    throw new NoSuchElementException();
   1.459 +                A result = elems.head;
   1.460 +                elems = elems.tail;
   1.461 +                return result;
   1.462 +            }
   1.463 +            public void remove() {
   1.464 +                throw new UnsupportedOperationException();
   1.465 +            }
   1.466 +        };
   1.467 +    }
   1.468 +
   1.469 +    public A get(int index) {
   1.470 +        if (index < 0)
   1.471 +            throw new IndexOutOfBoundsException(String.valueOf(index));
   1.472 +
   1.473 +        List<A> l = this;
   1.474 +        for (int i = index; i-- > 0 && !l.isEmpty(); l = l.tail)
   1.475 +            ;
   1.476 +
   1.477 +        if (l.isEmpty())
   1.478 +            throw new IndexOutOfBoundsException("Index: " + index + ", " +
   1.479 +                                                "Size: " + size());
   1.480 +        return l.head;
   1.481 +    }
   1.482 +
   1.483 +    public boolean addAll(int index, Collection<? extends A> c) {
   1.484 +        if (c.isEmpty())
   1.485 +            return false;
   1.486 +        throw new UnsupportedOperationException();
   1.487 +    }
   1.488 +
   1.489 +    public A set(int index, A element) {
   1.490 +        throw new UnsupportedOperationException();
   1.491 +    }
   1.492 +
   1.493 +    public void add(int index, A element) {
   1.494 +        throw new UnsupportedOperationException();
   1.495 +    }
   1.496 +
   1.497 +    public A remove(int index) {
   1.498 +        throw new UnsupportedOperationException();
   1.499 +    }
   1.500 +
   1.501 +    public int indexOf(Object o) {
   1.502 +        int i = 0;
   1.503 +        for (List<A> l = this; l.tail != null; l = l.tail, i++) {
   1.504 +            if (l.head == null ? o == null : l.head.equals(o))
   1.505 +                return i;
   1.506 +        }
   1.507 +        return -1;
   1.508 +    }
   1.509 +
   1.510 +    public int lastIndexOf(Object o) {
   1.511 +        int last = -1;
   1.512 +        int i = 0;
   1.513 +        for (List<A> l = this; l.tail != null; l = l.tail, i++) {
   1.514 +            if (l.head == null ? o == null : l.head.equals(o))
   1.515 +                last = i;
   1.516 +        }
   1.517 +        return last;
   1.518 +    }
   1.519 +
   1.520 +    public ListIterator<A> listIterator() {
   1.521 +        return Collections.unmodifiableList(new ArrayList<A>(this)).listIterator();
   1.522 +    }
   1.523 +
   1.524 +    public ListIterator<A> listIterator(int index) {
   1.525 +        return Collections.unmodifiableList(new ArrayList<A>(this)).listIterator(index);
   1.526 +    }
   1.527 +
   1.528 +    public java.util.List<A> subList(int fromIndex, int toIndex) {
   1.529 +        if  (fromIndex < 0 || toIndex > size() || fromIndex > toIndex)
   1.530 +            throw new IllegalArgumentException();
   1.531 +
   1.532 +        ArrayList<A> a = new ArrayList<A>(toIndex - fromIndex);
   1.533 +        int i = 0;
   1.534 +        for (List<A> l = this; l.tail != null; l = l.tail, i++) {
   1.535 +            if (i == toIndex)
   1.536 +                break;
   1.537 +            if (i >= fromIndex)
   1.538 +                a.add(l.head);
   1.539 +        }
   1.540 +
   1.541 +        return Collections.unmodifiableList(a);
   1.542 +    }
   1.543 +}

mercurial