From 0292929d7e083e64b16bf00d44d2307f2a949948 Mon Sep 17 00:00:00 2001 From: alexey-ivanov-es Date: Mon, 9 Dec 2024 16:51:15 +0000 Subject: [PATCH] Immutable collections --- .../java/org/elasticsearch/TransportVersion.java | 12 +++++------- .../java/org/elasticsearch/TransportVersions.java | 2 +- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/TransportVersion.java b/server/src/main/java/org/elasticsearch/TransportVersion.java index aee4a6f9d4407..8457973f15840 100644 --- a/server/src/main/java/org/elasticsearch/TransportVersion.java +++ b/server/src/main/java/org/elasticsearch/TransportVersion.java @@ -16,7 +16,6 @@ import org.elasticsearch.plugins.ExtensionLoader; import java.io.IOException; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -24,6 +23,7 @@ import java.util.ServiceLoader; import java.util.function.Function; import java.util.stream.Collectors; +import java.util.stream.Stream; /** * Represents the version of the wire protocol used to communicate between a pair of ES nodes. @@ -172,14 +172,12 @@ private static class VersionsHolder { if (extendedVersions.isEmpty()) { ALL_VERSIONS = TransportVersions.DEFINED_VERSIONS; } else { - List allVersions = new ArrayList<>(TransportVersions.DEFINED_VERSIONS.size() + extendedVersions.size()); - allVersions.addAll(TransportVersions.DEFINED_VERSIONS); - allVersions.addAll(extendedVersions); - Collections.sort(allVersions); - ALL_VERSIONS = Collections.unmodifiableList(allVersions); + ALL_VERSIONS = Stream.concat(TransportVersions.DEFINED_VERSIONS.stream(), extendedVersions.stream()) + .sorted() + .toList(); } - ALL_VERSIONS_MAP = ALL_VERSIONS.stream().collect(Collectors.toMap(TransportVersion::id, Function.identity())); + ALL_VERSIONS_MAP = ALL_VERSIONS.stream().collect(Collectors.toUnmodifiableMap(TransportVersion::id, Function.identity())); CURRENT = ALL_VERSIONS.getLast(); } diff --git a/server/src/main/java/org/elasticsearch/TransportVersions.java b/server/src/main/java/org/elasticsearch/TransportVersions.java index ea1512447b8e3..6cc6bfb01140f 100644 --- a/server/src/main/java/org/elasticsearch/TransportVersions.java +++ b/server/src/main/java/org/elasticsearch/TransportVersions.java @@ -260,7 +260,7 @@ public static List collectAllVersionIdsDefinedInClass(Class Collections.sort(definedTransportVersions); - return Collections.unmodifiableList(definedTransportVersions); + return List.copyOf(definedTransportVersions); } static final IntFunction VERSION_LOOKUP = ReleaseVersions.generateVersionsLookup(TransportVersions.class, LATEST_DEFINED.id());