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  
27  
28  /**
29   * A class that encapsulate the values into an array
30   *
31   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
32   */
33  /* No qualifier */class ValueArrayCursor<V> implements ValueCursor<V>
34  {
35      /** Store the current position in the array or in the BTree */
36      private int currentPos;
37  
38      /** The array storing values (1 to N) */
39      private V[] valueArray;
40  
41  
42      /**
43       * Create an instance
44       */
45      public ValueArrayCursor( V[] valueArray )
46      {
47          // Start at -1 to be positioned before the first element
48          currentPos = BEFORE_FIRST;
49          this.valueArray = valueArray;
50      }
51  
52  
53      /**
54       * {@inheritDoc}
55       */
56      @Override
57      public boolean hasNext()
58      {
59          return ( currentPos < valueArray.length - 1 ) && ( currentPos != AFTER_LAST );
60      }
61  
62  
63      /**
64       * {@inheritDoc}
65       */
66      public V next()
67      {
68          if ( valueArray == null )
69          {
70              // Deserialize the array
71              return null;
72          }
73          else
74          {
75              currentPos++;
76  
77              if ( currentPos == valueArray.length )
78              {
79                  currentPos = AFTER_LAST;
80  
81                  // We have reached the end of the array
82                  return null;
83              }
84              else
85              {
86                  return valueArray[currentPos];
87              }
88          }
89      }
90  
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      public boolean hasPrev() throws EndOfFileExceededException, IOException
97      {
98          return currentPos > 0 || currentPos == AFTER_LAST;
99      }
100 
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public void close()
107     {
108     }
109 
110 
111     /**
112      * {@inheritDoc}
113      */
114     @Override
115     public void beforeFirst() throws IOException
116     {
117         currentPos = BEFORE_FIRST;
118     }
119 
120 
121     /**
122      * {@inheritDoc}
123      */
124     @Override
125     public void afterLast() throws IOException
126     {
127         currentPos = AFTER_LAST;
128     }
129 
130 
131     /**
132      * {@inheritDoc}
133      */
134     @Override
135     public V prev() throws EndOfFileExceededException, IOException
136     {
137         if ( valueArray == null )
138         {
139             // Deserialize the array
140             return null;
141         }
142         else
143         {
144             if ( currentPos == AFTER_LAST )
145             {
146                 currentPos = valueArray.length - 1;
147             }
148             else
149             {
150                 currentPos--;
151             }
152 
153             if ( currentPos == BEFORE_FIRST )
154             {
155                 // We have reached the end of the array
156                 return null;
157             }
158             else
159             {
160                 return valueArray[currentPos];
161             }
162         }
163     }
164 
165 
166     /**
167      * {@inheritDoc}
168      */
169     @Override
170     public int size()
171     {
172         return valueArray.length;
173     }
174 }