Optimize collection handling

pull/140/head
Richard Körber 2023-04-29 15:15:22 +02:00
parent 00830858ca
commit d0c2aafaf1
No known key found for this signature in database
GPG Key ID: AAB9FD19C78AA3E0
1 changed files with 11 additions and 15 deletions

View File

@ -14,7 +14,8 @@
package org.shredzone.acme4j; package org.shredzone.acme4j;
import static java.util.Collections.unmodifiableList; import static java.util.Collections.unmodifiableList;
import static java.util.stream.Collectors.toList; import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toUnmodifiableList;
import java.io.IOException; import java.io.IOException;
import java.io.Writer; import java.io.Writer;
@ -22,8 +23,7 @@ import java.net.URL;
import java.security.KeyPair; import java.security.KeyPair;
import java.security.cert.CertificateEncodingException; import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.util.ArrayList; import java.util.Collection;
import java.util.Collections;
import java.util.List; import java.util.List;
import edu.umd.cs.findbugs.annotations.Nullable; import edu.umd.cs.findbugs.annotations.Nullable;
@ -46,8 +46,8 @@ public class Certificate extends AcmeResource {
private static final long serialVersionUID = 7381527770159084201L; private static final long serialVersionUID = 7381527770159084201L;
private static final Logger LOG = LoggerFactory.getLogger(Certificate.class); private static final Logger LOG = LoggerFactory.getLogger(Certificate.class);
private @Nullable ArrayList<X509Certificate> certChain; private @Nullable List<X509Certificate> certChain;
private @Nullable ArrayList<URL> alternates; private @Nullable Collection<URL> alternates;
protected Certificate(Login login, URL certUrl) { protected Certificate(Login login, URL certUrl) {
super(login, certUrl); super(login, certUrl);
@ -68,8 +68,8 @@ public class Certificate extends AcmeResource {
LOG.debug("download"); LOG.debug("download");
try (var conn = getSession().connect()) { try (var conn = getSession().connect()) {
conn.sendCertificateRequest(getLocation(), getLogin()); conn.sendCertificateRequest(getLocation(), getLogin());
alternates = new ArrayList<>(conn.getLinks("alternate")); alternates = conn.getLinks("alternate");
certChain = new ArrayList<>(conn.readCertificates()); certChain = conn.readCertificates();
} }
} }
} }
@ -81,7 +81,7 @@ public class Certificate extends AcmeResource {
*/ */
public X509Certificate getCertificate() { public X509Certificate getCertificate() {
lazyDownload(); lazyDownload();
return certChain.get(0); return requireNonNull(certChain).get(0);
} }
/** /**
@ -93,7 +93,7 @@ public class Certificate extends AcmeResource {
*/ */
public List<X509Certificate> getCertificateChain() { public List<X509Certificate> getCertificateChain() {
lazyDownload(); lazyDownload();
return unmodifiableList(certChain); return unmodifiableList(requireNonNull(certChain));
} }
/** /**
@ -103,11 +103,7 @@ public class Certificate extends AcmeResource {
*/ */
public List<URL> getAlternates() { public List<URL> getAlternates() {
lazyDownload(); lazyDownload();
if (alternates != null) { return requireNonNull(alternates).stream().collect(toUnmodifiableList());
return unmodifiableList(alternates);
} else {
return Collections.emptyList();
}
} }
/** /**
@ -120,7 +116,7 @@ public class Certificate extends AcmeResource {
var login = getLogin(); var login = getLogin();
return getAlternates().stream() return getAlternates().stream()
.map(login::bindCertificate) .map(login::bindCertificate)
.collect(toList()); .collect(toUnmodifiableList());
} }
/** /**