Skip to content

Instantly share code, notes, and snippets.

@zeryx
Created April 26, 2019 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeryx/c6e7317bbe4cdc5f5506cadbcd0f545a to your computer and use it in GitHub Desktop.
Save zeryx/c6e7317bbe4cdc5f5506cadbcd0f545a to your computer and use it in GitHub Desktop.
public class AlgorithmHandler<INPUT, O, CONTEXT> {
private BiFunction<INPUT, CONTEXT, O> applyFunc;
private Supplier<CONTEXT> loadFunc;
private HashMap<String, Object> context;
private <T, J, L> BiFunction<T, J, L> applyHandler(FunctionWithException<T, J, L> fe) {
return (arg1, arg2) -> {
try {
return fe.apply(arg1, arg2);
} catch (Exception e) {
throw new RuntimeException(e);
}
};
}
private <J> Supplier<J> loadHandler(SupplierWithException<J> fe) {
return () -> {
try {
return fe.apply();
} catch (Exception e) {
throw new RuntimeException(e);
}
};
}
public AlgorithmHandler(){ }
public AlgorithmHandler(FunctionWithException<INPUT, CONTEXT, O> applyFunc, SupplierWithException<CONTEXT> loadFunc){
this.applyFunc = applyHandler(applyFunc);
this.loadFunc = loadHandler(loadFunc);
}
public AlgorithmHandler(FunctionWithException<INPUT, O> applyFunc){
this.applyFunc = applyHandler(applyFunc);
}
public void setApply(FunctionWithException<INPUT, CONTEXT, O> func){
applyFunc = applyHandler(func);
}
public void setLoad(SupplierWithException<CONTEXT> func){
loadFunc = loadHandler(func);
}
public void run(){
System.out.println(this.applyFunc.getClass());
System.out.println(this.loadFunc.getClass());
System.out.println("Not implemented");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment