1 17 package org.apache.ldap.server.db; 18 19 20 import org.apache.regexp.RE; 21 22 import javax.naming.NamingEnumeration ; 23 import javax.naming.NamingException ; 24 import java.util.NoSuchElementException ; 25 26 27 33 public class IndexEnumeration 34 implements NamingEnumeration 35 { 36 37 private final RE re; 38 39 private final IndexRecord tmp = new IndexRecord(); 40 41 private final IndexRecord returned = new IndexRecord(); 42 43 private final IndexRecord prefetched = new IndexRecord(); 44 45 private final boolean swapKeyVal; 46 47 private final NamingEnumeration underlying; 48 49 50 private boolean hasMore = true; 51 52 53 57 58 64 public IndexEnumeration( NamingEnumeration list ) throws NamingException 65 { 66 this( list, false, null ); 67 } 68 69 70 77 public IndexEnumeration( NamingEnumeration list, boolean swapKeyVal ) 78 throws NamingException 79 { 80 this( list, swapKeyVal, null ); 81 } 82 83 84 92 public IndexEnumeration( NamingEnumeration list, boolean swapKeyVal, 93 RE regex ) 94 throws NamingException 95 { 96 re = regex; 97 underlying = list; 98 this.swapKeyVal = swapKeyVal; 99 100 if ( ! underlying.hasMore() ) 101 { 102 hasMore = false; 103 return; 104 } 105 106 prefetch(); 107 } 108 109 110 114 115 118 public Object next() 119 throws NamingException 120 { 121 returned.copy( prefetched ); 122 prefetch(); 123 return returned; 124 } 125 126 127 130 public Object nextElement() 131 { 132 try 133 { 134 return next(); 135 } 136 catch ( NamingException ne ) 137 { 138 throw new NoSuchElementException (); 139 } 140 } 141 142 143 146 public boolean hasMore() 147 { 148 return hasMore; 149 } 150 151 152 155 public boolean hasMoreElements() 156 { 157 return hasMore; 158 } 159 160 161 164 public void close() throws NamingException 165 { 166 hasMore = false; 167 underlying.close(); 168 } 169 170 171 175 176 181 private void prefetch() throws NamingException 182 { 183 while ( underlying.hasMore() ) 184 { 185 Tuple tuple = ( Tuple ) underlying.next(); 186 187 if ( swapKeyVal ) 188 { 189 tmp.setSwapped( tuple, null ); 190 } 191 else 192 { 193 tmp.setTuple( tuple, null ); 194 } 195 196 if ( null == re || re.match( ( String ) tmp.getIndexKey() ) ) 200 { 201 prefetched.copy( tmp ); 202 return; 203 } 204 } 205 206 hasMore = false; 208 } 209 } 210 | Popular Tags |