Minor Improvements:

* Add final to variable
* Unnecessary semicolon
* Use Standard Charset object
* Unnecessary conversion to String
* Simplifiable conditional expression
* Replace 'Arrays.asList()' with Collections.singletonList
* Redundant local variable. Simplify
This commit is contained in:
Arturo Bernal 2020-12-31 08:39:22 +01:00 committed by Oleg Kalnichevski
parent 25c124917b
commit 36e1bde6ff
26 changed files with 54 additions and 50 deletions

View File

@ -27,6 +27,7 @@
package org.apache.hc.client5.http.impl.cache;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
@ -86,7 +87,7 @@ class ResponseCachingPolicy {
this.sharedCache = sharedCache;
this.neverCache1_0ResponsesWithQueryString = neverCache1_0ResponsesWithQueryString;
if (allow303Caching) {
uncacheableStatusCodes = new HashSet<>(Arrays.asList(HttpStatus.SC_PARTIAL_CONTENT));
uncacheableStatusCodes = new HashSet<>(Collections.singletonList(HttpStatus.SC_PARTIAL_CONTENT));
} else {
uncacheableStatusCodes = new HashSet<>(Arrays.asList(HttpStatus.SC_PARTIAL_CONTENT, HttpStatus.SC_SEE_OTHER));
}

View File

@ -43,7 +43,7 @@ import org.apache.hc.core5.http.Header;
class WarningValue {
private int offs;
private int init_offs;
private final int init_offs;
private final String src;
private int warnCode;
private String warnAgent;

View File

@ -51,7 +51,7 @@ public abstract class AbstractProtocolTest {
protected static final int MAX_BYTES = 1024;
protected static final int MAX_ENTRIES = 100;
protected int entityLength = 128;
protected final int entityLength = 128;
protected HttpHost host;
protected HttpRoute route;
protected HttpEntity body;

View File

@ -29,7 +29,7 @@ package org.apache.hc.client5.http.impl.cache;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.Collections;
import org.apache.hc.client5.http.impl.RequestCopier;
import org.apache.hc.core5.http.HttpRequest;
@ -68,7 +68,7 @@ public class TestRequestProtocolCompliance {
final HttpRequest req = new BasicHttpRequest("PUT", "http://example.com/");
req.setHeader("If-Match", "W/\"weak\"");
impl = new RequestProtocolCompliance(true);
assertEquals(Arrays.asList(), impl.requestIsFatallyNonCompliant(req));
assertEquals(Collections.emptyList(), impl.requestIsFatallyNonCompliant(req));
}
@Test

View File

@ -35,7 +35,7 @@ public class TestConcurrentCountMap
private static final String IDENTIFIER = "some-identifier";
private ConcurrentCountMap<String> map = new ConcurrentCountMap<>();
private final ConcurrentCountMap<String> map = new ConcurrentCountMap<>();
@Test
public void testBasics() {

View File

@ -26,7 +26,7 @@
*/
package org.apache.hc.client5.testing.async;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
@ -505,7 +505,7 @@ public abstract class AbstractHttpAsyncClientAuthentication<T extends CloseableH
});
final RequestConfig config = RequestConfig.custom()
.setTargetPreferredAuthSchemes(Arrays.asList("MyBasic"))
.setTargetPreferredAuthSchemes(Collections.singletonList("MyBasic"))
.build();
final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);

View File

@ -29,7 +29,7 @@ package org.apache.hc.client5.testing.fluent;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.apache.hc.client5.http.ClientProtocolException;
import org.apache.hc.client5.http.fluent.Content;
@ -140,11 +140,11 @@ public class TestFluent extends LocalServerTestBase {
public void testContentAsStringWithCharset() throws Exception {
final HttpHost target = start();
final String baseURL = "http://localhost:" + target.getPort();
final Content content = Request.post(baseURL + "/echo").bodyByteArray("Ü".getBytes("utf-8")).execute()
final Content content = Request.post(baseURL + "/echo").bodyByteArray("Ü".getBytes(StandardCharsets.UTF_8)).execute()
.returnContent();
Assert.assertEquals((byte)-61, content.asBytes()[0]);
Assert.assertEquals((byte)-100, content.asBytes()[1]);
Assert.assertEquals("Ü", content.asString(Charset.forName("utf-8")));
Assert.assertEquals("Ü", content.asString(StandardCharsets.UTF_8));
}
@Test

View File

@ -30,7 +30,7 @@ import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
@ -650,7 +650,7 @@ public class TestClientAuthentication extends LocalServerTestBase {
new UsernamePasswordCredentials("test", "test".toCharArray()));
final RequestConfig config = RequestConfig.custom()
.setTargetPreferredAuthSchemes(Arrays.asList("MyBasic"))
.setTargetPreferredAuthSchemes(Collections.singletonList("MyBasic"))
.build();
final Registry<AuthSchemeFactory> authSchemeRegistry = RegistryBuilder.<AuthSchemeFactory>create()
.register("MyBasic", myBasicAuthSchemeFactory)

View File

@ -29,7 +29,7 @@ package org.apache.hc.client5.testing.sync;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
@ -630,7 +630,7 @@ public class TestRedirects extends LocalServerTestBase {
@Test
public void testDefaultHeadersRedirect() throws Exception {
this.clientBuilder.setDefaultHeaders(Arrays.asList(new BasicHeader(HttpHeaders.USER_AGENT, "my-test-client")));
this.clientBuilder.setDefaultHeaders(Collections.singletonList(new BasicHeader(HttpHeaders.USER_AGENT, "my-test-client")));
final HttpHost target = start(null, new Decorator<HttpServerRequestHandler>() {

View File

@ -91,9 +91,9 @@ public class TestSPNegoScheme extends LocalServerTestBase {
*/
private static class NegotiateSchemeWithMockGssManager extends SPNegoScheme {
GSSManager manager = Mockito.mock(GSSManager.class);
GSSName name = Mockito.mock(GSSName.class);
GSSContext context = Mockito.mock(GSSContext.class);
final GSSManager manager = Mockito.mock(GSSManager.class);
final GSSName name = Mockito.mock(GSSName.class);
final GSSContext context = Mockito.mock(GSSContext.class);
NegotiateSchemeWithMockGssManager() throws Exception {
super(KerberosConfig.DEFAULT, SystemDefaultDnsResolver.INSTANCE);

View File

@ -75,7 +75,7 @@ public class AuthScope {
this.host = host != null ? host.toLowerCase(Locale.ROOT) : null;
this.port = port >= 0 ? port: -1;
this.realm = realm;
this.schemeName = schemeName != null ? schemeName : null;
this.schemeName = schemeName;
}
/**
@ -98,7 +98,7 @@ public class AuthScope {
this.host = origin.getHostName().toLowerCase(Locale.ROOT);
this.port = origin.getPort() >= 0 ? origin.getPort() : -1;
this.realm = realm;
this.schemeName = schemeName != null ? schemeName : null;
this.schemeName = schemeName;
}
/**

View File

@ -87,7 +87,7 @@ public final class CookieOrigin {
}
buffer.append(this.host);
buffer.append(':');
buffer.append(Integer.toString(this.port));
buffer.append(this.port);
buffer.append(this.path);
buffer.append(']');
return buffer.toString();

View File

@ -33,7 +33,7 @@ import org.slf4j.LoggerFactory;
class LoggingExceptionCallback implements Callback<Exception> {
static LoggingExceptionCallback INSTANCE = new LoggingExceptionCallback();
static final LoggingExceptionCallback INSTANCE = new LoggingExceptionCallback();
private static final Logger LOG = LoggerFactory.getLogger("org.apache.hc.client5.http.impl.async");

View File

@ -798,7 +798,7 @@ final class NTLMEngineImpl implements NTLMEngine {
enum Mode
{
CLIENT, SERVER;
CLIENT, SERVER
}
static class Handle
@ -1113,8 +1113,8 @@ final class NTLMEngineImpl implements NTLMEngine {
// Check to be sure there's a type 2 message indicator next
final int type = readULong(SIGNATURE.length);
if (type != expectedType) {
throw new NTLMEngineException("NTLM type " + Integer.toString(expectedType)
+ " message expected - instead got type " + Integer.toString(type));
throw new NTLMEngineException("NTLM type " + expectedType
+ " message expected - instead got type " + type);
}
currentOutputPosition = messageContents.length;

View File

@ -145,7 +145,7 @@ public class ManagedHttpClientConnectionFactory implements HttpConnectionFactory
charEncoder.onMalformedInput(malformedInputAction);
charEncoder.onUnmappableCharacter(unmappableInputAction);
}
final String id = "http-outgoing-" + Long.toString(COUNTER.getAndIncrement());
final String id = "http-outgoing-" + COUNTER.getAndIncrement();
final DefaultManagedHttpClientConnection conn = new DefaultManagedHttpClientConnection(
id,
charDecoder,

View File

@ -33,7 +33,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.apache.hc.core5.annotation.Contract;
@ -88,7 +88,7 @@ public final class PublicSuffixMatcherLoader {
LOG.warn("Failure loading public suffix list from default resource", ex);
}
} else {
DEFAULT_INSTANCE = new PublicSuffixMatcher(DomainType.ICANN, Arrays.asList("com"), null);
DEFAULT_INSTANCE = new PublicSuffixMatcher(DomainType.ICANN, Collections.singletonList("com"), null);
}
}
}

View File

@ -334,7 +334,7 @@ public class TestMultipartForm {
final FormBodyPart p1 = FormBodyPartBuilder.create(
"field1",
new StringBody(s1, ContentType.create("text/plain", Charset.forName("ISO-8859-1")))).build();
new StringBody(s1, ContentType.create("text/plain", StandardCharsets.ISO_8859_1))).build();
final FormBodyPart p2 = FormBodyPartBuilder.create(
"field2",
new StringBody(s2, ContentType.create("text/plain", Charset.forName("KOI8-R")))).build();

View File

@ -293,7 +293,7 @@ public class TestMultipartMixed {
final String s2 = constructString(RUSSIAN_HELLO);
final MultipartPart p1 = MultipartPartBuilder.create(
new StringBody(s1, ContentType.create("text/plain", Charset.forName("ISO-8859-1")))).build();
new StringBody(s1, ContentType.create("text/plain", StandardCharsets.ISO_8859_1))).build();
final MultipartPart p2 = MultipartPartBuilder.create(
new StringBody(s2, ContentType.create("text/plain", Charset.forName("KOI8-R")))).build();
final HttpStrictMultipart multipart = new HttpStrictMultipart(null, "foo",

View File

@ -29,6 +29,7 @@ package org.apache.hc.client5.http.entity.mime;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.apache.hc.core5.http.ContentType;
@ -47,8 +48,8 @@ public class TestMultipartPartBuilder {
Assert.assertEquals(stringBody, part.getBody());
final Header header = part.getHeader();
Assert.assertNotNull(header);
assertFields(Arrays.asList(
new MimeField("Content-Type", "text/plain; charset=ISO-8859-1")),
assertFields(Collections.singletonList(
new MimeField("Content-Type", "text/plain; charset=ISO-8859-1")),
header.getFields());
}
@ -63,8 +64,8 @@ public class TestMultipartPartBuilder {
Assert.assertEquals(stringBody, part1.getBody());
final Header header1 = part1.getHeader();
Assert.assertNotNull(header1);
assertFields(Arrays.asList(
new MimeField("Content-Type", "text/plain; charset=ISO-8859-1")),
assertFields(Collections.singletonList(
new MimeField("Content-Type", "text/plain; charset=ISO-8859-1")),
header1.getFields());
final FileBody fileBody = new FileBody(new File("/path/stuff.bin"), ContentType.DEFAULT_BINARY);
final MultipartPart part2 = builder
@ -75,8 +76,8 @@ public class TestMultipartPartBuilder {
Assert.assertEquals(fileBody, part2.getBody());
final Header header2 = part2.getHeader();
Assert.assertNotNull(header2);
assertFields(Arrays.asList(
new MimeField("Content-Type", "application/octet-stream")),
assertFields(Collections.singletonList(
new MimeField("Content-Type", "application/octet-stream")),
header2.getFields());
}

View File

@ -32,6 +32,7 @@ import java.net.UnknownHostException;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import javax.net.ssl.SSLContext;
@ -196,7 +197,7 @@ public class ClientConfiguration {
.setCookieSpec(StandardCookieSpec.STRICT)
.setExpectContinueEnabled(true)
.setTargetPreferredAuthSchemes(Arrays.asList(StandardAuthScheme.NTLM, StandardAuthScheme.DIGEST))
.setProxyPreferredAuthSchemes(Arrays.asList(StandardAuthScheme.BASIC))
.setProxyPreferredAuthSchemes(Collections.singletonList(StandardAuthScheme.BASIC))
.build();
// Create an HttpClient with the given custom dependencies and configuration.

View File

@ -88,7 +88,7 @@ public class TestNTLMEngineImpl {
while (i < answer.length) {
if (answer[i] != correctAnswer[i]) {
throw new Exception("Answer value for MD4('" + input + "') disagrees at position "
+ Integer.toString(i));
+ i);
}
i++;
}

View File

@ -71,7 +71,7 @@ public class TestFutureRequestExecutionService {
private final AtomicBoolean blocked = new AtomicBoolean(false);
@Rule
public ExpectedException thrown = ExpectedException.none();
public final ExpectedException thrown = ExpectedException.none();
@Before
public void before() throws Exception {

View File

@ -28,6 +28,7 @@
package org.apache.hc.client5.http.impl.cookie;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.apache.hc.client5.http.cookie.CommonCookieAttributeHandler;
@ -264,7 +265,7 @@ public class TestRFC6265CookieSpec {
final Cookie cookie1 = new BasicClientCookie("name1", "value");
final RFC6265CookieSpec cookiespec = new RFC6265CookieSpec();
final List<Header> headers = cookiespec.formatCookies(Arrays.asList(cookie1));
final List<Header> headers = cookiespec.formatCookies(Collections.singletonList(cookie1));
Assert.assertNotNull(headers);
Assert.assertEquals(1, headers.size());
final Header header = headers.get(0);

View File

@ -53,7 +53,7 @@ public class TestPublicSuffixListParser {
}
Assert.assertNotNull(suffixList);
Assert.assertEquals(Arrays.asList("xx", "jp", "ac.jp", "*.tokyo.jp", "no", "h\u00E5.no"), suffixList.getRules());
Assert.assertEquals(Arrays.asList("metro.tokyo.jp"), suffixList.getExceptions());
Assert.assertEquals(Collections.singletonList("metro.tokyo.jp"), suffixList.getExceptions());
}
@Test
@ -74,7 +74,7 @@ public class TestPublicSuffixListParser {
Assert.assertNotNull(publicSuffixList1);
Assert.assertEquals(DomainType.ICANN, publicSuffixList1.getType());
Assert.assertEquals(Arrays.asList("jp", "ac.jp", "*.tokyo.jp"), publicSuffixList1.getRules());
Assert.assertEquals(Arrays.asList("metro.tokyo.jp"), publicSuffixList1.getExceptions());
Assert.assertEquals(Collections.singletonList("metro.tokyo.jp"), publicSuffixList1.getExceptions());
final PublicSuffixList publicSuffixList2 = suffixLists.get(1);
Assert.assertNotNull(publicSuffixList2);

View File

@ -43,7 +43,6 @@ import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@ -376,18 +375,18 @@ public class TestDefaultHostnameVerifier {
@Test // Check compressed IPv6 hostname matching
public void testHTTPCLIENT_1316() throws Exception{
final String host1 = "2001:0db8:aaaa:bbbb:cccc:0:0:0001";
DefaultHostnameVerifier.matchIPv6Address(host1, Arrays.asList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc:0:0:0001")));
DefaultHostnameVerifier.matchIPv6Address(host1, Arrays.asList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc::1")));
DefaultHostnameVerifier.matchIPv6Address(host1, Collections.singletonList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc:0:0:0001")));
DefaultHostnameVerifier.matchIPv6Address(host1, Collections.singletonList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc::1")));
try {
DefaultHostnameVerifier.matchIPv6Address(host1, Arrays.asList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc::10")));
DefaultHostnameVerifier.matchIPv6Address(host1, Collections.singletonList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc::10")));
Assert.fail("SSLException expected");
} catch (final SSLException expected) {
}
final String host2 = "2001:0db8:aaaa:bbbb:cccc::1";
DefaultHostnameVerifier.matchIPv6Address(host2, Arrays.asList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc:0:0:0001")));
DefaultHostnameVerifier.matchIPv6Address(host2, Arrays.asList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc::1")));
DefaultHostnameVerifier.matchIPv6Address(host2, Collections.singletonList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc:0:0:0001")));
DefaultHostnameVerifier.matchIPv6Address(host2, Collections.singletonList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc::1")));
try {
DefaultHostnameVerifier.matchIPv6Address(host2, Arrays.asList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc::10")));
DefaultHostnameVerifier.matchIPv6Address(host2, Collections.singletonList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc::10")));
Assert.fail("SSLException expected");
} catch (final SSLException expected) {
}

View File

@ -28,6 +28,7 @@ package org.apache.hc.client5.http.utils;
import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.net.URIBuilder;
@ -276,7 +277,7 @@ public class TestURIUtils {
final URI redirect = new URI("http://localhost/people.html");
final URI location = URIUtils.resolve(requestURI, target, Arrays.asList(redirect));
final URI location = URIUtils.resolve(requestURI, target, Collections.singletonList(redirect));
final URI expectedURI = new URIBuilder()
.setHost(target.getHostName())
.setScheme(target.getSchemeName())