KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > util > ComposedLazyList


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.mdr.util;
20
21 import java.util.*;
22
23 /**
24  *
25  * @author Martin Matula
26  * @version
27  */

28 public class ComposedLazyList extends AbstractList {
29     private final List lists = new ArrayList();
30     private int elementCount = 0;
31
32     /** Creates new ComposedLazyList */
33     public ComposedLazyList() {
34         super();
35     }
36
37     public ComposedLazyList(Collection col) {
38         this();
39         addAll(col);
40     }
41
42     public boolean addAll(Collection col) {
43         List result = (col instanceof List) ? (List) col : new ArrayList(col);
44         
45         this.modCount++;
46         
47         elementCount += result.size();
48         return lists.add(result);
49     }
50     
51     public int size() {
52         return elementCount;
53     }
54     
55     public Object JavaDoc get(int index) {
56         int listUBound = 0;
57         int listLBound = 0;
58         List currentList = null;
59         
60         for (Iterator it = lists.iterator(); it.hasNext() && listUBound <= index; listUBound += currentList.size()) {
61             currentList = (List) it.next();
62             listLBound = listUBound;
63         }
64
65         return currentList.get(index - listLBound);
66     }
67     
68     public Iterator iterator() {
69         return new LazyIterator();
70     }
71     
72     private class LazyIterator implements Iterator {
73         Iterator listIterator;
74         Iterator currentIterator;
75         
76         private LazyIterator() {
77             listIterator = ComposedLazyList.this.lists.iterator();
78             currentIterator = null;
79         }
80             
81         public void remove() {
82             throw new UnsupportedOperationException JavaDoc();
83         }
84         
85         public Object JavaDoc next() {
86             hasNext();
87             return currentIterator.next();
88         }
89         
90         public boolean hasNext() {
91             if (currentIterator == null) {
92                 if (listIterator.hasNext()) {
93                     currentIterator = ((List) listIterator.next()).iterator();
94                 } else {
95                     return false;
96                 }
97             }
98             
99             while (!currentIterator.hasNext()) {
100                 if (listIterator.hasNext()) {
101                     currentIterator = ((List) listIterator.next()).iterator();
102                 } else {
103                     return false;
104                 }
105             }
106             return true;
107         }
108     }
109 }
110
Popular Tags