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