//============================================================================== // AbstractDocumentIterator.java //============================================================================== package tribble.repository; import java.io.Closeable; import java.io.IOException; import java.lang.Exception; import java.lang.UnsupportedOperationException; import java.lang.String; import java.util.ArrayList; import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.NoSuchElementException; /******************************************************************************* * Abstract generic document set iterator. * *

* A repository document set iterator is an iterator over a collection of * repository documents. Such a set is typically the result of a query * performed on a repository. * *

* Note: This requires Java 1.5 or later. * * *

*
Source code:
*
* http://david.tribble.com/src/java/tribble/repository/AbstractDocumentIterator.java *
*
Documentation:
*
* http://david.tribble.com/docs/tribble/repository/AbstractDocumentIterator.html *
*
* * * @version API 3.0, $Revision: 1.3 $ $Date: 2012/03/17 22:59:40 $ * @since 2008-04-09 * @author David R. Tribble (david@tribble.com)
* Copyright ©2008-2012 by David R. Tribble, all rights reserved.
* Permission is granted to any person or entity except those designated * by the United States Department of State as a terrorist or terrorist * government or agency, to use and distribute this source code provided * that the original copyright notice remains present and unaltered. * * @see AbstractDocument */ public class AbstractDocumentIterator implements DocumentIterator { static final String REV = "@(#)tribble/repository/AbstractDocumentIterator.java $Revision: 1.3 $ $Date: 2012/03/17 22:59:40 $\n"; // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ // Variables /** Document folder for this iterator. */ protected Folder m_folder; /** Document set. */ protected ArrayList m_docs; /** Current document index within the set. */ protected int m_pos; /** Modification serial number. */ protected int m_modCount; // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ // Constructors /*************************************************************************** * Constructor. * * @param folder * Document folder containing the documents for this iterator. * * @param docs * Parent list of documents for which this iterator was created. * * @since API 3.0, 2012-02-17 */ protected AbstractDocumentIterator(Folder folder, ArrayList docs) { m_folder = folder; m_docs = docs; } // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ // Methods /*************************************************************************** * Close this document set iterator. * Deallocates all resources associated with the document set. Any further * use of this object will result in exceptions. * *

* Note that this method does not throw any exceptions. * *

* Note that this method to be called more than once. * * @since API 2.0, 2008-04-09 */ //@Override public void close() { m_docs = null; } /*************************************************************************** * Determine if there are any more documents within the document set. * * @return * True if one or more documents remain within the document set, false * otherwise. * * @throws IOException * Thrown if an error occurred while accessing the document * iterator. * * @since API 2.0, 2008-04-09 */ //@Override public boolean hasNext() { if (m_docs == null) return false; return (m_pos < m_docs.size()); } /*************************************************************************** * Retrieve the next document within the document set. * Also advances the iterator to the next document in the set. * *

* Note that this method also removes the previous document from the document * set, so calling {@link #remove remove()} is not necessary. * Note also that removed documents do not have their * {@link Document#close close()} methods called, which could leave * them in an unstable state. Callers of this method should therefore call * the close() method for each document returned. * * @return * The next repository document in the set. * * @since API 2.0, 2008-04-09 */ //@Override public DocType next() { DocType doc; // Sanity checks if (m_docs == null) throw new NoSuchElementException(); if (m_pos >= m_docs.size()) throw new NoSuchElementException(); // Remove the previous document from the set, // since it is no longer needed if (m_pos > 0) { m_modCount++; m_docs.set(m_pos - 1, null); } // Get the next document and advance the pointer doc = m_docs.get(m_pos++); return doc; } /*************************************************************************** * Remove the current document from the document set. * *

* Note that calling this method is not necessary, since {@link #next} * automatically removes each document it previously returned. * Note also that removed documents do not have their * {@link Document#close close()} methods called, which could leave * them in an unstable state. * * @throws UnsupportedOperationException (unchecked) * Thrown if this operation is not supported by this iterator. * * @throws IllegalStateException (unchecked) * Thrown if {@link #next} has not been called, or if {@link #remove} has * already been called on the same item in the set. * * @since API 2.0, 2008-04-09 */ //@Override public void remove() throws UnsupportedOperationException, IllegalStateException { // Sanity checks if (m_docs == null) throw new NoSuchElementException(); if (m_pos == 0) throw new IllegalStateException("next() not called yet"); if (m_docs.get(m_pos) == null) throw new IllegalStateException("Document already removed"); // Remove the document from the document set m_modCount++; m_docs.set(m_pos, null); } /*************************************************************************** * Retrieve the repository folder for this document set iterator. * * @return * The parent repository folder for this iterator, or null if it does not * have one. * * @throws IOException * Thrown if an error occurred while accessing the document. * * @since API 3.0, 2012-02-17 */ public Folder getFolder() { return m_folder; } } // End AbstractDocumentIterator.java