KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ldap > server > db > NoDupsEnumeration


1 /*
2  * Copyright 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package org.apache.ldap.server.db;
18
19
20 import javax.naming.NamingEnumeration JavaDoc;
21 import javax.naming.NamingException JavaDoc;
22 import java.util.NoSuchElementException JavaDoc;
23
24
25 /**
26  * A simple NamingEnumeration over a TupleBrowser on a table that does not allow
27  * duplicates.
28  *
29  * <p> WARNING: The Tuple returned by this listing is always the same instance
30  * object returned every time. It is reused to for the sake of efficency rather
31  * than creating a new tuple for each hasMore() call.
32  * </p>
33  *
34  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
35  * @version $Rev: 169198 $
36  */

37 public class NoDupsEnumeration
38     implements NamingEnumeration JavaDoc
39 {
40     /** Temporary Tuple used to return results */
41     private final Tuple returned = new Tuple();
42     /** Temporary Tuple used to store prefetched values */
43     private final Tuple prefetched = new Tuple();
44     /** The JDBM TupleBrowser this NamingEnumeration wraps */
45     private final TupleBrowser browser;
46     /** The direction of this NamingEnumeration */
47     private final boolean doAscendingScan;
48     /** Whether or not this NamingEnumeration can advance */
49     private boolean hasNext = true;
50
51
52     // ------------------------------------------------------------------------
53
// C O N S T R U C T O R
54
// ------------------------------------------------------------------------
55

56
57     /**
58      * Creates a cursor over a TupleBrowser where duplicates are not expected.
59      *
60      * @param browser TODO
61      * @param doAscendingScan TODO
62      * @throws NamingException TODO
63      */

64     public NoDupsEnumeration( TupleBrowser browser, boolean doAscendingScan )
65         throws NamingException JavaDoc
66     {
67         this.browser = browser;
68         this.doAscendingScan = doAscendingScan;
69         prefetch();
70     }
71
72
73     // ------------------------------------------------------------------------
74
// NamingEnumeration Interface Method Implementations
75
// ------------------------------------------------------------------------
76

77
78     /**
79      * Returns the same Tuple every time but with different key/value pairs.
80      *
81      * @see javax.naming.NamingEnumeration#next()
82      */

83     public Object JavaDoc next()
84         throws NamingException JavaDoc
85     {
86         // Load values into the Tuple to return
87
returned.setKey( prefetched.getKey() );
88         returned.setValue( prefetched.getValue() );
89
90         // Prefetch next set of values to return if and return last prefetched
91
prefetch();
92         return returned;
93     }
94     
95     
96     /**
97      * Returns the same Tuple every time but with different key/value pairs.
98      *
99      * @see java.util.Enumeration#nextElement()
100      */

101     public Object JavaDoc nextElement()
102     {
103         try
104         {
105             return next();
106         }
107         catch ( NamingException JavaDoc e )
108         {
109             throw new NoSuchElementException JavaDoc();
110         }
111     }
112
113
114     /**
115      * @see javax.naming.NamingEnumeration#hasMore()
116      */

117     public boolean hasMore()
118     {
119         return hasNext;
120     }
121
122
123     /**
124      * Calls hasMore.
125      *
126      * @see java.util.Enumeration#hasMoreElements()
127      */

128     public boolean hasMoreElements()
129     {
130         return hasNext;
131     }
132
133
134     /**
135      * Sets hasNext to false.
136      *
137      * @see javax.naming.NamingEnumeration#close()
138      */

139     public void close()
140     {
141         hasNext = false;
142     }
143
144
145     // ------------------------------------------------------------------------
146
// Private/Package Friendly Methods
147
// ------------------------------------------------------------------------
148

149
150     /**
151      * Gets the direction of this NamingEnumeration.
152      *
153      * @return true if this NamingEnumeration is ascending on keys, false
154      * otherwise.
155      */

156     boolean doAscendingScan()
157     {
158         return doAscendingScan;
159     }
160
161
162     /**
163      * Prefetches a value into prefetched over writing whatever values were
164      * contained in the Tuple.
165      *
166      * @throws NamingException if the TupleBrowser browser could not advance
167      */

168     private void prefetch() throws NamingException JavaDoc
169     {
170         // Prefetch into tuple!
171
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