Skip to content

Commit e2de905

Browse files
committed
PR update - clarify duplicated options code
1 parent 49f1f13 commit e2de905

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

driver-core/src/main/com/mongodb/ConnectionString.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import static com.mongodb.internal.connection.OidcAuthenticator.OidcValidator.validateCreateOidcCredential;
5757
import static java.lang.String.format;
5858
import static java.util.Arrays.asList;
59+
import static java.util.Collections.emptyList;
5960
import static java.util.Collections.singletonList;
6061
import static java.util.Collections.unmodifiableList;
6162

@@ -1260,20 +1261,24 @@ private void validateProxyParameters(final Map<String, List<String>> optionsMap)
12601261
"Both proxyUsername and proxyPassword must be set together. They cannot be set individually");
12611262
}
12621263

1263-
if (proxyHost != null && optionsMap.get("proxyhost").size() > 1) {
1264+
if (containsDuplicatedOptions("proxyhost", optionsMap)) {
12641265
throw new IllegalArgumentException("Duplicated values for proxyHost: " + optionsMap.get("proxyhost"));
12651266
}
1266-
if (proxyPort != null && optionsMap.get("proxyport").size() > 1) {
1267+
if (containsDuplicatedOptions("proxyport", optionsMap)) {
12671268
throw new IllegalArgumentException("Duplicated values for proxyPort: " + optionsMap.get("proxyport"));
12681269
}
1269-
if (proxyPassword != null && optionsMap.get("proxypassword").size() > 1) {
1270+
if (containsDuplicatedOptions("proxypassword", optionsMap)) {
12701271
throw new IllegalArgumentException("Duplicated values for proxyPassword: " + optionsMap.get("proxypassword"));
12711272
}
1272-
if (proxyUsername != null && optionsMap.get("proxyusername").size() > 1) {
1273+
if (containsDuplicatedOptions("proxyusername", optionsMap)) {
12731274
throw new IllegalArgumentException("Duplicated values for proxyUsername: " + optionsMap.get("proxyusername"));
12741275
}
12751276
}
12761277

1278+
private static boolean containsDuplicatedOptions(final String optionName, final Map<String, List<String>> optionsMap) {
1279+
return optionsMap.getOrDefault(optionName, emptyList()).size() > 1;
1280+
}
1281+
12771282
private int countOccurrences(final String haystack, final String needle) {
12781283
return haystack.length() - haystack.replace(needle, "").length();
12791284
}

0 commit comments

Comments
 (0)