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.BTree;
27  
28  
29  /**
30   * A class that encapsulate the values into an sub-btree
31   *
32   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33   */
34  /* No qualifier */class ValueBTreeCursor<V> implements ValueCursor<V>
35  {
36      /** Store the current position in the array or in the BTree */
37      private TupleCursor<V, V> cursor;
38  
39      /** The Value sub-btree */
40      private BTree<V, V> valueBtree;
41  
42  
43      /**
44       * Create an instance
45       */
46      public ValueBTreeCursor( BTree<V, V> valueBtree )
47      {
48          this.valueBtree = valueBtree;
49  
50          // Start at -1 to be positioned before the first element
51          try
52          {
53              if ( valueBtree != null )
54              {
55                  cursor = valueBtree.browse();
56              }
57          }
58          catch ( IOException e )
59          {
60              // TODO Auto-generated catch block
61              e.printStackTrace();
62          }
63      }
64  
65  
66      /**
67       * {@inheritDoc}}
68       */
69      @Override
70      public boolean hasNext()
71      {
72          if ( cursor == null )
73          {
74              return false;
75          }
76          else
77          {
78              try
79              {
80                  return cursor.hasNext();
81              }
82              catch ( EndOfFileExceededException e )
83              {
84                  e.printStackTrace();
85                  return false;
86              }
87              catch ( IOException e )
88              {
89                  e.printStackTrace();
90                  return false;
91              }
92          }
93      }
94  
95  
96      /**
97       * {@inheritDoc}}
98       */
99      public V next()
100     {
101         try
102         {
103             return cursor.next().getKey();
104         }
105         catch ( EndOfFileExceededException e )
106         {
107             e.printStackTrace();
108             return null;
109         }
110         catch ( IOException e )
111         {
112             e.printStackTrace();
113             return null;
114         }
115     }
116 
117 
118     /**
119      * {@inheritDoc}}
120      */
121     @Override
122     public boolean hasPrev() throws EndOfFileExceededException, IOException
123     {
124         if ( cursor == null )
125         {
126             return false;
127         }
128         else
129         {
130             try
131             {
132                 return cursor.hasPrev();
133             }
134             catch ( EndOfFileExceededException e )
135             {
136                 e.printStackTrace();
137                 return false;
138             }
139             catch ( IOException e )
140             {
141                 e.printStackTrace();
142                 return false;
143             }
144         }
145     }
146 
147 
148     /**
149      * {@inheritDoc}}
150      */
151     @Override
152     public void close()
153     {
154         if ( cursor != null )
155         {
156             cursor.close();
157         }
158     }
159 
160 
161     /**
162      * {@inheritDoc}}
163      */
164     @Override
165     public void beforeFirst() throws IOException
166     {
167         if ( cursor != null )
168         {
169             cursor.beforeFirst();
170         }
171     }
172 
173 
174     /**
175      * {@inheritDoc}}
176      */
177     @Override
178     public void afterLast() throws IOException
179     {
180         if ( cursor != null )
181         {
182             cursor.afterLast();
183         }
184     }
185 
186 
187     /**
188      * {@inheritDoc}}
189      */
190     @Override
191     public V prev() throws EndOfFileExceededException, IOException
192     {
193         try
194         {
195             return cursor.prev().getKey();
196         }
197         catch ( EndOfFileExceededException e )
198         {
199             e.printStackTrace();
200             return null;
201         }
202         catch ( IOException e )
203         {
204             e.printStackTrace();
205             return null;
206         }
207     }
208 
209 
210     /**
211      * {@inheritDoc}
212      */
213     @Override
214     public int size()
215     {
216         return ( int ) valueBtree.getNbElems();
217     }
218 
219 
220     /**
221      * @see Object#toString()
222      */
223     public String toString()
224     {
225         return "BTreeCursor";
226     }
227 }