Skip to content

Commit

Permalink
Private method does not throw checked exception
Browse files Browse the repository at this point in the history
- Remove it from the private method signatures
- Remove exception handling no longer needed
- Add missing @OverRide
- No need to nest in an else clause (reduce clutter)
- Use ternary expression
- public methods in a package-private class can be package-private
  • Loading branch information
garydgregory committed Aug 26, 2024
1 parent a1eed80 commit 25f080f
Show file tree
Hide file tree
Showing 34 changed files with 299 additions and 371 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ public final HttpCacheEntry getEntry(final String key) throws ResourceIOExceptio
final HttpCacheStorageEntry entry = serializer.deserialize(storageObject);
if (key.equals(entry.getKey())) {
return entry.getContent();
} else {
return null;
}
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,12 +534,11 @@ public void cancelled() {
exchangeId, asyncExecCallback, backendResponse, entityDetails);
cachingConsumerRef.set(cachingDataConsumer);
return cachingDataConsumer;
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("{} backend response is not cacheable", exchangeId);
}
return asyncExecCallback.handleResponse(backendResponse, entityDetails);
}
if (LOG.isDebugEnabled()) {
LOG.debug("{} backend response is not cacheable", exchangeId);
}
return asyncExecCallback.handleResponse(backendResponse, entityDetails);
}

@Override
Expand Down Expand Up @@ -1079,10 +1078,9 @@ public AsyncDataConsumer handleResponse(final HttpResponse response, final Entit
LOG.debug("{} serving stale response due to {} status and stale-if-error enabled", exchangeId, status);
}
return null;
} else {
committed.set(response);
return asyncExecCallback.handleResponse(response, entityDetails);
}
committed.set(response);
return asyncExecCallback.handleResponse(response, entityDetails);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,8 @@ public void cancelled() {

}));
return;
} else {
callback.completed(new CacheMatch(null, new CacheHit(rootKey, root)));
}
callback.completed(new CacheMatch(null, new CacheHit(rootKey, root)));
} else {
callback.completed(new CacheMatch(new CacheHit(rootKey, root), null));
}
Expand Down Expand Up @@ -350,39 +349,38 @@ public void completed(final Boolean result) {
}

});
} else {
final String variantCacheKey = variantKey + rootKey;
}
final String variantCacheKey = variantKey + rootKey;

if (LOG.isDebugEnabled()) {
LOG.debug("Store variant entry in cache: {}", variantCacheKey);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Store variant entry in cache: {}", variantCacheKey);
}

return storeInternal(variantCacheKey, entry, new CallbackContribution<Boolean>(callback) {
return storeInternal(variantCacheKey, entry, new CallbackContribution<Boolean>(callback) {

@Override
public void completed(final Boolean result) {
if (LOG.isDebugEnabled()) {
LOG.debug("Update root entry: {}", rootKey);
}
@Override
public void completed(final Boolean result) {
if (LOG.isDebugEnabled()) {
LOG.debug("Update root entry: {}", rootKey);
}

updateInternal(rootKey,
existing -> {
final Set<String> variantMap = existing != null ? new HashSet<>(existing.getVariants()) : new HashSet<>();
variantMap.add(variantKey);
return cacheEntryFactory.createRoot(entry, variantMap);
},
new CallbackContribution<Boolean>(callback) {

@Override
public void completed(final Boolean result) {
callback.completed(new CacheHit(rootKey, variantCacheKey, entry));
}
updateInternal(rootKey,
existing -> {
final Set<String> variantMap = existing != null ? new HashSet<>(existing.getVariants()) : new HashSet<>();
variantMap.add(variantKey);
return cacheEntryFactory.createRoot(entry, variantMap);
},
new CallbackContribution<Boolean>(callback) {

@Override
public void completed(final Boolean result) {
callback.completed(new CacheHit(rootKey, variantCacheKey, entry));
}

});
}
});
}

});
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,8 @@ public CacheMatch match(final HttpHost host, final HttpRequest request) {
}
}
return new CacheMatch(null, new CacheHit(rootKey, root));
} else {
return new CacheMatch(new CacheHit(rootKey, root), null);
}
return new CacheMatch(new CacheHit(rootKey, root), null);
}

@Override
Expand Down Expand Up @@ -197,26 +196,25 @@ CacheHit store(final String rootKey, final String variantKey, final HttpCacheEnt
}
storeInternal(rootKey, entry);
return new CacheHit(rootKey, entry);
} else {
final String variantCacheKey = variantKey + rootKey;

if (LOG.isDebugEnabled()) {
LOG.debug("Store variant entry in cache: {}", variantCacheKey);
}
}
final String variantCacheKey = variantKey + rootKey;

storeInternal(variantCacheKey, entry);
if (LOG.isDebugEnabled()) {
LOG.debug("Store variant entry in cache: {}", variantCacheKey);
}

if (LOG.isDebugEnabled()) {
LOG.debug("Update root entry: {}", rootKey);
}
storeInternal(variantCacheKey, entry);

updateInternal(rootKey, existing -> {
final Set<String> variants = existing != null ? new HashSet<>(existing.getVariants()) : new HashSet<>();
variants.add(variantKey);
return cacheEntryFactory.createRoot(entry, variants);
});
return new CacheHit(rootKey, variantCacheKey, entry);
if (LOG.isDebugEnabled()) {
LOG.debug("Update root entry: {}", rootKey);
}

updateInternal(rootKey, existing -> {
final Set<String> variants = existing != null ? new HashSet<>(existing.getVariants()) : new HashSet<>();
variants.add(variantKey);
return cacheEntryFactory.createRoot(entry, variants);
});
return new CacheHit(rootKey, variantCacheKey, entry);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ public Header generate(final RequestCacheControl cacheControl) {
BasicHeaderValueFormatter.INSTANCE.formatNameValuePair(buf, params.get(i), false);
}
return BufferedHeader.create(buf);
} else {
return null;
}
return null;
}

public void generate(final RequestCacheControl cacheControl, final HttpMessage message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,29 +237,28 @@ public static void normalizeElements(final Iterator<Header> iterator, final Cons
.map(e -> {
if (e.getValue() == null && e.getParameterCount() == 0) {
return e.getName().toLowerCase(Locale.ROOT);
} else {
final CharArrayBuffer buf = new CharArrayBuffer(1024);
BasicHeaderValueFormatter.INSTANCE.formatNameValuePair(
buf,
new BasicNameValuePair(
e.getName().toLowerCase(Locale.ROOT),
!TextUtils.isBlank(e.getValue()) ? e.getValue() : null),
false);
if (e.getParameterCount() > 0) {
for (final NameValuePair nvp : e.getParameters()) {
if (!TextUtils.isBlank(nvp.getName())) {
buf.append(';');
BasicHeaderValueFormatter.INSTANCE.formatNameValuePair(
buf,
new BasicNameValuePair(
nvp.getName().toLowerCase(Locale.ROOT),
!TextUtils.isBlank(nvp.getValue()) ? nvp.getValue() : null),
false);
}
}
final CharArrayBuffer buf = new CharArrayBuffer(1024);
BasicHeaderValueFormatter.INSTANCE.formatNameValuePair(
buf,
new BasicNameValuePair(
e.getName().toLowerCase(Locale.ROOT),
!TextUtils.isBlank(e.getValue()) ? e.getValue() : null),
false);
if (e.getParameterCount() > 0) {
for (final NameValuePair nvp : e.getParameters()) {
if (!TextUtils.isBlank(nvp.getName())) {
buf.append(';');
BasicHeaderValueFormatter.INSTANCE.formatNameValuePair(
buf,
new BasicNameValuePair(
nvp.getName().toLowerCase(Locale.ROOT),
!TextUtils.isBlank(nvp.getValue()) ? nvp.getValue() : null),
false);
}
}
return buf.toString();
}
return buf.toString();
})
.sorted()
.distinct()
Expand Down Expand Up @@ -311,9 +310,8 @@ public String generateVariantKey(final HttpRequest request, final HttpCacheEntry
if (entry.containsHeader(HttpHeaders.VARY)) {
final List<String> variantNames = variantNames(entry);
return generateVariantKey(request, variantNames);
} else {
return null;
}
return null;
}

/**
Expand All @@ -334,9 +332,8 @@ public String generateKey(final HttpHost host, final HttpRequest request, final
final List<String> variantNames = variantNames(entry);
if (variantNames.isEmpty()) {
return rootKey;
} else {
return generateVariantKey(request, variantNames) + rootKey;
}
return generateVariantKey(request, variantNames) + rootKey;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ public static URI getLocationURI(final URI requestUri, final MessageHeaders resp
}
if (locationUri.isAbsolute()) {
return locationUri;
} else {
return URIUtils.resolve(requestUri, locationUri);
}
return URIUtils.resolve(requestUri, locationUri);
}

public static boolean isSameOrigin(final URI requestURI, final URI targetURI) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,8 @@ public TimeValue getFreshnessLifetime(final ResponseCacheControl responseCacheCo
LOG.debug("No explicit expiration time present in the response. Using heuristic freshness lifetime calculation.");
}
return getHeuristicFreshnessLifetime(entry);
} else {
return TimeValue.ZERO_MILLISECONDS;
}
return TimeValue.ZERO_MILLISECONDS;
}

TimeValue getHeuristicFreshnessLifetime(final HttpCacheEntry entry) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,26 +176,24 @@ public CacheSuitability assessSuitability(final RequestCacheControl requestCache
if (stale >= requestCacheControl.getMaxStale()) {
LOG.debug("Response from cache is not suitable due to the request max-stale requirement");
return CacheSuitability.REVALIDATION_REQUIRED;
} else {
LOG.debug("The cache entry is fresh enough");
return CacheSuitability.FRESH_ENOUGH;
}
LOG.debug("The cache entry is fresh enough");
return CacheSuitability.FRESH_ENOUGH;
}

if (fresh) {
LOG.debug("The cache entry is fresh");
return CacheSuitability.FRESH;
} else {
if (responseCacheControl.getStaleWhileRevalidate() > 0) {
final long stale = currentAge.compareTo(freshnessLifetime) > 0 ? currentAge.toSeconds() - freshnessLifetime.toSeconds() : 0;
if (stale < responseCacheControl.getStaleWhileRevalidate()) {
LOG.debug("The cache entry is stale but suitable while being revalidated");
return CacheSuitability.STALE_WHILE_REVALIDATED;
}
}
if (responseCacheControl.getStaleWhileRevalidate() > 0) {
final long stale = currentAge.compareTo(freshnessLifetime) > 0 ? currentAge.toSeconds() - freshnessLifetime.toSeconds() : 0;
if (stale < responseCacheControl.getStaleWhileRevalidate()) {
LOG.debug("The cache entry is stale but suitable while being revalidated");
return CacheSuitability.STALE_WHILE_REVALIDATED;
}
LOG.debug("The cache entry is stale");
return CacheSuitability.STALE;
}
LOG.debug("The cache entry is stale");
return CacheSuitability.STALE;
}

boolean requestMethodMatch(final HttpRequest request, final HttpCacheEntry entry) {
Expand All @@ -209,9 +207,8 @@ boolean requestUriMatch(final HttpRequest request, final HttpCacheEntry entry) {
final URI cacheURI = new URI(entry.getRequestURI());
if (requestURI.isAbsolute()) {
return Objects.equals(requestURI, cacheURI);
} else {
return Objects.equals(requestURI.getPath(), cacheURI.getPath()) && Objects.equals(requestURI.getQuery(), cacheURI.getQuery());
}
return Objects.equals(requestURI.getPath(), cacheURI.getPath()) && Objects.equals(requestURI.getQuery(), cacheURI.getQuery());
} catch (final URISyntaxException ex) {
return false;
}
Expand Down
Loading

0 comments on commit 25f080f

Please sign in to comment.