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.io.IOException;
23  import java.util.Iterator;
24  
25  import org.apache.directory.api.i18n.I18n;
26  
27  
28  /**
29   * Simple class that contains often used Cursor code.
30   *
31   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
32   * @param <E> The type of element on which this cursor will iterate
33   */
34  public abstract class AbstractCursor<E> implements Cursor<E>
35  {
36      /** The default associated monitor */
37      private ClosureMonitor monitor = new DefaultClosureMonitor();
38  
39  
40      /**
41       * {@inheritDoc}
42       */
43      @Override
44      public void setClosureMonitor( ClosureMonitor monitor )
45      {
46          if ( monitor == null )
47          {
48              throw new IllegalArgumentException( I18n.err( I18n.ERR_02001_MONITOR ) );
49          }
50  
51          this.monitor = monitor;
52      }
53  
54  
55      /**
56       * Check that the cursor is not closed before executing an operation.
57       * 
58       * @param operation The operation we try to execute
59       * @throws CursorClosedException If there is a problem during the check
60       */
61      public final void checkNotClosed( String operation ) throws CursorClosedException
62      {
63          monitor.checkNotClosed();
64      }
65  
66  
67      /**
68       * {@inheritDoc}
69       */
70      @Override
71      public boolean isClosed()
72      {
73          return monitor.isClosed();
74      }
75  
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public void close( Exception cause ) throws IOException
82      {
83          monitor.close( cause );
84      }
85  
86  
87      /**
88       * {@inheritDoc}
89       */
90      @Override
91      public void close() throws IOException
92      {
93          monitor.close();
94      }
95  
96  
97      /**
98       * {@inheritDoc}
99       */
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 }