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