KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openidex > search > CompoundSearchIterator


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 2004 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openidex.search;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.NoSuchElementException JavaDoc;
24
25 /**
26  *
27  * @author Marian Petras
28  */

29 class CompoundSearchIterator implements Iterator JavaDoc {
30
31     /** */
32     private final SearchInfo[] elements;
33     /** */
34     private int elementIndex;
35     /** */
36     private Iterator JavaDoc elementIterator;
37     /** */
38     private Object JavaDoc nextObject;
39     /** */
40     private boolean upToDate;
41
42     /**
43      * Creates a new instance of <code>CompoundSearchIterator</code>.
44      *
45      * @param elements elements of the compound iterator
46      * @exception java.lang.IllegalArgumentException
47      * if the argument is <code>null</code>
48      */

49     CompoundSearchIterator(SearchInfo[] elements) {
50         if (elements == null) {
51             throw new IllegalArgumentException JavaDoc();
52         }
53         
54         if (elements.length == 0) {
55             this.elements = null;
56             elementIndex = 0;
57             upToDate = true; //hasNext() returns always false
58
} else {
59             this.elements = elements;
60             elementIterator = elements[elementIndex = 0].objectsToSearch();
61             upToDate = false;
62         }
63     }
64
65     /**
66      */

67     public boolean hasNext() {
68         if (!upToDate) {
69             update();
70         }
71         return elementIndex < elements.length;
72     }
73
74     /**
75      */

76     public Object JavaDoc next() {
77         if (!hasNext()) {
78             throw new NoSuchElementException JavaDoc();
79         }
80         
81         upToDate = false;
82         return nextObject;
83     }
84     
85     /**
86      */

87     private void update() {
88         assert upToDate == false;
89         
90         while (!elementIterator.hasNext()) {
91             elements[elementIndex] = null;
92             
93             if (++elementIndex == elements.length) {
94                 break;
95             }
96             
97             elementIterator = elements[elementIndex].objectsToSearch();
98         }
99         
100         if (elementIndex < elements.length) {
101             nextObject = elementIterator.next();
102         } else {
103             elementIterator = null;
104             nextObject = null;
105         }
106         
107         upToDate = true;
108     }
109
110     /**
111      * @exception java.lang.UnsupportedOperationException
112      * always - this operation is not supported
113      */

114     public void remove() {
115         throw new UnsupportedOperationException JavaDoc();
116     }
117     
118 }
119
Popular Tags