Skip to content

Commit 25f080f

Browse files
committed
Private method does not throw checked exception
- 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
1 parent a1eed80 commit 25f080f

34 files changed

+299
-371
lines changed

httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/AbstractSerializingCacheStorage.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,8 @@ public final HttpCacheEntry getEntry(final String key) throws ResourceIOExceptio
8989
final HttpCacheStorageEntry entry = serializer.deserialize(storageObject);
9090
if (key.equals(entry.getKey())) {
9191
return entry.getContent();
92-
} else {
93-
return null;
9492
}
93+
return null;
9594
}
9695

9796
@Override

httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/AsyncCachingExec.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -534,12 +534,11 @@ public void cancelled() {
534534
exchangeId, asyncExecCallback, backendResponse, entityDetails);
535535
cachingConsumerRef.set(cachingDataConsumer);
536536
return cachingDataConsumer;
537-
} else {
538-
if (LOG.isDebugEnabled()) {
539-
LOG.debug("{} backend response is not cacheable", exchangeId);
540-
}
541-
return asyncExecCallback.handleResponse(backendResponse, entityDetails);
542537
}
538+
if (LOG.isDebugEnabled()) {
539+
LOG.debug("{} backend response is not cacheable", exchangeId);
540+
}
541+
return asyncExecCallback.handleResponse(backendResponse, entityDetails);
543542
}
544543

545544
@Override
@@ -1079,10 +1078,9 @@ public AsyncDataConsumer handleResponse(final HttpResponse response, final Entit
10791078
LOG.debug("{} serving stale response due to {} status and stale-if-error enabled", exchangeId, status);
10801079
}
10811080
return null;
1082-
} else {
1083-
committed.set(response);
1084-
return asyncExecCallback.handleResponse(response, entityDetails);
10851081
}
1082+
committed.set(response);
1083+
return asyncExecCallback.handleResponse(response, entityDetails);
10861084
}
10871085

10881086
@Override

httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpAsyncCache.java

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,8 @@ public void cancelled() {
144144

145145
}));
146146
return;
147-
} else {
148-
callback.completed(new CacheMatch(null, new CacheHit(rootKey, root)));
149147
}
148+
callback.completed(new CacheMatch(null, new CacheHit(rootKey, root)));
150149
} else {
151150
callback.completed(new CacheMatch(new CacheHit(rootKey, root), null));
152151
}
@@ -350,39 +349,38 @@ public void completed(final Boolean result) {
350349
}
351350

352351
});
353-
} else {
354-
final String variantCacheKey = variantKey + rootKey;
352+
}
353+
final String variantCacheKey = variantKey + rootKey;
355354

356-
if (LOG.isDebugEnabled()) {
357-
LOG.debug("Store variant entry in cache: {}", variantCacheKey);
358-
}
355+
if (LOG.isDebugEnabled()) {
356+
LOG.debug("Store variant entry in cache: {}", variantCacheKey);
357+
}
359358

360-
return storeInternal(variantCacheKey, entry, new CallbackContribution<Boolean>(callback) {
359+
return storeInternal(variantCacheKey, entry, new CallbackContribution<Boolean>(callback) {
361360

362-
@Override
363-
public void completed(final Boolean result) {
364-
if (LOG.isDebugEnabled()) {
365-
LOG.debug("Update root entry: {}", rootKey);
366-
}
361+
@Override
362+
public void completed(final Boolean result) {
363+
if (LOG.isDebugEnabled()) {
364+
LOG.debug("Update root entry: {}", rootKey);
365+
}
367366

368-
updateInternal(rootKey,
369-
existing -> {
370-
final Set<String> variantMap = existing != null ? new HashSet<>(existing.getVariants()) : new HashSet<>();
371-
variantMap.add(variantKey);
372-
return cacheEntryFactory.createRoot(entry, variantMap);
373-
},
374-
new CallbackContribution<Boolean>(callback) {
375-
376-
@Override
377-
public void completed(final Boolean result) {
378-
callback.completed(new CacheHit(rootKey, variantCacheKey, entry));
379-
}
367+
updateInternal(rootKey,
368+
existing -> {
369+
final Set<String> variantMap = existing != null ? new HashSet<>(existing.getVariants()) : new HashSet<>();
370+
variantMap.add(variantKey);
371+
return cacheEntryFactory.createRoot(entry, variantMap);
372+
},
373+
new CallbackContribution<Boolean>(callback) {
374+
375+
@Override
376+
public void completed(final Boolean result) {
377+
callback.completed(new CacheHit(rootKey, variantCacheKey, entry));
378+
}
380379

381-
});
382-
}
380+
});
381+
}
383382

384-
});
385-
}
383+
});
386384
}
387385

388386
@Override

httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpCache.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,8 @@ public CacheMatch match(final HttpHost host, final HttpRequest request) {
164164
}
165165
}
166166
return new CacheMatch(null, new CacheHit(rootKey, root));
167-
} else {
168-
return new CacheMatch(new CacheHit(rootKey, root), null);
169167
}
168+
return new CacheMatch(new CacheHit(rootKey, root), null);
170169
}
171170

172171
@Override
@@ -197,26 +196,25 @@ CacheHit store(final String rootKey, final String variantKey, final HttpCacheEnt
197196
}
198197
storeInternal(rootKey, entry);
199198
return new CacheHit(rootKey, entry);
200-
} else {
201-
final String variantCacheKey = variantKey + rootKey;
202-
203-
if (LOG.isDebugEnabled()) {
204-
LOG.debug("Store variant entry in cache: {}", variantCacheKey);
205-
}
199+
}
200+
final String variantCacheKey = variantKey + rootKey;
206201

207-
storeInternal(variantCacheKey, entry);
202+
if (LOG.isDebugEnabled()) {
203+
LOG.debug("Store variant entry in cache: {}", variantCacheKey);
204+
}
208205

209-
if (LOG.isDebugEnabled()) {
210-
LOG.debug("Update root entry: {}", rootKey);
211-
}
206+
storeInternal(variantCacheKey, entry);
212207

213-
updateInternal(rootKey, existing -> {
214-
final Set<String> variants = existing != null ? new HashSet<>(existing.getVariants()) : new HashSet<>();
215-
variants.add(variantKey);
216-
return cacheEntryFactory.createRoot(entry, variants);
217-
});
218-
return new CacheHit(rootKey, variantCacheKey, entry);
208+
if (LOG.isDebugEnabled()) {
209+
LOG.debug("Update root entry: {}", rootKey);
219210
}
211+
212+
updateInternal(rootKey, existing -> {
213+
final Set<String> variants = existing != null ? new HashSet<>(existing.getVariants()) : new HashSet<>();
214+
variants.add(variantKey);
215+
return cacheEntryFactory.createRoot(entry, variants);
216+
});
217+
return new CacheHit(rootKey, variantCacheKey, entry);
220218
}
221219

222220
@Override

httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheControlHeaderGenerator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,8 @@ public Header generate(final RequestCacheControl cacheControl) {
9292
BasicHeaderValueFormatter.INSTANCE.formatNameValuePair(buf, params.get(i), false);
9393
}
9494
return BufferedHeader.create(buf);
95-
} else {
96-
return null;
9795
}
96+
return null;
9897
}
9998

10099
public void generate(final RequestCacheControl cacheControl, final HttpMessage message) {

httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheKeyGenerator.java

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -237,29 +237,28 @@ public static void normalizeElements(final Iterator<Header> iterator, final Cons
237237
.map(e -> {
238238
if (e.getValue() == null && e.getParameterCount() == 0) {
239239
return e.getName().toLowerCase(Locale.ROOT);
240-
} else {
241-
final CharArrayBuffer buf = new CharArrayBuffer(1024);
242-
BasicHeaderValueFormatter.INSTANCE.formatNameValuePair(
243-
buf,
244-
new BasicNameValuePair(
245-
e.getName().toLowerCase(Locale.ROOT),
246-
!TextUtils.isBlank(e.getValue()) ? e.getValue() : null),
247-
false);
248-
if (e.getParameterCount() > 0) {
249-
for (final NameValuePair nvp : e.getParameters()) {
250-
if (!TextUtils.isBlank(nvp.getName())) {
251-
buf.append(';');
252-
BasicHeaderValueFormatter.INSTANCE.formatNameValuePair(
253-
buf,
254-
new BasicNameValuePair(
255-
nvp.getName().toLowerCase(Locale.ROOT),
256-
!TextUtils.isBlank(nvp.getValue()) ? nvp.getValue() : null),
257-
false);
258-
}
240+
}
241+
final CharArrayBuffer buf = new CharArrayBuffer(1024);
242+
BasicHeaderValueFormatter.INSTANCE.formatNameValuePair(
243+
buf,
244+
new BasicNameValuePair(
245+
e.getName().toLowerCase(Locale.ROOT),
246+
!TextUtils.isBlank(e.getValue()) ? e.getValue() : null),
247+
false);
248+
if (e.getParameterCount() > 0) {
249+
for (final NameValuePair nvp : e.getParameters()) {
250+
if (!TextUtils.isBlank(nvp.getName())) {
251+
buf.append(';');
252+
BasicHeaderValueFormatter.INSTANCE.formatNameValuePair(
253+
buf,
254+
new BasicNameValuePair(
255+
nvp.getName().toLowerCase(Locale.ROOT),
256+
!TextUtils.isBlank(nvp.getValue()) ? nvp.getValue() : null),
257+
false);
259258
}
260259
}
261-
return buf.toString();
262260
}
261+
return buf.toString();
263262
})
264263
.sorted()
265264
.distinct()
@@ -311,9 +310,8 @@ public String generateVariantKey(final HttpRequest request, final HttpCacheEntry
311310
if (entry.containsHeader(HttpHeaders.VARY)) {
312311
final List<String> variantNames = variantNames(entry);
313312
return generateVariantKey(request, variantNames);
314-
} else {
315-
return null;
316313
}
314+
return null;
317315
}
318316

319317
/**
@@ -334,9 +332,8 @@ public String generateKey(final HttpHost host, final HttpRequest request, final
334332
final List<String> variantNames = variantNames(entry);
335333
if (variantNames.isEmpty()) {
336334
return rootKey;
337-
} else {
338-
return generateVariantKey(request, variantNames) + rootKey;
339335
}
336+
return generateVariantKey(request, variantNames) + rootKey;
340337
}
341338

342339
}

httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheSupport.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ public static URI getLocationURI(final URI requestUri, final MessageHeaders resp
5555
}
5656
if (locationUri.isAbsolute()) {
5757
return locationUri;
58-
} else {
59-
return URIUtils.resolve(requestUri, locationUri);
6058
}
59+
return URIUtils.resolve(requestUri, locationUri);
6160
}
6261

6362
public static boolean isSameOrigin(final URI requestURI, final URI targetURI) {

httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheValidityPolicy.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,8 @@ public TimeValue getFreshnessLifetime(final ResponseCacheControl responseCacheCo
131131
LOG.debug("No explicit expiration time present in the response. Using heuristic freshness lifetime calculation.");
132132
}
133133
return getHeuristicFreshnessLifetime(entry);
134-
} else {
135-
return TimeValue.ZERO_MILLISECONDS;
136134
}
135+
return TimeValue.ZERO_MILLISECONDS;
137136
}
138137

139138
TimeValue getHeuristicFreshnessLifetime(final HttpCacheEntry entry) {

httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedResponseSuitabilityChecker.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -176,26 +176,24 @@ public CacheSuitability assessSuitability(final RequestCacheControl requestCache
176176
if (stale >= requestCacheControl.getMaxStale()) {
177177
LOG.debug("Response from cache is not suitable due to the request max-stale requirement");
178178
return CacheSuitability.REVALIDATION_REQUIRED;
179-
} else {
180-
LOG.debug("The cache entry is fresh enough");
181-
return CacheSuitability.FRESH_ENOUGH;
182179
}
180+
LOG.debug("The cache entry is fresh enough");
181+
return CacheSuitability.FRESH_ENOUGH;
183182
}
184183

185184
if (fresh) {
186185
LOG.debug("The cache entry is fresh");
187186
return CacheSuitability.FRESH;
188-
} else {
189-
if (responseCacheControl.getStaleWhileRevalidate() > 0) {
190-
final long stale = currentAge.compareTo(freshnessLifetime) > 0 ? currentAge.toSeconds() - freshnessLifetime.toSeconds() : 0;
191-
if (stale < responseCacheControl.getStaleWhileRevalidate()) {
192-
LOG.debug("The cache entry is stale but suitable while being revalidated");
193-
return CacheSuitability.STALE_WHILE_REVALIDATED;
194-
}
187+
}
188+
if (responseCacheControl.getStaleWhileRevalidate() > 0) {
189+
final long stale = currentAge.compareTo(freshnessLifetime) > 0 ? currentAge.toSeconds() - freshnessLifetime.toSeconds() : 0;
190+
if (stale < responseCacheControl.getStaleWhileRevalidate()) {
191+
LOG.debug("The cache entry is stale but suitable while being revalidated");
192+
return CacheSuitability.STALE_WHILE_REVALIDATED;
195193
}
196-
LOG.debug("The cache entry is stale");
197-
return CacheSuitability.STALE;
198194
}
195+
LOG.debug("The cache entry is stale");
196+
return CacheSuitability.STALE;
199197
}
200198

201199
boolean requestMethodMatch(final HttpRequest request, final HttpCacheEntry entry) {
@@ -209,9 +207,8 @@ boolean requestUriMatch(final HttpRequest request, final HttpCacheEntry entry) {
209207
final URI cacheURI = new URI(entry.getRequestURI());
210208
if (requestURI.isAbsolute()) {
211209
return Objects.equals(requestURI, cacheURI);
212-
} else {
213-
return Objects.equals(requestURI.getPath(), cacheURI.getPath()) && Objects.equals(requestURI.getQuery(), cacheURI.getQuery());
214210
}
211+
return Objects.equals(requestURI.getPath(), cacheURI.getPath()) && Objects.equals(requestURI.getQuery(), cacheURI.getQuery());
215212
} catch (final URISyntaxException ex) {
216213
return false;
217214
}

0 commit comments

Comments
 (0)