Skip to content

Commit

Permalink
Use ConcurrentHashMap.newKeySet
Browse files Browse the repository at this point in the history
In places where a ConcurrentHashMap was used as a set by wrapping it
with Collections.newSetFromMap, switch to just using the set returned
by ConcurrentHashMap.newKeySet directly.

Closes gh-32294
  • Loading branch information
kilink authored and sbrannen committed Feb 21, 2024
1 parent ff9c714 commit f9fe8ef
Show file tree
Hide file tree
Showing 14 changed files with 16 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -136,7 +135,7 @@ public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport
@Nullable
private BeanFactory beanFactory;

private final Set<String> targetSourcedBeans = Collections.newSetFromMap(new ConcurrentHashMap<>(16));
private final Set<String> targetSourcedBeans = ConcurrentHashMap.newKeySet(16);

private final Map<Object, Object> earlyBeanReferences = new ConcurrentHashMap<>(16);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.lang.reflect.Modifier;
import java.net.URL;
import java.security.ProtectionDomain;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -88,7 +87,7 @@ public final class CachedIntrospectionResults {
* accept classes from, even if the classes do not qualify as cache-safe.
*/
static final Set<ClassLoader> acceptedClassLoaders =
Collections.newSetFromMap(new ConcurrentHashMap<>(16));
ConcurrentHashMap.newKeySet(16);

/**
* Map keyed by Class containing CachedIntrospectionResults, strongly held.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
Expand Down Expand Up @@ -178,7 +177,7 @@ public class AutowiredAnnotationBeanPostProcessor implements SmartInstantiationA
@Nullable
private MetadataReaderFactory metadataReaderFactory;

private final Set<String> lookupMethodsChecked = Collections.newSetFromMap(new ConcurrentHashMap<>(256));
private final Set<String> lookupMethodsChecked = ConcurrentHashMap.newKeySet(256);

private final Map<Class<?>, Constructor<?>[]> candidateConstructorsCache = new ConcurrentHashMap<>(256);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.springframework.beans.factory.config;

import java.util.Collections;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Set;
Expand Down Expand Up @@ -77,7 +76,7 @@ public class PropertyOverrideConfigurer extends PropertyResourceConfigurer {
/**
* Contains names of beans that have overrides.
*/
private final Set<String> beanNames = Collections.newSetFromMap(new ConcurrentHashMap<>(16));
private final Set<String> beanNames = ConcurrentHashMap.newKeySet(16);


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -165,7 +164,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
private final Map<String, RootBeanDefinition> mergedBeanDefinitions = new ConcurrentHashMap<>(256);

/** Names of beans that have already been created at least once. */
private final Set<String> alreadyCreated = Collections.newSetFromMap(new ConcurrentHashMap<>(256));
private final Set<String> alreadyCreated = ConcurrentHashMap.newKeySet(256);

/** Names of beans that are currently in creation. */
private final ThreadLocal<Object> prototypesCurrentlyInCreation =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.IdentityHashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -169,7 +168,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
private final Map<String, BeanDefinitionHolder> mergedBeanDefinitionHolders = new ConcurrentHashMap<>(256);

// Set of bean definition names with a primary marker. */
private final Set<String> primaryBeanNames = Collections.newSetFromMap(new ConcurrentHashMap<>(16));
private final Set<String> primaryBeanNames = ConcurrentHashMap.newKeySet(16);

/** Map of singleton and non-singleton bean names, keyed by dependency type. */
private final Map<Class<?>, String[]> allBeanNamesByType = new ConcurrentHashMap<>(64);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.springframework.beans.factory.support;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -91,12 +90,10 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements
private final Lock singletonLock = new ReentrantLock();

/** Names of beans that are currently in creation. */
private final Set<String> singletonsCurrentlyInCreation =
Collections.newSetFromMap(new ConcurrentHashMap<>(16));
private final Set<String> singletonsCurrentlyInCreation = ConcurrentHashMap.newKeySet(16);

/** Names of beans currently excluded from in creation checks. */
private final Set<String> inCreationCheckExclusions =
Collections.newSetFromMap(new ConcurrentHashMap<>(16));
private final Set<String> inCreationCheckExclusions = ConcurrentHashMap.newKeySet(16);

@Nullable
private volatile Thread singletonCreationThread;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -82,7 +81,7 @@ public class EventListenerMethodProcessor
@Nullable
private final EventExpressionEvaluator evaluator;

private final Set<Class<?>> nonAnnotatedClasses = Collections.newSetFromMap(new ConcurrentHashMap<>(64));
private final Set<Class<?>> nonAnnotatedClasses = ConcurrentHashMap.newKeySet(64);


public EventListenerMethodProcessor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public boolean isRunning() {

void stopForRestart() {
if (this.running) {
this.stoppedBeans = Collections.newSetFromMap(new ConcurrentHashMap<>());
this.stoppedBeans = ConcurrentHashMap.newKeySet();
stopBeans();
this.running = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.LinkedHashSet;
import java.util.List;
Expand Down Expand Up @@ -149,7 +148,7 @@ public class ScheduledAnnotationBeanPostProcessor
@Nullable
private TaskSchedulerRouter localScheduler;

private final Set<Class<?>> nonAnnotatedClasses = Collections.newSetFromMap(new ConcurrentHashMap<>(64));
private final Set<Class<?>> nonAnnotatedClasses = ConcurrentHashMap.newKeySet(64);

private final Map<Object, Set<ScheduledTask>> scheduledTasks = new IdentityHashMap<>(16);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.springframework.core;

import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

Expand All @@ -39,9 +38,9 @@ public abstract class DecoratingClassLoader extends ClassLoader {
}


private final Set<String> excludedPackages = Collections.newSetFromMap(new ConcurrentHashMap<>(8));
private final Set<String> excludedPackages = ConcurrentHashMap.newKeySet(8);

private final Set<String> excludedClasses = Collections.newSetFromMap(new ConcurrentHashMap<>(8));
private final Set<String> excludedClasses = ConcurrentHashMap.newKeySet(8);


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.springframework.core.task;

import java.io.Serializable;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -183,7 +182,7 @@ public void setTaskDecorator(TaskDecorator taskDecorator) {
public void setTaskTerminationTimeout(long timeout) {
Assert.isTrue(timeout >= 0, "Timeout value must be >=0");
this.taskTerminationTimeout = timeout;
this.activeThreads = (timeout > 0 ? Collections.newSetFromMap(new ConcurrentHashMap<>()) : null);
this.activeThreads = (timeout > 0 ? ConcurrentHashMap.newKeySet() : null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class SessionAttributesHandler {

private final Set<Class<?>> attributeTypes = new HashSet<>();

private final Set<String> knownAttributeNames = Collections.newSetFromMap(new ConcurrentHashMap<>(4));
private final Set<String> knownAttributeNames = ConcurrentHashMap.newKeySet(4);

private final SessionAttributeStore sessionAttributeStore;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class SessionAttributesHandler {

private final Set<Class<?>> attributeTypes = new HashSet<>();

private final Set<String> knownAttributeNames = Collections.newSetFromMap(new ConcurrentHashMap<>(4));
private final Set<String> knownAttributeNames = ConcurrentHashMap.newKeySet(4);


/**
Expand Down

0 comments on commit f9fe8ef

Please sign in to comment.