langtools/src/share/classes/com/sun/tools/javac/util/List.java
author mcimadamore
Tue, 13 Jan 2009 13:27:14 +0000
changeset 1789 7ac8c0815000
parent 10 06bc494ca11e
child 5520 86e4b9a9da40
permissions -rw-r--r--
6765045: Remove rawtypes warnings from langtools Summary: Removed all occurrences of rawtypes warnings from langtools Reviewed-by: jjg, bpatel
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 1999-2006 Sun Microsystems, Inc.  All Rights Reserved.
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.util;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.Array;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.util.ArrayList;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.util.Collection;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.util.Collections;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.util.Iterator;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.util.AbstractCollection;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.util.ListIterator;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import java.util.NoSuchElementException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
/** A class for generic linked lists. Links are supposed to be
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 *  immutable, the only exception being the incremental construction of
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 *  lists via ListBuffers.  List is the main container class in
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 *  GJC. Most data structures and algorthms in GJC use lists rather
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 *  than arrays.
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 *  <p>Lists are always trailed by a sentinel element, whose head and tail
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 *  are both null.
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 *  you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
public class List<A> extends AbstractCollection<A> implements java.util.List<A> {
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    /** The first element of the list, supposed to be immutable.
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    public A head;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    /** The remainder of the list except for its first element, supposed
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
     *  to be immutable.
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    //@Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    public List<A> tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    /** Construct a list given its head and tail.
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    List(A head, List<A> tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        this.tail = tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
        this.head = head;
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    /** Construct an empty list.
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    @SuppressWarnings("unchecked")
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    public static <A> List<A> nil() {
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 10
diff changeset
    74
        return (List<A>)EMPTY_LIST;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    }
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 10
diff changeset
    76
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 10
diff changeset
    77
    private static List<?> EMPTY_LIST = new List<Object>(null,null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        public List<Object> setTail(List<Object> tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
            throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        public boolean isEmpty() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
    };
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    /** Construct a list consisting of given element.
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    public static <A> List<A> of(A x1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        return new List<A>(x1, List.<A>nil());
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
    /** Construct a list consisting of given elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
    public static <A> List<A> of(A x1, A x2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        return new List<A>(x1, of(x2));
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    /** Construct a list consisting of given elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    public static <A> List<A> of(A x1, A x2, A x3) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        return new List<A>(x1, of(x2, x3));
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    /** Construct a list consisting of given elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    public static <A> List<A> of(A x1, A x2, A x3, A... rest) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        return new List<A>(x1, new List<A>(x2, new List<A>(x3, from(rest))));
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     * Construct a list consisting all elements of given array.
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     * @param array an array; if {@code null} return an empty list
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    public static <A> List<A> from(A[] array) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        List<A> xs = nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        if (array != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
            for (int i = array.length - 1; i >= 0; i--)
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
                xs = new List<A>(array[i], xs);
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        return xs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    /** Construct a list consisting of a given number of identical elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
     *  @param len    The number of elements in the list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
     *  @param init   The value of each element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
    @Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    public static <A> List<A> fill(int len, A init) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        List<A> l = nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        for (int i = 0; i < len; i++) l = new List<A>(init, l);
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        return l;
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    /** Does list have no elements?
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
    @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    public boolean isEmpty() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
        return tail == null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
    /** Does list have elements?
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
    //@Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    public boolean nonEmpty() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        return tail != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
    /** Return the number of elements in this list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
    //@Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
    public int length() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        List<A> l = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        int len = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        while (l.tail != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
            l = l.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
            len++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        return len;
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
    @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
    public int size() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
        return length();
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
    public List<A> setTail(List<A> tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        this.tail = tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        return tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
    /** Prepend given element to front of list, forming and returning
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
     *  a new list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
    public List<A> prepend(A x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
        return new List<A>(x, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
    /** Prepend given list of elements to front of list, forming and returning
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
     *  a new list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
    public List<A> prependList(List<A> xs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        if (this.isEmpty()) return xs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        if (xs.isEmpty()) return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
        if (xs.tail.isEmpty()) return prepend(xs.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        // return this.prependList(xs.tail).prepend(xs.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
        List<A> result = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        List<A> rev = xs.reverse();
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
        assert rev != xs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
        // since xs.reverse() returned a new list, we can reuse the
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
        // individual List objects, instead of allocating new ones.
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
        while (rev.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
            List<A> h = rev;
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
            rev = rev.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
            h.setTail(result);
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
            result = h;
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
    /** Reverse list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
     * If the list is empty or a singleton, then the same list is returned.
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
     * Otherwise a new list is formed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
    public List<A> reverse() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
        // if it is empty or a singleton, return itself
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
        if (isEmpty() || tail.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
        List<A> rev = nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
        for (List<A> l = this; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
            rev = new List<A>(l.head, rev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
        return rev;
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
    /** Append given element at length, forming and returning
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
     *  a new list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
    public List<A> append(A x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
        return of(x).prependList(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
    /** Append given list at length, forming and returning
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
     *  a new list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
    public List<A> appendList(List<A> x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        return x.prependList(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
     * Append given list buffer at length, forming and returning a new
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
     * list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
    public List<A> appendList(ListBuffer<A> x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
        return appendList(x.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
    /** Copy successive elements of this list into given vector until
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
     *  list is exhausted or end of vector is reached.
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
    @Override @SuppressWarnings("unchecked")
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
    public <T> T[] toArray(T[] vec) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
        int i = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
        List<A> l = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
        Object[] dest = vec;
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
        while (l.nonEmpty() && i < vec.length) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
            dest[i] = l.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
            l = l.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
            i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
        if (l.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
            if (i < vec.length)
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
                vec[i] = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
            return vec;
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
        vec = (T[])Array.newInstance(vec.getClass().getComponentType(), size());
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
        return toArray(vec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
    public Object[] toArray() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
        return toArray(new Object[size()]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
    /** Form a string listing all elements with given separator character.
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
    public String toString(String sep) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
        if (isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
            return "";
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
            StringBuffer buf = new StringBuffer();
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
            buf.append(head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
            for (List<A> l = tail; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
                buf.append(sep);
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
                buf.append(l.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
            return buf.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
    /** Form a string listing all elements with comma as the separator character.
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
    @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
    public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
        return toString(",");
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
    /** Compute a hash code, overrides Object
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
     *  @see java.util.List#hashCode
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
    @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
    public int hashCode() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
        List<A> l = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
        int h = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
        while (l.tail != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
            h = h * 31 + (l.head == null ? 0 : l.head.hashCode());
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
            l = l.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
        return h;
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
    /** Is this list the same as other list?
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
     *  @see java.util.List#equals
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
    @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
    public boolean equals(Object other) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
        if (other instanceof List<?>)
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
            return equals(this, (List<?>)other);
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
        if (other instanceof java.util.List<?>) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
            List<A> t = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
            Iterator<?> oIter = ((java.util.List<?>) other).iterator();
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
            while (t.tail != null && oIter.hasNext()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
                Object o = oIter.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
                if ( !(t.head == null ? o == null : t.head.equals(o)))
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
                t = t.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
            return (t.isEmpty() && !oIter.hasNext());
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
    /** Are the two lists the same?
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
     */
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 10
diff changeset
   322
    public static boolean equals(List<?> xs, List<?> ys) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
        while (xs.tail != null && ys.tail != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
            if (xs.head == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
                if (ys.head != null) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
                if (!xs.head.equals(ys.head)) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
            xs = xs.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
            ys = ys.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
        return xs.tail == null && ys.tail == null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
    /** Does the list contain the specified element?
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
    @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
    public boolean contains(Object x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
        List<A> l = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
        while (l.tail != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
            if (x == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
                if (l.head == null) return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
                if (l.head.equals(x)) return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
            l = l.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
    /** The last element in the list, if any, or null.
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
    public A last() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
        A last = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
        List<A> t = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
        while (t.tail != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
            last = t.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
            t = t.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
        return last;
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
    @SuppressWarnings("unchecked")
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
    public static <T> List<T> convert(Class<T> klass, List<?> list) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
        if (list == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
        for (Object o : list)
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
            klass.cast(o);
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
        return (List<T>)list;
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 10
diff changeset
   372
    private static Iterator<?> EMPTYITERATOR = new Iterator<Object>() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
            public boolean hasNext() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
            public Object next() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
                throw new java.util.NoSuchElementException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
            public void remove() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
                throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
    @SuppressWarnings("unchecked")
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
    private static <A> Iterator<A> emptyIterator() {
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 10
diff changeset
   386
        return (Iterator<A>)EMPTYITERATOR;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
    @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
    public Iterator<A> iterator() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
        if (tail == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
            return emptyIterator();
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
        return new Iterator<A>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
            List<A> elems = List.this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
            public boolean hasNext() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
                return elems.tail != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
            public A next() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
                if (elems.tail == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
                    throw new NoSuchElementException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
                A result = elems.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
                elems = elems.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
                return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
            public void remove() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
                throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
    public A get(int index) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
        if (index < 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
            throw new IndexOutOfBoundsException(String.valueOf(index));
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
        List<A> l = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
        for (int i = index; i-- > 0 && !l.isEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
            ;
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
        if (l.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
            throw new IndexOutOfBoundsException("Index: " + index + ", " +
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
                                                "Size: " + size());
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
        return l.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
    public boolean addAll(int index, Collection<? extends A> c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
        if (c.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
        throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
    public A set(int index, A element) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
        throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
    public void add(int index, A element) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
        throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
    public A remove(int index) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
        throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
    public int indexOf(Object o) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
        int i = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
        for (List<A> l = this; l.tail != null; l = l.tail, i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
            if (l.head == null ? o == null : l.head.equals(o))
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
                return i;
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
        return -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
    public int lastIndexOf(Object o) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
        int last = -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
        int i = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
        for (List<A> l = this; l.tail != null; l = l.tail, i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
            if (l.head == null ? o == null : l.head.equals(o))
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
                last = i;
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
        return last;
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
    public ListIterator<A> listIterator() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
        return Collections.unmodifiableList(new ArrayList<A>(this)).listIterator();
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
    public ListIterator<A> listIterator(int index) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
        return Collections.unmodifiableList(new ArrayList<A>(this)).listIterator(index);
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
    public java.util.List<A> subList(int fromIndex, int toIndex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
        if  (fromIndex < 0 || toIndex > size() || fromIndex > toIndex)
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
            throw new IllegalArgumentException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
        ArrayList<A> a = new ArrayList<A>(toIndex - fromIndex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
        int i = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
        for (List<A> l = this; l.tail != null; l = l.tail, i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
            if (i == toIndex)
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
            if (i >= fromIndex)
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
                a.add(l.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
        return Collections.unmodifiableList(a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
}