Skip to content

Commit

Permalink
Use JDK 1.6's Collections.newSetFromMap instead of manually accessing…
Browse files Browse the repository at this point in the history
… Maps with dummy Boolean values
  • Loading branch information
jhoeller committed Mar 28, 2013
1 parent e0c56a1 commit 853826a
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -20,8 +20,10 @@
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.aopalliance.aop.Advice;
Expand Down Expand Up @@ -136,11 +138,11 @@ public abstract class AbstractAutoProxyCreator extends ProxyConfig

private final Map<Object, Boolean> advisedBeans = new ConcurrentHashMap<Object, Boolean>(64);

// using a ConcurrentHashMap as a Set
private final Map<String, Boolean> targetSourcedBeans = new ConcurrentHashMap<String, Boolean>(16);
private final Set<String> targetSourcedBeans =
Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>(16));

// using a ConcurrentHashMap as a Set
private final Map<Object, Boolean> earlyProxyReferences = new ConcurrentHashMap<Object, Boolean>(16);
private final Set<Object> earlyProxyReferences =
Collections.newSetFromMap(new ConcurrentHashMap<Object, Boolean>(16));

private final Map<Object, Class<?>> proxyTypes = new ConcurrentHashMap<Object, Class<?>>(16);

Expand Down Expand Up @@ -262,14 +264,14 @@ public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, Strin

public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
Object cacheKey = getCacheKey(bean.getClass(), beanName);
this.earlyProxyReferences.put(cacheKey, Boolean.TRUE);
this.earlyProxyReferences.add(cacheKey);
return wrapIfNecessary(bean, beanName, cacheKey);
}

public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
Object cacheKey = getCacheKey(beanClass, beanName);

if (beanName == null || !this.targetSourcedBeans.containsKey(beanName)) {
if (beanName == null || !this.targetSourcedBeans.contains(beanName)) {
if (this.advisedBeans.containsKey(cacheKey)) {
return null;
}
Expand All @@ -285,7 +287,7 @@ public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName
if (beanName != null) {
TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
if (targetSource != null) {
this.targetSourcedBeans.put(beanName, Boolean.TRUE);
this.targetSourcedBeans.add(beanName);
Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource);
this.proxyTypes.put(cacheKey, proxy.getClass());
Expand Down Expand Up @@ -318,7 +320,7 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) {
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean != null) {
Object cacheKey = getCacheKey(bean.getClass(), beanName);
if (!this.earlyProxyReferences.containsKey(cacheKey)) {
if (!this.earlyProxyReferences.contains(cacheKey)) {
return wrapIfNecessary(bean, beanName, cacheKey);
}
}
Expand All @@ -344,7 +346,7 @@ protected Object getCacheKey(Class<?> beanClass, String beanName) {
* @return a proxy wrapping the bean, or the raw bean instance as-is
*/
protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
if (beanName != null && this.targetSourcedBeans.containsKey(beanName)) {
if (beanName != null && this.targetSourcedBeans.contains(beanName)) {
return bean;
}
if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -20,8 +20,9 @@
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;
Expand Down Expand Up @@ -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));


/**
Expand Down Expand Up @@ -139,7 +140,7 @@ public PropertyValues postProcessPropertyValues(
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) {
Expand All @@ -151,7 +152,7 @@ public PropertyValues postProcessPropertyValues(
throw new BeanInitializationException(buildExceptionMessage(invalidProperties, beanName));
}
}
this.validatedBeanNames.put(beanName, Boolean.TRUE);
this.validatedBeanNames.add(beanName);
}
return pvs;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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));


/**
Expand Down Expand Up @@ -130,7 +130,7 @@ protected void processKey(ConfigurableListableBeanFactory factory, String key, S
}
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 + "]");
Expand Down Expand Up @@ -161,7 +161,7 @@ protected void applyPropertyValue(
* the named bean
*/
public boolean hasPropertyOverridesFor(String beanName) {
return this.beanNames.containsKey(beanName);
return this.beanNames.contains(beanName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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;
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -1379,7 +1379,7 @@ protected Class<?> getTypeForFactoryBean(String beanName, RootBeanDefinition mbd
* @param beanName the name of the bean
*/
protected void markBeanAsCreated(String beanName) {
this.alreadyCreated.put(beanName, Boolean.TRUE);
this.alreadyCreated.add(beanName);
}

/**
Expand All @@ -1390,7 +1390,7 @@ protected void markBeanAsCreated(String beanName) {
* at this point already
*/
protected boolean isBeanEligibleForMetadataCaching(String beanName) {
return this.alreadyCreated.containsKey(beanName);
return this.alreadyCreated.contains(beanName);
}

/**
Expand All @@ -1400,7 +1400,7 @@ protected boolean isBeanEligibleForMetadataCaching(String beanName) {
* @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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -290,7 +293,7 @@ public int getSingletonCount() {
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);
Expand All @@ -299,7 +302,7 @@ public void setCurrentlyInCreation(String beanName, boolean inCreation) {

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) {
Expand All @@ -312,18 +315,18 @@ protected boolean isActuallyInCreation(String beanName) {
* @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);
}
}
Expand All @@ -335,7 +338,7 @@ protected void beforeSingletonCreation(String beanName) {
* @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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

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;
Expand Down Expand Up @@ -48,14 +49,14 @@
@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;

Expand Down Expand Up @@ -299,27 +300,27 @@ public Method getResolvedFactoryMethod() {


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) {
Expand Down
Loading

0 comments on commit 853826a

Please sign in to comment.