i trying create api chaining tasks.
first, designed simple interface defining task:
public interface task<u, v> { u execute(v input); } where v input type , u output type of task.
what whish create chain class chain list of tasks.
for instance, execute: task1<typea, typeb> -> task2<typeb, typec> -> task3<typec, typed>
this chain class task<typea, typed>.
so wrote code, not compile :
public class chain<u, v> implements task<u, v> { list<task<?, ?>> tasklist; public chain() { tasklist = new linkedlist<task<?, ?>>(); } @override public u execute(v input) { v currentinput = input; u output = null; (task<?, ?> task : tasklist) { output = task.execute(currentinput); // compile error because currentinput of type v // , output of type u currentinput = output; // compile error } return output; } // other methods add , remove tasks in list } i understand why cannot compile have no idea how implement works , answers problem.
has ever faced kind of problem?
kind regards,
ben
your task class pretty same guava class function. use instead of defining own one.
an advantage of using function use helper methods in functions class, 1 of compose():
function<string,myobject1> stringtomyobject1 = ...; function<myobject1, anotherobject> myobject1toanotherobject = ...; function<string,anotherobject> stringtoanotherobject = functions.compose(myobject1toanotherobject, stringtomyobject1); deeper chaining can achieved repeated calls compose().
even if don't want use guava reason, can take inspiration approach: chaining 2 functions/task known type arguments creates new function/task easily-calculated type arguments.
Comments
Post a Comment