1 17 package org.apache.ldap.server.db; 18 19 20 import javax.naming.NamingEnumeration ; 21 import javax.naming.NamingException ; 22 import java.util.*; 23 24 25 36 public class DupsEnumeration implements NamingEnumeration 37 { 38 39 private boolean hasMore = true; 40 41 private final Tuple returned = new Tuple(); 42 43 private final Tuple prefetched = new Tuple(); 44 45 private final NoDupsEnumeration underlying; 46 47 53 private Tuple duplicates; 54 58 private Iterator dupIterator; 59 60 61 65 66 73 public DupsEnumeration( NoDupsEnumeration list ) throws NamingException 74 { 75 underlying = list; 76 77 if ( ! underlying.hasMore() ) 79 { 80 close(); 81 return; 82 } 83 84 prefetch(); 85 } 86 87 88 92 93 98 public Object next() throws NamingException 99 { 100 returned.setKey( prefetched.getKey() ); 101 returned.setValue( prefetched.getValue() ); 102 103 prefetch(); 104 105 return returned; 106 } 107 108 109 114 public Object nextElement() 115 { 116 try 117 { 118 return next(); 119 } 120 catch ( NamingException ne ) 121 { 122 throw new NoSuchElementException(); 123 } 124 } 125 126 127 130 public boolean hasMore() 131 { 132 return hasMore; 133 } 134 135 136 141 public boolean hasMoreElements() 142 { 143 return hasMore; 144 } 145 146 147 152 public void close() 153 { 154 hasMore = false; 155 underlying.close(); 156 } 157 158 159 163 164 178 private void prefetch() throws NamingException 179 { 180 184 while ( null == dupIterator || ! dupIterator.hasNext() ) 185 { 186 190 if ( underlying.hasMore() ) 191 { 192 duplicates = ( Tuple ) underlying.next(); 193 TreeSet set = ( TreeSet ) duplicates.getValue(); 194 195 if ( underlying.doAscendingScan() ) 196 { 197 dupIterator = set.iterator(); 198 } 199 else 200 { 201 209 ArrayList list = new ArrayList( set.size() ); 210 list.addAll( set ); 211 Collections.reverse( list ); 212 dupIterator = list.iterator(); 213 } 214 } 215 else 216 { 217 close(); 218 return; 219 } 220 } 221 222 229 prefetched.setKey( duplicates.getKey() ); 230 prefetched.setValue( dupIterator.next() ); 231 } 232 } 233 | Popular Tags |