org.deegree.commons.jdbc
Class ResultSetIterator<T>

java.lang.Object
  extended by org.deegree.commons.jdbc.ResultSetIterator<T>
Type Parameters:
T - type of the iterated objects
All Implemented Interfaces:
java.util.Iterator<T>, CloseableIterator<T>

public abstract class ResultSetIterator<T>
extends java.lang.Object
implements CloseableIterator<T>

Abstract base class for easy implementation of CloseableIterators that are backed by an SQL result set.

Version:
$Revision: 22129 $, $Date: 2010-01-21 19:03:13 +0100 (Do, 21. Jan 2010) $
Author:
Markus Schneider, last edited by: $Author: mschneider $

Constructor Summary
protected ResultSetIterator(java.sql.ResultSet rs, java.sql.Connection conn, java.sql.Statement stmt)
          Creates a new ResultSetIterator instance.
 
Method Summary
 void close()
          Frees all underlying resources, no other method calls are permitted after calling this.
protected abstract  T createElement(java.sql.ResultSet rs)
          Invoked to create the next element in the iteration sequence from the ResultSet (usually from one row).
 java.util.Collection<T> getAsCollectionAndClose(java.util.Collection<T> collection)
          Copies the elements into the given Collection and closes the iterator.
 java.util.List<T> getAsListAndClose()
          Returns the elements as an easy accessible List and closes the iterator.
 boolean hasNext()
           
 T next()
           
 void remove()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ResultSetIterator

protected ResultSetIterator(java.sql.ResultSet rs,
                            java.sql.Connection conn,
                            java.sql.Statement stmt)
Creates a new ResultSetIterator instance.

Parameters:
rs - result set that the iterator uses to build the elements, must not be null
conn - connection that was used to obtain the result set, must not be null
stmt - statement that was used to obtain the result set, must not be null
Method Detail

close

public void close()
Description copied from interface: CloseableIterator
Frees all underlying resources, no other method calls are permitted after calling this.

Specified by:
close in interface CloseableIterator<T>

hasNext

public boolean hasNext()
Specified by:
hasNext in interface java.util.Iterator<T>

next

public T next()
Specified by:
next in interface java.util.Iterator<T>

createElement

protected abstract T createElement(java.sql.ResultSet rs)
                            throws java.sql.SQLException
Invoked to create the next element in the iteration sequence from the ResultSet (usually from one row).

Parameters:
rs - result set that is used to build the element, this is never null
Returns:
new element from the iteration sequence
Throws:
java.sql.SQLException - if the accessing of the result set or element creation fails

remove

public void remove()
Specified by:
remove in interface java.util.Iterator<T>

getAsListAndClose

public java.util.List<T> getAsListAndClose()
Description copied from interface: CloseableIterator
Returns the elements as an easy accessible List and closes the iterator.

NOTE: This should only be used for small numbers of elements.

Specified by:
getAsListAndClose in interface CloseableIterator<T>
Returns:
list that contains all elements, never null

getAsCollectionAndClose

public java.util.Collection<T> getAsCollectionAndClose(java.util.Collection<T> collection)
Description copied from interface: CloseableIterator
Copies the elements into the given Collection and closes the iterator.

Specified by:
getAsCollectionAndClose in interface CloseableIterator<T>
Parameters:
collection - collection where the elements are copied, must not be null
Returns:
collection that contains all elements, same instance as the parameter


Copyright © 2011. All Rights Reserved.