langtools/test/tools/javac/generics/odersky/List.java
author never
Fri, 25 Mar 2011 18:50:10 -0700
changeset 8867 6febcc945802
parent 5520 86e4b9a9da40
permissions -rw-r--r--
7022204: LogFile wildcarding should use %p instead of star Reviewed-by: coleenp, jrose
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     2
 * Copyright (c) 2002, 2003, 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
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    21
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
class List<A> {
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
    /** The first element of the list, supposed to be immutable.
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
    public A head;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
    /** The remainder of the list except for its first element, supposed
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
     *  to be immutable.
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
    public List<A> tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
    /** Construct a list given its head and tail.
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
    public List(A head, List<A> tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
        this.tail = tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
        this.head = head;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
    /** Construct an empty list.
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
    public List() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
        this(null, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    /** Construct a list consisting of given element.
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    public static <A> List<A> make(A x1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
        return new List<A>(x1, new List<A>());
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    /** Construct a list consisting of given elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    public static <A> List<A> make(A x1, A x2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        return new List<A>(x1, new List<A>(x2, new List<A>()));
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    /** Construct a list consisting of given elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    public static <A> List<A> make(A x1, A x2, A x3) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
        return new List<A>(x1, new List<A>(x2, new List<A>(x3, new List<A>())));
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    /** Construct a list consisting all elements of given array.
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    public static <A> List<A> make(A[] vec) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        List<A> xs = new List<A>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
        for (int i = vec.length - 1; i >= 0; i--)
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
            xs = new List<A>(vec[i], xs);
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
        return xs;
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    /** Construct a list consisting of a given number of identical elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
     *  @param len    The number of elements in the list.
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
     *  @param init   The value of each element.
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    public static <A> List<A> make(int len, A init) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        List<A> l = new List<A>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        for (int i = 0; i < len; i++) l = new List<A>(init, l);
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        return l;
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    /** Does list have no elements?
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    public boolean isEmpty() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        return tail == null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    /** Does list have elements?
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    public boolean nonEmpty() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        return tail != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    /** Return the number of elements in this list.
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
    public int length() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        List<A> l = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        int len = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        while (l.tail != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
            l = l.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
            len++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        return len;
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
    /** Prepend given element to front of list, forming and returning
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
     *  a new list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    public List<A> prepend(A x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        return new List<A>(x, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
    /** Prepend given list of elements to front of list, forming and returning
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
     *  a new list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
    public List<A> prependList(List<A> xs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        if (this.isEmpty()) return xs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        else if (xs.isEmpty()) return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        else return this.prependList(xs.tail).prepend(xs.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
    /** Reverse list, forming and returning a new list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    public List<A> reverse() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        List<A> rev = new List<A>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        for (List<A> l = this; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
            rev = new List<A>(l.head, rev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        return rev;
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    /** Append given element at length, forming and returning
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
     *  a new list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
    public List<A> append(A x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
        return make(x).prependList(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
    /** Append given list at length, forming and returning
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
     *  a new list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
    public List<A> appendList(List<A> x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        return x.prependList(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
    /** Copy successive elements of this list into given vector until
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
     *  list is exhausted or end of vector is reached.
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
    public A[] toArray(A[] vec) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        int i = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        List<A> l = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        while (l.nonEmpty() && i < vec.length) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
            vec[i] = l.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
            l = l.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
            i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
        return vec;
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
    /** Form a string listing all elements with given separator character.
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
    public String toString(String sep) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        if (isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
            return "";
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
            StringBuffer buf = new StringBuffer();
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
            buf.append(((Object)head).toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
            for (List<A> l = tail; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
                buf.append(sep);
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
                buf.append(((Object)l.head).toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
            return buf.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
    /** Form a string listing all elements with comma as the separator character.
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
    public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        return toString(",");
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
    /** Compute a hash code, overrides Object
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
    public int hashCode() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
        List<A> l = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
        int h = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
        while (l.tail != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
            h = h * 41 + (head != null ? head.hashCode() : 0);
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
            l = l.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
        return h;
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
    /** Is this list the same as other list?
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
    public boolean equals(Object other) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
        return other instanceof List && equals(this, (List)other);
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
    /** Are the two lists the same?
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
    public static boolean equals(List xs, List ys) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
        while (xs.tail != null && ys.tail != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
            if (xs.head == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
                if (ys.head != null) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
                if (!xs.head.equals(ys.head)) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
            xs = xs.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
            ys = ys.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
        return xs.tail == null && ys.tail == null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
    /** Does the list contain the specified element?
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
    public boolean contains(A x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
        List<A> l = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
        while (l.tail != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
            if (x == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
                if (l.head == null) return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
                if (x.equals(l.head)) return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
            l = l.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
}