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.shared.ldap.cursor; 021 022 023 import java.util.Iterator; 024 025 import org.apache.directory.shared.i18n.I18n; 026 027 028 /** 029 * An Iterator over a Cursor so Cursors can be Iterable for using in foreach 030 * constructs. 031 * 032 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 033 * @version $$Rev$$ 034 */ 035 public class CursorIterator<E> implements Iterator<E> 036 { 037 /** The inner cursor we will iterate */ 038 private final Cursor<E> cursor; 039 040 /** A flag used to store the cursor state */ 041 private boolean available; 042 043 044 /** 045 * 046 * Creates a new instance of CursorIterator. 047 * 048 * @param cursor The inner cursor 049 */ 050 public CursorIterator( Cursor<E> cursor ) 051 { 052 this.cursor = cursor; 053 this.available = cursor.available(); 054 } 055 056 057 /** 058 * {@inheritDoc} 059 */ 060 public boolean hasNext() 061 { 062 return available; 063 } 064 065 066 /** 067 * {@inheritDoc} 068 */ 069 public E next() 070 { 071 try 072 { 073 E element = cursor.get(); 074 available = cursor.next(); 075 return element; 076 } 077 catch ( Exception e ) 078 { 079 throw new RuntimeException( I18n.err( I18n.ERR_02002 ), e ); 080 } 081 } 082 083 084 /** 085 * {@inheritDoc} 086 */ 087 public void remove() 088 { 089 throw new UnsupportedOperationException( I18n.err( I18n.ERR_02003 ) ); 090 } 091 }