Reuse map if it is a SortedMap instead of creating a new one

This commit is contained in:
Isira Seneviratne 2024-07-20 16:55:56 +05:30
parent ec5fb7dc9a
commit 7fb4dbefec
1 changed files with 8 additions and 3 deletions

View File

@ -5,6 +5,7 @@ import javax.annotation.Nullable;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap; import java.util.TreeMap;
/** /**
@ -13,7 +14,7 @@ import java.util.TreeMap;
public class Response { public class Response {
private final int responseCode; private final int responseCode;
private final String responseMessage; private final String responseMessage;
private final Map<String, List<String>> responseHeaders; private final SortedMap<String, List<String>> responseHeaders;
private final String responseBody; private final String responseBody;
private final String latestUrl; private final String latestUrl;
@ -25,8 +26,12 @@ public class Response {
this.responseCode = responseCode; this.responseCode = responseCode;
this.responseMessage = responseMessage; this.responseMessage = responseMessage;
this.responseHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); if (responseHeaders instanceof SortedMap) {
this.responseHeaders.putAll(responseHeaders); this.responseHeaders = (SortedMap<String, List<String>>) responseHeaders;
} else {
this.responseHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
this.responseHeaders.putAll(responseHeaders);
}
this.responseBody = responseBody == null ? "" : responseBody; this.responseBody = responseBody == null ? "" : responseBody;
this.latestUrl = latestUrl; this.latestUrl = latestUrl;