KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > jmi > query > UnionQuery


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.lib.jmi.query;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.NoSuchElementException JavaDoc;
23
24 /** Takes two or more queries and makes an union of it. It *does not*
25  * remove duplicates.
26  *
27  * @author Petr Hrebejk
28  */

29 public class UnionQuery implements Query {
30
31     Query[] queries;
32
33     public UnionQuery( Query queryA, Query queryB ) {
34         queries = new Query[] { queryA, queryB };
35     }
36
37     public UnionQuery(Query[] queries) {
38         this.queries = queries;
39     }
40
41     public Iterator JavaDoc iterator() {
42         return new UnionIterator( );
43     }
44     
45     public boolean contains( Object JavaDoc object ) {
46         
47         for( int i = 0; i < queries.length; i++ ) {
48             if ( queries[i].contains( object ) ) {
49                 return true;
50             }
51         }
52         
53         return false;
54     }
55     
56     // Inner classes -----------------------------------------------------------
57

58     private class UnionIterator extends QueryIterator {
59         
60         int index = 0;
61         Iterator JavaDoc current = null;
62         
63         UnionIterator( ) {
64             super ();
65             
66             if ( queries.length > 0 ) {
67                 current = queries[0].iterator();
68             }
69         }
70         
71         public boolean hasNext() {
72             
73             if ( current == null ) {
74                 return false;
75             }
76             else if ( current.hasNext() ) {
77                 return true;
78             }
79             else {
80                 index++;
81                 if ( index >= queries.length ) {
82                     current = null;
83                     return false;
84                 }
85                 else {
86                     current = queries[index].iterator();
87                     return hasNext();
88                 }
89             }
90         }
91         
92         public Object JavaDoc next() {
93             
94             if ( current == null ) {
95                 throw new NoSuchElementException JavaDoc();
96             }
97             else if ( current.hasNext() ) {
98                 return current.next();
99             }
100             else {
101                 index++;
102                 if ( index >= queries.length ) {
103                     current = null;
104                     throw new NoSuchElementException JavaDoc();
105                 }
106                 else {
107                     current = queries[index].iterator();
108                     return next();
109                 }
110             }
111         }
112     }
113 }
114
Popular Tags