Skip to content

Commit

Permalink
Use Map#computeIfAbsent() instead of get(), test, and put()
Browse files Browse the repository at this point in the history
- Make local variable final
- Inline single use variable
- Format nits
  • Loading branch information
garydgregory committed Oct 30, 2024
1 parent bce3474 commit 0bb2d8d
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ public Map<String, Object> execute(final String defaultURI, final Map<String, Ob
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.");
}

Expand All @@ -80,7 +80,7 @@ public Map<String, Object> execute(final String defaultURI, final Map<String, Ob

// 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public Map<String, Object> initRequest() throws TestingFrameworkException {
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);
Expand All @@ -119,7 +119,7 @@ private void moveAnyParametersInPathToQuery(final Map<String, Object> request) t
for (final NameValuePair param : params) {
queryMap.put(param.getName(), param.getValue());
}
if (! params.isEmpty()) {
if (!params.isEmpty()) {
request.put(PATH, uri.getPath());
}
}
Expand All @@ -135,7 +135,7 @@ private void moveAnyParametersInPathToQuery(final Map<String, Object> request) t
*/
public Map<String, Object> initResponseExpectations() {
// 200 is the default status.
if (! response.containsKey(STATUS)) {
if (!response.containsKey(STATUS)) {
response.put(STATUS, 200);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ private void callAdapter(final FrameworkTest test) throws TestingFrameworkExcept
/*
* If the adapter does not support the particular request, skip the test.
*/
if (! adapter.isRequestSupported(request)) {
if (!adapter.isRequestSupported(request)) {
return;
}

Expand Down Expand Up @@ -323,7 +323,7 @@ private void assertResponseMatchesExpectation(final Object method, final Map<Str
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));
}
Expand Down Expand Up @@ -365,7 +365,7 @@ private void assertContentTypeMatchesExpectation(final Object actualContentType,
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);
}
Expand All @@ -380,10 +380,10 @@ private void assertHeadersMatchExpectation(final Map<String, String> actualHeade
}
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void handle(final ClassicHttpRequest request, final ClassicHttpResponse r
*/
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);
}
Expand Down Expand Up @@ -145,12 +145,12 @@ public void handle(final ClassicHttpRequest request, final ClassicHttpResponse r
}
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);
}
Expand All @@ -170,12 +170,12 @@ public void handle(final ClassicHttpRequest request, final ClassicHttpResponse r
}
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);
}
Expand All @@ -189,7 +189,7 @@ public void handle(final ClassicHttpRequest request, final ClassicHttpResponse r
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);
}
Expand All @@ -203,7 +203,7 @@ public void handle(final ClassicHttpRequest request, final ClassicHttpResponse r
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);
}
Expand All @@ -215,7 +215,7 @@ public void handle(final ClassicHttpRequest request, final ClassicHttpResponse r
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,7 @@ public void close() {
}

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
Expand Down

0 comments on commit 0bb2d8d

Please sign in to comment.