Created
August 27, 2018 07:19
-
-
Save Jezza/76f3907dc07e06b3aa8d7a487689a38d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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