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 */
020 package org.apache.directory.server.core.partition.impl.btree;
021
022
023 import java.util.Iterator;
024
025 import org.apache.directory.server.xdbm.IndexCursor;
026 import org.apache.directory.server.xdbm.IndexEntry;
027 import org.apache.directory.shared.ldap.cursor.ClosureMonitor;
028 import org.apache.directory.shared.ldap.cursor.Cursor;
029 import org.apache.directory.shared.ldap.cursor.CursorIterator;
030 import org.apache.directory.shared.ldap.entry.ServerEntry;
031
032
033 /**
034 * Adapts index cursors to return just ServerEntry objects.
035 *
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 * @version $Rev$, $Date$
038 */
039 public class ServerEntryCursorAdaptor<ID> implements Cursor<ServerEntry>
040 {
041 private final BTreePartition<ID> db;
042 private final IndexCursor<ID, ServerEntry, ID> indexCursor;
043
044
045 public ServerEntryCursorAdaptor( BTreePartition<ID> db, IndexCursor<ID, ServerEntry, ID> indexCursor )
046 {
047 this.db = db;
048 this.indexCursor = indexCursor;
049 }
050
051
052 /*
053 * @see Cursor#after(java.lang.Object)
054 */
055 public void after( ServerEntry element ) throws Exception
056 {
057 throw new UnsupportedOperationException();
058 }
059
060
061 /*
062 * @see Cursor#afterLast()
063 */
064 public void afterLast() throws Exception
065 {
066 this.indexCursor.afterLast();
067 }
068
069
070 /*
071 * @see Cursor#available()
072 */
073 public boolean available()
074 {
075 return indexCursor.available();
076 }
077
078
079 /*
080 * @see Cursor#before(java.lang.Object)
081 */
082 public void before( ServerEntry element ) throws Exception
083 {
084 throw new UnsupportedOperationException();
085 }
086
087
088 /*
089 * @see Cursor#beforeFirst()
090 */
091 public void beforeFirst() throws Exception
092 {
093 indexCursor.beforeFirst();
094 }
095
096
097 public final void setClosureMonitor( ClosureMonitor monitor )
098 {
099 indexCursor.setClosureMonitor( monitor );
100 }
101
102
103 /*
104 * @see Cursor#close()
105 */
106 public void close() throws Exception
107 {
108 indexCursor.close();
109 }
110
111
112 /*
113 * @see Cursor#close()
114 */
115 public void close( Exception e ) throws Exception
116 {
117 indexCursor.close( e );
118 }
119
120
121 /*
122 * @see Cursor#first()
123 */
124 public boolean first() throws Exception
125 {
126 return indexCursor.first();
127 }
128
129
130 /*
131 * @see Cursor#get()
132 */
133 public ServerEntry get() throws Exception
134 {
135 IndexEntry<ID, ServerEntry, ID> indexEntry = indexCursor.get();
136
137 if ( indexEntry.getObject() == null )
138 {
139 indexEntry.setObject( db.lookup( indexEntry.getId() ) );
140 }
141
142 return indexEntry.getObject();
143 }
144
145
146 /*
147 * @see Cursor#isClosed()
148 */
149 public boolean isClosed() throws Exception
150 {
151 return indexCursor.isClosed();
152 }
153
154
155 /*
156 * @see Cursor#isElementReused()
157 */
158 public boolean isElementReused()
159 {
160 return indexCursor.isElementReused();
161 }
162
163
164 /*
165 * @see Cursor#last()
166 */
167 public boolean last() throws Exception
168 {
169 return indexCursor.last();
170 }
171
172
173 /*
174 * @see Cursor#next()
175 */
176 public boolean next() throws Exception
177 {
178 return indexCursor.next();
179 }
180
181
182 /*
183 * @see Cursor#previous()
184 */
185 public boolean previous() throws Exception
186 {
187 return indexCursor.previous();
188 }
189
190
191 /*
192 * @see Iterable#iterator()
193 */
194 public Iterator<ServerEntry> iterator()
195 {
196 return new CursorIterator<ServerEntry>( this );
197 }
198 }