1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.ldap.server.partition.impl.btree;
18
19
20 import java.util.NoSuchElementException;
21
22 import javax.naming.NamingEnumeration;
23 import javax.naming.NamingException;
24
25
26 /***
27 * A simple NamingEnumeration over a TupleBrowser on a table that does not allow
28 * duplicates.
29 *
30 * <p> WARNING: The Tuple returned by this listing is always the same instance
31 * object returned every time. It is reused to for the sake of efficency rather
32 * than creating a new tuple for each hasMore() call.
33 * </p>
34 *
35 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36 * @version $Rev: 264732 $
37 */
38 public class NoDupsEnumeration
39 implements NamingEnumeration
40 {
41 /*** Temporary Tuple used to return results */
42 private final Tuple returned = new Tuple();
43 /*** Temporary Tuple used to store prefetched values */
44 private final Tuple prefetched = new Tuple();
45 /*** The JDBM TupleBrowser this NamingEnumeration wraps */
46 private final TupleBrowser browser;
47 /*** The direction of this NamingEnumeration */
48 private final boolean doAscendingScan;
49 /*** Whether or not this NamingEnumeration can advance */
50 private boolean hasNext = true;
51
52
53
54
55
56
57
58 /***
59 * Creates a cursor over a TupleBrowser where duplicates are not expected.
60 */
61 public NoDupsEnumeration( TupleBrowser browser, boolean doAscendingScan )
62 throws NamingException
63 {
64 this.browser = browser;
65 this.doAscendingScan = doAscendingScan;
66 prefetch();
67 }
68
69
70
71
72
73
74
75 /***
76 * Returns the same Tuple every time but with different key/value pairs.
77 *
78 * @see javax.naming.NamingEnumeration#next()
79 */
80 public Object next()
81 throws NamingException
82 {
83
84 returned.setKey( prefetched.getKey() );
85 returned.setValue( prefetched.getValue() );
86
87
88 prefetch();
89 return returned;
90 }
91
92
93 /***
94 * Returns the same Tuple every time but with different key/value pairs.
95 *
96 * @see java.util.Enumeration#nextElement()
97 */
98 public Object nextElement()
99 {
100 try
101 {
102 return next();
103 }
104 catch ( NamingException e )
105 {
106 throw new NoSuchElementException();
107 }
108 }
109
110
111 /***
112 * @see javax.naming.NamingEnumeration#hasMore()
113 */
114 public boolean hasMore()
115 {
116 return hasNext;
117 }
118
119
120 /***
121 * Calls hasMore.
122 *
123 * @see java.util.Enumeration#hasMoreElements()
124 */
125 public boolean hasMoreElements()
126 {
127 return hasNext;
128 }
129
130
131 /***
132 * Sets hasNext to false.
133 *
134 * @see javax.naming.NamingEnumeration#close()
135 */
136 public void close()
137 {
138 hasNext = false;
139 }
140
141
142
143
144
145
146
147 /***
148 * Gets the direction of this NamingEnumeration.
149 *
150 * @return true if this NamingEnumeration is ascending on keys, false
151 * otherwise.
152 */
153 boolean doAscendingScan()
154 {
155 return doAscendingScan;
156 }
157
158
159 /***
160 * Prefetches a value into prefetched over writing whatever values were
161 * contained in the Tuple.
162 *
163 * @throws NamingException if the TupleBrowser browser could not advance
164 */
165 private void prefetch() throws NamingException
166 {
167
168 boolean isSuccess = false;
169
170 if ( doAscendingScan )
171 {
172 isSuccess = browser.getNext( prefetched );
173 }
174 else
175 {
176 isSuccess = browser.getPrevious( prefetched );
177 }
178
179 hasNext = isSuccess;
180 }
181 }