1 17 package org.apache.ldap.server.db; 18 19 20 import javax.naming.NamingEnumeration ; 21 import javax.naming.NamingException ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 import java.util.NoSuchElementException ; 25 26 27 36 public class DisjunctionEnumeration implements NamingEnumeration 37 { 38 39 private final NamingEnumeration [] children; 40 41 private final Map candidates = new HashMap (); 42 43 private int index = 0; 44 45 private final IndexRecord candidate = new IndexRecord(); 46 47 private final IndexRecord prefetched = new IndexRecord(); 48 49 private boolean hasMore = true; 50 51 52 56 57 65 public DisjunctionEnumeration( NamingEnumeration [] children ) 66 throws NamingException 67 { 68 this.children = children; 69 70 if ( children.length <= 0 ) 72 { 73 hasMore = false; 74 return; 75 } 76 77 while ( ! children[index].hasMore() ) 79 { 80 index++; 81 82 if ( index >= children.length ) 85 { 86 close(); 87 return; 88 } 89 } 90 91 IndexRecord rec = ( IndexRecord ) children[index].next(); 93 prefetched.copy( rec ); 94 candidates.put( rec.getEntryId(), rec.getEntryId() ); 95 } 96 97 98 102 103 106 public Object nextElement() 107 { 108 try 109 { 110 return next(); 111 } 112 catch ( NamingException e ) 113 { 114 throw new NoSuchElementException (); 115 } 116 } 117 118 119 122 public boolean hasMoreElements() 123 { 124 return hasMore(); 125 } 126 127 128 132 133 142 public Object next() throws NamingException 143 { 144 candidate.copy( prefetched ); 146 147 do 148 { 149 while ( ! children[index].hasMore() ) 151 { 152 index++; 153 154 158 if ( index >= children.length ) 159 { 160 close(); 161 return candidate; 162 } 163 } 164 165 IndexRecord rec = ( IndexRecord ) children[index].next(); 167 prefetched.copy( rec ); 168 169 } while ( candidates.containsKey( prefetched.getEntryId() ) ); 172 173 candidates.put( candidate.getEntryId(), candidate.getEntryId() ); 175 176 return candidate; 178 } 179 180 181 187 public boolean hasMore() 188 { 189 return hasMore; 190 } 191 192 193 199 public void close() throws NamingException 200 { 201 Throwable throwable = null; 202 hasMore = false; 203 204 for ( int ii = 0; ii < children.length; ii++ ) 205 { 206 try 207 { 208 children[ii].close(); 211 } 212 catch ( Throwable t ) 213 { 214 throwable = t; 215 } 216 } 217 218 if ( null != throwable && throwable instanceof NamingException ) 219 { 220 throw ( NamingException ) throwable; 221 } 222 else if ( null != throwable ) 223 { 224 NamingException ne = new NamingException (); 225 ne.setRootCause( throwable ); 226 throw ne; 227 } 228 } 229 } 230 | Popular Tags |