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  package org.apache.directory.server.core.partition.impl.btree;
20  
21  
22  import org.apache.directory.server.core.cursor.InvalidCursorPositionException;
23  import org.apache.directory.server.core.cursor.AbstractCursor;
24  import org.apache.directory.server.xdbm.Tuple;
25  import org.apache.directory.shared.ldap.NotImplementedException;
26  
27  import java.util.Arrays;
28  import java.util.List;
29  
30  
31  /**
32   * A Cursor which returns the values of a single key as Tuples.
33   *
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   * @version $Rev$, $Date$
36   */
37  public class ValueArrayCursor<K,V> extends AbstractCursor<Tuple>
38  {
39      private static final int BEFORE_FIRST = -1;
40  
41      private final K key;
42      private final List<V> values;
43      private final Tuple<K,V> tuple = new Tuple<K,V>();
44  
45      private int pos = BEFORE_FIRST;
46  
47  
48      public ValueArrayCursor( final K key, final V[] values )
49      {
50          this.key = key;
51          this.tuple.setKey( key );
52          this.values = Arrays.asList( values );
53      }
54  
55  
56      public ValueArrayCursor( final K key, final List<V> values )
57      {
58          this.key = key;
59          this.tuple.setKey( key );
60          this.values = values;
61      }
62  
63  
64      public boolean available()
65      {
66          return inRangeOnValue();
67      }
68  
69  
70      public void before( Tuple element ) throws Exception
71      {
72          throw new NotImplementedException();
73      }
74  
75  
76      public void after( Tuple element ) throws Exception
77      {
78          throw new NotImplementedException();
79      }
80  
81  
82      public void beforeFirst() throws Exception
83      {
84          checkNotClosed( "beforeFirst()" );
85          pos = BEFORE_FIRST;
86      }
87  
88  
89      public void afterLast() throws Exception
90      {
91          checkNotClosed( "afterLast()" );
92          pos = values.size();
93      }
94  
95  
96      public boolean absolute( int absolutePosition ) throws Exception
97      {
98          checkNotClosed( "absolute()" );
99          if ( absolutePosition >= values.size() )
100         {
101             pos = values.size();
102             return false;
103         }
104 
105         if ( absolutePosition < 0 )
106         {
107             pos = BEFORE_FIRST;
108             return false;
109         }
110 
111         pos = absolutePosition;
112         return true;
113     }
114 
115 
116     public boolean first() throws Exception
117     {
118         checkNotClosed( "first()" );
119         pos = 0;
120         return true;
121     }
122 
123 
124     public boolean last() throws Exception
125     {
126         checkNotClosed( "last()" );
127         pos = values.size() - 1;
128         return true;
129     }
130 
131 
132     public boolean isFirst() throws Exception
133     {
134         checkNotClosed( "isFirst()" );
135         return pos == 0;
136     }
137 
138 
139     public boolean isLast() throws Exception
140     {
141         checkNotClosed( "isLast()" );
142         return pos == values.size() - 1;
143     }
144 
145 
146     public boolean isAfterLast() throws Exception
147     {
148         checkNotClosed( "isAfterLast()" );
149         return pos == values.size();
150     }
151 
152 
153     public boolean isBeforeFirst() throws Exception
154     {
155         checkNotClosed( "isBeforeFirst()" );
156         return pos == BEFORE_FIRST;
157     }
158 
159 
160     public boolean previous() throws Exception
161     {
162         checkNotClosed( "previous()" );
163         if ( pos <= BEFORE_FIRST )
164         {
165             return false;
166         }
167 
168         pos--;
169         return inRangeOnValue();
170     }
171 
172 
173     private boolean inRangeOnValue()
174     {
175         return pos > BEFORE_FIRST && pos < values.size();
176     }
177 
178 
179     public boolean next() throws Exception
180     {
181         checkNotClosed( "next()" );
182         if ( pos >= values.size() )
183         {
184             return false;
185         }
186 
187         pos++;
188         return inRangeOnValue();
189     }
190 
191 
192     public Tuple get() throws Exception
193     {
194         checkNotClosed( "get()" );
195         if ( inRangeOnValue() )
196         {
197             return tuple.setBoth( key, values.get( pos ) );
198         }
199 
200         throw new InvalidCursorPositionException( "Cursor pos (" + pos
201                 + ") not in value range [0-" + ( values.size() - 1 )+ "]" );
202     }
203 
204 
205     public boolean isElementReused()
206     {
207         return true;
208     }
209 }