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 *  https://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_13101_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     * @throws CursorClosedException If there is a problem during the check
059     */
060    public final void checkNotClosed() throws CursorClosedException
061    {
062        monitor.checkNotClosed();
063    }
064
065
066    /**
067     * {@inheritDoc}
068     */
069    @Override
070    public boolean isClosed()
071    {
072        return monitor.isClosed();
073    }
074
075
076    /**
077     * {@inheritDoc}
078     */
079    @Override
080    public void close( Exception cause ) throws IOException
081    {
082        monitor.close( cause );
083    }
084
085
086    /**
087     * {@inheritDoc}
088     */
089    @Override
090    public void close() throws IOException
091    {
092        monitor.close();
093    }
094
095
096    /**
097     * {@inheritDoc}
098     */
099    @Override
100    public Iterator<E> iterator()
101    {
102        return new CursorIterator<>( this );
103    }
104
105
106    /**
107     * {@inheritDoc}
108     */
109    @Override
110    public boolean isAfterLast()
111    {
112        throw new UnsupportedOperationException( I18n.err( I18n.ERR_13102_UNSUPPORTED_OPERATION, getClass().getName()
113            .concat( "." ).concat( "isAfterLast()" ) ) );
114    }
115
116
117    /**
118     * {@inheritDoc}
119     */
120    @Override
121    public boolean isBeforeFirst()
122    {
123        throw new UnsupportedOperationException( I18n.err( I18n.ERR_13102_UNSUPPORTED_OPERATION, getClass().getName()
124            .concat( "." ).concat( "isBeforeFirst()" ) ) );
125    }
126
127
128    /**
129     * {@inheritDoc}
130     */
131    @Override
132    public boolean isFirst()
133    {
134        throw new UnsupportedOperationException( I18n.err( I18n.ERR_13102_UNSUPPORTED_OPERATION, getClass().getName()
135            .concat( "." ).concat( "isFirst()" ) ) );
136    }
137
138
139    /**
140     * {@inheritDoc}
141     */
142    @Override
143    public boolean isLast()
144    {
145        throw new UnsupportedOperationException( I18n.err( I18n.ERR_13102_UNSUPPORTED_OPERATION, getClass().getName()
146            .concat( "." ).concat( "isLast()" ) ) );
147    }
148
149
150    /**
151     * {@inheritDoc}
152     */
153    @Override
154    public String toString( String tabs )
155    {
156        return tabs;
157    }
158}