I play with lambdas and realized that I want to try creating a simple db / object mapper as part of the training.
Yes, there are many frameworks that already do this, but it has more to do with learning, and the problem I came across is technical.
At first I wanted to define all the display logic in an enumeration.
It started simple and simple with a few field names:
enum ThingColumn {
id, language;
}
This allows me to create the following method (implementation is not relevant) that gives the api a compilation command using columns:
public Collection<Thing> findAll(ThingColumn... columns);
After that, I would like to define more rules in the enumeration, in particular, how the results are displayed from the class java.sql.ResultSetto mine Thing.
Running simple, I created a functional interface:
@FunctionalInterface
static interface ThingResultMapper {
void map(Thing to, ResultSet from, String column) ;
}
and added it to the listing:
enum ThingColumn {
id((t, rs, col) -> t.setId(rs.getLong(col))),
language((t, rs, col) ->t.setLanguage(rs.getString(col)));
ThingColumn(ThingResultMapper mapper){..}
}
mapResultSetRow, lambdas ResultSet:
public Thing mapResultSetRow(ResultSet rs, ThingColumn... fields) {
Thing t = new Thing();
Stream.of(fields)
.forEach(f -> f.getMapper().map(t, rs, f.name()));
return t;
}
findAll mapResultSetRow ResultSet. .
. , , . :
enum ThingColumn {
id(ResultSet::getLong, Thing::setId),
language(ResultSet::getString, Thing::setLanguage);
}
, , , , /. , :
enum ThingColumn {
id(ResultSet::getLong);
ThingColumn(Function<String,?> resultSetExtractor) {..}
}
: Cannot make a static reference to the non-static method getLong(String) from the type ResultSet.
, , , , , labmda .
:
Java 8
( , ) , .
:)
?