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;
023import java.util.Iterator;
024
025import org.apache.directory.api.i18n.I18n;
026
027
028/**
029 * Simple class that contains often used Cursor code.
030 *
031 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
032 * @param <E> The type of element on which this cursor will iterate
033 */
034public abstract class AbstractCursor<E> implements Cursor<E>
035{
036    /** The default associated monitor */
037    private ClosureMonitor monitor = new DefaultClosureMonitor();
038
039
040    /**
041     * {@inheritDoc}
042     */
043    public void setClosureMonitor( ClosureMonitor monitor )
044    {
045        if ( monitor == null )
046        {
047            throw new IllegalArgumentException( I18n.err( I18n.ERR_02001_MONITOR ) );
048        }
049
050        this.monitor = monitor;
051    }
052
053
054    /**
055     * Check that the cursor is not closed before executing an operation.
056     * 
057     * @param operation The operation we try to execute
058     * @throws CursorClosedException If there is a problem during the check
059     */
060    public final void checkNotClosed( String operation ) throws CursorClosedException
061    {
062        monitor.checkNotClosed();
063    }
064
065
066    /**
067     * {@inheritDoc}
068     */
069    public boolean isClosed()
070    {
071        return monitor.isClosed();
072    }
073
074
075    /**
076     * {@inheritDoc}
077     */
078    public void close( Exception cause ) throws IOException
079    {
080        monitor.close( cause );
081    }
082
083
084    /**
085     * {@inheritDoc}
086     */
087    public void close() throws IOException
088    {
089        monitor.close();
090    }
091
092
093    /**
094     * {@inheritDoc}
095     */
096    public Iterator<E> iterator()
097    {
098        return new CursorIterator<E>( this );
099    }
100
101
102    /**
103     * {@inheritDoc}
104     */
105    public boolean isAfterLast()
106    {
107        throw new UnsupportedOperationException( I18n.err( I18n.ERR_02014_UNSUPPORTED_OPERATION, getClass().getName()
108            .concat( "." ).concat( "isAfterLast()" ) ) );
109    }
110
111
112    /**
113     * {@inheritDoc}
114     */
115    public boolean isBeforeFirst()
116    {
117        throw new UnsupportedOperationException( I18n.err( I18n.ERR_02014_UNSUPPORTED_OPERATION, getClass().getName()
118            .concat( "." ).concat( "isBeforeFirst()" ) ) );
119    }
120
121
122    /**
123     * {@inheritDoc}
124     */
125    public boolean isFirst()
126    {
127        throw new UnsupportedOperationException( I18n.err( I18n.ERR_02014_UNSUPPORTED_OPERATION, getClass().getName()
128            .concat( "." ).concat( "isFirst()" ) ) );
129    }
130
131
132    /**
133     * {@inheritDoc}
134     */
135    public boolean isLast()
136    {
137        throw new UnsupportedOperationException( I18n.err( I18n.ERR_02014_UNSUPPORTED_OPERATION, getClass().getName()
138            .concat( "." ).concat( "isLast()" ) ) );
139    }
140
141
142    /**
143     * {@inheritDoc}
144     */
145    public String toString( String tabs )
146    {
147        return tabs;
148    }
149}