KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > LazyImmutableList


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.modules.javacore;
20
21 import java.util.AbstractSequentialList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.ListIterator JavaDoc;
24 import java.util.NoSuchElementException JavaDoc;
25
26 /**
27  *
28  * @author Martin Matula
29  */

30 public class LazyImmutableList extends AbstractSequentialList JavaDoc {
31     private LazyIterator internal;
32     private boolean complete = false;
33     private final Object JavaDoc[] array;
34     private int nextIndex = 0;
35
36     /** Creates a new instance of LazyImmutableList */
37     public LazyImmutableList(LazyIterator internalIterator) {
38         internal = internalIterator;
39         array = new Object JavaDoc[internalIterator.maxEstimatedSize()];
40     }
41     
42     public int size() {
43         while (internal != null) {
44             nextItem();
45         }
46         return nextIndex;
47     }
48     
49     public ListIterator JavaDoc listIterator(int index) {
50         return new LazyImmutableIterator(index);
51     }
52     
53     public int maxEstimatedSize() {
54         return internal == null ? nextIndex : internal.maxEstimatedSize();
55     }
56     
57     private void nextItem() {
58         if (internal != null && internal.hasNext()) {
59             array[nextIndex] = internal.next();
60             nextIndex++;
61         } else {
62             internal = null;
63         }
64     }
65     
66     private class LazyImmutableIterator implements ListIterator JavaDoc {
67         private int currentIndex;
68         
69         public LazyImmutableIterator(int index) {
70             currentIndex = index;
71         }
72
73         public boolean hasNext() {
74             while (nextIndex <= currentIndex && internal != null) {
75                 nextItem();
76             }
77             return nextIndex > currentIndex;
78         }
79
80         public int previousIndex() {
81             return currentIndex - 1;
82         }
83
84         public Object JavaDoc previous() {
85             if (hasPrevious()) {
86                 currentIndex--;
87                 return array[currentIndex];
88             }
89             throw new NoSuchElementException JavaDoc();
90         }
91
92         public boolean hasPrevious() {
93             return currentIndex > 0;
94         }
95
96         public Object JavaDoc next() {
97             if (hasNext()) {
98                 currentIndex++;
99                 return array[currentIndex - 1];
100             }
101             throw new NoSuchElementException JavaDoc();
102         }
103
104         public int nextIndex() {
105             return currentIndex;
106         }
107
108         public void add(Object JavaDoc o) {
109             throw new UnsupportedOperationException JavaDoc();
110         }
111
112         public void set(Object JavaDoc o) {
113             throw new UnsupportedOperationException JavaDoc();
114         }
115
116         public void remove() {
117             throw new UnsupportedOperationException JavaDoc();
118         }
119     }
120     
121     public static abstract class LazyIterator implements Iterator JavaDoc {
122         public final void remove() {
123             throw new UnsupportedOperationException JavaDoc();
124         }
125         
126         protected abstract int maxEstimatedSize();
127     }
128 }
129
Popular Tags