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    @Override
044    public void setClosureMonitor( ClosureMonitor monitor )
045    {
046        if ( monitor == null )
047        {
048            throw new IllegalArgumentException( I18n.err( I18n.ERR_02001_MONITOR ) );
049        }
050
051        this.monitor = monitor;
052    }
053
054
055    /**
056     * Check that the cursor is not closed before executing an operation.
057     * 
058     * @param operation The operation we try to execute
059     * @throws CursorClosedException If there is a problem during the check
060     */
061    public final void checkNotClosed( String operation ) throws CursorClosedException
062    {
063        monitor.checkNotClosed();
064    }
065
066
067    /**
068     * {@inheritDoc}
069     */
070    @Override
071    public boolean isClosed()
072    {
073        return monitor.isClosed();
074    }
075
076
077    /**
078     * {@inheritDoc}
079     */
080    @Override
081    public void close( Exception cause ) throws IOException
082    {
083        monitor.close( cause );
084    }
085
086
087    /**
088     * {@inheritDoc}
089     */
090    @Override
091    public void close() throws IOException
092    {
093        monitor.close();
094    }
095
096
097    /**
098     * {@inheritDoc}
099     */
100    @Override
101    public Iterator<E> iterator()
102    {
103        return new CursorIterator<>( this );
104    }
105
106
107    /**
108     * {@inheritDoc}
109     */
110    @Override
111    public boolean isAfterLast()
112    {
113        throw new UnsupportedOperationException( I18n.err( I18n.ERR_02014_UNSUPPORTED_OPERATION, getClass().getName()
114            .concat( "." ).concat( "isAfterLast()" ) ) );
115    }
116
117
118    /**
119     * {@inheritDoc}
120     */
121    @Override
122    public boolean isBeforeFirst()
123    {
124        throw new UnsupportedOperationException( I18n.err( I18n.ERR_02014_UNSUPPORTED_OPERATION, getClass().getName()
125            .concat( "." ).concat( "isBeforeFirst()" ) ) );
126    }
127
128
129    /**
130     * {@inheritDoc}
131     */
132    @Override
133    public boolean isFirst()
134    {
135        throw new UnsupportedOperationException( I18n.err( I18n.ERR_02014_UNSUPPORTED_OPERATION, getClass().getName()
136            .concat( "." ).concat( "isFirst()" ) ) );
137    }
138
139
140    /**
141     * {@inheritDoc}
142     */
143    @Override
144    public boolean isLast()
145    {
146        throw new UnsupportedOperationException( I18n.err( I18n.ERR_02014_UNSUPPORTED_OPERATION, getClass().getName()
147            .concat( "." ).concat( "isLast()" ) ) );
148    }
149
150
151    /**
152     * {@inheritDoc}
153     */
154    @Override
155    public String toString( String tabs )
156    {
157        return tabs;
158    }
159}