Java Reference
In-Depth Information
Listing 5-9. The tryAdvance Method for the ResultSetSpliterator
/**
* Given a {@link ResultSet} instance on a current row, return a
* {@code RESULT_T} instance for that row.
* This code should not call {@link java.sql.ResultSet#next()} or
* otherwise mutate the result set: it should be treated as read-only.
*
* @param resultSet The result set to load.
* @return The result instance.
* @throws SQLException If an error occurs.
*/
protected abstract RESULT_T processRow(ResultSet resultSet)
throws SQLException;
/**
* If a remaining element exists, performs the given action on it,
* returning {@code true}; else returns {@code false}. If this
* Spliterator is {@link #ORDERED} the action is performed on the
* next element in encounter order. Exceptions thrown by the
* action are relayed to the caller.
*
* @param action The action
* @return {@code false} if no remaining elements existed
* upon entry to this method, else {@code true}.
* @throws NullPointerException if the specified action is null
*/
@Override
public boolean tryAdvance(final Consumer<? super RESULT_T> action) {
Objects.requireNonNull(action, "action to be performed");
try {
if (resultSet.isClosed() || !resultSet.next()) {
return false;
}
RESULT_T result = processRow(resultSet);
if (result == null) {
throw new NullPointerException("Returned null from processRow");
} else {
action.accept(result);
return true;
}
} catch (SQLException sqle) {
throw new RuntimeException("SQL Exception while processing result set",
sqle);
}
}
The constructor for this class will have to extend the AbstractSpliterator constructor. That constructor
takes two arguments: the estimated size of the spliterator and the characteristics of the spliterator. The
estimated size of the spliterator is just that: it is a vague estimation of the number of elements returned.
If the size of the spliterator is not known at all, then the magic value Long.MAX_VALUE is used. The second
argument is the characteristics of the spliterator, and this is how you suggest to the runtime how your
spliterator can be used.
 
Search WWH ::




Custom Search