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    public EmptyCursor()
047    {
048        if ( IS_DEBUG )
049        {
050            LOG_CURSOR.debug( "Creating EmptyCursor : {}", this );
051        }
052    }
053
054
055    /**
056     * {@inheritDoc}
057     */
058    public boolean available()
059    {
060        return false;
061    }
062
063
064    /**
065     * {@inheritDoc}
066     */
067    public void before( E element ) throws LdapException, CursorException
068    {
069        checkNotClosed( "before()" );
070    }
071
072
073    /**
074     * {@inheritDoc}
075     */
076    public void after( E element ) throws LdapException, CursorException
077    {
078        checkNotClosed( "after()" );
079    }
080
081
082    /**
083     * {@inheritDoc}
084     */
085    public void beforeFirst() throws LdapException, CursorException
086    {
087        checkNotClosed( "beforeFirst()" );
088    }
089
090
091    /**
092     * {@inheritDoc}
093     */
094    public void afterLast() throws LdapException, CursorException
095    {
096        checkNotClosed( "afterLast()" );
097    }
098
099
100    /**
101     * {@inheritDoc}
102     */
103    public boolean first() throws LdapException, CursorException
104    {
105        checkNotClosed( "first()" );
106        return false;
107    }
108
109
110    /**
111     * {@inheritDoc}
112     */
113    public boolean last() throws LdapException, CursorException
114    {
115        checkNotClosed( "last()" );
116        return false;
117    }
118
119
120    /**
121     * {@inheritDoc}
122     */
123    public boolean previous() throws LdapException, CursorException
124    {
125        checkNotClosed( "previous()" );
126        return false;
127    }
128
129
130    /**
131     * {@inheritDoc}
132     */
133    public boolean next() throws LdapException, CursorException
134    {
135        checkNotClosed( "next()" );
136        return false;
137    }
138
139
140    /**
141     * {@inheritDoc}
142     */
143    public E get() throws CursorException
144    {
145        checkNotClosed( "get()" );
146        throw new InvalidCursorPositionException( I18n.err( I18n.ERR_02004_EMPTY_CURSOR ) );
147    }
148
149
150    /**
151     * {@inheritDoc}
152     */
153    @Override
154    public void close() throws IOException
155    {
156        if ( IS_DEBUG )
157        {
158            LOG_CURSOR.debug( "Closing EmptyCursor {}", this );
159        }
160
161        super.close();
162    }
163
164
165    /**
166     * {@inheritDoc}
167     */
168    @Override
169    public void close( Exception cause ) throws IOException
170    {
171        if ( IS_DEBUG )
172        {
173            LOG_CURSOR.debug( "Closing EmptyCursor {}", this );
174        }
175
176        super.close( cause );
177    }
178}