KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > LazyIterator


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
20 package org.netbeans.core.startup;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.NoSuchElementException JavaDoc;
24 import org.openide.util.Lookup;
25
26 /**
27  * A special filtering and lazy iterator used by our XML factories
28  *
29  * @author Petr Nejedly
30  */

31 class LazyIterator implements Iterator JavaDoc<Class JavaDoc> {
32     Class JavaDoc first;
33     Class JavaDoc step;
34     Class JavaDoc<?> template;
35     Object JavaDoc skip;
36     Iterator JavaDoc delegate;
37
38     LazyIterator(Class JavaDoc first, Class JavaDoc template, Class JavaDoc skip) {
39         assert first != null;
40
41         this.first = first;
42         this.template = template;
43         this.skip = skip;
44     }
45
46     public boolean hasNext() {
47         if (first != null) return true;
48
49         // lazily prepare delegate
50
if (delegate == null) delegate = prepareDelegate();
51
52         // check next step
53
if (step != null) return true;
54
55         // prepare next step
56
while (delegate.hasNext() && step == null) {
57             Class JavaDoc next = ((Lookup.Item)delegate.next()).getType();
58             if (next != skip) step = next;
59         }
60
61         return step != null;
62     }
63
64     public Class JavaDoc next() {
65         if (first != null) {
66             Class JavaDoc ret = first;
67             first = null;
68             return ret;
69         }
70         // lazily prepare delegate
71
if (delegate == null) delegate = prepareDelegate();
72
73         // check next step
74
if (step != null) {
75             Class JavaDoc ret = step;
76             step = null;
77             return ret;
78         }
79
80         // return directly next without storing
81
while (delegate.hasNext()) {
82             Class JavaDoc next = ((Lookup.Item)delegate.next()).getType();
83             if (next != skip) return next;
84         }
85
86         throw new NoSuchElementException JavaDoc();
87     }
88
89     private Iterator JavaDoc prepareDelegate() {
90         return Lookup.getDefault().lookupResult(template).allItems().iterator();
91     }
92
93     public void remove() {
94         throw new UnsupportedOperationException JavaDoc();
95     }
96 }
97
Popular Tags