Java Reference
In-Depth Information
Representing the Intermediary Query Results
Java's standard SQL library represents the result set of the query as a java.sql.ResultSet . A result set
provides a streaming set of results, so we will represent it functionally using the java.util.function.Stream .
But what is the type of the elements of that stream? Traditionally, the answer would be a Java bean, with
each field in the query result becoming a property of the Java bean: you would have getters and setters for
all four of the fields in the SQL query. In a world with lambdas, however, we will instead use an immutable
Java object, as given in Listing 5-1.
Listing 5-1. The Java Object Representing the Results
/**
* Functional-friendly class for storing results of querying the word
* usage database.
*/
public class WordUsage {
private final String textName;
private final int lineOffset;
private final String word;
private final int wordOffset;
public WordUsage(
final String textName, final int lineOffset,
final String word, final int wordOffset
) {
this.textName = textName;
this.lineOffset = lineOffset;
this.word = word;
this.wordOffset = wordOffset;
}
public String getTextName() {
return textName;
}
public WordUsage withTextName(String textName) {
return new WordUsage(textName, lineOffset, word, wordOffset);
}
public int getLineOffset() {
return lineOffset;
}
public WordUsage withLineOffset(int lineOffset) {
return new WordUsage(textName, lineOffset, word, wordOffset);
}
public String getWord() {
return word;
}
 
Search WWH ::




Custom Search