samedi 29 août 2015

Can we use Function which takes dynamic values for optional interface for java8

I am new to java 8. Trying to pass Few inputs dynamically to get the values? Please let me know better suggestion

I have one object Request which will have input1(),input2() methods

void methodExecute(){ 
  Optional<Request> request = Optional.of(new Request()); 
 request.map(request::getInput1); //gives input1 value
  request.map(request::getInput2); //gives input2 value 
}

if we get getInput1,getInput2 comes as input(dynically) to this above method, can we dynamically get the values

below one is my approach. but not worked out

@FunctionalInterface
public interface Function_WithExceptions<T, V,R, E extends Exception> {
    R apply(T t,V v) throws E;
}

public class LambdaUtil<Input1, Input2> {
    public static <Input1,Input2, R, E extends Exception> 
                            Function_WithExceptions<Input1,Input2, R,E> rethrowFunction(Function_WithExceptions<Input1,Input2, R, E> function) throws E  {
        return (t,v) -> {
            try {
                return function.apply(t,v);
            } catch (Exception exception) {
                throwActualException(exception);
                return null;
            }
        };
    }
    @SuppressWarnings("unchecked")
    private static <E extends Exception> void throwActualException(Exception exception) throws E {
        throw (E) exception;
    }
}

public Function_WithExceptions getFunction(){
        Function_WithExceptions<GathererRequest, String,Object,Exception> requestObjParamFun = (reqObj,req)->MethodUtils.invokeExactMethod(reqObj, req);
        return requestObjParamFun;
}





Aucun commentaire:

Enregistrer un commentaire