1 19 package org.netbeans.lib.jmi.query; 20 21 import java.util.Iterator ; 22 import java.util.NoSuchElementException ; 23 24 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 iterator() { 42 return new UnionIterator( ); 43 } 44 45 public boolean contains( Object 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 58 private class UnionIterator extends QueryIterator { 59 60 int index = 0; 61 Iterator 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 next() { 93 94 if ( current == null ) { 95 throw new NoSuchElementException (); 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 (); 105 } 106 else { 107 current = queries[index].iterator(); 108 return next(); 109 } 110 } 111 } 112 } 113 } 114 | Popular Tags |