View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.directory.api.ldap.model.cursor;
20  
21  
22  import java.util.Iterator;
23  
24  import org.apache.directory.api.i18n.I18n;
25  
26  
27  /**
28   * Simple class that contains often used Cursor code.
29   *
30   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
31   * @param <E> The type of element on which this cursor will iterate
32   */
33  public abstract class AbstractCursor<E> implements Cursor<E>
34  {
35      /** The default associated monitor */
36      private ClosureMonitor monitor = new DefaultClosureMonitor();
37  
38  
39      /**
40       * {@inheritDoc}
41       */
42      public void setClosureMonitor( ClosureMonitor monitor )
43      {
44          if ( monitor == null )
45          {
46              throw new IllegalArgumentException( I18n.err( I18n.ERR_02001_MONITOR ) );
47          }
48  
49          this.monitor = monitor;
50      }
51  
52  
53      /**
54       * Check that the cursor is not closed before executing an operation.
55       * 
56       * @param operation The operation we try to execute
57       * @throws CursorClosedException If there is a problem during the check
58       */
59      public final void checkNotClosed( String operation ) throws CursorClosedException
60      {
61          monitor.checkNotClosed();
62      }
63  
64  
65      /**
66       * {@inheritDoc}
67       */
68      public boolean isClosed()
69      {
70          return monitor.isClosed();
71      }
72  
73  
74      /**
75       * {@inheritDoc}
76       */
77      public void close( Exception cause )
78      {
79          monitor.close( cause );
80      }
81  
82  
83      /**
84       * {@inheritDoc}
85       */
86      public void close()
87      {
88          monitor.close();
89      }
90  
91  
92      /**
93       * {@inheritDoc}
94       */
95      public Iterator<E> iterator()
96      {
97          return new CursorIterator<E>( this );
98      }
99  
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 }