2

I have this function httpGet() which calls http():

public static int httpGet(String url, StringBuilder response) throws IOException {
    return http(url,(http)->http.setRequestMethod("GET"), response);
}

private static int http(String url, httpMethod method, StringBuilder response) throws IOException {
    HttpURLConnection http = (HttpURLConnection)new URL(url).openConnection();
    http.setConnectTimeout(5000);
    http.setReadTimeout(5000);
    method.doMethod(http);
    int status = 404;
    ......
    ......
    return status;
}

I want to add an additional parameter for readTimeout, which needs to be optional with a default value that will be used otherwise.

In this case readTimeout is set to 5000 for all the calls, but I want this specific call to be executed for longer timeouts.

I think I need this new parameter to be optional as I don't want to change the implementations where this http() method has been called.

This is how I call it:

Assert.assertEquals(HTTP_OK, httpGet(DEFAULT_BROWSCAP_ENDPOINT, result));

How can I implement a new optional parameter for readTimeout?

2
  • Java doesn't have default values like Python, C++, VBA, Delphi, and many languages. Create new constructors with the alternate signature. Commented Aug 27, 2015 at 8:21
  • You need to define a new method, (unfortunately?) java doesn't support optional parameters. Commented Aug 27, 2015 at 8:22

3 Answers 3

2

Java doesn't have default values like Python, C++, VBA, Delphi, and many languages. Create new constructors with the alternate signature.

public static int httpGet(String url, StringBuilder response) throws IOException {
    return httpGet(URL, response, 5000)
}

public static int httpGet(String url, StringBuilder response, int readTimeout) throws IOException {
    return http(url,(http)->http.setRequestMethod("GET"), response, readTimeout);
}


private static int http(String url, httpMethod method, StringBuilder response) throws IOException {
    return http(url, method, response, 5000);
}

private static int http(String url, httpMethod method, StringBuilder response, int readTimeout) throws IOException {
    HttpURLConnection http = (HttpURLConnection)new URL(url).openConnection();
    http.setConnectTimeout(5000);
    http.setReadTimeout(readTimeout;
    method.doMethod(http);
    int status = 404;
    ......
    ......
    return status;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I think there's no need to create two versions of the private http() method, right?
No need in having an unused constructor. I didn't know if there was other code that was dependent on the shorter constructor.
2

You need to create 2 versions of the httpGet method (one with the readTimeout parameter, and another one without it which will call the first version with a default value):

private static final long DEFAULT_READ_TIMEOUT = 5000;

public static int httpGet(String url, StringBuilder response) throws IOException {
    return httpGet(url, response, DEFAULT_READ_TIMEOUT);
}

public static int httpGet(String url, StringBuilder response, long readTimeout) throws IOException {
    return http(url,(http)->http.setRequestMethod("GET"), response, readTimeout);
}

private static int http(String url, httpMethod method, StringBuilder response, long readTimeout) throws IOException {
    HttpURLConnection http = (HttpURLConnection)new URL(url).openConnection();
    http.setConnectTimeout(5000);
    http.setReadTimeout(readTimeout);
    method.doMethod(http);
    int status = 404;
    ......
    ......
    return status;
}

Now you can decide to provide the timeout

httpGet(DEFAULT_BROWSCAP_ENDPOINT, result, mytimeout)

or not

httpGet(DEFAULT_BROWSCAP_ENDPOINT, result)

Comments

0

Generally the way this is done is by providing many different functions with different signatures that just assume a default value. This can be done by "chaining" a bunch of functions together, each specifying it's default value and calling the more detailed function. For example:

public void myFunctionWithALotOfArguments(int a, int b, int c, int d) {
    // do stuff
}
public void myFunctionWithLessArguments(int a, int b, int c) {
    myFunctionWithALotOfArguments(a, b, c, 50);
}
public void myFunctionWithFewArguments(int a, int b) {
    myFunctionWithLessArguments(a, b, 25);
}
public void mySimplifiedFunction(int a) {
    myFunctionWithFewArguments(a, 9);
}
public void justGuessWhatToDo() {
    mySimplifiedFunction(42);
}

Comments

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.