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   */
20  package org.apache.directory.server.core.partition.impl.btree;
21  
22  
23  import java.util.Iterator;
24  
25  import org.apache.directory.server.core.cursor.ClosureMonitor;
26  import org.apache.directory.server.core.cursor.Cursor;
27  import org.apache.directory.server.core.cursor.CursorIterator;
28  import org.apache.directory.server.core.entry.ServerEntry;
29  import org.apache.directory.server.core.partition.Partition;
30  import org.apache.directory.server.xdbm.IndexCursor;
31  import org.apache.directory.server.xdbm.IndexEntry;
32  
33  
34  /**
35   * Adapts index cursors to return just ServerEntry objects.
36   *
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   * @version $Rev$, $Date$
39   */
40  public class ServerEntryCursorAdaptor implements Cursor<ServerEntry>
41  {
42      private final Partition db;
43      private final IndexCursor<Long, ServerEntry> indexCursor;
44  
45      
46      public ServerEntryCursorAdaptor( Partition db, IndexCursor<Long, ServerEntry> indexCursor )
47      {
48          this.db = db;
49          this.indexCursor = indexCursor;
50      }
51      
52      
53      /* 
54       * @see Cursor#after(java.lang.Object)
55       */
56      public void after( ServerEntry element ) throws Exception
57      {
58          throw new UnsupportedOperationException();
59      }
60  
61  
62      /* 
63       * @see Cursor#afterLast()
64       */
65      public void afterLast() throws Exception
66      {
67          this.indexCursor.afterLast();
68      }
69  
70  
71      /* 
72       * @see Cursor#available()
73       */
74      public boolean available()
75      {
76          return indexCursor.available();
77      }
78  
79  
80      /* 
81       * @see Cursor#before(java.lang.Object)
82       */
83      public void before( ServerEntry element ) throws Exception
84      {
85          throw new UnsupportedOperationException();
86      }
87  
88  
89      /* 
90       * @see Cursor#beforeFirst()
91       */
92      public void beforeFirst() throws Exception
93      {
94          indexCursor.beforeFirst();
95      }
96  
97      
98      public final void setClosureMonitor( ClosureMonitor monitor )
99      {
100         indexCursor.setClosureMonitor( monitor );
101     }
102     
103 
104     /* 
105      * @see Cursor#close()
106      */
107     public void close() throws Exception
108     {
109         indexCursor.close();
110     }
111 
112 
113     /* 
114      * @see Cursor#close()
115      */
116     public void close( Exception e ) throws Exception
117     {
118         indexCursor.close( e );
119     }
120 
121 
122     /* 
123      * @see Cursor#first()
124      */
125     public boolean first() throws Exception
126     {
127         return indexCursor.first();
128     }
129 
130 
131     /* 
132      * @see Cursor#get()
133      */
134     public ServerEntry get() throws Exception
135     {
136         IndexEntry<Long,ServerEntry> indexEntry = indexCursor.get();
137 
138         if ( indexEntry.getObject() == null )
139         {
140             indexEntry.setObject( db.lookup( indexEntry.getId() ) );
141         }
142 
143         return indexEntry.getObject();
144     }
145 
146 
147     /* 
148      * @see Cursor#isClosed()
149      */
150     public boolean isClosed() throws Exception
151     {
152         return indexCursor.isClosed();
153     }
154 
155 
156     /* 
157      * @see Cursor#isElementReused()
158      */
159     public boolean isElementReused()
160     {
161         return indexCursor.isElementReused();
162     }
163 
164 
165     /* 
166      * @see Cursor#last()
167      */
168     public boolean last() throws Exception
169     {
170         return indexCursor.last();
171     }
172 
173 
174     /* 
175      * @see Cursor#next()
176      */
177     public boolean next() throws Exception
178     {
179         return indexCursor.next();
180     }
181 
182 
183     /* 
184      * @see Cursor#previous()
185      */
186     public boolean previous() throws Exception
187     {
188         return indexCursor.previous();
189     }
190 
191 
192     /* 
193      * @see Iterable#iterator()
194      */
195     public Iterator<ServerEntry> iterator()
196     {
197         return new CursorIterator<ServerEntry>( this );
198     }
199 }