8281298: Revise the creation of unmodifiable list

Reviewed-by: redestad
This commit is contained in:
Xue-Lei Andrew Fan 2022-02-07 06:30:10 +00:00
parent 5dfff7406e
commit f2302822c0

View file

@ -26,12 +26,7 @@
package javax.net.ssl; package javax.net.ssl;
import java.security.AlgorithmConstraints; import java.security.AlgorithmConstraints;
import java.util.Map; import java.util.*;
import java.util.List;
import java.util.HashMap;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
/** /**
* Encapsulates parameters for an SSL/TLS/DTLS connection. The parameters * Encapsulates parameters for an SSL/TLS/DTLS connection. The parameters
@ -81,8 +76,8 @@ public class SSLParameters {
private boolean needClientAuth; private boolean needClientAuth;
private String identificationAlgorithm; private String identificationAlgorithm;
private AlgorithmConstraints algorithmConstraints; private AlgorithmConstraints algorithmConstraints;
private Map<Integer, SNIServerName> sniNames = null; private List<SNIServerName> sniNames = null; // immutable list
private Map<Integer, SNIMatcher> sniMatchers = null; private Collection<SNIMatcher> sniMatchers = null; // immutable collection
private boolean preferLocalCipherSuites; private boolean preferLocalCipherSuites;
private boolean enableRetransmissions = true; private boolean enableRetransmissions = true;
private int maximumPacketSize = 0; private int maximumPacketSize = 0;
@ -331,22 +326,29 @@ public class SSLParameters {
* @since 1.8 * @since 1.8
*/ */
public final void setServerNames(List<SNIServerName> serverNames) { public final void setServerNames(List<SNIServerName> serverNames) {
if (serverNames != null) { if (this.sniNames == serverNames) {
if (!serverNames.isEmpty()) { return;
sniNames = new LinkedHashMap<>(serverNames.size()); }
if (serverNames == null) {
sniNames = null;
} else if (serverNames.isEmpty()) {
sniNames = Collections.emptyList();
} else {
List<Integer> sniTypes = new ArrayList<>(serverNames.size());
List<SNIServerName> sniValues = new ArrayList<>(serverNames.size());
for (SNIServerName serverName : serverNames) { for (SNIServerName serverName : serverNames) {
if (sniNames.put(serverName.getType(), if (sniTypes.contains(serverName.getType())) {
serverName) != null) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Duplicated server name of type " + "Duplicated server name of type " +
serverName.getType()); serverName.getType());
}
}
} else { } else {
sniNames = Collections.<Integer, SNIServerName>emptyMap(); sniTypes.add(serverName.getType());
sniValues.add(serverName);
} }
} else { }
sniNames = null;
sniNames = Collections.unmodifiableList(sniValues);
} }
} }
@ -388,15 +390,7 @@ public class SSLParameters {
* @since 1.8 * @since 1.8
*/ */
public final List<SNIServerName> getServerNames() { public final List<SNIServerName> getServerNames() {
if (sniNames != null) { return sniNames;
if (!sniNames.isEmpty()) {
return List.copyOf(sniNames.values());
} else {
return Collections.<SNIServerName>emptyList();
}
}
return null;
} }
/** /**
@ -424,22 +418,29 @@ public class SSLParameters {
* @since 1.8 * @since 1.8
*/ */
public final void setSNIMatchers(Collection<SNIMatcher> matchers) { public final void setSNIMatchers(Collection<SNIMatcher> matchers) {
if (matchers != null) { if (this.sniMatchers == matchers) {
if (!matchers.isEmpty()) { return;
sniMatchers = new HashMap<>(matchers.size()); }
if (matchers == null) {
this.sniMatchers = null;
} else if (matchers.isEmpty()) {
sniMatchers = Collections.emptyList();
} else {
List<Integer> matcherTypes = new ArrayList<>(matchers.size());
List<SNIMatcher> matcherValues = new ArrayList<>(matchers.size());
for (SNIMatcher matcher : matchers) { for (SNIMatcher matcher : matchers) {
if (sniMatchers.put(matcher.getType(), if (matcherTypes.contains(matcher.getType())) {
matcher) != null) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Duplicated server name of type " + "Duplicated server name of type " +
matcher.getType()); matcher.getType());
}
}
} else { } else {
sniMatchers = Collections.<Integer, SNIMatcher>emptyMap(); matcherTypes.add(matcher.getType());
matcherValues.add(matcher);
} }
} else { }
sniMatchers = null;
this.sniMatchers = Collections.unmodifiableList(matcherValues);
} }
} }
@ -462,15 +463,7 @@ public class SSLParameters {
* @since 1.8 * @since 1.8
*/ */
public final Collection<SNIMatcher> getSNIMatchers() { public final Collection<SNIMatcher> getSNIMatchers() {
if (sniMatchers != null) { return sniMatchers;
if (!sniMatchers.isEmpty()) {
return List.copyOf(sniMatchers.values());
} else {
return Collections.<SNIMatcher>emptyList();
}
}
return null;
} }
/** /**