This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git
The following commit(s) were added to refs/heads/master by this push:
new 0bb2d8d86 Use Map#computeIfAbsent() instead of get(), test, and put()
0bb2d8d86 is described below
commit 0bb2d8d86215df4b9de259ae30e429fa1ed4ccff
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Wed Oct 30 11:53:09 2024 -0400
Use Map#computeIfAbsent() instead of get(), test, and put()
- Make local variable final
- Inline single use variable
- Format nits
---
.../org/apache/hc/core5/http2/hpack/StaticTable.java | 6 ++----
.../testing/framework/ClassicTestClientAdapter.java | 6 +++---
.../apache/hc/core5/testing/framework/FrameworkTest.java | 6 +++---
.../hc/core5/testing/framework/TestingFramework.java | 10 +++++-----
.../framework/TestingFrameworkRequestHandler.java | 16 ++++++++--------
.../java/org/apache/hc/core5/pool/StrictConnPool.java | 7 +------
6 files changed, 22 insertions(+), 29 deletions(-)
diff --git
a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/hpack/StaticTable.java
b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/hpack/StaticTable.java
index 91b3db39a..c58ee5043 100644
---
a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/hpack/StaticTable.java
+++
b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/hpack/StaticTable.java
@@ -112,12 +112,10 @@ final class StaticTable {
for (int i = 0; i < headers.length; i++) {
final HPackHeader header = headers[i];
-
final String key = header.getName();
- CopyOnWriteArrayList<HPackEntry> entries = this.mapByName.get(key);
+ final CopyOnWriteArrayList<HPackEntry> entries =
this.mapByName.get(key);
if (entries == null) {
- entries = new CopyOnWriteArrayList<>(new HPackEntry[] { new
InternalEntry(header, i) });
- this.mapByName.put(key, entries);
+ this.mapByName.put(key, new CopyOnWriteArrayList<>(new
HPackEntry[] { new InternalEntry(header, i) }));
} else {
entries.add(new InternalEntry(header, i));
}
diff --git
a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java
b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java
index 9c83e8010..ef65bfb87 100644
---
a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java
+++
b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java
@@ -61,10 +61,10 @@ public class ClassicTestClientAdapter extends
ClientPOJOAdapter {
if (request == null) {
throw new HttpException("request cannot be null");
}
- if (! request.containsKey(PATH)) {
+ if (!request.containsKey(PATH)) {
throw new HttpException("Request path should be set.");
}
- if (! request.containsKey(METHOD)) {
+ if (!request.containsKey(METHOD)) {
throw new HttpException("Request method should be set.");
}
@@ -80,7 +80,7 @@ public class ClassicTestClientAdapter extends ClientPOJOAdapter {
// Append the path to the defaultURI.
String tempDefaultURI = defaultURI;
- if (! defaultURI.endsWith("/")) {
+ if (!defaultURI.endsWith("/")) {
tempDefaultURI += "/";
}
final URI startingURI = new URI(tempDefaultURI + request.get(PATH));
diff --git
a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/FrameworkTest.java
b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/FrameworkTest.java
index 968548793..0a3f0147b 100644
---
a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/FrameworkTest.java
+++
b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/FrameworkTest.java
@@ -96,7 +96,7 @@ public class FrameworkTest {
ret.put(PROTOCOL_VERSION,
TestingFramework.DEFAULT_REQUEST_PROTOCOL_VERSION);
// GET is the default method.
- if (! request.containsKey(METHOD)) {
+ if (!request.containsKey(METHOD)) {
request.put(METHOD, "GET");
}
ret.putAll(request);
@@ -119,7 +119,7 @@ public class FrameworkTest {
for (final NameValuePair param : params) {
queryMap.put(param.getName(), param.getValue());
}
- if (! params.isEmpty()) {
+ if (!params.isEmpty()) {
request.put(PATH, uri.getPath());
}
}
@@ -135,7 +135,7 @@ public class FrameworkTest {
*/
public Map<String, Object> initResponseExpectations() {
// 200 is the default status.
- if (! response.containsKey(STATUS)) {
+ if (!response.containsKey(STATUS)) {
response.put(STATUS, 200);
}
diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFramework.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFramework.java
index 8368156cd..98bb8ddd9 100644
---
a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFramework.java
+++
b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFramework.java
@@ -263,7 +263,7 @@ public class TestingFramework {
/*
* If the adapter does not support the particular request, skip the
test.
*/
- if (! adapter.isRequestSupported(request)) {
+ if (!adapter.isRequestSupported(request)) {
return;
}
@@ -323,7 +323,7 @@ public class TestingFramework {
if (actualResponse.get(STATUS) != TestingFramework.ALREADY_CHECKED) {
assertStatusMatchesExpectation(actualResponse.get(STATUS),
expectedResponse.get(STATUS));
}
- if (! method.equals("HEAD")) {
+ if (!method.equals("HEAD")) {
if (actualResponse.get(BODY) != TestingFramework.ALREADY_CHECKED)
{
assertBodyMatchesExpectation(actualResponse.get(BODY),
expectedResponse.get(BODY));
}
@@ -365,7 +365,7 @@ public class TestingFramework {
if (actualContentType == null) {
throw new TestingFrameworkException("Returned contentType is
null.");
}
- if (! actualContentType.equals(expectedContentType)) {
+ if (!actualContentType.equals(expectedContentType)) {
throw new TestingFrameworkException("Expected content type not
found. expected="
+ expectedContentType + "; actual=" +
actualContentType);
}
@@ -380,10 +380,10 @@ public class TestingFramework {
}
for (final Map.Entry<String, String> expectedHeader :
expectedHeaders.entrySet()) {
final String expectedHeaderName = expectedHeader.getKey();
- if (! actualHeaders.containsKey(expectedHeaderName)) {
+ if (!actualHeaders.containsKey(expectedHeaderName)) {
throw new TestingFrameworkException("Expected header not found:
name=" + expectedHeaderName);
}
- if (!
actualHeaders.get(expectedHeaderName).equals(expectedHeaders.get(expectedHeaderName)))
{
+ if
(!actualHeaders.get(expectedHeaderName).equals(expectedHeaders.get(expectedHeaderName)))
{
throw new TestingFrameworkException("Header value not expected:
name=" + expectedHeaderName
+ "; expected=" +
expectedHeaders.get(expectedHeaderName)
+ "; actual=" +
actualHeaders.get(expectedHeaderName));
diff --git
a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFrameworkRequestHandler.java
b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFrameworkRequestHandler.java
index 3ad934b09..a472e4844 100644
---
a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFrameworkRequestHandler.java
+++
b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFrameworkRequestHandler.java
@@ -117,7 +117,7 @@ public class TestingFrameworkRequestHandler implements
HttpRequestHandler {
*/
final String actualMethod = request.getMethod();
final String expectedMethod = (String)
requestExpectations.get(METHOD);
- if (! actualMethod.equals(expectedMethod)) {
+ if (!actualMethod.equals(expectedMethod)) {
throw new TestingFrameworkException("Method not expected. " +
" expected=" + expectedMethod + "; actual=" +
actualMethod);
}
@@ -145,12 +145,12 @@ public class TestingFrameworkRequestHandler implements
HttpRequestHandler {
}
for (final Map.Entry<String, String> expectedParam :
expectedQuery.entrySet()) {
final String key = expectedParam.getKey();
- if (! actualParamsMap.containsKey(key)) {
+ if (!actualParamsMap.containsKey(key)) {
throw new TestingFrameworkException("Expected parameter
not found: " + key);
}
final String actualParamValue = actualParamsMap.get(key);
final String expectedParamValue =
expectedParam.getValue();
- if (! actualParamValue.equals(expectedParamValue)) {
+ if (!actualParamValue.equals(expectedParamValue)) {
throw new TestingFrameworkException("Expected parameter
value not found. " +
" Parameter=" + key + "; expected=" + expectedParamValue +
"; actual=" + actualParamValue);
}
@@ -170,12 +170,12 @@ public class TestingFrameworkRequestHandler implements
HttpRequestHandler {
}
for (final Entry<String, String> expectedHeader :
expectedHeaders.entrySet()) {
final String key = expectedHeader.getKey();
- if (! actualHeadersMap.containsKey(key)) {
+ if (!actualHeadersMap.containsKey(key)) {
throw new TestingFrameworkException("Expected header not
found: " + key);
}
final String actualHeaderValue =
actualHeadersMap.get(key);
final String expectedHeaderValue =
expectedHeader.getValue();
- if (! actualHeaderValue.equals(expectedHeaderValue)) {
+ if (!actualHeaderValue.equals(expectedHeaderValue)) {
throw new TestingFrameworkException("Expected header value
not found. " +
" Name=" + key + "; expected=" + expectedHeaderValue +
"; actual=" + actualHeaderValue);
}
@@ -189,7 +189,7 @@ public class TestingFrameworkRequestHandler implements
HttpRequestHandler {
if (expectedBody != null) {
final HttpEntity entity = request.getEntity();
final String data = EntityUtils.toString(entity);
- if (! data.equals(expectedBody)) {
+ if (!data.equals(expectedBody)) {
throw new TestingFrameworkException("Expected body not found.
" +
" Body=" + data + "; expected=" + expectedBody);
}
@@ -203,7 +203,7 @@ public class TestingFrameworkRequestHandler implements
HttpRequestHandler {
final HttpEntity entity = request.getEntity();
final String contentType = entity.getContentType();
final String expectedContentType = (String)
requestExpectations.get(CONTENT_TYPE);
- if (! contentType.equals(expectedContentType)) {
+ if (!contentType.equals(expectedContentType)) {
throw new TestingFrameworkException("Expected request content
type not found. " +
" Content Type=" + contentType + "; expected=" +
expectedContentType);
}
@@ -215,7 +215,7 @@ public class TestingFrameworkRequestHandler implements
HttpRequestHandler {
if (requestExpectations.containsKey(PROTOCOL_VERSION)) {
final ProtocolVersion protocolVersion = request.getVersion();
final ProtocolVersion expectedProtocolVersion =
(ProtocolVersion) requestExpectations.get(PROTOCOL_VERSION);
- if (! protocolVersion.equals(expectedProtocolVersion)) {
+ if (!protocolVersion.equals(expectedProtocolVersion)) {
throw new TestingFrameworkException("Expected request protocol
version not found. " +
" Protocol Version=" + protocolVersion + ";
expected=" + expectedProtocolVersion);
}
diff --git
a/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java
b/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java
index 819238b38..0bc0c1b6d 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java
@@ -157,12 +157,7 @@ public class StrictConnPool<T, C extends ModalCloseable>
implements ManagedConnP
}
private PerRoutePool<T, C> getPool(final T route) {
- PerRoutePool<T, C> pool = this.routeToPool.get(route);
- if (pool == null) {
- pool = new PerRoutePool<>(route, this.disposalCallback);
- this.routeToPool.put(route, pool);
- }
- return pool;
+ return this.routeToPool.computeIfAbsent(route, r -> new
PerRoutePool<>(route, this.disposalCallback));
}
@Override