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   *  https://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_13101_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       * @throws CursorClosedException If there is a problem during the check
59       */
60      public final void checkNotClosed() throws CursorClosedException
61      {
62          monitor.checkNotClosed();
63      }
64  
65  
66      /**
67       * {@inheritDoc}
68       */
69      @Override
70      public boolean isClosed()
71      {
72          return monitor.isClosed();
73      }
74  
75  
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      public void close( Exception cause ) throws IOException
81      {
82          monitor.close( cause );
83      }
84  
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      public void close() throws IOException
91      {
92          monitor.close();
93      }
94  
95  
96      /**
97       * {@inheritDoc}
98       */
99      @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 }