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 */
020
021package org.apache.directory.server.core.partition.impl.btree.mavibot;
022
023
024import java.io.IOException;
025
026import org.apache.directory.api.ldap.model.cursor.AbstractCursor;
027import org.apache.directory.api.ldap.model.cursor.CursorException;
028import org.apache.directory.api.ldap.model.exception.LdapException;
029import org.apache.directory.mavibot.btree.ValueCursor;
030import org.apache.directory.server.i18n.I18n;
031
032
033/**
034 * TODO ValueTreeCursor.
035 *
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 */
038public class ValueTreeCursor<V> extends AbstractCursor<V>
039{
040
041    private ValueCursor<V> wrapped;
042
043    private V available;
044
045    // marker to detect the availability (cause Mavibot supports null values also)
046    private V notAvailable = ( V ) new Object();
047
048
049    public ValueTreeCursor( ValueCursor<V> cursor )
050    {
051        this.wrapped = cursor;
052    }
053
054
055    @Override
056    public boolean available()
057    {
058        return ( available != notAvailable );
059    }
060
061
062    @Override
063    public void before( V element ) throws LdapException, CursorException
064    {
065        throw new UnsupportedOperationException( I18n.err( I18n.ERR_446 ) );
066    }
067
068
069    @Override
070    public void after( V element ) throws LdapException, CursorException
071    {
072        throw new UnsupportedOperationException( I18n.err( I18n.ERR_446 ) );
073    }
074
075
076    @Override
077    public void beforeFirst() throws LdapException, CursorException
078    {
079    }
080
081
082    @Override
083    public void afterLast() throws LdapException, CursorException
084    {
085        throw new UnsupportedOperationException( I18n.err( I18n.ERR_446 ) );
086    }
087
088
089    @Override
090    public boolean first() throws LdapException, CursorException
091    {
092        throw new UnsupportedOperationException( I18n.err( I18n.ERR_446 ) );
093    }
094
095
096    @Override
097    public boolean last() throws LdapException, CursorException
098    {
099        throw new UnsupportedOperationException( I18n.err( I18n.ERR_446 ) );
100    }
101
102
103    @Override
104    public boolean previous() throws LdapException, CursorException
105    {
106        try
107        {
108            if ( wrapped.hasPrev() )
109            {
110                available = wrapped.prev();
111                return true;
112            }
113            else
114            {
115                available = notAvailable;
116                return false;
117            }
118        }
119        catch ( IOException e )
120        {
121            throw new CursorException( e );
122        }
123    }
124
125
126    @Override
127    public boolean next() throws LdapException, CursorException
128    {
129        try
130        {
131            if ( wrapped.hasNext() )
132            {
133                available = wrapped.next();
134                return true;
135            }
136            else
137            {
138                available = notAvailable;
139                return false;
140            }
141        }
142        catch ( IOException e )
143        {
144            throw new CursorException( e );
145        }
146    }
147
148
149    @Override
150    public V get() throws CursorException
151    {
152        return available;
153    }
154
155}