test/tools/javac/generics/odersky/List.java

Tue, 28 Dec 2010 15:54:52 -0800

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 554
9d9f26857129
child 2525
2eb010b6cb22
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

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

mercurial