0

APIResponse res = kpiAPIObject.getALLKPIDefinition(); --> function a

Boolean status = res.getNodeValues("shortName").contains(kpiName); --> function b

public void dynamicWait(function a,function b)
{
    long t = System.currentTimeMillis();
    while (t > System.currentTimeMillis() - 180000 ) {
        res = /* execute function a here */
        if(/* execute function b here */) {
            break;
        } else {
            Thread.sleep(30000);
            continue;
        }
    }  
}

Thanks in advance

1
  • if(res.getNodeValues("shortName").contains(kpiName)) {} Commented Jan 11, 2019 at 5:40

2 Answers 2

3

Sounds like you could use some lambdas:

Supplier<APIResponse> a = kpiAPIObject::getALLKPIDefinition;
Predicate<APIResponse> b = res -> res.getNodeValues("shortName").contains(kpiName);

Then call them like:

APIResponse res = a.get();
if (b.test(res)) {
    break;
}
Sign up to request clarification or add additional context in comments.

5 Comments

And the method signature would look something like: public <T> void dynamicWait(Supplier<T> a, Predicate<T> b)
In which case the first line would be T res = a.get();.
Lambda expression's parameter res cannot redeclare another local variable defined in an enclosing scope. in Predicate<APIResponse> b = res -> res.getNodeValues("shortName").contains(kpiName);
@Srikanta So rename res to something else.
still get the error ...Lambda expression's parameter res cannot redeclare another local variable defined in an enclosing scope.
3

In Java, there are various ways. If you take a look at java.util.function package, you can see

  • Function: Takes one argument, produces one result
  • Consumer: Takes one argument, produces nothing.
  • BiConsumer: Takes two arguments, produces nothing.
  • Supplier: Takes no argument, produces one result.
  • Predicate: Boolean value function of one argument

You can used them as inputs for your method and execute it within.

In you case, you will use Supplier for function a and Predicate for function b.

Supplier<APIResponse> a = () -> { return kpiAPIObject.getALLKPIDefinition(); }; Predicate<APIResponse> b = res -> res.getNodeValues("shortName").contains(kpiName);

public void dynamicWait(Supplier<APIResponse> a,Predicate<APIResponse> b)

{

long t = System.currentTimeMillis();

while (t > System.currentTimeMillis() - 180000 ){

            res = a.get();
            if(b.test(res)){
                break;
            }
            else{
                Thread.sleep(30000);
                continue;
            }
        }  

}

4 Comments

Lambda expression's parameter res cannot redeclare another local variable defined in an enclosing scope. in Predicate<APIResponse> b = res -> res.getNodeValues("shortName").contains(kpiName);
@Srikanta rename it to something else like response
still get the error ...Lambda expression's parameter res cannot redeclare another local variable defined in an enclosing scope.
@Srikanta change to Predicate<APIResponse> b = response -> response.getNodeValues("shortName").contains(kpiName);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.