/*******************************************************************************
* Copyright (c) 2005, 2007 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0, which accompanies this distribution
* and is available at http://www.eclipse.org/legal/epl-v10.html.
*
* Contributors:
* Oracle - initial API and implementation
******************************************************************************/
package org.eclipse.jpt.utility.internal.iterators;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import org.eclipse.jpt.utility.internal.StringTools;
/**
* A ResultSetIterator
wraps a ResultSet
* and transforms its rows for client consumption. Subclasses can override
* #buildNext(ResultSet)
to build the expected object from
* the current row of the result set.
*
* To use, supply:
ResultSet
* Adapter
that converts a row in the ResultSet
* into the desired object
* (alternatively, subclass ResultSetIterator
* and override the buildNext(ResultSet)
method)
*
*/
public class ResultSetIteratorSQLException
s will
* be caught and wrapped in a RuntimeException
.
*/
protected E buildNext(ResultSet rs) throws SQLException {
return this.adapter.buildNext(rs);
}
public boolean hasNext() {
return ! this.done;
}
public E next() {
if (this.done) {
throw new NoSuchElementException();
}
E temp = this.next;
this.next = this.buildNext();
return temp;
}
public void remove() {
throw new UnsupportedOperationException();
}
@Override
public String toString() {
return StringTools.buildToStringFor(this, this.resultSet);
}
// ********** interface **********
/**
* Used by ResultSetIterator
to convert a
* ResultSet
's current row into the next object
* to be returned by the Iterator
.
*/
public interface AdapterSQLException
s will
* be caught and wrapped in a RuntimeException
.
* @see java.sql.ResultSet
*/
T buildNext(ResultSet rs) throws SQLException;
final class Default implements Adapter {
@SuppressWarnings("unchecked")
public static final Adapter INSTANCE = new Default();
@SuppressWarnings("unchecked")
public static