Skip to content

Commit 8c31bf1

Browse files
committed
Use UnaryOperator instead
1 parent 24ac6ca commit 8c31bf1

7 files changed

Lines changed: 46 additions & 165 deletions

File tree

httpclient5/src/main/java/org/apache/hc/client5/http/entity/compress/ContentCodecRegistry.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727

2828
package org.apache.hc.client5.http.entity.compress;
2929

30-
import java.io.IOException;
3130
import java.util.Arrays;
3231
import java.util.Collections;
3332
import java.util.EnumMap;
3433
import java.util.Map;
34+
import java.util.function.UnaryOperator;
3535
import java.util.zip.GZIPInputStream;
3636

3737
import org.apache.hc.client5.http.entity.DeflateInputStream;
@@ -44,7 +44,7 @@
4444

4545
/**
4646
* Run-time catalogue of built-in and Commons-Compress
47-
* {@linkplain Encoder encoders} / {@linkplain Decoder decoders}.
47+
* {@linkplain java.util.function.UnaryOperator encoders} / {@linkplain java.util.function.UnaryOperator decoders}.
4848
*
4949
* <p>Entries are wired once at class-load time and published through an
5050
* unmodifiable map, so lookups are lock-free and thread-safe.</p>
@@ -103,50 +103,49 @@ private static Map<ContentCoding, Codec> build() {
103103
return Collections.unmodifiableMap(m);
104104
}
105105

106-
public static HttpEntity wrap(final ContentCoding coding, final HttpEntity src) {
107-
final Codec c = REGISTRY.get(coding);
108-
return c != null && c.encoder != null ? c.encoder.wrap(src) : null;
106+
public static HttpEntity wrap(final ContentCoding c, final HttpEntity src) {
107+
final Codec k = REGISTRY.get(c);
108+
return k != null && k.encoder != null ? k.encoder.apply(src) : null;
109109
}
110110

111-
public static HttpEntity unwrap(final ContentCoding coding, final HttpEntity src) throws IOException {
112-
final Codec c = REGISTRY.get(coding);
113-
return c != null && c.decoder != null ? c.decoder.wrap(src) : null;
111+
public static HttpEntity unwrap(final ContentCoding c, final HttpEntity src) {
112+
final Codec k = REGISTRY.get(c);
113+
return k != null && k.decoder != null ? k.decoder.apply(src) : null;
114114
}
115115

116116
private ContentCodecRegistry() {
117117
}
118118

119119
/**
120-
* Returns the {@link Decoder} for the given coding, or {@code null}.
120+
* Returns the {@link java.util.function.UnaryOperator}&lt;HttpEntity&gt; for the given coding, or {@code null}.
121121
*/
122-
public static Decoder decoder(final ContentCoding coding) {
122+
public static UnaryOperator<HttpEntity> decoder(final ContentCoding coding) {
123123
final Codec c = REGISTRY.get(coding);
124124
return c != null ? c.decoder : null;
125125
}
126126

127127
/**
128-
* Returns the {@link Encoder} for the given coding, or {@code null}.
128+
* Returns the {@link java.util.function.UnaryOperator}&lt;HttpEntity&gt; for the given coding, or {@code null}.
129129
*/
130-
public static Encoder encoder(final ContentCoding coding) {
130+
public static UnaryOperator<HttpEntity> encoder(final ContentCoding coding) {
131131
final Codec c = REGISTRY.get(coding);
132132
return c != null ? c.encoder : null;
133133
}
134134

135-
136135
static final class Codec {
137-
final Encoder encoder;
138-
final Decoder decoder;
136+
final UnaryOperator<HttpEntity> encoder;
137+
final UnaryOperator<HttpEntity> decoder;
139138

140-
Codec(final Encoder enc, final Decoder dec) {
139+
Codec(final UnaryOperator<HttpEntity> enc, final UnaryOperator<HttpEntity> dec) {
141140
this.encoder = enc;
142141
this.decoder = dec;
143142
}
144143

145-
static Codec encodeOnly(final Encoder e) {
144+
static Codec encodeOnly(final UnaryOperator<HttpEntity> e) {
146145
return new Codec(e, null);
147146
}
148147

149-
static Codec decodeOnly(final Decoder d) {
148+
static Codec decodeOnly(final UnaryOperator<HttpEntity> d) {
150149
return new Codec(null, d);
151150
}
152151
}

httpclient5/src/main/java/org/apache/hc/client5/http/entity/compress/Decoder.java

Lines changed: 0 additions & 60 deletions
This file was deleted.

httpclient5/src/main/java/org/apache/hc/client5/http/entity/compress/Encoder.java

Lines changed: 0 additions & 59 deletions
This file was deleted.

httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ContentCompressionExec.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@
3333
import java.util.List;
3434
import java.util.Locale;
3535
import java.util.Map;
36+
import java.util.function.UnaryOperator;
3637

3738
import org.apache.hc.client5.http.classic.ExecChain;
3839
import org.apache.hc.client5.http.classic.ExecChainHandler;
3940
import org.apache.hc.client5.http.config.RequestConfig;
4041
import org.apache.hc.client5.http.entity.compress.ContentCodecRegistry;
4142
import org.apache.hc.client5.http.entity.compress.ContentCoding;
42-
import org.apache.hc.client5.http.entity.compress.Decoder;
4343
import org.apache.hc.client5.http.protocol.HttpClientContext;
4444
import org.apache.hc.core5.annotation.Contract;
4545
import org.apache.hc.core5.annotation.Internal;
@@ -74,12 +74,12 @@
7474
public final class ContentCompressionExec implements ExecChainHandler {
7575

7676
private final Header acceptEncoding;
77-
private final Lookup<Decoder> decoderRegistry;
77+
private final Lookup<UnaryOperator<HttpEntity>> decoderRegistry;
7878
private final boolean ignoreUnknown;
7979

8080
public ContentCompressionExec(
8181
final List<String> acceptEncoding,
82-
final Lookup<Decoder> decoderRegistry,
82+
final Lookup<UnaryOperator<HttpEntity>> decoderRegistry,
8383
final boolean ignoreUnknown) {
8484
this.acceptEncoding = MessageSupport.headerOfTokens(HttpHeaders.ACCEPT_ENCODING,
8585
Args.notEmpty(acceptEncoding, "Encoding list"));
@@ -88,15 +88,15 @@ public ContentCompressionExec(
8888
}
8989

9090
public ContentCompressionExec(final boolean ignoreUnknown) {
91-
final Map<ContentCoding, Decoder> decoderMap = new EnumMap<>(ContentCoding.class);
91+
final Map<ContentCoding, UnaryOperator<HttpEntity>> decoderMap = new EnumMap<>(ContentCoding.class);
9292
for (final ContentCoding c : ContentCoding.values()) {
93-
final Decoder d = ContentCodecRegistry.decoder(c);
93+
final UnaryOperator<HttpEntity> d = ContentCodecRegistry.decoder(c);
9494
if (d != null) {
9595
decoderMap.put(c, d);
9696
}
9797
}
9898

99-
final RegistryBuilder<Decoder> builder = RegistryBuilder.create();
99+
final RegistryBuilder<UnaryOperator<HttpEntity>> builder = RegistryBuilder.create();
100100
final List<String> acceptList = new ArrayList<>(decoderMap.size() + 1);
101101
decoderMap.forEach((coding, decoder) -> {
102102
acceptList.add(coding.token());
@@ -146,9 +146,9 @@ public ClassicHttpResponse execute(
146146
final HeaderElement[] codecs = BasicHeaderValueParser.INSTANCE.parseElements(contentEncoding, cursor);
147147
for (final HeaderElement codec : codecs) {
148148
final String codecname = codec.getName().toLowerCase(Locale.ROOT);
149-
final Decoder decoder = decoderRegistry.lookup(codecname);
149+
final UnaryOperator<HttpEntity> decoder = decoderRegistry.lookup(codecname);
150150
if (decoder != null) {
151-
response.setEntity(decoder.wrap(response.getEntity()));
151+
response.setEntity(decoder.apply(response.getEntity()));
152152
response.removeHeaders(HttpHeaders.CONTENT_LENGTH);
153153
response.removeHeaders(HttpHeaders.CONTENT_ENCODING);
154154
response.removeHeaders(HttpHeaders.CONTENT_MD5);

httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/HttpClientBuilder.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.List;
3939
import java.util.Map;
4040
import java.util.function.Function;
41+
import java.util.function.UnaryOperator;
4142

4243
import org.apache.hc.client5.http.AuthenticationStrategy;
4344
import org.apache.hc.client5.http.ConnectionKeepAliveStrategy;
@@ -57,7 +58,6 @@
5758
import org.apache.hc.client5.http.entity.InputStreamFactory;
5859
import org.apache.hc.client5.http.entity.compress.ContentCodecRegistry;
5960
import org.apache.hc.client5.http.entity.compress.ContentCoding;
60-
import org.apache.hc.client5.http.entity.compress.Decoder;
6161
import org.apache.hc.client5.http.impl.ChainElement;
6262
import org.apache.hc.client5.http.impl.CookieSpecSupport;
6363
import org.apache.hc.client5.http.impl.DefaultAuthenticationStrategy;
@@ -92,6 +92,7 @@
9292
import org.apache.hc.core5.annotation.Internal;
9393
import org.apache.hc.core5.http.ConnectionReuseStrategy;
9494
import org.apache.hc.core5.http.Header;
95+
import org.apache.hc.core5.http.HttpEntity;
9596
import org.apache.hc.core5.http.HttpHost;
9697
import org.apache.hc.core5.http.HttpRequestInterceptor;
9798
import org.apache.hc.core5.http.HttpResponseInterceptor;
@@ -241,9 +242,8 @@ private ExecInterceptorEntry(
241242
/**
242243
* Custom decoders keyed by {@link ContentCoding}.
243244
*
244-
* @since 5.6
245245
*/
246-
private LinkedHashMap<ContentCoding, Decoder> contentDecoder;
246+
private LinkedHashMap<ContentCoding, UnaryOperator<HttpEntity>> contentDecoder;
247247

248248
public static HttpClientBuilder create() {
249249
return new HttpClientBuilder();
@@ -715,8 +715,8 @@ public final HttpClientBuilder setContentDecoderRegistry(
715715
}
716716

717717
/**
718-
* Sets a map of {@link Decoder decoders}, keyed by {@link ContentCoding},
719-
* to be used for automatic response decompression.
718+
* Sets a map of {@linkplain java.util.function.UnaryOperator}&lt;HttpEntity&gt; decoders,
719+
* keyed by {@link ContentCoding}, to be used for automatic response decompression.
720720
*
721721
* @param contentDecoder decoder map, or {@code null} to fall back to the
722722
* defaults from {@link ContentCodecRegistry}.
@@ -725,11 +725,12 @@ public final HttpClientBuilder setContentDecoderRegistry(
725725
* @since 5.6
726726
*/
727727
public final HttpClientBuilder setContentDecoder(
728-
final LinkedHashMap<ContentCoding, Decoder> contentDecoder) {
728+
final LinkedHashMap<ContentCoding, UnaryOperator<HttpEntity>> contentDecoder) {
729729
this.contentDecoder = contentDecoder;
730730
return this;
731731
}
732732

733+
733734
/**
734735
* Sets default {@link RequestConfig} instance which will be used
735736
* for request execution if not explicitly set in the client execution
@@ -993,13 +994,13 @@ public CloseableHttpClient build() {
993994
// Custom decoder map supplied by the caller
994995
if (contentDecoder != null) {
995996
final List<String> encodings = new ArrayList<>(contentDecoder.size());
996-
final RegistryBuilder<Decoder> b2 = RegistryBuilder.create();
997-
for (final Map.Entry<ContentCoding, Decoder> entry : contentDecoder.entrySet()) {
997+
final RegistryBuilder<UnaryOperator<HttpEntity>> b2 = RegistryBuilder.create();
998+
for (final Map.Entry<ContentCoding, UnaryOperator<HttpEntity>> entry : contentDecoder.entrySet()) {
998999
final String token = entry.getKey().token();
9991000
encodings.add(token);
10001001
b2.register(token, entry.getValue());
10011002
}
1002-
final Registry<Decoder> decoderRegistry = b2.build();
1003+
final Registry<UnaryOperator<HttpEntity>> decoderRegistry = b2.build();
10031004

10041005
execChainDefinition.addFirst(
10051006
new ContentCompressionExec(encodings, decoderRegistry, true),

httpclient5/src/test/java/org/apache/hc/client5/http/entity/TestDeflate.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929

3030
import java.io.ByteArrayOutputStream;
3131
import java.nio.charset.StandardCharsets;
32+
import java.util.function.UnaryOperator;
3233
import java.util.zip.Deflater;
3334

3435
import org.apache.hc.client5.http.entity.compress.ContentCodecRegistry;
3536
import org.apache.hc.client5.http.entity.compress.ContentCoding;
36-
import org.apache.hc.client5.http.entity.compress.Encoder;
3737
import org.apache.hc.core5.http.ContentType;
3838
import org.apache.hc.core5.http.HttpEntity;
3939
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
@@ -59,7 +59,7 @@ void testCompressDecompress() throws Exception {
5959

6060
final HttpEntity entity = ContentCodecRegistry
6161
.decoder(ContentCoding.DEFLATE)
62-
.wrap(new ByteArrayEntity(compressed, 0, len, ContentType.APPLICATION_OCTET_STREAM));
62+
.apply(new ByteArrayEntity(compressed, 0, len, ContentType.APPLICATION_OCTET_STREAM));
6363

6464
Assertions.assertEquals(s, EntityUtils.toString(entity));
6565
}
@@ -70,17 +70,17 @@ void testEncodeThenDecode() throws Exception {
7070
final String text = "some kind of text";
7171

7272
final HttpEntity plain = new StringEntity(text, ContentType.TEXT_PLAIN);
73-
final Encoder encoder = ContentCodecRegistry.encoder(ContentCoding.DEFLATE);
73+
final UnaryOperator<HttpEntity> encoder = ContentCodecRegistry.encoder(ContentCoding.DEFLATE);
7474
Assertions.assertNotNull(encoder, "deflate encoder must exist");
7575

76-
final HttpEntity deflated = encoder.wrap(plain);
76+
final HttpEntity deflated = encoder.apply(plain);
7777

7878
final ByteArrayOutputStream buf = new ByteArrayOutputStream();
7979
deflated.writeTo(buf);
8080

8181
final HttpEntity decoded = ContentCodecRegistry
8282
.decoder(ContentCoding.DEFLATE)
83-
.wrap(new ByteArrayEntity(buf.toByteArray(), ContentType.APPLICATION_OCTET_STREAM));
83+
.apply(new ByteArrayEntity(buf.toByteArray(), ContentType.APPLICATION_OCTET_STREAM));
8484

8585
Assertions.assertEquals(text, EntityUtils.toString(decoded, StandardCharsets.US_ASCII));
8686
}

0 commit comments

Comments
 (0)