Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to avoid repetition with same params

I am trying to find out a way to avoid the kind of repetition below

try {
        await().pollInterval(5, TimeUnit.SECONDS).pollDelay(500, TimeUnit.MILLISECONDS) 
          .atMost(30, TimeUnit.SECONDS).until(() -> {
                // 1. methods in here
            });
    } catch (final Exception e) {
        //TODO: can be handled internally
    }

It happens in several places in my code, and I want to make it less repetitive, but I am not finding a way to do so. I thought about lambdas, but I don`t know much about it nor if it would fit in here.

Inside of it can be many different things, it is not the same for all nor they have the same inheritance.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

public static void main(String... args) {
    awaitUntil(() -> {
        // payload 1
        return true;
    });

    awaitUntil(() -> {
        // payload 2
        return true;
    });
}

public static void awaitUntil(Callable<Boolean> conditionEvaluator) {
    try {
        Awaitility.await()
                  .pollInterval(5, TimeUnit.SECONDS)
                  .pollDelay(500, TimeUnit.MILLISECONDS)
                  .atMost(30, TimeUnit.SECONDS)
                  .until(conditionEvaluator);
    } catch(Exception e) {
        //TODO: can be handled internally
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading