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.cursor;
20  
21  
22  import java.util.Comparator;
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 SingletonCursor<E> extends AbstractCursor<E>
32  {
33      private boolean beforeFirst = true;
34      private boolean afterLast;
35      private boolean onSingleton;
36      private final Comparator<E> comparator;
37      private final E singleton;
38  
39  
40      public SingletonCursor( E singleton )
41      {
42          this( singleton, null );
43      }
44  
45  
46      public SingletonCursor( E singleton, Comparator<E> comparator )
47      {
48          this.singleton = singleton;
49          this.comparator = comparator;
50      }
51  
52  
53      public boolean available()
54      {
55          return onSingleton;
56      }
57      
58  
59      public void before( E element ) throws Exception
60      {
61          checkNotClosed( "before()" );
62  
63          if ( comparator == null )
64          {
65              throw new UnsupportedOperationException(
66                      "Without a comparator I cannot advance to just before the specified element." );
67          }
68  
69          int comparison = comparator.compare( singleton, element );
70  
71          if ( comparison < 0 )
72          {
73              first();
74          }
75          else
76          {
77              beforeFirst();
78          }
79      }
80  
81  
82      public void after( E element ) throws Exception
83      {
84          checkNotClosed( "after()" );
85  
86          if ( comparator == null )
87          {
88              throw new UnsupportedOperationException(
89                      "Without a comparator I cannot advance to just after the specified element." );
90          }
91  
92          int comparison = comparator.compare( singleton, element );
93  
94          if ( comparison > 0 )
95          {
96              first();
97          }
98          else
99          {
100             afterLast();
101         }
102     }
103 
104 
105     public void beforeFirst() throws Exception
106     {
107         checkNotClosed( "()" );
108         beforeFirst = true;
109         afterLast = false;
110         onSingleton = false;
111     }
112 
113 
114     public void afterLast() throws Exception
115     {
116         checkNotClosed( "()" );
117         beforeFirst = false;
118         afterLast = true;
119         onSingleton = false;
120     }
121 
122 
123     public boolean first() throws Exception
124     {
125         checkNotClosed( "()" );
126         beforeFirst = false;
127         onSingleton = true;
128         afterLast = false;
129         return true;
130     }
131 
132 
133     public boolean last() throws Exception
134     {
135         checkNotClosed( "()" );
136         beforeFirst = false;
137         onSingleton = true;
138         afterLast = false;
139         return true;
140     }
141 
142 
143     public boolean isFirst() throws Exception
144     {
145         checkNotClosed( "()" );
146         return onSingleton;
147     }
148 
149 
150     public boolean isLast() throws Exception
151     {
152         checkNotClosed( "()" );
153         return onSingleton;
154     }
155 
156 
157     public boolean isAfterLast() throws Exception
158     {
159         checkNotClosed( "()" );
160         return afterLast;
161     }
162 
163 
164     public boolean isBeforeFirst() throws Exception
165     {
166         checkNotClosed( "()" );
167         return beforeFirst;
168     }
169 
170 
171     public boolean previous() throws Exception
172     {
173         checkNotClosed( "()" );
174         if ( beforeFirst )
175         {
176             return false;
177         }
178 
179         if ( afterLast )
180         {
181             beforeFirst = false;
182             onSingleton = true;
183             afterLast = false;
184             return true;
185         }
186 
187         // must be on the singleton
188         beforeFirst = true;
189         onSingleton = false;
190         afterLast = false;
191         return false;
192     }
193 
194 
195     public boolean next() throws Exception
196     {
197         checkNotClosed( "()" );
198         if ( beforeFirst )
199         {
200             beforeFirst = false;
201             onSingleton = true;
202             afterLast = false;
203             return true;
204         }
205 
206         if ( afterLast )
207         {
208             return false;
209         }
210 
211         // must be on the singleton
212         beforeFirst = false;
213         onSingleton = false;
214         afterLast = true;
215         return false;
216     }
217 
218 
219     public E get() throws Exception
220     {
221         checkNotClosed( "()" );
222         if ( onSingleton )
223         {
224             return singleton;
225         }
226 
227         if ( beforeFirst )
228         {
229             throw new InvalidCursorPositionException( "Cannot access element if positioned before first." );
230         }
231         else
232         {
233             throw new InvalidCursorPositionException( "Cannot access element if positioned after last." );
234         }
235     }
236 
237 
238     public boolean isElementReused()
239     {
240         return true;
241     }
242 }