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 *  
019 */
020package org.apache.directory.shared.ldap.model.cursor;
021
022
023import java.util.Iterator;
024
025import org.apache.directory.shared.i18n.I18n;
026
027
028/**
029 * An abstract TupleCursor.
030 *
031 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
032 * @param <K> The key type for the Tuple
033 * @param <V> The associated Value type
034 */
035public abstract class AbstractTupleCursor<K, V> implements TupleCursor<K, V>
036{
037    /** The default associated monitor */
038    private ClosureMonitor monitor = new DefaultClosureMonitor();
039
040
041    /**
042     * {@inheritDoc}
043     */
044    public final void setClosureMonitor( ClosureMonitor monitor )
045    {
046        if ( monitor == null )
047        {
048            throw new IllegalArgumentException( "monitor" );
049        }
050
051        this.monitor = monitor;
052    }
053
054
055    /**
056     * Check if the cursor is closed for the given operation
057     *
058     * @param operation The operation that will be applied if the cursor is not closed
059     * @throws Exception If there is an issue
060     */
061    protected final void checkNotClosed( String operation ) throws Exception
062    {
063        monitor.checkNotClosed();
064    }
065
066
067    /**
068     * {@inheritDoc}
069     */
070    public final boolean isClosed()
071    {
072        return monitor.isClosed();
073    }
074
075
076    /**
077     * {@inheritDoc}
078     */
079    public void close() throws Exception
080    {
081        monitor.close();
082    }
083
084
085    /**
086     * {@inheritDoc}
087     */
088    public void close( Exception cause ) throws Exception
089    {
090        monitor.close( cause );
091    }
092
093
094    /**
095     * {@inheritDoc}
096     */
097    public Iterator<Tuple<K, V>> iterator()
098    {
099        return new CursorIterator<Tuple<K, V>>( this );
100    }
101
102
103    /**
104     * {@inheritDoc}
105     */
106    public boolean isAfterLast() throws Exception
107    {
108        throw new UnsupportedOperationException( I18n.err( I18n.ERR_02014_UNSUPPORTED_OPERATION, getClass().getName()
109            .concat( "." ).concat( "isAfterLast()" ) ) );
110    }
111
112
113    /**
114     * {@inheritDoc}
115     */
116    public boolean isBeforeFirst() throws Exception
117    {
118        throw new UnsupportedOperationException( I18n.err( I18n.ERR_02014_UNSUPPORTED_OPERATION, getClass().getName()
119            .concat( "." ).concat( "isBeforeFirst()" ) ) );
120    }
121
122
123    /**
124     * {@inheritDoc}
125     */
126    public boolean isFirst() throws Exception
127    {
128        throw new UnsupportedOperationException( I18n.err( I18n.ERR_02014_UNSUPPORTED_OPERATION, getClass().getName()
129            .concat( "." ).concat( "isFirst()" ) ) );
130    }
131
132
133    /**
134     * {@inheritDoc}
135     */
136    public boolean isLast() throws Exception
137    {
138        throw new UnsupportedOperationException( I18n.err( I18n.ERR_02014_UNSUPPORTED_OPERATION, getClass().getName()
139            .concat( "." ).concat( "isLast()" ) ) );
140    }
141}