mercredi 1 novembre 2017

Java 8: Pass object's method & its arguments as parameter

I have two classes A & B , like this:

class A {
    public Integer fetchMax() {
       // Make a network call & return result
    } 
}

class B {
    public Double fetchPercentile(Integer input) {
        // Make a network call & return result
    } 
}

Now I need to provide retry mechanism for both methods fetchMax() & fetchPercentile(Integer). I want to provide this behaviour using a helper class, where the retry method which can take instance of (A or B), method-name and method-arguments. The retry would basically do reattempts on the provided method of the object.

Something like this:

class Retry {
     public static R retry(T obj, Function<T, R> method,  Object... arguments) {
           // Retry logic
           while(/* retry condition */)
           {
                obj.method(arguments);
           }
     }
}

Aucun commentaire:

Enregistrer un commentaire