Skip to content

Instantly share code, notes, and snippets.

@Jezza
Created August 27, 2018 07:19
Show Gist options
  • Save Jezza/76f3907dc07e06b3aa8d7a487689a38d to your computer and use it in GitHub Desktop.
Save Jezza/76f3907dc07e06b3aa8d7a487689a38d to your computer and use it in GitHub Desktop.
public interface CheckedFunction<I, O, T extends Throwable> {
O apply(I input) throws T;
}
private static <I, O, R, T extends Throwable> CheckedFunction<I, R, T> combine(CheckedFunction<I, O, T> left, CheckedFunction<O, R, T> right) {
return input -> right.apply(left.apply(input));
}
public static final class DBRow {
final String query;
public DBRow(String query) {
this.query = query;
}
public DBResult save() {
return DBResult.SUCCESS;
}
}
public enum DBResult {
ERROR,
SUCCESS
}
public static final class DBException extends Exception {
private static final long serialVersionUID = 3046735561004119190L;
}
public static DBRow fetchRow(String input) throws DBException {
if (input == null) {
throw new DBException();
}
return new DBRow(input);
}
public static String fetchRow0(String input) {
return input;
}
public static void main(String[] args) {
CheckedFunction<String, DBResult, DBException> fetchThenSave = combine(AbstractRepoModule::fetchRow, DBRow::save);
try {
fetchThenSave.apply("");
} catch (DBException e) {
e.printStackTrace();
}
CheckedFunction<String, byte[], RuntimeException> combine = combine(AbstractRepoModule::fetchRow0, String::getBytes);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment