1 17 package org.apache.ldap.server.db; 18 19 20 import javax.naming.NamingEnumeration ; 21 import javax.naming.NamingException ; 22 import java.util.NoSuchElementException ; 23 24 25 37 public class NoDupsEnumeration 38 implements NamingEnumeration 39 { 40 41 private final Tuple returned = new Tuple(); 42 43 private final Tuple prefetched = new Tuple(); 44 45 private final TupleBrowser browser; 46 47 private final boolean doAscendingScan; 48 49 private boolean hasNext = true; 50 51 52 56 57 64 public NoDupsEnumeration( TupleBrowser browser, boolean doAscendingScan ) 65 throws NamingException 66 { 67 this.browser = browser; 68 this.doAscendingScan = doAscendingScan; 69 prefetch(); 70 } 71 72 73 77 78 83 public Object next() 84 throws NamingException 85 { 86 returned.setKey( prefetched.getKey() ); 88 returned.setValue( prefetched.getValue() ); 89 90 prefetch(); 92 return returned; 93 } 94 95 96 101 public Object nextElement() 102 { 103 try 104 { 105 return next(); 106 } 107 catch ( NamingException e ) 108 { 109 throw new NoSuchElementException (); 110 } 111 } 112 113 114 117 public boolean hasMore() 118 { 119 return hasNext; 120 } 121 122 123 128 public boolean hasMoreElements() 129 { 130 return hasNext; 131 } 132 133 134 139 public void close() 140 { 141 hasNext = false; 142 } 143 144 145 149 150 156 boolean doAscendingScan() 157 { 158 return doAscendingScan; 159 } 160 161 162 168 private void prefetch() throws NamingException 169 { 170 boolean isSuccess = false; 172 173 if ( doAscendingScan ) 174 { 175 isSuccess = browser.getNext( prefetched ); 176 } 177 else 178 { 179 isSuccess = browser.getPrevious( prefetched ); 180 } 181 182 hasNext = isSuccess; 183 } 184 } 185 | Popular Tags |