langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/util/List.java
author mcimadamore
Fri, 16 Dec 2016 15:27:34 +0000
changeset 42827 36468b5fa7f4
parent 32708 1a5b80de4f57
child 42828 cce89649f958
permissions -rw-r--r--
8181370: Convert anonymous inner classes into lambdas/method references Reviewed-by: jjg, rfield, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15705
diff changeset
     2
 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
10
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
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1789
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1789
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
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
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1789
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1789
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1789
diff changeset
    23
 * questions.
10
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;
29554
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 28456
diff changeset
    36
import java.util.function.Function;
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 25874
diff changeset
    37
import java.util.stream.Collector;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
/** A class for generic linked lists. Links are supposed to be
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 *  immutable, the only exception being the incremental construction of
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 *  lists via ListBuffers.  List is the main container class in
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15705
diff changeset
    42
 *  GJC. Most data structures and algorithms in GJC use lists rather
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 *  than arrays.
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 *  <p>Lists are always trailed by a sentinel element, whose head and tail
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 *  are both null.
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5846
diff changeset
    48
 *  <p><b>This is NOT part of any supported API.
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5846
diff changeset
    49
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
public class List<A> extends AbstractCollection<A> implements java.util.List<A> {
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    /** The first element of the list, supposed to be immutable.
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    public A head;
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    /** The remainder of the list except for its first element, supposed
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
     *  to be immutable.
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    //@Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    public List<A> tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    /** Construct a list given its head and tail.
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    List(A head, List<A> tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        this.tail = tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        this.head = head;
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    /** Construct an empty list.
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    @SuppressWarnings("unchecked")
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    public static <A> List<A> nil() {
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 10
diff changeset
    76
        return (List<A>)EMPTY_LIST;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    }
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 10
diff changeset
    78
14801
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents: 14263
diff changeset
    79
    private static final List<?> EMPTY_LIST = new List<Object>(null,null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        public List<Object> setTail(List<Object> tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
            throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        public boolean isEmpty() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    };
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 8032
diff changeset
    88
    /** Returns the list obtained from 'l' after removing all elements 'elem'
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 8032
diff changeset
    89
     */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 8032
diff changeset
    90
    public static <A> List<A> filter(List<A> l, A elem) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 8032
diff changeset
    91
        Assert.checkNonNull(elem);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 8032
diff changeset
    92
        List<A> res = List.nil();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 8032
diff changeset
    93
        for (A a : l) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 8032
diff changeset
    94
            if (a != null && !a.equals(elem)) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 8032
diff changeset
    95
                res = res.prepend(a);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 8032
diff changeset
    96
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 8032
diff changeset
    97
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 8032
diff changeset
    98
        return res.reverse();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 8032
diff changeset
    99
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 8032
diff changeset
   100
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 14801
diff changeset
   101
    public List<A> intersect(List<A> that) {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
   102
        ListBuffer<A> buf = new ListBuffer<>();
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 14801
diff changeset
   103
        for (A el : this) {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 14801
diff changeset
   104
            if (that.contains(el)) {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 14801
diff changeset
   105
                buf.append(el);
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 14801
diff changeset
   106
            }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 14801
diff changeset
   107
        }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 14801
diff changeset
   108
        return buf.toList();
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 14801
diff changeset
   109
    }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 14801
diff changeset
   110
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 14801
diff changeset
   111
    public List<A> diff(List<A> that) {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
   112
        ListBuffer<A> buf = new ListBuffer<>();
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 14801
diff changeset
   113
        for (A el : this) {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 14801
diff changeset
   114
            if (!that.contains(el)) {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 14801
diff changeset
   115
                buf.append(el);
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 14801
diff changeset
   116
            }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 14801
diff changeset
   117
        }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 14801
diff changeset
   118
        return buf.toList();
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 14801
diff changeset
   119
    }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 14801
diff changeset
   120
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 17582
diff changeset
   121
    /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 17582
diff changeset
   122
     * Create a new list from the first {@code n} elements of this list
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 17582
diff changeset
   123
     */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 17582
diff changeset
   124
    public List<A> take(int n) {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
   125
        ListBuffer<A> buf = new ListBuffer<>();
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 17582
diff changeset
   126
        int count = 0;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 17582
diff changeset
   127
        for (A el : this) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 17582
diff changeset
   128
            if (count++ == n) break;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 17582
diff changeset
   129
            buf.append(el);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 17582
diff changeset
   130
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 17582
diff changeset
   131
        return buf.toList();
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 17582
diff changeset
   132
    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 17582
diff changeset
   133
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    /** Construct a list consisting of given element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    public static <A> List<A> of(A x1) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   137
        return new List<>(x1, List.<A>nil());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
    /** Construct a list consisting of given elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
    public static <A> List<A> of(A x1, A x2) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   143
        return new List<>(x1, of(x2));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
    /** Construct a list consisting of given elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
    public static <A> List<A> of(A x1, A x2, A x3) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   149
        return new List<>(x1, of(x2, x3));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
    /** Construct a list consisting of given elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
     */
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 5847
diff changeset
   154
    @SuppressWarnings({"varargs", "unchecked"})
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
    public static <A> List<A> of(A x1, A x2, A x3, A... rest) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   156
        return new List<>(x1, new List<>(x2, new List<>(x3, from(rest))));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
     * Construct a list consisting all elements of given array.
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
     * @param array an array; if {@code null} return an empty list
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
    public static <A> List<A> from(A[] array) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        List<A> xs = nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        if (array != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
            for (int i = array.length - 1; i >= 0; i--)
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   167
                xs = new List<>(array[i], xs);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
        return xs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 8032
diff changeset
   171
    public static <A> List<A> from(Iterable<? extends A> coll) {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
   172
        ListBuffer<A> xs = new ListBuffer<>();
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 8032
diff changeset
   173
        for (A a : coll) {
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 17578
diff changeset
   174
            xs.append(a);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 8032
diff changeset
   175
        }
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 17578
diff changeset
   176
        return xs.toList();
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 8032
diff changeset
   177
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 8032
diff changeset
   178
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
    /** Construct a list consisting of a given number of identical elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
     *  @param len    The number of elements in the list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
     *  @param init   The value of each element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
    @Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
    public static <A> List<A> fill(int len, A init) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        List<A> l = nil();
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   186
        for (int i = 0; i < len; i++) l = new List<>(init, l);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
        return l;
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
    /** Does list have no elements?
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
    @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
    public boolean isEmpty() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
        return tail == null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
    /** Does list have elements?
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
    //@Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
    public boolean nonEmpty() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
        return tail != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
    /** Return the number of elements in this list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
    //@Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
    public int length() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
        List<A> l = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
        int len = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
        while (l.tail != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
            l = l.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
            len++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
        return len;
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
    @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
    public int size() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
        return length();
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
    public List<A> setTail(List<A> tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
        this.tail = tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
        return tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
    /** Prepend given element to front of list, forming and returning
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
     *  a new list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
    public List<A> prepend(A x) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   230
        return new List<>(x, this);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
    /** Prepend given list of elements to front of list, forming and returning
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
     *  a new list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
    public List<A> prependList(List<A> xs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
        if (this.isEmpty()) return xs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
        if (xs.isEmpty()) return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
        if (xs.tail.isEmpty()) return prepend(xs.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
        // return this.prependList(xs.tail).prepend(xs.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
        List<A> result = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
        List<A> rev = xs.reverse();
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7681
diff changeset
   243
        Assert.check(rev != xs);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
        // since xs.reverse() returned a new list, we can reuse the
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
        // individual List objects, instead of allocating new ones.
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
        while (rev.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
            List<A> h = rev;
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
            rev = rev.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
            h.setTail(result);
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
            result = h;
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
    /** Reverse list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
     * If the list is empty or a singleton, then the same list is returned.
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
     * Otherwise a new list is formed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
    public List<A> reverse() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
        // if it is empty or a singleton, return itself
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
        if (isEmpty() || tail.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
        List<A> rev = nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
        for (List<A> l = this; l.nonEmpty(); l = l.tail)
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   266
            rev = new List<>(l.head, rev);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
        return rev;
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
    /** Append given element at length, forming and returning
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
     *  a new list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
    public List<A> append(A x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
        return of(x).prependList(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
    /** Append given list at length, forming and returning
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
     *  a new list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
    public List<A> appendList(List<A> x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
        return x.prependList(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
     * Append given list buffer at length, forming and returning a new
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
     * list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
    public List<A> appendList(ListBuffer<A> x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
        return appendList(x.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
    /** Copy successive elements of this list into given vector until
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
     *  list is exhausted or end of vector is reached.
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
    @Override @SuppressWarnings("unchecked")
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
    public <T> T[] toArray(T[] vec) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
        int i = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
        List<A> l = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
        Object[] dest = vec;
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
        while (l.nonEmpty() && i < vec.length) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
            dest[i] = l.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
            l = l.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
            i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
        if (l.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
            if (i < vec.length)
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
                vec[i] = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
            return vec;
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
        vec = (T[])Array.newInstance(vec.getClass().getComponentType(), size());
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
        return toArray(vec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
    public Object[] toArray() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
        return toArray(new Object[size()]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
    /** Form a string listing all elements with given separator character.
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
    public String toString(String sep) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
        if (isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
            return "";
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
        } else {
14263
473b1eaede64 8000310: Clean up use of StringBuffer in langtools
jjg
parents: 14057
diff changeset
   325
            StringBuilder buf = new StringBuilder();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
            buf.append(head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
            for (List<A> l = tail; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
                buf.append(sep);
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
                buf.append(l.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
            return buf.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
    /** Form a string listing all elements with comma as the separator character.
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 String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
        return toString(",");
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
    /** Compute a hash code, overrides Object
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
     *  @see java.util.List#hashCode
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
    @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
    public int hashCode() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
        List<A> l = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
        int h = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
        while (l.tail != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
            h = h * 31 + (l.head == null ? 0 : l.head.hashCode());
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
            l = l.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
        return h;
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
    /** Is this list the same as other list?
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
     *  @see java.util.List#equals
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
    @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
    public boolean equals(Object other) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
        if (other instanceof List<?>)
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
            return equals(this, (List<?>)other);
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
        if (other instanceof java.util.List<?>) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
            List<A> t = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
            Iterator<?> oIter = ((java.util.List<?>) other).iterator();
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
            while (t.tail != null && oIter.hasNext()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
                Object o = oIter.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
                if ( !(t.head == null ? o == null : t.head.equals(o)))
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
                t = t.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
            return (t.isEmpty() && !oIter.hasNext());
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
        }
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
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
    /** Are the two lists the same?
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
     */
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 10
diff changeset
   379
    public static boolean equals(List<?> xs, List<?> ys) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
        while (xs.tail != null && ys.tail != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
            if (xs.head == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
                if (ys.head != null) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
                if (!xs.head.equals(ys.head)) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
            xs = xs.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
            ys = ys.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
        return xs.tail == null && ys.tail == null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
    /** Does the list contain the specified element?
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
    @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
    public boolean contains(Object x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
        List<A> l = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
        while (l.tail != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
            if (x == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
                if (l.head == null) return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
                if (l.head.equals(x)) return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
            l = l.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
    /** The last element in the list, if any, or null.
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
    public A last() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
        A last = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
        List<A> t = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
        while (t.tail != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
            last = t.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
            t = t.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
        return last;
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
29556
f539d3fc9d72 8075509: List.map should return itself if list is unchanged
mcimadamore
parents: 29554
diff changeset
   420
    @SuppressWarnings("unchecked")
29554
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 28456
diff changeset
   421
    public <Z> List<Z> map(Function<A, Z> mapper) {
32708
1a5b80de4f57 8077306: Recursive implementation of List.map leads to stack overflow
mcimadamore
parents: 29556
diff changeset
   422
        boolean changed = false;
1a5b80de4f57 8077306: Recursive implementation of List.map leads to stack overflow
mcimadamore
parents: 29556
diff changeset
   423
        ListBuffer<Z> buf = new ListBuffer<>();
1a5b80de4f57 8077306: Recursive implementation of List.map leads to stack overflow
mcimadamore
parents: 29556
diff changeset
   424
        for (A a : this) {
1a5b80de4f57 8077306: Recursive implementation of List.map leads to stack overflow
mcimadamore
parents: 29556
diff changeset
   425
            Z z = mapper.apply(a);
1a5b80de4f57 8077306: Recursive implementation of List.map leads to stack overflow
mcimadamore
parents: 29556
diff changeset
   426
            buf.append(z);
1a5b80de4f57 8077306: Recursive implementation of List.map leads to stack overflow
mcimadamore
parents: 29556
diff changeset
   427
            changed |= (z != a);
29554
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 28456
diff changeset
   428
        }
32708
1a5b80de4f57 8077306: Recursive implementation of List.map leads to stack overflow
mcimadamore
parents: 29556
diff changeset
   429
        return changed ? buf.toList() : (List<Z>)this;
29554
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 28456
diff changeset
   430
    }
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 28456
diff changeset
   431
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
    @SuppressWarnings("unchecked")
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
    public static <T> List<T> convert(Class<T> klass, List<?> list) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
        if (list == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
        for (Object o : list)
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
            klass.cast(o);
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
        return (List<T>)list;
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
14801
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents: 14263
diff changeset
   441
    private static final Iterator<?> EMPTYITERATOR = new Iterator<Object>() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
            public boolean hasNext() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
            public Object next() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
                throw new java.util.NoSuchElementException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
            public void remove() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
                throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
    @SuppressWarnings("unchecked")
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
    private static <A> Iterator<A> emptyIterator() {
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 10
diff changeset
   455
        return (Iterator<A>)EMPTYITERATOR;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
    @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
    public Iterator<A> iterator() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
        if (tail == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
            return emptyIterator();
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
        return new Iterator<A>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
            List<A> elems = List.this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
            public boolean hasNext() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
                return elems.tail != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
            public A next() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
                if (elems.tail == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
                    throw new NoSuchElementException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
                A result = elems.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
                elems = elems.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
                return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
            public void remove() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
                throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
    public A get(int index) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
        if (index < 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
            throw new IndexOutOfBoundsException(String.valueOf(index));
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
        List<A> l = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
        for (int i = index; i-- > 0 && !l.isEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
            ;
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
        if (l.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
            throw new IndexOutOfBoundsException("Index: " + index + ", " +
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
                                                "Size: " + size());
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
        return l.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
    public boolean addAll(int index, Collection<? extends A> c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
        if (c.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
        throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
    public A set(int index, A element) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
        throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
    public void add(int index, A element) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
        throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
    public A remove(int index) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
        throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
    public int indexOf(Object o) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
        int i = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
        for (List<A> l = this; l.tail != null; l = l.tail, i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
            if (l.head == null ? o == null : l.head.equals(o))
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
                return i;
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
        return -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
    public int lastIndexOf(Object o) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
        int last = -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
        int i = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
        for (List<A> l = this; l.tail != null; l = l.tail, i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
            if (l.head == null ? o == null : l.head.equals(o))
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
                last = i;
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
        return last;
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
    public ListIterator<A> listIterator() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
        return Collections.unmodifiableList(new ArrayList<A>(this)).listIterator();
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
    public ListIterator<A> listIterator(int index) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
        return Collections.unmodifiableList(new ArrayList<A>(this)).listIterator(index);
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
    public java.util.List<A> subList(int fromIndex, int toIndex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
        if  (fromIndex < 0 || toIndex > size() || fromIndex > toIndex)
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
            throw new IllegalArgumentException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   543
        ArrayList<A> a = new ArrayList<>(toIndex - fromIndex);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
        int i = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
        for (List<A> l = this; l.tail != null; l = l.tail, i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
            if (i == toIndex)
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
            if (i >= fromIndex)
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
                a.add(l.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
        return Collections.unmodifiableList(a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
    }
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 25874
diff changeset
   554
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 25874
diff changeset
   555
    /**
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 25874
diff changeset
   556
     * Collect elements into a new list (using a @code{ListBuffer})
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 25874
diff changeset
   557
     */
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 25874
diff changeset
   558
    public static <Z> Collector<Z, ListBuffer<Z>, List<Z>> collector() {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 25874
diff changeset
   559
        return Collector.of(ListBuffer::new,
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 32708
diff changeset
   560
                ListBuffer::add,
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 25874
diff changeset
   561
                (buf1, buf2)-> { buf1.addAll(buf2); return buf1; },
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 32708
diff changeset
   562
                ListBuffer::toList);
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 25874
diff changeset
   563
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
}