src/share/classes/com/sun/tools/javac/util/List.java

Tue, 25 May 2010 15:54:51 -0700

author
ohair
date
Tue, 25 May 2010 15:54:51 -0700
changeset 554
9d9f26857129
parent 184
905e151a185a
child 580
46cf751559ae
permissions
-rw-r--r--

6943119: Rebrand source copyright notices
Reviewed-by: darcy

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

mercurial