001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.directory.mavibot.btree;
021
022
023import java.io.IOException;
024import java.util.NoSuchElementException;
025
026
027/**
028 * A Cursor which is used when we have no element to return
029 *
030 * @param <K> The type for the Key
031 * @param <V> The type for the stored value
032 *
033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034 */
035public class EmptyTupleCursor<K, V> extends TupleCursor<K, V>
036{
037    /** AN empty cursor does not have a revision */
038    private static final long NO_REVISION = -1L;
039
040    /** The creation date */
041    private long creationDate;
042
043
044    /**
045     * Creates a new instance of EmptyTupleCursor. It will never return any result
046     */
047    public EmptyTupleCursor()
048    {
049        super();
050
051        creationDate = System.currentTimeMillis();
052    }
053
054
055    /**
056     * Change the position in the current cursor to set it after the last key
057     */
058    public void afterLast() throws IOException
059    {
060    }
061
062
063    /**
064     * Change the position in the current cursor before the first key
065     */
066    public void beforeFirst() throws IOException
067    {
068    }
069
070
071    /**
072     * Always return false.
073     *
074     * @return Always false
075     */
076    public boolean hasNext()
077    {
078        return false;
079    }
080
081
082    /**
083     * Always throws a NoSuchElementException.
084     *
085     * @return Nothing
086     * @throws NoSuchElementException There is no element in a EmptyTupleCursor
087     */
088    public Tuple<K, V> next() throws NoSuchElementException
089    {
090        throw new NoSuchElementException( "No tuple present" );
091    }
092
093
094    /**
095     * Always throws a NoSuchElementException.
096     *
097     * @return Nothing
098     * @throws NoSuchElementException There is no element in a EmptyTupleCursor
099     */
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}