Java 中的可选方法参数

Optional method parameter in Java

我有这个函数 httpGet() 调用 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;
}

我想为 readTimeout 添加一个附加参数,该参数必须是可选的,并具有默认值,否则将使用该默认值。

在这种情况下,所有调用的 readTimeout 都设置为 5000,但我希望执行此特定调用的超时时间更长。

我想我需要这个新参数是可选的,因为我不想更改调用此 http() 方法的实现。

我是这样称呼它的:

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

如何为 readTimeout 实现新的可选参数?

通常,完成此操作的方法是提供许多具有不同签名的不同函数,这些函数仅采用默认值。这可以通过 "chaining" 一堆函数一起完成,每个函数指定它的默认值并调用更详细的函数。例如:

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);
}

您需要创建 2 个版本的 httpGet 方法(一个带有 readTimeout 参数,另一个没有它,它将使用默认值调用第一个版本):

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;
}

现在您可以决定提供超时

httpGet(DEFAULT_BROWSCAP_ENDPOINT, result, mytimeout)

是否

httpGet(DEFAULT_BROWSCAP_ENDPOINT, result)

Java 没有像 Python、C++、VBA、Delphi 和许多语言那样的默认值。使用备用签名创建新的构造函数。

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;
}