From 3c9f1f85e1a17338281f97f153ca6b1804b2d664 Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Mon, 25 Jan 2021 20:12:15 +0100 Subject: [PATCH] Minor Improvements: * Use Empty collections * Unnecessary toString --- .../hc/client5/http/impl/cache/CacheKeyGenerator.java | 2 +- .../client5/http/impl/cache/RequestProtocolCompliance.java | 2 +- .../hc/client5/testing/async/AsyncRandomHandler.java | 2 +- .../apache/hc/client5/testing/classic/RandomHandler.java | 2 +- .../client5/http/entity/mime/MultipartEntityBuilder.java | 7 ++++++- .../client5/http/impl/classic/ContentCompressionExec.java | 7 ++++++- 6 files changed, 16 insertions(+), 6 deletions(-) diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheKeyGenerator.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheKeyGenerator.java index bb84aee7f..08f4eb767 100644 --- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheKeyGenerator.java +++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheKeyGenerator.java @@ -98,7 +98,7 @@ public class CacheKeyGenerator implements Resolver { if (headers == null) { return ""; } - final StringBuilder buf = new StringBuilder(""); + final StringBuilder buf = new StringBuilder(); for (int i = 0; i < headers.length; i++) { final Header hdr = headers[i]; if (i > 0) { diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/RequestProtocolCompliance.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/RequestProtocolCompliance.java index 1c9e13981..d4b380519 100644 --- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/RequestProtocolCompliance.java +++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/RequestProtocolCompliance.java @@ -121,7 +121,7 @@ class RequestProtocolCompliance { } private String buildHeaderFromElements(final List outElts) { - final StringBuilder newHdr = new StringBuilder(""); + final StringBuilder newHdr = new StringBuilder(); boolean first = true; for(final HeaderElement elt : outElts) { if (!first) { diff --git a/httpclient5-testing/src/main/java/org/apache/hc/client5/testing/async/AsyncRandomHandler.java b/httpclient5-testing/src/main/java/org/apache/hc/client5/testing/async/AsyncRandomHandler.java index 844ea4331..fa0e1486a 100644 --- a/httpclient5-testing/src/main/java/org/apache/hc/client5/testing/async/AsyncRandomHandler.java +++ b/httpclient5-testing/src/main/java/org/apache/hc/client5/testing/async/AsyncRandomHandler.java @@ -96,7 +96,7 @@ public class AsyncRandomHandler implements AsyncServerExchangeHandler { final String path = uri.getPath(); final int slash = path.lastIndexOf('/'); if (slash != -1) { - final String payload = path.substring(slash + 1, path.length()); + final String payload = path.substring(slash + 1); final long n; if (!payload.isEmpty()) { try { diff --git a/httpclient5-testing/src/main/java/org/apache/hc/client5/testing/classic/RandomHandler.java b/httpclient5-testing/src/main/java/org/apache/hc/client5/testing/classic/RandomHandler.java index 814121561..b68048f53 100644 --- a/httpclient5-testing/src/main/java/org/apache/hc/client5/testing/classic/RandomHandler.java +++ b/httpclient5-testing/src/main/java/org/apache/hc/client5/testing/classic/RandomHandler.java @@ -87,7 +87,7 @@ public class RandomHandler implements HttpRequestHandler { final String path = uri.getPath(); final int slash = path.lastIndexOf('/'); if (slash != -1) { - final String payload = path.substring(slash + 1, path.length()); + final String payload = path.substring(slash + 1); final long n; if (!payload.isEmpty()) { try { diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MultipartEntityBuilder.java b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MultipartEntityBuilder.java index dfeadedcd..f506a71aa 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MultipartEntityBuilder.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MultipartEntityBuilder.java @@ -63,6 +63,11 @@ public class MultipartEntityBuilder { private Charset charset; private List multipartParts; + /** + * An empty immutable {@code NameValuePair} array. + */ + private static final NameValuePair[] EMPTY_NAME_VALUE_ARRAY = new NameValuePair[0]; + public static MultipartEntityBuilder create() { return new MultipartEntityBuilder(); } @@ -201,7 +206,7 @@ public class MultipartEntityBuilder { if (charsetCopy != null) { paramsList.add(new BasicNameValuePair("charset", charsetCopy.name())); } - final NameValuePair[] params = paramsList.toArray(new NameValuePair[paramsList.size()]); + final NameValuePair[] params = paramsList.toArray(EMPTY_NAME_VALUE_ARRAY); final ContentType contentTypeCopy; if (contentType != null) { diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ContentCompressionExec.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ContentCompressionExec.java index 94a565318..f467fdb77 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ContentCompressionExec.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ContentCompressionExec.java @@ -75,13 +75,18 @@ public final class ContentCompressionExec implements ExecChainHandler { private final Lookup decoderRegistry; private final boolean ignoreUnknown; + /** + * An empty immutable {@code String} array. + */ + private static final String[] EMPTY_STRING_ARRAY = new String[0]; + public ContentCompressionExec( final List acceptEncoding, final Lookup decoderRegistry, final boolean ignoreUnknown) { this.acceptEncoding = MessageSupport.format(HttpHeaders.ACCEPT_ENCODING, acceptEncoding != null ? acceptEncoding.toArray( - new String[acceptEncoding.size()]) : new String[] {"gzip", "x-gzip", "deflate"}); + EMPTY_STRING_ARRAY) : new String[] {"gzip", "x-gzip", "deflate"}); this.decoderRegistry = decoderRegistry != null ? decoderRegistry : RegistryBuilder.create()