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  import java.util.NoSuchElementException;
25  
26  
27  /**
28   * A Cursor which is used when we have no element to return
29   *
30   * @param <K> The type for the Key
31   * @param <V> The type for the stored value
32   *
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   */
35  public class EmptyTupleCursor<K, V> extends TupleCursor<K, V>
36  {
37      /** AN empty cursor does not have a revision */
38      private static final long NO_REVISION = -1L;
39  
40      /** The creation date */
41      private long creationDate;
42  
43  
44      /**
45       * Creates a new instance of EmptyTupleCursor. It will never return any result
46       */
47      public EmptyTupleCursor()
48      {
49          super();
50  
51          creationDate = System.currentTimeMillis();
52      }
53  
54  
55      /**
56       * Change the position in the current cursor to set it after the last key
57       */
58      public void afterLast() throws IOException
59      {
60      }
61  
62  
63      /**
64       * Change the position in the current cursor before the first key
65       */
66      public void beforeFirst() throws IOException
67      {
68      }
69  
70  
71      /**
72       * Always return false.
73       *
74       * @return Always false
75       */
76      public boolean hasNext()
77      {
78          return false;
79      }
80  
81  
82      /**
83       * Always throws a NoSuchElementException.
84       *
85       * @return Nothing
86       * @throws NoSuchElementException There is no element in a EmptyTupleCursor
87       */
88      public Tuple<K, V> next() throws NoSuchElementException
89      {
90          throw new NoSuchElementException( "No tuple present" );
91      }
92  
93  
94      /**
95       * Always throws a NoSuchElementException.
96       *
97       * @return Nothing
98       * @throws NoSuchElementException There is no element in a EmptyTupleCursor
99       */
100     public Tuple<K, V> nextKey() throws NoSuchElementException
101     {
102         // This is the end : no more value
103         throw new NoSuchElementException( "No more tuples present" );
104     }
105 
106 
107     /**
108      * Always false
109      *
110      * @return false
111      */
112     public boolean hasNextKey()
113     {
114         return false;
115     }
116 
117 
118     /**
119      * Always false
120      * 
121      * @return false
122      */
123     public boolean hasPrev()
124     {
125         return false;
126     }
127 
128 
129     /**
130      * Always throws a NoSuchElementException.
131      *
132      * @return Nothing
133      * @throws NoSuchElementException There is no element in a EmptyTupleCursor
134      */
135     public Tuple<K, V> prev() throws NoSuchElementException
136     {
137         throw new NoSuchElementException( "No more tuple present" );
138     }
139 
140 
141     /**
142      * Always throws a NoSuchElementException.
143      *
144      * @return Nothing
145      * @throws NoSuchElementException There is no element in a EmptyTupleCursor
146      */
147     public Tuple<K, V> prevKey() throws NoSuchElementException
148     {
149         throw new NoSuchElementException( "No more tuples present" );
150     }
151 
152 
153     /**
154      * Always false
155      * 
156      * @return false
157      */
158     public boolean hasPrevKey()
159     {
160         return false;
161     }
162 
163 
164     /**
165      * {@inheritDoc}
166      */
167     public void close()
168     {
169     }
170 
171 
172     /**
173      * Get the creation date
174      * 
175      * @return The creation date for this cursor
176      */
177     public long getCreationDate()
178     {
179         return creationDate;
180     }
181 
182 
183     /**
184      * Always -1L for an empty cursor
185      *
186      * @return -1L
187      */
188     public long getRevision()
189     {
190         return NO_REVISION;
191     }
192 
193 
194     /**
195      * @see Object#toString()
196      */
197     public String toString()
198     {
199         return "EmptyTupleCursor";
200     }
201 }