From 19aecb0f7da078880aae0ec7e70eb8e00429d3d5 Mon Sep 17 00:00:00 2001 From: Atri Sharma Date: Sat, 24 Aug 2019 03:01:28 +0530 Subject: [PATCH] SOLR-13655:Upgrade Collections.unModifiableSet to Set.of and Set.copyOf (#817) --- .../dataimport/config/ConfigNameConstants.java | 9 ++++----- .../cloud/api/collections/CategoryRoutedAlias.java | 10 ++++------ .../solr/cloud/autoscaling/ScheduledTriggers.java | 5 ++--- .../autoscaling/sim/SnapshotClusterStateProvider.java | 5 ++--- .../src/java/org/apache/solr/core/InitParams.java | 3 +-- .../apache/solr/handler/component/DebugComponent.java | 4 +--- .../src/java/org/apache/solr/util/TimeZoneUtils.java | 11 +++-------- .../org/apache/solr/client/solrj/SolrRequest.java | 7 ++----- .../solr/client/solrj/cloud/autoscaling/Policy.java | 2 +- .../solr/client/solrj/cloud/autoscaling/Variable.java | 7 +++---- .../solrj/impl/DelegationTokenHttpSolrClient.java | 2 +- .../org/apache/solr/common/cloud/ZkStateReader.java | 5 ++--- .../apache/solr/common/cloud/rule/ImplicitSnitch.java | 3 +-- .../org/apache/solr/common/params/CommonParams.java | 7 ++----- 14 files changed, 29 insertions(+), 51 deletions(-) diff --git a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/config/ConfigNameConstants.java b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/config/ConfigNameConstants.java index a34e31d430d..ee7f8755980 100644 --- a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/config/ConfigNameConstants.java +++ b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/config/ConfigNameConstants.java @@ -16,12 +16,11 @@ */ package org.apache.solr.handler.dataimport.config; -import org.apache.solr.handler.dataimport.SolrWriter; - -import java.util.Collections; import java.util.HashSet; import java.util.Set; +import org.apache.solr.handler.dataimport.SolrWriter; + public class ConfigNameConstants { public static final String SCRIPT = "script"; @@ -55,6 +54,6 @@ public class ConfigNameConstants { rw.add("functions"); rw.add("session"); rw.add(SolrWriter.LAST_INDEX_KEY); - RESERVED_WORDS = Collections.unmodifiableSet(rw); - } + RESERVED_WORDS = Set.copyOf(rw); + } } diff --git a/solr/core/src/java/org/apache/solr/cloud/api/collections/CategoryRoutedAlias.java b/solr/core/src/java/org/apache/solr/cloud/api/collections/CategoryRoutedAlias.java index c2f4ec8baf2..a7f2ac0b3d8 100644 --- a/solr/core/src/java/org/apache/solr/cloud/api/collections/CategoryRoutedAlias.java +++ b/solr/core/src/java/org/apache/solr/cloud/api/collections/CategoryRoutedAlias.java @@ -19,9 +19,7 @@ package org.apache.solr.cloud.api.collections; import java.lang.invoke.MethodHandles; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -58,12 +56,12 @@ public class CategoryRoutedAlias extends RoutedAlias { * Parameters required for creating a category routed alias */ @SuppressWarnings("WeakerAccess") - public static final Set REQUIRED_ROUTER_PARAMS = Collections.unmodifiableSet(new HashSet<>(Arrays.asList( + public static final Set REQUIRED_ROUTER_PARAMS = Set.of( CommonParams.NAME, ROUTER_TYPE_NAME, ROUTER_FIELD, ROUTER_MAX_CARDINALITY - ))); + ); public static final String ROUTER_MUST_MATCH = "router.mustMatch"; @@ -71,9 +69,9 @@ public class CategoryRoutedAlias extends RoutedAlias { * Optional parameters for creating a category routed alias excluding parameters for collection creation. */ @SuppressWarnings("WeakerAccess") - public static final Set OPTIONAL_ROUTER_PARAMS = Collections.unmodifiableSet(new HashSet<>(Arrays.asList( + public static final Set OPTIONAL_ROUTER_PARAMS = Set.of( ROUTER_MAX_CARDINALITY, - ROUTER_MUST_MATCH))); + ROUTER_MUST_MATCH); private Aliases aliases; private final String aliasName; diff --git a/solr/core/src/java/org/apache/solr/cloud/autoscaling/ScheduledTriggers.java b/solr/core/src/java/org/apache/solr/cloud/autoscaling/ScheduledTriggers.java index 768d8a77398..9a83db24c05 100644 --- a/solr/core/src/java/org/apache/solr/cloud/autoscaling/ScheduledTriggers.java +++ b/solr/core/src/java/org/apache/solr/cloud/autoscaling/ScheduledTriggers.java @@ -23,7 +23,6 @@ import java.lang.invoke.MethodHandles; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; -import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Locale; @@ -43,9 +42,9 @@ import java.util.concurrent.locks.ReentrantLock; import java.util.stream.Collectors; import org.apache.commons.lang3.exception.ExceptionUtils; -import org.apache.solr.client.solrj.cloud.autoscaling.AutoScalingConfig; import org.apache.solr.client.solrj.cloud.DistribStateManager; import org.apache.solr.client.solrj.cloud.SolrCloudManager; +import org.apache.solr.client.solrj.cloud.autoscaling.AutoScalingConfig; import org.apache.solr.client.solrj.cloud.autoscaling.TriggerEventProcessorStage; import org.apache.solr.client.solrj.cloud.autoscaling.VersionedData; import org.apache.solr.client.solrj.request.CollectionAdminRequest.RequestStatusResponse; @@ -508,7 +507,7 @@ public class ScheduledTriggers implements Closeable { * @return an unmodifiable set of names of all triggers being managed by this class */ public synchronized Set getScheduledTriggerNames() { - return Collections.unmodifiableSet(new HashSet<>(scheduledTriggerWrappers.keySet())); // shallow copy + return Set.copyOf(scheduledTriggerWrappers.keySet()); // shallow copy } /** diff --git a/solr/core/src/java/org/apache/solr/cloud/autoscaling/sim/SnapshotClusterStateProvider.java b/solr/core/src/java/org/apache/solr/cloud/autoscaling/sim/SnapshotClusterStateProvider.java index 4289b28e722..3655fb38202 100644 --- a/solr/core/src/java/org/apache/solr/cloud/autoscaling/sim/SnapshotClusterStateProvider.java +++ b/solr/core/src/java/org/apache/solr/cloud/autoscaling/sim/SnapshotClusterStateProvider.java @@ -21,7 +21,6 @@ import java.io.UnsupportedEncodingException; import java.util.Collection; import java.util.Collections; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Objects; @@ -45,7 +44,7 @@ public class SnapshotClusterStateProvider implements ClusterStateProvider { final Map clusterProperties; public SnapshotClusterStateProvider(ClusterStateProvider other) throws Exception { - liveNodes = Collections.unmodifiableSet(new HashSet<>(other.getLiveNodes())); + liveNodes = Set.copyOf(other.getLiveNodes()); ClusterState otherState = other.getClusterState(); clusterState = new ClusterState(otherState.getZNodeVersion(), liveNodes, otherState.getCollectionsMap()); clusterProperties = new HashMap<>(other.getClusterProperties()); @@ -53,7 +52,7 @@ public class SnapshotClusterStateProvider implements ClusterStateProvider { public SnapshotClusterStateProvider(Map snapshot) { Objects.requireNonNull(snapshot); - liveNodes = Collections.unmodifiableSet(new HashSet<>((Collection)snapshot.getOrDefault("liveNodes", Collections.emptySet()))); + liveNodes = Set.copyOf((Collection)snapshot.getOrDefault("liveNodes", Collections.emptySet())); clusterProperties = (Map)snapshot.getOrDefault("clusterProperties", Collections.emptyMap()); Map stateMap = new HashMap<>((Map)snapshot.getOrDefault("clusterState", Collections.emptyMap())); Number version = (Number)stateMap.remove("version"); diff --git a/solr/core/src/java/org/apache/solr/core/InitParams.java b/solr/core/src/java/org/apache/solr/core/InitParams.java index 031c7b78234..8e7b874ecd6 100644 --- a/solr/core/src/java/org/apache/solr/core/InitParams.java +++ b/solr/core/src/java/org/apache/solr/core/InitParams.java @@ -16,7 +16,6 @@ */ package org.apache.solr.core; -import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -49,7 +48,7 @@ public class InitParams { Set paths = null; String pathStr = p.attributes.get(PATH); if (pathStr != null) { - paths = Collections.unmodifiableSet(new HashSet<>(StrUtils.splitSmart(pathStr, ','))); + paths = Set.copyOf(StrUtils.splitSmart(pathStr, ',')); } this.paths = paths; NamedList nl = (NamedList) p.initArgs.get(DEFAULTS); diff --git a/solr/core/src/java/org/apache/solr/handler/component/DebugComponent.java b/solr/core/src/java/org/apache/solr/handler/component/DebugComponent.java index 87076a0b00b..c9768a4af77 100644 --- a/solr/core/src/java/org/apache/solr/handler/component/DebugComponent.java +++ b/solr/core/src/java/org/apache/solr/handler/component/DebugComponent.java @@ -18,10 +18,8 @@ package org.apache.solr.handler.component; import java.io.IOException; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -214,7 +212,7 @@ public class DebugComponent extends SearchComponent } } - private final static Set EXCLUDE_SET = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("explain"))); + private final static Set EXCLUDE_SET = Set.of("explain"); @Override public void finishStage(ResponseBuilder rb) { diff --git a/solr/core/src/java/org/apache/solr/util/TimeZoneUtils.java b/solr/core/src/java/org/apache/solr/util/TimeZoneUtils.java index 9ab49110e18..5881042ff2f 100644 --- a/solr/core/src/java/org/apache/solr/util/TimeZoneUtils.java +++ b/solr/core/src/java/org/apache/solr/util/TimeZoneUtils.java @@ -16,14 +16,10 @@ */ package org.apache.solr.util; -import java.util.TimeZone; import java.util.Set; -import java.util.HashSet; -import java.util.Collections; -import java.util.Arrays; - -import java.util.regex.Pattern; +import java.util.TimeZone; import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.apache.solr.common.SolrException; @@ -44,8 +40,7 @@ public final class TimeZoneUtils { * @see TimeZone#getAvailableIDs */ public static final Set KNOWN_TIMEZONE_IDS - = Collections.unmodifiableSet(new HashSet<> - (Arrays.asList(TimeZone.getAvailableIDs()))); + = Set.of(TimeZone.getAvailableIDs()); /** * This method is provided as a replacement for TimeZone.getTimeZone but diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/SolrRequest.java b/solr/solrj/src/java/org/apache/solr/client/solrj/SolrRequest.java index c87282005b2..ec78be2d17a 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/SolrRequest.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/SolrRequest.java @@ -23,7 +23,6 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; -import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; @@ -32,8 +31,6 @@ import org.apache.solr.client.solrj.request.RequestWriter; import org.apache.solr.common.params.SolrParams; import org.apache.solr.common.util.ContentStream; -import static java.util.Collections.unmodifiableSet; - /** * * @@ -58,11 +55,11 @@ public abstract class SolrRequest implements Serializabl DELETE }; - public static final Set SUPPORTED_METHODS = unmodifiableSet(new HashSet<>(Arrays.asList( + public static final Set SUPPORTED_METHODS = Set.of( METHOD.GET.toString(), METHOD.POST.toString(), METHOD.PUT.toString(), - METHOD.DELETE.toString()))); + METHOD.DELETE.toString()); private METHOD method = METHOD.GET; private String path = null; diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Policy.java b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Policy.java index 7b5c6c4786c..afe04b51ba7 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Policy.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Policy.java @@ -78,7 +78,7 @@ public class Policy implements MapWriter { public static final String POLICIES = "policies"; public static final String CLUSTER_POLICY = "cluster-policy"; public static final String CLUSTER_PREFERENCES = "cluster-preferences"; - public static final Set GLOBAL_ONLY_TAGS = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("cores", CollectionAdminParams.WITH_COLLECTION))); + public static final Set GLOBAL_ONLY_TAGS = Set.of("cores", CollectionAdminParams.WITH_COLLECTION); public static final List DEFAULT_PREFERENCES = Collections.unmodifiableList( Arrays.asList( // NOTE - if you change this, make sure to update the solrcloud-autoscaling-overview.adoc which diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Variable.java b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Variable.java index 0f494e3b650..3f6cd8e282b 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Variable.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Variable.java @@ -23,7 +23,6 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.util.Arrays; import java.util.HashMap; -import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.function.Consumer; @@ -32,7 +31,7 @@ import org.apache.solr.common.cloud.rule.ImplicitSnitch; import static java.util.Collections.emptySet; import static java.util.Collections.unmodifiableMap; -import static java.util.Collections.unmodifiableSet; + /** * A Variable Type used in Autoscaling policy rules. Each variable type may have unique implementation @@ -277,7 +276,7 @@ public interface Variable { this.metricsAttribute = readStr(meta.metricsKey()); this.supportedComputedTypes = meta.computedValues()[0] == ComputedType.NULL ? emptySet() : - unmodifiableSet(new HashSet(Arrays.asList(meta.computedValues()))); + Set.of(meta.computedValues()); this.wildCards = readSet(meta.wildCards()); } @@ -297,7 +296,7 @@ public interface Variable { Set readSet(String[] vals) { if (NULL.equals(vals[0])) return emptySet(); - return unmodifiableSet(new HashSet<>(Arrays.asList(vals))); + return Set.of(vals); } @Override diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/DelegationTokenHttpSolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/DelegationTokenHttpSolrClient.java index e878110a3a3..2466cf4a0f6 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/DelegationTokenHttpSolrClient.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/DelegationTokenHttpSolrClient.java @@ -99,7 +99,7 @@ public class DelegationTokenHttpSolrClient extends HttpSolrClient { @Override public void setQueryParams(Set queryParams) { queryParams = queryParams == null ? - Collections.unmodifiableSet(new HashSet(Arrays.asList(DELEGATION_TOKEN_PARAM))): queryParams; + Set.of(DELEGATION_TOKEN_PARAM): queryParams; if (!queryParams.contains(DELEGATION_TOKEN_PARAM)) { queryParams = new HashSet(queryParams); queryParams.add(DELEGATION_TOKEN_PARAM); diff --git a/solr/solrj/src/java/org/apache/solr/common/cloud/ZkStateReader.java b/solr/solrj/src/java/org/apache/solr/common/cloud/ZkStateReader.java index 5a5fcdb5a3f..4aa3ab0e235 100644 --- a/solr/solrj/src/java/org/apache/solr/common/cloud/ZkStateReader.java +++ b/solr/solrj/src/java/org/apache/solr/common/cloud/ZkStateReader.java @@ -73,7 +73,6 @@ import static java.util.Collections.EMPTY_MAP; import static java.util.Collections.emptyMap; import static java.util.Collections.emptySet; import static java.util.Collections.emptySortedSet; -import static java.util.Collections.unmodifiableSet; import static org.apache.solr.common.util.Utils.fromJSON; public class ZkStateReader implements SolrCloseable { @@ -272,7 +271,7 @@ public class ZkStateReader implements SolrCloseable { } - public static final Set KNOWN_CLUSTER_PROPS = unmodifiableSet(new HashSet<>(asList( + public static final Set KNOWN_CLUSTER_PROPS = Set.of( LEGACY_CLOUD, URL_SCHEME, AUTO_ADD_REPLICAS, @@ -281,7 +280,7 @@ public class ZkStateReader implements SolrCloseable { MAX_CORES_PER_NODE, SAMPLE_PERCENTAGE, SOLR_ENVIRONMENT, - CollectionAdminParams.DEFAULTS))); + CollectionAdminParams.DEFAULTS); /** * Returns config set name for collection. diff --git a/solr/solrj/src/java/org/apache/solr/common/cloud/rule/ImplicitSnitch.java b/solr/solrj/src/java/org/apache/solr/common/cloud/rule/ImplicitSnitch.java index ec55ccb5a4f..e00bf7253f3 100644 --- a/solr/solrj/src/java/org/apache/solr/common/cloud/rule/ImplicitSnitch.java +++ b/solr/solrj/src/java/org/apache/solr/common/cloud/rule/ImplicitSnitch.java @@ -22,7 +22,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -54,7 +53,7 @@ public class ImplicitSnitch extends Snitch { public static final String HEAPUSAGE = "heapUsage"; public static final String DISKTYPE = "diskType"; public static final List IP_SNITCHES = Collections.unmodifiableList(Arrays.asList("ip_1", "ip_2", "ip_3", "ip_4")); - public static final Set tags = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(NODE, PORT, HOST, CORES, DISK, ROLE, "ip_1", "ip_2", "ip_3", "ip_4"))); + public static final Set tags = Set.of(NODE, PORT, HOST, CORES, DISK, ROLE, "ip_1", "ip_2", "ip_3", "ip_4"); @Override public void getTags(String solrNode, Set requestedTags, SnitchContext ctx) { diff --git a/solr/solrj/src/java/org/apache/solr/common/params/CommonParams.java b/solr/solrj/src/java/org/apache/solr/common/params/CommonParams.java index 4ce1f5084e7..d0c85a882d2 100644 --- a/solr/solrj/src/java/org/apache/solr/common/params/CommonParams.java +++ b/solr/solrj/src/java/org/apache/solr/common/params/CommonParams.java @@ -16,9 +16,6 @@ */ package org.apache.solr.common.params; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashSet; import java.util.Locale; import java.util.Set; @@ -196,7 +193,7 @@ public interface CommonParams { String OK = "OK"; String FAILURE = "FAILURE"; - Set ADMIN_PATHS = Collections.unmodifiableSet(new HashSet<>(Arrays.asList( + Set ADMIN_PATHS = Set.of( CORES_HANDLER_PATH, COLLECTIONS_HANDLER_PATH, HEALTH_CHECK_HANDLER_PATH, @@ -209,7 +206,7 @@ public interface CommonParams { AUTOSCALING_PATH, AUTOSCALING_HISTORY_PATH, AUTOSCALING_DIAGNOSTICS_PATH, - AUTOSCALING_SUGGESTIONS_PATH))); + AUTOSCALING_SUGGESTIONS_PATH); String APISPEC_LOCATION = "apispec/"; String INTROSPECT = "/_introspect";