Java Reference
In-Depth Information
* 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()) {
doClose();
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);
}
}
private void doClose() {
try {
if (resultSet != null && !resultSet.isClosed()) resultSet.close();
} catch (SQLException ignore) {}
for (int i = 0; i < toClose.length; i++) {
try {
AutoCloseable closeMe = toClose[i];
toClose[i] = null;
if (closeMe != null && closeMe != resultSet) closeMe.close();
} catch (Exception ignore) {}
}
}
}
With our utility class finally built, we can build our stream for the WordUsage query. We will simply
declare an instance of the ResultSetSpliterator that delegates its ResultSet handling to our WordUsage.
fromResultSet utility method. Since we will return a distinct WordUsage each time, we can pass the
DISTINCT characteristic. We also pass it the connection from the ResultSet, so that it will close the
connection as soon as the spliterator is exhausted. With that spliterator constructed, we will then pass in the
spliterator to generate a parallel stream. That resulting code is in Listing 5-12.
 
Search WWH ::




Custom Search