Skip to content

Instantly share code, notes, and snippets.

View nwillc's full-sized avatar

Nwillc nwillc

View GitHub Profile
@FunctionalInterface
public interface Extractor<T> {
/**
* Extract type T from the current position in the ResultSet.
*
* @param rs the ResultSet to extract from
* @return the type T extracted
* @throws SQLException should the extraction fail
*/
T extract(ResultSet rs) throws SQLException;
public static <T, B> void copy(B bean,
BiConsumer<B, T> setter,
ResultSet rs,
BiFunction<ResultSet, Integer, T> getter,
Integer index) {
setter.accept(bean, getter.apply(rs, index));
}
BiFunction<ResultSet, Integer, String> STRING = (r, i) -> {
try {
return r.getString(i);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
class Example1 {
final static BiFunction<ResultSet, Integer, String> STRING =
(r, index) -> {
try {
return r.getString(index);
} catch (SQLException e) {
throw new RuntimeException(e);
}
};
void example(ResultSet rs, Integer i) {
public final class ExtractorFactory<B> {
public ExtractorFactory<B> factory(Supplier<B> factory) { /* ... */ }
public <T> ExtractorFactory<B> add(
BiConsumer<B, T> setter,
BiFunction<ResultSet, Integer, T> getter,
Integer index) { /* ... */ }
public Extractor<B> getExtractor() { /* ... */ }
class Example {
final static BiFunction<ResultSet, Integer, String> STRING =
(r, index) -> {
try {
return r.getString(index);
} catch (SQLException e) {
throw new RuntimeException(e);
}
};
void example() {
public final class ExtractorFactory<B> {
private BiConsumer<B, ResultSet> consumer = (b, r) -> {};
private Supplier<B> factory;
public ExtractorFactory<B> factory(Supplier<B> factory) {
this.factory = factory;
return this;
}
public <T> ExtractorFactory<B> add(BiConsumer<B, T> setter,
public final class Extractors {
private Extractors() {}
public static final BiFunction<ResultSet, Integer, String> STRING_I = (r, i) -> {
try {
return r.getString(i);
} catch (SQLException e) {
throw new UncheckedSQLException(e);
}
};
@FunctionalInterface
public interface ThrowingBiFunction<T, U, R> extends BiFunction<T, U, R> {
@Override
default R apply(T t, U u) {
try {
return applyThrows(t, u);
} catch (Exception e) {
throw new RuntimeException(e);
}
public final class Extractors {
private Extractors() {
}
public static final ThrowingBiFunction<ResultSet, Integer, String> STRING_I
= ResultSet::getString;
public static final ThrowingBiFunction<ResultSet, String, String> STRING_S
= ResultSet::getString;