Use JDK 1.6's Collections.newSetFromMap instead of manually accessing Maps with dummy Boolean values

This commit is contained in:
Juergen Hoeller
2013-03-28 22:14:26 +01:00
parent e0c56a124a
commit 853826a774
8 changed files with 73 additions and 65 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,8 +20,9 @@ import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
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;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.beans.BeansException;
@@ -90,9 +91,9 @@ public class RequiredAnnotationBeanPostProcessor extends InstantiationAwareBeanP
/**
* Cache for validated bean names, skipping re-validation for the same bean
* (using a ConcurrentHashMap as a Set)
*/
private final Map<String, Boolean> validatedBeanNames = new ConcurrentHashMap<String, Boolean>(64);
private final Set<String> validatedBeanNames =
Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>(64));
/**
@@ -139,7 +140,7 @@ public class RequiredAnnotationBeanPostProcessor extends InstantiationAwareBeanP
PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)
throws BeansException {
if (!this.validatedBeanNames.containsKey(beanName)) {
if (!this.validatedBeanNames.contains(beanName)) {
if (!shouldSkip(this.beanFactory, beanName)) {
List<String> invalidProperties = new ArrayList<String>();
for (PropertyDescriptor pd : pds) {
@@ -151,7 +152,7 @@ public class RequiredAnnotationBeanPostProcessor extends InstantiationAwareBeanP
throw new BeanInitializationException(buildExceptionMessage(invalidProperties, beanName));
}
}
this.validatedBeanNames.put(beanName, Boolean.TRUE);
this.validatedBeanNames.add(beanName);
}
return pvs;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,9 +16,10 @@
package org.springframework.beans.factory.config;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.beans.BeansException;
@@ -72,9 +73,8 @@ public class PropertyOverrideConfigurer extends PropertyResourceConfigurer {
/**
* Contains names of beans that have overrides
* (using a ConcurrentHashMap as a Set)
*/
private Map<String, Boolean> beanNames = new ConcurrentHashMap<String, Boolean>(16);
private final Set<String> beanNames = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>(16));
/**
@@ -130,7 +130,7 @@ public class PropertyOverrideConfigurer extends PropertyResourceConfigurer {
}
String beanName = key.substring(0, separatorIndex);
String beanProperty = key.substring(separatorIndex+1);
this.beanNames.put(beanName, Boolean.TRUE);
this.beanNames.add(beanName);
applyPropertyValue(factory, beanName, beanProperty, value);
if (logger.isDebugEnabled()) {
logger.debug("Property '" + key + "' set to value [" + value + "]");
@@ -161,7 +161,7 @@ public class PropertyOverrideConfigurer extends PropertyResourceConfigurer {
* the named bean
*/
public boolean hasPropertyOverridesFor(String beanName) {
return this.beanNames.containsKey(beanName);
return this.beanNames.contains(beanName);
}
}

View File

@@ -24,6 +24,7 @@ import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
@@ -161,9 +162,8 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
/**
* Names of beans that have already been created at least once
* (using a ConcurrentHashMap as a Set)
*/
private final Map<String, Boolean> alreadyCreated = new ConcurrentHashMap<String, Boolean>(64);
private final Set<String> alreadyCreated = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>(64));
/** Names of beans that are currently in creation */
private final ThreadLocal<Object> prototypesCurrentlyInCreation =
@@ -1379,7 +1379,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
* @param beanName the name of the bean
*/
protected void markBeanAsCreated(String beanName) {
this.alreadyCreated.put(beanName, Boolean.TRUE);
this.alreadyCreated.add(beanName);
}
/**
@@ -1390,7 +1390,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
* at this point already
*/
protected boolean isBeanEligibleForMetadataCaching(String beanName) {
return this.alreadyCreated.containsKey(beanName);
return this.alreadyCreated.contains(beanName);
}
/**
@@ -1400,7 +1400,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
* @return {@code true} if actually removed, {@code false} otherwise
*/
protected boolean removeSingletonIfCreatedForTypeCheckOnly(String beanName) {
if (!this.alreadyCreated.containsKey(beanName)) {
if (!this.alreadyCreated.contains(beanName)) {
removeSingleton(beanName);
return true;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
package org.springframework.beans.factory.support;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
@@ -92,11 +93,13 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements
/** Set of registered singletons, containing the bean names in registration order */
private final Set<String> registeredSingletons = new LinkedHashSet<String>(64);
/** Names of beans that are currently in creation (using a ConcurrentHashMap as a Set) */
private final Map<String, Boolean> singletonsCurrentlyInCreation = new ConcurrentHashMap<String, Boolean>(16);
/** Names of beans that are currently in creation */
private final Set<String> singletonsCurrentlyInCreation =
Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>(16));
/** Names of beans currently excluded from in creation checks (using a ConcurrentHashMap as a Set) */
private final Map<String, Boolean> inCreationCheckExclusions = new ConcurrentHashMap<String, Boolean>(16);
/** Names of beans currently excluded from in creation checks */
private final Set<String> inCreationCheckExclusions =
Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>(16));
/** List of suppressed Exceptions, available for associating related causes */
private Set<Exception> suppressedExceptions;
@@ -290,7 +293,7 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements
public void setCurrentlyInCreation(String beanName, boolean inCreation) {
Assert.notNull(beanName, "Bean name must not be null");
if (!inCreation) {
this.inCreationCheckExclusions.put(beanName, Boolean.TRUE);
this.inCreationCheckExclusions.add(beanName);
}
else {
this.inCreationCheckExclusions.remove(beanName);
@@ -299,7 +302,7 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements
public boolean isCurrentlyInCreation(String beanName) {
Assert.notNull(beanName, "Bean name must not be null");
return (!this.inCreationCheckExclusions.containsKey(beanName) && isActuallyInCreation(beanName));
return (!this.inCreationCheckExclusions.contains(beanName) && isActuallyInCreation(beanName));
}
protected boolean isActuallyInCreation(String beanName) {
@@ -312,18 +315,18 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements
* @param beanName the name of the bean
*/
public boolean isSingletonCurrentlyInCreation(String beanName) {
return this.singletonsCurrentlyInCreation.containsKey(beanName);
return this.singletonsCurrentlyInCreation.contains(beanName);
}
/**
* Callback before singleton creation.
* <p>Default implementation register the singleton as currently in creation.
* <p>The default implementation register the singleton as currently in creation.
* @param beanName the name of the singleton about to be created
* @see #isSingletonCurrentlyInCreation
*/
protected void beforeSingletonCreation(String beanName) {
if (!this.inCreationCheckExclusions.containsKey(beanName) &&
this.singletonsCurrentlyInCreation.put(beanName, Boolean.TRUE) != null) {
if (!this.inCreationCheckExclusions.contains(beanName) &&
!this.singletonsCurrentlyInCreation.add(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
}
@@ -335,7 +338,7 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements
* @see #isSingletonCurrentlyInCreation
*/
protected void afterSingletonCreation(String beanName) {
if (!this.inCreationCheckExclusions.containsKey(beanName) &&
if (!this.inCreationCheckExclusions.contains(beanName) &&
!this.singletonsCurrentlyInCreation.remove(beanName)) {
throw new IllegalStateException("Singleton '" + beanName + "' isn't currently in creation");
}

View File

@@ -18,7 +18,8 @@ package org.springframework.beans.factory.support;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.beans.MutablePropertyValues;
@@ -48,14 +49,14 @@ import org.springframework.util.Assert;
@SuppressWarnings("serial")
public class RootBeanDefinition extends AbstractBeanDefinition {
// using a ConcurrentHashMap as a Set
private final Map<Member, Boolean> externallyManagedConfigMembers = new ConcurrentHashMap<Member, Boolean>(0);
private final Set<Member> externallyManagedConfigMembers =
Collections.newSetFromMap(new ConcurrentHashMap<Member, Boolean>(0));
// using a ConcurrentHashMap as a Set
private final Map<String, Boolean> externallyManagedInitMethods = new ConcurrentHashMap<String, Boolean>(0);
private final Set<String> externallyManagedInitMethods =
Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>(0));
// using a ConcurrentHashMap as a Set
private final Map<String, Boolean> externallyManagedDestroyMethods = new ConcurrentHashMap<String, Boolean>(0);
private final Set<String> externallyManagedDestroyMethods =
Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>(0));
private BeanDefinitionHolder decoratedDefinition;
@@ -299,27 +300,27 @@ public class RootBeanDefinition extends AbstractBeanDefinition {
public void registerExternallyManagedConfigMember(Member configMember) {
this.externallyManagedConfigMembers.put(configMember, Boolean.TRUE);
this.externallyManagedConfigMembers.add(configMember);
}
public boolean isExternallyManagedConfigMember(Member configMember) {
return this.externallyManagedConfigMembers.containsKey(configMember);
return this.externallyManagedConfigMembers.contains(configMember);
}
public void registerExternallyManagedInitMethod(String initMethod) {
this.externallyManagedInitMethods.put(initMethod, Boolean.TRUE);
this.externallyManagedInitMethods.add(initMethod);
}
public boolean isExternallyManagedInitMethod(String initMethod) {
return this.externallyManagedInitMethods.containsKey(initMethod);
return this.externallyManagedInitMethods.contains(initMethod);
}
public void registerExternallyManagedDestroyMethod(String destroyMethod) {
this.externallyManagedDestroyMethods.put(destroyMethod, Boolean.TRUE);
this.externallyManagedDestroyMethods.add(destroyMethod);
}
public boolean isExternallyManagedDestroyMethod(String destroyMethod) {
return this.externallyManagedDestroyMethods.containsKey(destroyMethod);
return this.externallyManagedDestroyMethods.contains(destroyMethod);
}
public void setDecoratedDefinition(BeanDefinitionHolder decoratedDefinition) {