How to combine two similar methods into one?

Chris Ragner

I have two similar methods that differs in their check conditions. Give two similar methods, how can i combine these two into one? Is this possible with closures?

def someCondition = false

def method1() {
    while(!someCondition) {
        def connectionStatus = getConnectionStatus() // return 200, 404, etc.
        if (connectionStatus == 200) {
            someCondition = true
        } else {
            println "repeat until connection is 200"
            sleep 15
        }
    }
}

and

def someCondition = false

def method2(){
    while(!someCondition) {
        def result = getResult() // A, B, C, etc. 
        if (result in ["A", "B", "C"]) {
            someCondition = true
        } else {
            println "waiting until result is either A, B, or C"
            sleep 15
            result = getResult() // call again to check if result changed
        }
    }
}

I could try making it into one method and have different cases where it waits on different if condition depending on the case, but that is no different than just using two different methods. Is there any elegant way to solve this problem in Groovy?

tim_yates

You could write a method like this, which takes a Closure that returns true if successful:

def waitUntil(int sleep = 1, int maxRetries = 10, Closure<Boolean> test) {
    int retries = 0
    while (retries++ < maxRetries && !test()) {
        println "Failed at attempt $retries/$maxRetries, sleeping for $sleep ms before retrying"
        Thread.sleep(sleep)
    }
}

Then, you could call it like so:

waitUntil {
    getResult() in ["A", "B", "C"]
}

Or make it sleep longer with:

waitUntil(100) {
    getResult() in ["A", "B", "C"]
}

Or make it sleep longer between retries, and only retry 5 times:

waitUntil(100, 5) {
    getResult() in ["A", "B", "C"]
}

Is that what you meant?

Edit

As pointed out, this creates a list each time the closure is called, so for long-running fast looping tasks this could (maybe) create a lot of dead objects for the Garbage Collector to pick up.

A way to fix this, is to pass the expected values in to the main method, and pass this as an argument to the closure.

We then only get one list created:

def waitUntil(expectedResult, int sleep = 1, int maxRetries = 10, Closure<Boolean> test) {
    int retries = 0
    while (retries++ < maxRetries && !test(expectedResult)) {
        println "Failed at attempt $retries/$maxRetries, sleeping for $sleep ms before retrying"
        Thread.sleep(sleep)
    }
}

waitUntil(["A", "B", "C"]) {
    getResult() in it
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to combine two similar methods in java?

How to combine two almost similar RxJava methods?

How to Combine many similar methods and views into one

How to combine similar methods?

How to refactor two similar methods in one

How to use generics to combine these two methods Into one?

Combine two methods into one

Combine two similar functions to one

How can i combine two similar php codes into one?

Combine two .join methods into one

Java: How can I combine two very similar methods in java which result in two seperate ListArrays?

How can I combine two very similar methods in java which result in two seperate ListArrays?

How to efficiently combine one link calling on two methods?

Combine two similar rows into one with new columns

how to combine similar lines of code into one

How do you combine two similar nested dictionaries into one, each with some shared and unique nested elements (Python)?

Is there any way to combine these two methods into one method, or overloaded methods?

How to combine two partitions into one?

How to combine two service into one

How to combine two groupby into one

how to combine this two sql into one

How to combine two SKTextures into one

How to simplify several methods with similar bodies into one?

Is there a way to combine two similar legends into one simple legend using ggplot?

How to combine all similar array looping methods? Java 1.8

How to combine two methods for uploading file?

How to search for one of two similar strings in Vim?

How to combine two similar functions that convert between hiragana and katakana?

How to combine two list containing dictionary with similar keys?