EMMA Coverage Report (generated Thu Dec 06 15:24:05 GMT 2007)
[all classes][com.sun.tools.javac.util]

COVERAGE SUMMARY FOR SOURCE FILE [List.java]

nameclass, %method, %block, %line, %
List.java100% (4/4)49%  (26/53)37%  (289/779)43%  (75/174)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class List100% (1/1)45%  (19/42)34%  (244/714)42%  (67/161)
add (int, Object): void 0%   (0/1)0%   (0/4)0%   (0/1)
addAll (int, Collection): boolean 0%   (0/1)0%   (0/9)0%   (0/3)
appendList (List): List 0%   (0/1)0%   (0/4)0%   (0/1)
appendList (ListBuffer): List 0%   (0/1)0%   (0/5)0%   (0/1)
contains (Object): boolean 0%   (0/1)0%   (0/25)0%   (0/7)
convert (Class, List): List 0%   (0/1)0%   (0/20)0%   (0/5)
equals (List, List): boolean 0%   (0/1)0%   (0/39)0%   (0/7)
equals (Object): boolean 0%   (0/1)0%   (0/55)0%   (0/13)
fill (int, Object): List 0%   (0/1)0%   (0/17)0%   (0/3)
from (Object []): List 0%   (0/1)0%   (0/23)0%   (0/5)
get (int): Object 0%   (0/1)0%   (0/47)0%   (0/7)
hashCode (): int 0%   (0/1)0%   (0/26)0%   (0/6)
indexOf (Object): int 0%   (0/1)0%   (0/27)0%   (0/5)
lastIndexOf (Object): int 0%   (0/1)0%   (0/29)0%   (0/6)
listIterator (): ListIterator 0%   (0/1)0%   (0/7)0%   (0/1)
listIterator (int): ListIterator 0%   (0/1)0%   (0/8)0%   (0/1)
of (Object, Object, Object): List 0%   (0/1)0%   (0/8)0%   (0/1)
of (Object, Object, Object, Object []): List 0%   (0/1)0%   (0/15)0%   (0/1)
remove (int): Object 0%   (0/1)0%   (0/4)0%   (0/1)
set (int, Object): Object 0%   (0/1)0%   (0/4)0%   (0/1)
size (): int 0%   (0/1)0%   (0/3)0%   (0/1)
subList (int, int): List 0%   (0/1)0%   (0/47)0%   (0/10)
toArray (): Object [] 0%   (0/1)0%   (0/6)0%   (0/1)
toString (String): String 100% (1/1)64%  (23/36)69%  (5.5/8)
toArray (Object []): Object [] 100% (1/1)65%  (32/49)77%  (10/13)
prependList (List): List 100% (1/1)86%  (43/50)96%  (12.5/13)
<static initializer> 100% (1/1)94%  (17/18)98%  (2.9/3)
List (Object, List): void 100% (1/1)100% (9/9)100% (4/4)
append (Object): List 100% (1/1)100% (5/5)100% (1/1)
emptyIterator (): Iterator 100% (1/1)100% (2/2)100% (1/1)
isEmpty (): boolean 100% (1/1)100% (7/7)100% (1/1)
iterator (): Iterator 100% (1/1)100% (10/10)100% (3/3)
last (): Object 100% (1/1)100% (16/16)100% (6/6)
length (): int 100% (1/1)100% (14/14)100% (6/6)
nil (): List 100% (1/1)100% (2/2)100% (1/1)
nonEmpty (): boolean 100% (1/1)100% (7/7)100% (1/1)
of (Object): List 100% (1/1)100% (6/6)100% (1/1)
of (Object, Object): List 100% (1/1)100% (7/7)100% (1/1)
prepend (Object): List 100% (1/1)100% (6/6)100% (1/1)
reverse (): List 100% (1/1)100% (29/29)100% (6/6)
setTail (List): List 100% (1/1)100% (5/5)100% (2/2)
toString (): String 100% (1/1)100% (4/4)100% (1/1)
     
class List$2100% (1/1)50%  (2/4)38%  (5/13)50%  (2/4)
next (): Object 0%   (0/1)0%   (0/4)0%   (0/1)
remove (): void 0%   (0/1)0%   (0/4)0%   (0/1)
List$2 (): void 100% (1/1)100% (3/3)100% (1/1)
hasNext (): boolean 100% (1/1)100% (2/2)100% (1/1)
     
class List$1100% (1/1)67%  (2/3)64%  (7/11)67%  (2/3)
setTail (List): List 0%   (0/1)0%   (0/4)0%   (0/1)
List$1 (Object, List): void 100% (1/1)100% (5/5)100% (1/1)
isEmpty (): boolean 100% (1/1)100% (2/2)100% (1/1)
     
class List$3100% (1/1)75%  (3/4)80%  (33/41)78%  (7/9)
remove (): void 0%   (0/1)0%   (0/4)0%   (0/1)
next (): Object 100% (1/1)79%  (15/19)80%  (4/5)
List$3 (List): void 100% (1/1)100% (10/10)100% (2/2)
hasNext (): boolean 100% (1/1)100% (8/8)100% (1/1)

1/*
2 * Copyright 1999-2006 Sun Microsystems, Inc.  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.  Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25 
26package com.sun.tools.javac.util;
27 
28import java.lang.reflect.Array;
29import java.util.ArrayList;
30import java.util.Collection;
31import java.util.Collections;
32import java.util.Iterator;
33import java.util.AbstractCollection;
34import java.util.ListIterator;
35import java.util.NoSuchElementException;
36 
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 */
51public class List<A> extends AbstractCollection<A> implements java.util.List<A> {
52 
53    /** The first element of the list, supposed to be immutable.
54     */
55    public A head;
56 
57    /** The remainder of the list except for its first element, supposed
58     *  to be immutable.
59     */
60    //@Deprecated
61    public List<A> tail;
62 
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    }
69 
70    /** Construct an empty list.
71     */
72    @SuppressWarnings("unchecked")
73    public static <A> List<A> nil() {
74        return EMPTY_LIST;
75    }
76    private static List EMPTY_LIST = new List<Object>(null,null) {
77        public List<Object> setTail(List<Object> tail) {
78            throw new UnsupportedOperationException();
79        }
80        public boolean isEmpty() {
81            return true;
82        }
83    };
84 
85    /** Construct a list consisting of given element.
86     */
87    public static <A> List<A> of(A x1) {
88        return new List<A>(x1, List.<A>nil());
89    }
90 
91    /** Construct a list consisting of given elements.
92     */
93    public static <A> List<A> of(A x1, A x2) {
94        return new List<A>(x1, of(x2));
95    }
96 
97    /** Construct a list consisting of given elements.
98     */
99    public static <A> List<A> of(A x1, A x2, A x3) {
100        return new List<A>(x1, of(x2, x3));
101    }
102 
103    /** Construct a list consisting of given elements.
104     */
105    public static <A> List<A> of(A x1, A x2, A x3, A... rest) {
106        return new List<A>(x1, new List<A>(x2, new List<A>(x3, from(rest))));
107    }
108 
109    /**
110     * Construct a list consisting all elements of given array.
111     * @param array an array; if {@code null} return an empty list
112     */
113    public static <A> List<A> from(A[] array) {
114        List<A> xs = nil();
115        if (array != null)
116            for (int i = array.length - 1; i >= 0; i--)
117                xs = new List<A>(array[i], xs);
118        return xs;
119    }
120 
121    /** Construct a list consisting of a given number of identical elements.
122     *  @param len    The number of elements in the list.
123     *  @param init   The value of each element.
124     */
125    @Deprecated
126    public static <A> List<A> fill(int len, A init) {
127        List<A> l = nil();
128        for (int i = 0; i < len; i++) l = new List<A>(init, l);
129        return l;
130    }
131 
132    /** Does list have no elements?
133     */
134    @Override
135    public boolean isEmpty() {
136        return tail == null;
137    }
138 
139    /** Does list have elements?
140     */
141    //@Deprecated
142    public boolean nonEmpty() {
143        return tail != null;
144    }
145 
146    /** Return the number of elements in this list.
147     */
148    //@Deprecated
149    public int length() {
150        List<A> l = this;
151        int len = 0;
152        while (l.tail != null) {
153            l = l.tail;
154            len++;
155        }
156        return len;
157    }
158    @Override
159    public int size() {
160        return length();
161    }
162 
163    public List<A> setTail(List<A> tail) {
164        this.tail = tail;
165        return tail;
166    }
167 
168    /** Prepend given element to front of list, forming and returning
169     *  a new list.
170     */
171    public List<A> prepend(A x) {
172        return new List<A>(x, this);
173    }
174 
175    /** Prepend given list of elements to front of list, forming and returning
176     *  a new list.
177     */
178    public List<A> prependList(List<A> xs) {
179        if (this.isEmpty()) return xs;
180        if (xs.isEmpty()) return this;
181        if (xs.tail.isEmpty()) return prepend(xs.head);
182        // return this.prependList(xs.tail).prepend(xs.head);
183        List<A> result = this;
184        List<A> rev = xs.reverse();
185        assert rev != xs;
186        // since xs.reverse() returned a new list, we can reuse the
187        // individual List objects, instead of allocating new ones.
188        while (rev.nonEmpty()) {
189            List<A> h = rev;
190            rev = rev.tail;
191            h.setTail(result);
192            result = h;
193        }
194        return result;
195    }
196 
197    /** Reverse list.
198     * If the list is empty or a singleton, then the same list is returned.
199     * Otherwise a new list is formed.
200     */
201    public List<A> reverse() {
202        // if it is empty or a singleton, return itself
203        if (isEmpty() || tail.isEmpty())
204            return this;
205 
206        List<A> rev = nil();
207        for (List<A> l = this; l.nonEmpty(); l = l.tail)
208            rev = new List<A>(l.head, rev);
209        return rev;
210    }
211 
212    /** Append given element at length, forming and returning
213     *  a new list.
214     */
215    public List<A> append(A x) {
216        return of(x).prependList(this);
217    }
218 
219    /** Append given list at length, forming and returning
220     *  a new list.
221     */
222    public List<A> appendList(List<A> x) {
223        return x.prependList(this);
224    }
225 
226    /**
227     * Append given list buffer at length, forming and returning a new
228     * list.
229     */
230    public List<A> appendList(ListBuffer<A> x) {
231        return appendList(x.toList());
232    }
233 
234    /** Copy successive elements of this list into given vector until
235     *  list is exhausted or end of vector is reached.
236     */
237    @Override @SuppressWarnings("unchecked")
238    public <T> T[] toArray(T[] vec) {
239        int i = 0;
240        List<A> l = this;
241        Object[] dest = vec;
242        while (l.nonEmpty() && i < vec.length) {
243            dest[i] = l.head;
244            l = l.tail;
245            i++;
246        }
247        if (l.isEmpty()) {
248            if (i < vec.length)
249                vec[i] = null;
250            return vec;
251        }
252 
253        vec = (T[])Array.newInstance(vec.getClass().getComponentType(), size());
254        return toArray(vec);
255    }
256 
257    public Object[] toArray() {
258        return toArray(new Object[size()]);
259    }
260 
261    /** Form a string listing all elements with given separator character.
262     */
263    public String toString(String sep) {
264        if (isEmpty()) {
265            return "";
266        } else {
267            StringBuffer buf = new StringBuffer();
268            buf.append(head);
269            for (List<A> l = tail; l.nonEmpty(); l = l.tail) {
270                buf.append(sep);
271                buf.append(l.head);
272            }
273            return buf.toString();
274        }
275    }
276 
277    /** Form a string listing all elements with comma as the separator character.
278     */
279    @Override
280    public String toString() {
281        return toString(",");
282    }
283 
284    /** Compute a hash code, overrides Object
285     *  @see java.util.List#hashCode
286     */
287    @Override
288    public int hashCode() {
289        List<A> l = this;
290        int h = 1;
291        while (l.tail != null) {
292            h = h * 31 + (l.head == null ? 0 : l.head.hashCode());
293            l = l.tail;
294        }
295        return h;
296    }
297 
298    /** Is this list the same as other list?
299     *  @see java.util.List#equals
300     */
301    @Override
302    public boolean equals(Object other) {
303        if (other instanceof List<?>)
304            return equals(this, (List<?>)other);
305        if (other instanceof java.util.List<?>) {
306            List<A> t = this;
307            Iterator<?> oIter = ((java.util.List<?>) other).iterator();
308            while (t.tail != null && oIter.hasNext()) {
309                Object o = oIter.next();
310                if ( !(t.head == null ? o == null : t.head.equals(o)))
311                    return false;
312                t = t.tail;
313            }
314            return (t.isEmpty() && !oIter.hasNext());
315        }
316        return false;
317    }
318 
319    /** Are the two lists the same?
320     */
321    public static boolean equals(List xs, List ys) {
322        while (xs.tail != null && ys.tail != null) {
323            if (xs.head == null) {
324                if (ys.head != null) return false;
325            } else {
326                if (!xs.head.equals(ys.head)) return false;
327            }
328            xs = xs.tail;
329            ys = ys.tail;
330        }
331        return xs.tail == null && ys.tail == null;
332    }
333 
334    /** Does the list contain the specified element?
335     */
336    @Override
337    public boolean contains(Object x) {
338        List<A> l = this;
339        while (l.tail != null) {
340            if (x == null) {
341                if (l.head == null) return true;
342            } else {
343                if (l.head.equals(x)) return true;
344            }
345            l = l.tail;
346        }
347        return false;
348    }
349 
350    /** The last element in the list, if any, or null.
351     */
352    public A last() {
353        A last = null;
354        List<A> t = this;
355        while (t.tail != null) {
356            last = t.head;
357            t = t.tail;
358        }
359        return last;
360    }
361 
362    @SuppressWarnings("unchecked")
363    public static <T> List<T> convert(Class<T> klass, List<?> list) {
364        if (list == null)
365            return null;
366        for (Object o : list)
367            klass.cast(o);
368        return (List<T>)list;
369    }
370 
371    private static Iterator EMPTYITERATOR = new Iterator() {
372            public boolean hasNext() {
373                return false;
374            }
375            public Object next() {
376                throw new java.util.NoSuchElementException();
377            }
378            public void remove() {
379                throw new UnsupportedOperationException();
380            }
381        };
382 
383    @SuppressWarnings("unchecked")
384    private static <A> Iterator<A> emptyIterator() {
385        return EMPTYITERATOR;
386    }
387 
388    @Override
389    public Iterator<A> iterator() {
390        if (tail == null)
391            return emptyIterator();
392        return new Iterator<A>() {
393            List<A> elems = List.this;
394            public boolean hasNext() {
395                return elems.tail != null;
396            }
397            public A next() {
398                if (elems.tail == null)
399                    throw new NoSuchElementException();
400                A result = elems.head;
401                elems = elems.tail;
402                return result;
403            }
404            public void remove() {
405                throw new UnsupportedOperationException();
406            }
407        };
408    }
409 
410    public A get(int index) {
411        if (index < 0)
412            throw new IndexOutOfBoundsException(String.valueOf(index));
413 
414        List<A> l = this;
415        for (int i = index; i-- > 0 && !l.isEmpty(); l = l.tail)
416            ;
417 
418        if (l.isEmpty())
419            throw new IndexOutOfBoundsException("Index: " + index + ", " +
420                                                "Size: " + size());
421        return l.head;
422    }
423 
424    public boolean addAll(int index, Collection<? extends A> c) {
425        if (c.isEmpty())
426            return false;
427        throw new UnsupportedOperationException();
428    }
429 
430    public A set(int index, A element) {
431        throw new UnsupportedOperationException();
432    }
433 
434    public void add(int index, A element) {
435        throw new UnsupportedOperationException();
436    }
437 
438    public A remove(int index) {
439        throw new UnsupportedOperationException();
440    }
441 
442    public int indexOf(Object o) {
443        int i = 0;
444        for (List<A> l = this; l.tail != null; l = l.tail, i++) {
445            if (l.head == null ? o == null : l.head.equals(o))
446                return i;
447        }
448        return -1;
449    }
450 
451    public int lastIndexOf(Object o) {
452        int last = -1;
453        int i = 0;
454        for (List<A> l = this; l.tail != null; l = l.tail, i++) {
455            if (l.head == null ? o == null : l.head.equals(o))
456                last = i;
457        }
458        return last;
459    }
460 
461    public ListIterator<A> listIterator() {
462        return Collections.unmodifiableList(new ArrayList<A>(this)).listIterator();
463    }
464 
465    public ListIterator<A> listIterator(int index) {
466        return Collections.unmodifiableList(new ArrayList<A>(this)).listIterator(index);
467    }
468 
469    public java.util.List<A> subList(int fromIndex, int toIndex) {
470        if  (fromIndex < 0 || toIndex > size() || fromIndex > toIndex)
471            throw new IllegalArgumentException();
472 
473        ArrayList<A> a = new ArrayList<A>(toIndex - fromIndex);
474        int i = 0;
475        for (List<A> l = this; l.tail != null; l = l.tail, i++) {
476            if (i == toIndex)
477                break;
478            if (i >= fromIndex)
479                a.add(l.head);
480        }
481 
482        return Collections.unmodifiableList(a);
483    }
484}

[all classes][com.sun.tools.javac.util]
EMMA 2.0.5312 (C) Vladimir Roubtsov