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

Wed, 06 Feb 2013 14:03:39 +0000

author
mcimadamore
date
Wed, 06 Feb 2013 14:03:39 +0000
changeset 1550
1df20330f6bd
parent 1442
fcf89720ae71
child 1755
ddb4a2bfcd82
permissions
-rw-r--r--

8007463: Cleanup inference related classes
Summary: Make Infer.InferenceContext an inner class; adjust bound replacement logic in Type.UndetVar
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 1999, 2012, 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 supported API.
    47  *  If 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 final 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     /** Returns the list obtained from 'l' after removing all elements 'elem'
    87      */
    88     public static <A> List<A> filter(List<A> l, A elem) {
    89         Assert.checkNonNull(elem);
    90         List<A> res = List.nil();
    91         for (A a : l) {
    92             if (a != null && !a.equals(elem)) {
    93                 res = res.prepend(a);
    94             }
    95         }
    96         return res.reverse();
    97     }
    99     public List<A> intersect(List<A> that) {
   100         ListBuffer<A> buf = ListBuffer.lb();
   101         for (A el : this) {
   102             if (that.contains(el)) {
   103                 buf.append(el);
   104             }
   105         }
   106         return buf.toList();
   107     }
   109     public List<A> diff(List<A> that) {
   110         ListBuffer<A> buf = ListBuffer.lb();
   111         for (A el : this) {
   112             if (!that.contains(el)) {
   113                 buf.append(el);
   114             }
   115         }
   116         return buf.toList();
   117     }
   119     /** Construct a list consisting of given element.
   120      */
   121     public static <A> List<A> of(A x1) {
   122         return new List<A>(x1, List.<A>nil());
   123     }
   125     /** Construct a list consisting of given elements.
   126      */
   127     public static <A> List<A> of(A x1, A x2) {
   128         return new List<A>(x1, of(x2));
   129     }
   131     /** Construct a list consisting of given elements.
   132      */
   133     public static <A> List<A> of(A x1, A x2, A x3) {
   134         return new List<A>(x1, of(x2, x3));
   135     }
   137     /** Construct a list consisting of given elements.
   138      */
   139     @SuppressWarnings({"varargs", "unchecked"})
   140     public static <A> List<A> of(A x1, A x2, A x3, A... rest) {
   141         return new List<A>(x1, new List<A>(x2, new List<A>(x3, from(rest))));
   142     }
   144     /**
   145      * Construct a list consisting all elements of given array.
   146      * @param array an array; if {@code null} return an empty list
   147      */
   148     public static <A> List<A> from(A[] array) {
   149         List<A> xs = nil();
   150         if (array != null)
   151             for (int i = array.length - 1; i >= 0; i--)
   152                 xs = new List<A>(array[i], xs);
   153         return xs;
   154     }
   156     public static <A> List<A> from(Iterable<? extends A> coll) {
   157         List<A> xs = nil();
   158         for (A a : coll) {
   159             xs = new List<A>(a, xs);
   160         }
   161         return xs;
   162     }
   164     /** Construct a list consisting of a given number of identical elements.
   165      *  @param len    The number of elements in the list.
   166      *  @param init   The value of each element.
   167      */
   168     @Deprecated
   169     public static <A> List<A> fill(int len, A init) {
   170         List<A> l = nil();
   171         for (int i = 0; i < len; i++) l = new List<A>(init, l);
   172         return l;
   173     }
   175     /** Does list have no elements?
   176      */
   177     @Override
   178     public boolean isEmpty() {
   179         return tail == null;
   180     }
   182     /** Does list have elements?
   183      */
   184     //@Deprecated
   185     public boolean nonEmpty() {
   186         return tail != null;
   187     }
   189     /** Return the number of elements in this list.
   190      */
   191     //@Deprecated
   192     public int length() {
   193         List<A> l = this;
   194         int len = 0;
   195         while (l.tail != null) {
   196             l = l.tail;
   197             len++;
   198         }
   199         return len;
   200     }
   201     @Override
   202     public int size() {
   203         return length();
   204     }
   206     public List<A> setTail(List<A> tail) {
   207         this.tail = tail;
   208         return tail;
   209     }
   211     /** Prepend given element to front of list, forming and returning
   212      *  a new list.
   213      */
   214     public List<A> prepend(A x) {
   215         return new List<A>(x, this);
   216     }
   218     /** Prepend given list of elements to front of list, forming and returning
   219      *  a new list.
   220      */
   221     public List<A> prependList(List<A> xs) {
   222         if (this.isEmpty()) return xs;
   223         if (xs.isEmpty()) return this;
   224         if (xs.tail.isEmpty()) return prepend(xs.head);
   225         // return this.prependList(xs.tail).prepend(xs.head);
   226         List<A> result = this;
   227         List<A> rev = xs.reverse();
   228         Assert.check(rev != xs);
   229         // since xs.reverse() returned a new list, we can reuse the
   230         // individual List objects, instead of allocating new ones.
   231         while (rev.nonEmpty()) {
   232             List<A> h = rev;
   233             rev = rev.tail;
   234             h.setTail(result);
   235             result = h;
   236         }
   237         return result;
   238     }
   240     /** Reverse list.
   241      * If the list is empty or a singleton, then the same list is returned.
   242      * Otherwise a new list is formed.
   243      */
   244     public List<A> reverse() {
   245         // if it is empty or a singleton, return itself
   246         if (isEmpty() || tail.isEmpty())
   247             return this;
   249         List<A> rev = nil();
   250         for (List<A> l = this; l.nonEmpty(); l = l.tail)
   251             rev = new List<A>(l.head, rev);
   252         return rev;
   253     }
   255     /** Append given element at length, forming and returning
   256      *  a new list.
   257      */
   258     public List<A> append(A x) {
   259         return of(x).prependList(this);
   260     }
   262     /** Append given list at length, forming and returning
   263      *  a new list.
   264      */
   265     public List<A> appendList(List<A> x) {
   266         return x.prependList(this);
   267     }
   269     /**
   270      * Append given list buffer at length, forming and returning a new
   271      * list.
   272      */
   273     public List<A> appendList(ListBuffer<A> x) {
   274         return appendList(x.toList());
   275     }
   277     /** Copy successive elements of this list into given vector until
   278      *  list is exhausted or end of vector is reached.
   279      */
   280     @Override @SuppressWarnings("unchecked")
   281     public <T> T[] toArray(T[] vec) {
   282         int i = 0;
   283         List<A> l = this;
   284         Object[] dest = vec;
   285         while (l.nonEmpty() && i < vec.length) {
   286             dest[i] = l.head;
   287             l = l.tail;
   288             i++;
   289         }
   290         if (l.isEmpty()) {
   291             if (i < vec.length)
   292                 vec[i] = null;
   293             return vec;
   294         }
   296         vec = (T[])Array.newInstance(vec.getClass().getComponentType(), size());
   297         return toArray(vec);
   298     }
   300     public Object[] toArray() {
   301         return toArray(new Object[size()]);
   302     }
   304     /** Form a string listing all elements with given separator character.
   305      */
   306     public String toString(String sep) {
   307         if (isEmpty()) {
   308             return "";
   309         } else {
   310             StringBuilder buf = new StringBuilder();
   311             buf.append(head);
   312             for (List<A> l = tail; l.nonEmpty(); l = l.tail) {
   313                 buf.append(sep);
   314                 buf.append(l.head);
   315             }
   316             return buf.toString();
   317         }
   318     }
   320     /** Form a string listing all elements with comma as the separator character.
   321      */
   322     @Override
   323     public String toString() {
   324         return toString(",");
   325     }
   327     /** Compute a hash code, overrides Object
   328      *  @see java.util.List#hashCode
   329      */
   330     @Override
   331     public int hashCode() {
   332         List<A> l = this;
   333         int h = 1;
   334         while (l.tail != null) {
   335             h = h * 31 + (l.head == null ? 0 : l.head.hashCode());
   336             l = l.tail;
   337         }
   338         return h;
   339     }
   341     /** Is this list the same as other list?
   342      *  @see java.util.List#equals
   343      */
   344     @Override
   345     public boolean equals(Object other) {
   346         if (other instanceof List<?>)
   347             return equals(this, (List<?>)other);
   348         if (other instanceof java.util.List<?>) {
   349             List<A> t = this;
   350             Iterator<?> oIter = ((java.util.List<?>) other).iterator();
   351             while (t.tail != null && oIter.hasNext()) {
   352                 Object o = oIter.next();
   353                 if ( !(t.head == null ? o == null : t.head.equals(o)))
   354                     return false;
   355                 t = t.tail;
   356             }
   357             return (t.isEmpty() && !oIter.hasNext());
   358         }
   359         return false;
   360     }
   362     /** Are the two lists the same?
   363      */
   364     public static boolean equals(List<?> xs, List<?> ys) {
   365         while (xs.tail != null && ys.tail != null) {
   366             if (xs.head == null) {
   367                 if (ys.head != null) return false;
   368             } else {
   369                 if (!xs.head.equals(ys.head)) return false;
   370             }
   371             xs = xs.tail;
   372             ys = ys.tail;
   373         }
   374         return xs.tail == null && ys.tail == null;
   375     }
   377     /** Does the list contain the specified element?
   378      */
   379     @Override
   380     public boolean contains(Object x) {
   381         List<A> l = this;
   382         while (l.tail != null) {
   383             if (x == null) {
   384                 if (l.head == null) return true;
   385             } else {
   386                 if (l.head.equals(x)) return true;
   387             }
   388             l = l.tail;
   389         }
   390         return false;
   391     }
   393     /** The last element in the list, if any, or null.
   394      */
   395     public A last() {
   396         A last = null;
   397         List<A> t = this;
   398         while (t.tail != null) {
   399             last = t.head;
   400             t = t.tail;
   401         }
   402         return last;
   403     }
   405     @SuppressWarnings("unchecked")
   406     public static <T> List<T> convert(Class<T> klass, List<?> list) {
   407         if (list == null)
   408             return null;
   409         for (Object o : list)
   410             klass.cast(o);
   411         return (List<T>)list;
   412     }
   414     private static final Iterator<?> EMPTYITERATOR = new Iterator<Object>() {
   415             public boolean hasNext() {
   416                 return false;
   417             }
   418             public Object next() {
   419                 throw new java.util.NoSuchElementException();
   420             }
   421             public void remove() {
   422                 throw new UnsupportedOperationException();
   423             }
   424         };
   426     @SuppressWarnings("unchecked")
   427     private static <A> Iterator<A> emptyIterator() {
   428         return (Iterator<A>)EMPTYITERATOR;
   429     }
   431     @Override
   432     public Iterator<A> iterator() {
   433         if (tail == null)
   434             return emptyIterator();
   435         return new Iterator<A>() {
   436             List<A> elems = List.this;
   437             public boolean hasNext() {
   438                 return elems.tail != null;
   439             }
   440             public A next() {
   441                 if (elems.tail == null)
   442                     throw new NoSuchElementException();
   443                 A result = elems.head;
   444                 elems = elems.tail;
   445                 return result;
   446             }
   447             public void remove() {
   448                 throw new UnsupportedOperationException();
   449             }
   450         };
   451     }
   453     public A get(int index) {
   454         if (index < 0)
   455             throw new IndexOutOfBoundsException(String.valueOf(index));
   457         List<A> l = this;
   458         for (int i = index; i-- > 0 && !l.isEmpty(); l = l.tail)
   459             ;
   461         if (l.isEmpty())
   462             throw new IndexOutOfBoundsException("Index: " + index + ", " +
   463                                                 "Size: " + size());
   464         return l.head;
   465     }
   467     public boolean addAll(int index, Collection<? extends A> c) {
   468         if (c.isEmpty())
   469             return false;
   470         throw new UnsupportedOperationException();
   471     }
   473     public A set(int index, A element) {
   474         throw new UnsupportedOperationException();
   475     }
   477     public void add(int index, A element) {
   478         throw new UnsupportedOperationException();
   479     }
   481     public A remove(int index) {
   482         throw new UnsupportedOperationException();
   483     }
   485     public int indexOf(Object o) {
   486         int i = 0;
   487         for (List<A> l = this; l.tail != null; l = l.tail, i++) {
   488             if (l.head == null ? o == null : l.head.equals(o))
   489                 return i;
   490         }
   491         return -1;
   492     }
   494     public int lastIndexOf(Object o) {
   495         int last = -1;
   496         int i = 0;
   497         for (List<A> l = this; l.tail != null; l = l.tail, i++) {
   498             if (l.head == null ? o == null : l.head.equals(o))
   499                 last = i;
   500         }
   501         return last;
   502     }
   504     public ListIterator<A> listIterator() {
   505         return Collections.unmodifiableList(new ArrayList<A>(this)).listIterator();
   506     }
   508     public ListIterator<A> listIterator(int index) {
   509         return Collections.unmodifiableList(new ArrayList<A>(this)).listIterator(index);
   510     }
   512     public java.util.List<A> subList(int fromIndex, int toIndex) {
   513         if  (fromIndex < 0 || toIndex > size() || fromIndex > toIndex)
   514             throw new IllegalArgumentException();
   516         ArrayList<A> a = new ArrayList<A>(toIndex - fromIndex);
   517         int i = 0;
   518         for (List<A> l = this; l.tail != null; l = l.tail, i++) {
   519             if (i == toIndex)
   520                 break;
   521             if (i >= fromIndex)
   522                 a.add(l.head);
   523         }
   525         return Collections.unmodifiableList(a);
   526     }
   527 }

mercurial