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 */
019package org.apache.directory.api.ldap.model.cursor;
020
021
022import java.io.IOException;
023
024import org.apache.directory.api.i18n.I18n;
025import org.apache.directory.api.ldap.model.constants.Loggers;
026import org.apache.directory.api.ldap.model.exception.LdapException;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030
031/**
032 * An empty Cursor implementation.
033 *
034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035 * @param <E> The type of element on which this cursor will iterate
036 */
037public class EmptyCursor<E> extends AbstractCursor<E>
038{
039    /** A dedicated log for cursors */
040    private static final Logger LOG_CURSOR = LoggerFactory.getLogger( Loggers.CURSOR_LOG.getName() );
041
042    /** Speedup for logs */
043    private static final boolean IS_DEBUG = LOG_CURSOR.isDebugEnabled();
044
045
046    /**
047     * Creates a new EmptyCursor instance
048     */
049    public EmptyCursor()
050    {
051        if ( IS_DEBUG )
052        {
053            LOG_CURSOR.debug( "Creating EmptyCursor : {}", this );
054        }
055    }
056
057
058    /**
059     * {@inheritDoc}
060     */
061    @Override
062    public boolean available()
063    {
064        return false;
065    }
066
067
068    /**
069     * {@inheritDoc}
070     */
071    @Override
072    public void before( E element ) throws LdapException, CursorException
073    {
074        checkNotClosed( "before()" );
075    }
076
077
078    /**
079     * {@inheritDoc}
080     */
081    @Override
082    public void after( E element ) throws LdapException, CursorException
083    {
084        checkNotClosed( "after()" );
085    }
086
087
088    /**
089     * {@inheritDoc}
090     */
091    @Override
092    public void beforeFirst() throws LdapException, CursorException
093    {
094        checkNotClosed( "beforeFirst()" );
095    }
096
097
098    /**
099     * {@inheritDoc}
100     */
101    @Override
102    public void afterLast() throws LdapException, CursorException
103    {
104        checkNotClosed( "afterLast()" );
105    }
106
107
108    /**
109     * {@inheritDoc}
110     */
111    @Override
112    public boolean first() throws LdapException, CursorException
113    {
114        checkNotClosed( "first()" );
115        return false;
116    }
117
118
119    /**
120     * {@inheritDoc}
121     */
122    @Override
123    public boolean last() throws LdapException, CursorException
124    {
125        checkNotClosed( "last()" );
126        return false;
127    }
128
129
130    /**
131     * {@inheritDoc}
132     */
133    @Override
134    public boolean previous() throws LdapException, CursorException
135    {
136        checkNotClosed( "previous()" );
137        return false;
138    }
139
140
141    /**
142     * {@inheritDoc}
143     */
144    @Override
145    public boolean next() throws LdapException, CursorException
146    {
147        checkNotClosed( "next()" );
148        return false;
149    }
150
151
152    /**
153     * {@inheritDoc}
154     */
155    @Override
156    public E get() throws CursorException
157    {
158        checkNotClosed( "get()" );
159        throw new InvalidCursorPositionException( I18n.err( I18n.ERR_02004_EMPTY_CURSOR ) );
160    }
161
162
163    /**
164     * {@inheritDoc}
165     */
166    @Override
167    public void close() throws IOException
168    {
169        if ( IS_DEBUG )
170        {
171            LOG_CURSOR.debug( "Closing EmptyCursor {}", this );
172        }
173
174        super.close();
175    }
176
177
178    /**
179     * {@inheritDoc}
180     */
181    @Override
182    public void close( Exception cause ) throws IOException
183    {
184        if ( IS_DEBUG )
185        {
186            LOG_CURSOR.debug( "Closing EmptyCursor {}", this );
187        }
188
189        super.close( cause );
190    }
191}