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;
024
025import org.apache.directory.mavibot.btree.exception.EndOfFileExceededException;
026
027
028/**
029 * A Cursor is used to fetch elements in a BTree and is returned by the
030 * @see BTree#browse method. The cursor <strng>must</strong> be closed
031 * when the user is done with it.
032 * <p>
033 *
034 * @param <K> The type for the Key
035 * 
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 */
038public interface Cursor<K>
039{
040    /** Static value for the beforeFrst and afterLast positions */
041    int BEFORE_FIRST = -1;
042    int AFTER_LAST = -2;
043
044
045    /**
046     * Tells if the cursor can return a next element
047     * @return true if there are some more elements
048     * @throws IOException 
049     * @throws EndOfFileExceededException 
050     */
051    boolean hasNext() throws EndOfFileExceededException, IOException;
052
053
054    /**
055     * Tells if the cursor can return a previous element
056     * @return true if there are some more elements
057     * @throws IOException 
058     * @throws EndOfFileExceededException 
059     */
060    boolean hasPrev() throws EndOfFileExceededException, IOException;
061
062
063    /**
064     * Closes the cursor, thus releases the associated transaction
065     */
066    void close();
067
068
069    /**
070     * moves the cursor to the same position that was given at the time of instantiating the cursor.
071     * 
072     *  For example, if the cursor was created using browse() method, then beforeFirst() will
073     *  place the cursor before the 0th position.
074     *  
075     *  If the cursor was created using browseFrom(K), then calling beforeFirst() will reset the position
076     *  to the just before the position where K is present.
077     */
078    void beforeFirst() throws IOException;
079
080
081    /**
082     * Places the cursor at the end of the last position
083     * 
084     * @throws IOException
085     */
086    void afterLast() throws IOException;
087}