From 00e23c119d4e2626e23d1bd2995494f669bebc1d Mon Sep 17 00:00:00 2001 From: Christine Poerschke Date: Fri, 25 Jun 2021 17:24:24 +0100 Subject: [PATCH] SOLR-15485: discourage Collections.singleton.forEach use (#179) --- .../validation/validate-source-patterns.gradle | 1 + .../java/org/apache/solr/core/CoreContainer.java | 5 +---- .../solr/BaseDistributedSearchTestCase.java | 4 +--- .../cloud/AbstractFullDistribZkTestBase.java | 16 ++++------------ 4 files changed, 7 insertions(+), 19 deletions(-) diff --git a/gradle/validation/validate-source-patterns.gradle b/gradle/validation/validate-source-patterns.gradle index 74a5af35bd0b..c39b4aa4eab9 100644 --- a/gradle/validation/validate-source-patterns.gradle +++ b/gradle/validation/validate-source-patterns.gradle @@ -156,6 +156,7 @@ class ValidateSourcePatternsTask extends DefaultTask { (~$/\$$Header\b/$) : 'svn keyword', (~$/\$$Source\b/$) : 'svn keyword', (~$/^\uFEFF/$) : 'UTF-8 byte order mark', + (~$/Collections\.singleton.*forEach/$) : 'potentially unnecessary forEach() use with Collections.singleton', (~$/import java\.lang\.\w+;/$) : 'java.lang import is unnecessary' ] diff --git a/solr/core/src/java/org/apache/solr/core/CoreContainer.java b/solr/core/src/java/org/apache/solr/core/CoreContainer.java index 90908cbb4ba6..5c93c8f781e6 100644 --- a/solr/core/src/java/org/apache/solr/core/CoreContainer.java +++ b/solr/core/src/java/org/apache/solr/core/CoreContainer.java @@ -125,7 +125,6 @@ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.Date; import java.util.LinkedHashSet; import java.util.List; @@ -1143,9 +1142,7 @@ public void shutdown() { } finally { try { if (updateShardHandler != null) { - customThreadPool.submit(() -> Collections.singleton(shardHandlerFactory).parallelStream().forEach(c -> { - updateShardHandler.close(); - })); + customThreadPool.submit(() -> updateShardHandler.close()); } } finally { try { diff --git a/solr/test-framework/src/java/org/apache/solr/BaseDistributedSearchTestCase.java b/solr/test-framework/src/java/org/apache/solr/BaseDistributedSearchTestCase.java index f91f22f3328a..6759af85a1e6 100644 --- a/solr/test-framework/src/java/org/apache/solr/BaseDistributedSearchTestCase.java +++ b/solr/test-framework/src/java/org/apache/solr/BaseDistributedSearchTestCase.java @@ -414,9 +414,7 @@ protected String getShardsString() { protected void destroyServers() throws Exception { ExecutorService customThreadPool = ExecutorUtil.newMDCAwareCachedThreadPool(new SolrNamedThreadFactory("closeThreadPool")); - customThreadPool.submit(() -> Collections.singleton(controlClient).parallelStream().forEach(c -> { - IOUtils.closeQuietly(c); - })); + customThreadPool.submit(() -> IOUtils.closeQuietly(controlClient)); customThreadPool.submit(() -> { try { diff --git a/solr/test-framework/src/java/org/apache/solr/cloud/AbstractFullDistribZkTestBase.java b/solr/test-framework/src/java/org/apache/solr/cloud/AbstractFullDistribZkTestBase.java index 3b200ac9d4ce..2ed7ae394036 100644 --- a/solr/test-framework/src/java/org/apache/solr/cloud/AbstractFullDistribZkTestBase.java +++ b/solr/test-framework/src/java/org/apache/solr/cloud/AbstractFullDistribZkTestBase.java @@ -1718,25 +1718,17 @@ public void distribTearDown() throws Exception { protected void destroyServers() throws Exception { ExecutorService customThreadPool = ExecutorUtil.newMDCAwareCachedThreadPool(new SolrNamedThreadFactory("closeThreadPool")); - customThreadPool.submit(() -> Collections.singleton(commonCloudSolrClient).parallelStream().forEach(c -> { - IOUtils.closeQuietly(c); - })); + customThreadPool.submit(() -> IOUtils.closeQuietly(commonCloudSolrClient)); - customThreadPool.submit(() -> Collections.singleton(controlClient).parallelStream().forEach(c -> { - IOUtils.closeQuietly(c); - })); + customThreadPool.submit(() -> IOUtils.closeQuietly(controlClient)); customThreadPool.submit(() -> coreClients.parallelStream().forEach(c -> { IOUtils.closeQuietly(c); })); - customThreadPool.submit(() -> Collections.singletonList(controlClientCloud).parallelStream().forEach(c -> { - IOUtils.closeQuietly(c); - })); + customThreadPool.submit(() -> IOUtils.closeQuietly(controlClientCloud)); - customThreadPool.submit(() -> Collections.singletonList(cloudClient).parallelStream().forEach(c -> { - IOUtils.closeQuietly(c); - })); + customThreadPool.submit(() -> IOUtils.closeQuietly(cloudClient)); ExecutorUtil.shutdownAndAwaitTermination(customThreadPool);