From 95771c7b625b1048b97eac34b48a544635154b59 Mon Sep 17 00:00:00 2001 From: Edgar Asatryan Date: Tue, 22 Mar 2022 21:15:08 +0400 Subject: [PATCH] chore: Store interceptors as fields. --- .../nstdio/http/ext/ExtendedHttpClient.java | 29 ++++--------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/src/main/java/io/github/nstdio/http/ext/ExtendedHttpClient.java b/src/main/java/io/github/nstdio/http/ext/ExtendedHttpClient.java index d974f1b..4e54326 100644 --- a/src/main/java/io/github/nstdio/http/ext/ExtendedHttpClient.java +++ b/src/main/java/io/github/nstdio/http/ext/ExtendedHttpClient.java @@ -33,8 +33,6 @@ import java.net.http.WebSocket; import java.time.Clock; import java.time.Duration; -import java.util.ArrayList; -import java.util.List; import java.util.Objects; import java.util.Optional; import java.util.concurrent.CompletableFuture; @@ -43,11 +41,9 @@ import java.util.function.Function; public class ExtendedHttpClient extends HttpClient { - private final Cache cache; - private final boolean isNullCache; + private final CompressionInterceptor compressionInterceptor; + private final CachingInterceptor cachingInterceptor; - private final boolean transparentEncoding; - private final Clock clock; private final HttpClient delegate; ExtendedHttpClient(HttpClient delegate, Cache cache, Clock clock) { @@ -56,10 +52,8 @@ public class ExtendedHttpClient extends HttpClient { ExtendedHttpClient(HttpClient delegate, Cache cache, boolean transparentEncoding, Clock clock) { this.delegate = delegate; - this.cache = cache; - this.isNullCache = cache instanceof NullCache; - this.transparentEncoding = transparentEncoding; - this.clock = clock; + this.cachingInterceptor = cache instanceof NullCache ? null : new CachingInterceptor(cache, clock); + this.compressionInterceptor = transparentEncoding ? new CompressionInterceptor() : null; } /** @@ -177,20 +171,9 @@ private CompletableFuture> send0(HttpRequest request, BodyHa } private Chain buildAndExecute(RequestContext ctx) { - List interceptors = new ArrayList<>(2); - if (transparentEncoding) { - interceptors.add(new CompressionInterceptor()); - } - if (!isNullCache) { - interceptors.add(new CachingInterceptor(cache, clock)); - } - Chain chain = Chain.of(ctx); - if (!interceptors.isEmpty()) { - for (var interceptor : interceptors) { - chain = interceptor.intercept(chain); - } - } + chain = compressionInterceptor != null ? compressionInterceptor.intercept(chain) : chain; + chain = cachingInterceptor != null ? cachingInterceptor.intercept(chain) : chain; return chain; }