Skip to content

Commit 4739a25

Browse files
committed
Use Java 11 utilities to create immutable copies of collections
1 parent 1b8ab7b commit 4739a25

9 files changed

+35
-57
lines changed

src/main/java/com/rabbitmq/stream/ByteCapacity.java

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020-2023 Broadcom. All Rights Reserved.
1+
// Copyright (c) 2020-2024 Broadcom. All Rights Reserved.
22
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
33
//
44
// This software, the RabbitMQ Stream Java client library, is dual-licensed under the
@@ -14,8 +14,6 @@
1414
1515
package com.rabbitmq.stream;
1616

17-
import java.util.Collections;
18-
import java.util.HashMap;
1917
import java.util.Map;
2018
import java.util.Objects;
2119
import java.util.function.BiFunction;
@@ -40,15 +38,11 @@ public class ByteCapacity implements Comparable<ByteCapacity> {
4038
private static final String UNIT_TB = "tb";
4139

4240
private static final Map<String, BiFunction<Long, String, ByteCapacity>> CONSTRUCTORS =
43-
Collections.unmodifiableMap(
44-
new HashMap<String, BiFunction<Long, String, ByteCapacity>>() {
45-
{
46-
put(UNIT_KB, (size, input) -> ByteCapacity.kB(size, input));
47-
put(UNIT_MB, (size, input) -> ByteCapacity.MB(size, input));
48-
put(UNIT_GB, (size, input) -> ByteCapacity.GB(size, input));
49-
put(UNIT_TB, (size, input) -> ByteCapacity.TB(size, input));
50-
}
51-
});
41+
Map.of(
42+
UNIT_KB, ByteCapacity::kB,
43+
UNIT_MB, ByteCapacity::MB,
44+
UNIT_GB, ByteCapacity::GB,
45+
UNIT_TB, ByteCapacity::TB);
5246

5347
private final long bytes;
5448
private final String input;

src/main/java/com/rabbitmq/stream/impl/Client.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ private static Map<String, String> clientProperties(Map<String, String> fromPara
444444
fromParameters = fromParameters == null ? Collections.emptyMap() : fromParameters;
445445
Map<String, String> clientProperties = new HashMap<>(fromParameters);
446446
clientProperties.putAll(ClientProperties.DEFAULT_CLIENT_PROPERTIES);
447-
return Collections.unmodifiableMap(clientProperties);
447+
return Map.copyOf(clientProperties);
448448
}
449449

450450
static void checkMessageFitsInFrame(int maxFrameSize, Codec.EncodedMessage encodedMessage) {
@@ -2224,7 +2224,7 @@ static class StreamStatsResponse extends Response {
22242224

22252225
StreamStatsResponse(short responseCode, Map<String, Long> info) {
22262226
super(responseCode);
2227-
this.info = Collections.unmodifiableMap(new HashMap<>(info));
2227+
this.info = Map.copyOf(info);
22282228
}
22292229

22302230
public Map<String, Long> getInfo() {
@@ -2249,7 +2249,7 @@ public StreamMetadata(String stream, short responseCode, Broker leader, List<Bro
22492249
this.replicas =
22502250
(replicas == null || replicas.isEmpty())
22512251
? Collections.emptyList()
2252-
: Collections.unmodifiableList(replicas);
2252+
: List.copyOf(replicas);
22532253
}
22542254

22552255
public short getResponseCode() {
@@ -2264,8 +2264,9 @@ public Broker getLeader() {
22642264
return leader;
22652265
}
22662266

2267+
@SuppressFBWarnings("EI_EXPOSE_REP")
22672268
public List<Broker> getReplicas() {
2268-
return this.replicas.isEmpty() ? Collections.emptyList() : new ArrayList<>(this.replicas);
2269+
return this.replicas;
22692270
}
22702271

22712272
boolean hasReplicas() {
@@ -2562,7 +2563,7 @@ int port() {
25622563
}
25632564

25642565
Map<String, String> clientProperties() {
2565-
return Collections.unmodifiableMap(this.clientProperties);
2566+
return Map.copyOf(this.clientProperties);
25662567
}
25672568

25682569
Codec codec() {

src/main/java/com/rabbitmq/stream/impl/ClientProperties.java

+7-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020-2023 Broadcom. All Rights Reserved.
1+
// Copyright (c) 2020-2024 Broadcom. All Rights Reserved.
22
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
33
//
44
// This software, the RabbitMQ Stream Java client library, is dual-licensed under the
@@ -15,8 +15,6 @@
1515
package com.rabbitmq.stream.impl;
1616

1717
import java.io.InputStream;
18-
import java.util.Collections;
19-
import java.util.HashMap;
2018
import java.util.Map;
2119
import java.util.Properties;
2220
import org.slf4j.Logger;
@@ -39,16 +37,12 @@ public final class ClientProperties {
3937
public static final String VERSION = getVersion();
4038

4139
public static final Map<String, String> DEFAULT_CLIENT_PROPERTIES =
42-
Collections.unmodifiableMap(
43-
new HashMap<String, String>() {
44-
{
45-
put("product", "RabbitMQ Stream");
46-
put("version", ClientProperties.VERSION);
47-
put("platform", "Java");
48-
put("copyright", "Copyright (c) 2020-2024 Broadcom Inc. and/or its subsidiaries.");
49-
put("information", "Licensed under the MPL 2.0. See https://www.rabbitmq.com/");
50-
}
51-
});
40+
Map.of(
41+
"product", "RabbitMQ Stream",
42+
"version", ClientProperties.VERSION,
43+
"platform", "Java",
44+
"copyright", "Copyright (c) 2020-2024 Broadcom Inc. and/or its subsidiaries.",
45+
"information", "Licensed under the MPL 2.0. See https://www.rabbitmq.com/");
5246

5347
private static String getVersion() {
5448
String version;

src/main/java/com/rabbitmq/stream/impl/ConsumersCoordinator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ private SubscriptionTracker(
443443
properties.putAll(subscriptionProperties);
444444
// we propagate the subscription name, used for monitoring
445445
properties.put("name", this.offsetTrackingReference);
446-
this.subscriptionProperties = Collections.unmodifiableMap(properties);
446+
this.subscriptionProperties = Map.copyOf(properties);
447447
}
448448
}
449449

src/main/java/com/rabbitmq/stream/impl/ProducersCoordinator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ List<BrokerWrapper> findCandidateNodes(String stream, boolean forceLeader) {
233233

234234
LOGGER.debug("Candidates to publish to {}: {}", stream, candidates);
235235

236-
return Collections.unmodifiableList(candidates);
236+
return List.copyOf(candidates);
237237
}
238238

239239
static Broker pickBroker(List<BrokerWrapper> candidates) {

src/main/java/com/rabbitmq/stream/impl/ServerFrameHandler.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020-2023 Broadcom. All Rights Reserved.
1+
// Copyright (c) 2020-2024 Broadcom. All Rights Reserved.
22
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
33
//
44
// This software, the RabbitMQ Stream Java client library, is dual-licensed under the
@@ -771,14 +771,11 @@ int doHandle(Client client, ChannelHandlerContext ctx, ByteBuf message) {
771771
}
772772

773773
OutstandingRequest<Map<String, String>> outstandingRequest =
774-
remove(
775-
client.outstandingRequests,
776-
correlationId,
777-
new ParameterizedTypeReference<Map<String, String>>() {});
774+
remove(client.outstandingRequests, correlationId, new ParameterizedTypeReference<>() {});
778775
if (outstandingRequest == null) {
779776
LOGGER.warn("Could not find outstanding request with correlation ID {}", correlationId);
780777
} else {
781-
outstandingRequest.response().set(Collections.unmodifiableMap(serverProperties));
778+
outstandingRequest.response().set(Map.copyOf(serverProperties));
782779
outstandingRequest.countDown();
783780
}
784781
return read;

src/main/java/com/rabbitmq/stream/impl/StreamConsumer.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020-2023 Broadcom. All Rights Reserved.
1+
// Copyright (c) 2020-2024 Broadcom. All Rights Reserved.
22
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
33
//
44
// This software, the RabbitMQ Stream Java client library, is dual-licensed under the
@@ -26,7 +26,6 @@
2626
import com.rabbitmq.stream.impl.StreamEnvironment.TrackingConsumerRegistration;
2727
import com.rabbitmq.stream.impl.Utils.CompositeConsumerUpdateListener;
2828
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
29-
import java.util.Collections;
3029
import java.util.Map;
3130
import java.util.Objects;
3231
import java.util.concurrent.CompletableFuture;
@@ -255,7 +254,7 @@ class StreamConsumer implements Consumer {
255254
subscriptionListener,
256255
trackingClosingCallback,
257256
closedAwareMessageHandler,
258-
Collections.unmodifiableMap(subscriptionProperties),
257+
Map.copyOf(subscriptionProperties),
259258
flowStrategy);
260259

261260
this.status = Status.RUNNING;

src/main/java/com/rabbitmq/stream/impl/Utils.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package com.rabbitmq.stream.impl;
1616

1717
import static java.lang.String.format;
18-
import static java.util.Collections.unmodifiableList;
18+
import static java.util.Map.copyOf;
1919

2020
import com.rabbitmq.stream.*;
2121
import com.rabbitmq.stream.impl.Client.ClientParameters;
@@ -71,7 +71,7 @@ final class Utils {
7171
LOGGER.info("Error while trying to access field Constants." + field.getName());
7272
}
7373
});
74-
CONSTANT_LABELS = Collections.unmodifiableMap(labels);
74+
CONSTANT_LABELS = copyOf(labels);
7575
}
7676

7777
static final AddressResolver DEFAULT_ADDRESS_RESOLVER = address -> address;
@@ -358,10 +358,7 @@ static class ClientFactoryContext {
358358
ClientParameters parameters, String targetKey, List<Client.Broker> candidates) {
359359
this.parameters = parameters;
360360
this.targetKey = targetKey;
361-
this.candidates =
362-
candidates == null
363-
? Collections.emptyList()
364-
: unmodifiableList(new ArrayList<>(candidates));
361+
this.candidates = candidates == null ? Collections.emptyList() : List.copyOf(candidates);
365362
}
366363

367364
ClientParameters parameters() {

src/main/java/com/rabbitmq/stream/sasl/DefaultSaslConfiguration.java

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020-2023 Broadcom. All Rights Reserved.
1+
// Copyright (c) 2020-2024 Broadcom. All Rights Reserved.
22
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
33
//
44
// This software, the RabbitMQ Stream Java client library, is dual-licensed under the
@@ -17,7 +17,6 @@
1717
import static java.lang.String.format;
1818

1919
import java.util.Collections;
20-
import java.util.HashMap;
2120
import java.util.List;
2221
import java.util.Map;
2322

@@ -32,14 +31,11 @@ public final class DefaultSaslConfiguration implements SaslConfiguration {
3231
new DefaultSaslConfiguration(AnonymousSaslMechanism.INSTANCE.getName());
3332

3433
private final Map<String, SaslMechanism> mechanisms =
35-
Collections.unmodifiableMap(
36-
new HashMap<String, SaslMechanism>() {
37-
{
38-
put(PlainSaslMechanism.INSTANCE.getName(), PlainSaslMechanism.INSTANCE);
39-
put(ExternalSaslMechanism.INSTANCE.getName(), ExternalSaslMechanism.INSTANCE);
40-
put(AnonymousSaslMechanism.INSTANCE.getName(), AnonymousSaslMechanism.INSTANCE);
41-
}
42-
});
34+
Map.of(
35+
PlainSaslMechanism.INSTANCE.getName(), PlainSaslMechanism.INSTANCE,
36+
ExternalSaslMechanism.INSTANCE.getName(), ExternalSaslMechanism.INSTANCE,
37+
AnonymousSaslMechanism.INSTANCE.getName(), AnonymousSaslMechanism.INSTANCE);
38+
4339
private final String mechanism;
4440

4541
public DefaultSaslConfiguration() {

0 commit comments

Comments
 (0)