View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.directory.mavibot.btree;
21  
22  
23  import java.io.IOException;
24  
25  import org.apache.directory.mavibot.btree.exception.EndOfFileExceededException;
26  import org.apache.directory.mavibot.btree.exception.KeyNotFoundException;
27  import org.apache.directory.mavibot.btree.BTree;
28  
29  
30  /**
31   * A class that encapsulate the values into an sub-btree
32   *
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   */
35  /* No qualifier */class ValueBTreeCursor<V> implements ValueCursor<V>
36  {
37      /** Store the current position in the array or in the BTree */
38      private KeyCursor<V> cursor;
39  
40      /** The Value sub-btree */
41      private BTree<V, V> valueBtree;
42  
43  
44      /**
45       * Create an instance
46       */
47      public ValueBTreeCursor( BTree<V, V> valueBtree )
48      {
49          this.valueBtree = valueBtree;
50  
51          // Start at -1 to be positioned before the first element
52          try
53          {
54              if ( valueBtree != null )
55              {
56                  cursor = valueBtree.browseKeys();
57              }
58          }
59          catch ( IOException e )
60          {
61              // TODO Auto-generated catch block
62              e.printStackTrace();
63          }
64          catch ( KeyNotFoundException knfe )
65          {
66              // TODO Auto-generated catch block
67              knfe.printStackTrace();
68          }
69      }
70  
71  
72      /**
73       * {@inheritDoc}}
74       */
75      @Override
76      public boolean hasNext()
77      {
78          if ( cursor == null )
79          {
80              return false;
81          }
82          else
83          {
84              try
85              {
86                  return cursor.hasNext();
87              }
88              catch ( EndOfFileExceededException e )
89              {
90                  e.printStackTrace();
91                  return false;
92              }
93              catch ( IOException e )
94              {
95                  e.printStackTrace();
96                  return false;
97              }
98          }
99      }
100 
101 
102     /**
103      * {@inheritDoc}}
104      */
105     public V next()
106     {
107         try
108         {
109             return cursor.next();
110         }
111         catch ( EndOfFileExceededException e )
112         {
113             e.printStackTrace();
114             return null;
115         }
116         catch ( IOException e )
117         {
118             e.printStackTrace();
119             return null;
120         }
121     }
122 
123 
124     /**
125      * {@inheritDoc}}
126      */
127     @Override
128     public boolean hasPrev() throws EndOfFileExceededException, IOException
129     {
130         if ( cursor == null )
131         {
132             return false;
133         }
134         else
135         {
136             try
137             {
138                 return cursor.hasPrev();
139             }
140             catch ( EndOfFileExceededException e )
141             {
142                 e.printStackTrace();
143                 return false;
144             }
145             catch ( IOException e )
146             {
147                 e.printStackTrace();
148                 return false;
149             }
150         }
151     }
152 
153 
154     /**
155      * {@inheritDoc}}
156      */
157     @Override
158     public void close()
159     {
160         if ( cursor != null )
161         {
162             cursor.close();
163         }
164     }
165 
166 
167     /**
168      * {@inheritDoc}}
169      */
170     @Override
171     public void beforeFirst() throws IOException
172     {
173         if ( cursor != null )
174         {
175             cursor.beforeFirst();
176         }
177     }
178 
179 
180     /**
181      * {@inheritDoc}}
182      */
183     @Override
184     public void afterLast() throws IOException
185     {
186         if ( cursor != null )
187         {
188             cursor.afterLast();
189         }
190     }
191 
192 
193     /**
194      * {@inheritDoc}}
195      */
196     @Override
197     public V prev() throws EndOfFileExceededException, IOException
198     {
199         try
200         {
201             return cursor.prev();
202         }
203         catch ( EndOfFileExceededException e )
204         {
205             e.printStackTrace();
206             return null;
207         }
208         catch ( IOException e )
209         {
210             e.printStackTrace();
211             return null;
212         }
213     }
214 
215 
216     /**
217      * {@inheritDoc}
218      */
219     @Override
220     public int size()
221     {
222         return ( int ) valueBtree.getNbElems();
223     }
224 
225 
226     /**
227      * @see Object#toString()
228      */
229     public String toString()
230     {
231         return "BTreeCursor";
232     }
233 }