0N/A/*
553N/A * Copyright (c) 2002, 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/Aclass List<A> {
0N/A
0N/A /** The first element of the list, supposed to be immutable.
0N/A */
0N/A public A head;
0N/A
0N/A /** The remainder of the list except for its first element, supposed
0N/A * to be immutable.
0N/A */
0N/A public List<A> tail;
0N/A
0N/A /** Construct a list given its head and tail.
0N/A */
0N/A public List(A head, List<A> tail) {
0N/A this.tail = tail;
0N/A this.head = head;
0N/A }
0N/A
0N/A /** Construct an empty list.
0N/A */
0N/A public List() {
0N/A this(null, null);
0N/A }
0N/A
0N/A /** Construct a list consisting of given element.
0N/A */
0N/A public static <A> List<A> make(A x1) {
0N/A return new List<A>(x1, new List<A>());
0N/A }
0N/A
0N/A /** Construct a list consisting of given elements.
0N/A */
0N/A public static <A> List<A> make(A x1, A x2) {
0N/A return new List<A>(x1, new List<A>(x2, new List<A>()));
0N/A }
0N/A
0N/A /** Construct a list consisting of given elements.
0N/A */
0N/A public static <A> List<A> make(A x1, A x2, A x3) {
0N/A return new List<A>(x1, new List<A>(x2, new List<A>(x3, new List<A>())));
0N/A }
0N/A
0N/A /** Construct a list consisting all elements of given array.
0N/A */
0N/A public static <A> List<A> make(A[] vec) {
0N/A List<A> xs = new List<A>();
0N/A for (int i = vec.length - 1; i >= 0; i--)
0N/A xs = new List<A>(vec[i], xs);
0N/A return xs;
0N/A }
0N/A
0N/A /** Construct a list consisting of a given number of identical elements.
0N/A * @param len The number of elements in the list.
0N/A * @param init The value of each element.
0N/A */
0N/A public static <A> List<A> make(int len, A init) {
0N/A List<A> l = new List<A>();
0N/A for (int i = 0; i < len; i++) l = new List<A>(init, l);
0N/A return l;
0N/A }
0N/A
0N/A /** Does list have no elements?
0N/A */
0N/A public boolean isEmpty() {
0N/A return tail == null;
0N/A }
0N/A
0N/A /** Does list have elements?
0N/A */
0N/A public boolean nonEmpty() {
0N/A return tail != null;
0N/A }
0N/A
0N/A /** Return the number of elements in this list.
0N/A */
0N/A public int length() {
0N/A List<A> l = this;
0N/A int len = 0;
0N/A while (l.tail != null) {
0N/A l = l.tail;
0N/A len++;
0N/A }
0N/A return len;
0N/A }
0N/A
0N/A /** Prepend given element to front of list, forming and returning
0N/A * a new list.
0N/A */
0N/A public List<A> prepend(A x) {
0N/A return new List<A>(x, this);
0N/A }
0N/A
0N/A /** Prepend given list of elements to front of list, forming and returning
0N/A * a new list.
0N/A */
0N/A public List<A> prependList(List<A> xs) {
0N/A if (this.isEmpty()) return xs;
0N/A else if (xs.isEmpty()) return this;
0N/A else return this.prependList(xs.tail).prepend(xs.head);
0N/A }
0N/A
0N/A /** Reverse list, forming and returning a new list.
0N/A */
0N/A public List<A> reverse() {
0N/A List<A> rev = new List<A>();
0N/A for (List<A> l = this; l.nonEmpty(); l = l.tail)
0N/A rev = new List<A>(l.head, rev);
0N/A return rev;
0N/A }
0N/A
0N/A /** Append given element at length, forming and returning
0N/A * a new list.
0N/A */
0N/A public List<A> append(A x) {
0N/A return make(x).prependList(this);
0N/A }
0N/A
0N/A /** Append given list at length, forming and returning
0N/A * a new list.
0N/A */
0N/A public List<A> appendList(List<A> x) {
0N/A return x.prependList(this);
0N/A }
0N/A
0N/A /** Copy successive elements of this list into given vector until
0N/A * list is exhausted or end of vector is reached.
0N/A */
0N/A public A[] toArray(A[] vec) {
0N/A int i = 0;
0N/A List<A> l = this;
0N/A while (l.nonEmpty() && i < vec.length) {
0N/A vec[i] = l.head;
0N/A l = l.tail;
0N/A i++;
0N/A }
0N/A return vec;
0N/A }
0N/A
0N/A /** Form a string listing all elements with given separator character.
0N/A */
0N/A public String toString(String sep) {
0N/A if (isEmpty()) {
0N/A return "";
0N/A } else {
0N/A StringBuffer buf = new StringBuffer();
0N/A buf.append(((Object)head).toString());
0N/A for (List<A> l = tail; l.nonEmpty(); l = l.tail) {
0N/A buf.append(sep);
0N/A buf.append(((Object)l.head).toString());
0N/A }
0N/A return buf.toString();
0N/A }
0N/A }
0N/A
0N/A /** Form a string listing all elements with comma as the separator character.
0N/A */
0N/A public String toString() {
0N/A return toString(",");
0N/A }
0N/A
0N/A /** Compute a hash code, overrides Object
0N/A */
0N/A public int hashCode() {
0N/A List<A> l = this;
0N/A int h = 0;
0N/A while (l.tail != null) {
0N/A h = h * 41 + (head != null ? head.hashCode() : 0);
0N/A l = l.tail;
0N/A }
0N/A return h;
0N/A }
0N/A
0N/A /** Is this list the same as other list?
0N/A */
0N/A public boolean equals(Object other) {
0N/A return other instanceof List && equals(this, (List)other);
0N/A }
0N/A
0N/A /** Are the two lists the same?
0N/A */
0N/A public static boolean equals(List xs, List ys) {
0N/A while (xs.tail != null && ys.tail != null) {
0N/A if (xs.head == null) {
0N/A if (ys.head != null) return false;
0N/A } else {
0N/A if (!xs.head.equals(ys.head)) return false;
0N/A }
0N/A xs = xs.tail;
0N/A ys = ys.tail;
0N/A }
0N/A return xs.tail == null && ys.tail == null;
0N/A }
0N/A
0N/A /** Does the list contain the specified element?
0N/A */
0N/A public boolean contains(A x) {
0N/A List<A> l = this;
0N/A while (l.tail != null) {
0N/A if (x == null) {
0N/A if (l.head == null) return true;
0N/A } else {
0N/A if (x.equals(l.head)) return true;
0N/A }
0N/A l = l.tail;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A}