Class DefaultBeanStore
- java.lang.Object
-
- net.sf.jguiraffe.di.impl.DefaultBeanStore
-
- All Implemented Interfaces:
BeanStore,MutableBeanStore
public class DefaultBeanStore extends Object implements MutableBeanStore
A simple yet fully functional default implementation of the
BeanStoreinterface.This implementation is based on a
HashMap.BeanProviderobjects can be added to this map using theaddBeanProvider()method. They can then be queried through thegetBeanProvidermethod.Note: This implementation is not thread-safe. The underlying map is not synchronized. This does not cause any problems when used read-only by the dependency injection framework. But if bean providers should be concurrently added or other properties are to be manipulated, manual synchronization is required. The intended use case is that an instance is created and populated with
BeanProviderobjects in an initialization phase. Then it should only be accessed in a read-only fashion by the dependency injection framework.- Version:
- $Id: DefaultBeanStore.java 205 2012-01-29 18:29:57Z oheger $
- Author:
- Oliver Heger
-
-
Constructor Summary
Constructors Constructor Description DefaultBeanStore()Creates a new instance ofDefaultBeanStore.DefaultBeanStore(String name, BeanStore parent)Creates a new instance ofDefaultBeanStoreand sets the name and the reference to the parent.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description StringaddAnonymousBeanProvider(int index, BeanProvider provider)Adds an anonymousBeanProvider.voidaddBeanProvider(String name, BeanProvider provider)Adds the specifiedBeanProviderto this bean store under the given name.voidclear()Removes allBeanProviders from this bean store.static ConversionHelperfetchConversionHelper(BeanStore store, boolean createIfNecessary)A convenience method for retrieving aConversionHelperobject from a hierarchy of bean stores.BeanProvidergetBeanProvider(String name)Returns theBeanProviderwith the specified name.ConversionHelpergetConversionHelper()Returns theConversionHelperassociated with this object.StringgetName()Returns the name of this bean store.BeanStoregetParent()Returns the parent of this bean store or null if this is a top level store.Set<String>providerNames()Returns a set with the names of all contained bean providers.BeanProviderremoveBeanProvider(String name)Removes theBeanProviderwith the specified name from this bean store.voidsetConversionHelper(ConversionHelper conversionHelper)Sets theConversionHelperobject to be associated with this instance.voidsetName(String n)Sets the name of this bean store.voidsetParent(BeanStore p)Sets the parent for this bean store.StringtoString()Returns a string representation of this object.
-
-
-
Method Detail
-
getConversionHelper
public ConversionHelper getConversionHelper()
Returns theConversionHelperassociated with this object.- Specified by:
getConversionHelperin interfaceBeanStore- Returns:
- the
ConversionHelper
-
setConversionHelper
public void setConversionHelper(ConversionHelper conversionHelper)
Sets theConversionHelperobject to be associated with this instance. The object passed to this method is returned bygetConversionHelper().- Parameters:
conversionHelper- theConversionHelperobject
-
fetchConversionHelper
public static ConversionHelper fetchConversionHelper(BeanStore store, boolean createIfNecessary)
A convenience method for retrieving aConversionHelperobject from a hierarchy of bean stores. This method queries the specifiedBeanStorefor itsConversionHelper. If it has one, the helper object is returned. Otherwise, the parentBeanStoreis queried. This continues until aConversionHelperobject is found or the top of the hierarchy is reached. If noConversionHelpercan be found and thecreateIfNecessaryflag is true, a new default helper object is created. Otherwise, the method returns null if no helper can be found.- Parameters:
store- theBeanStorewhere to start the searchcreateIfNecessary- a flag whether a default helper instance should be created if none can be found- Returns:
- the found
ConversionHelperor null
-
addBeanProvider
public void addBeanProvider(String name, BeanProvider provider)
Adds the specifiedBeanProviderto this bean store under the given name.- Specified by:
addBeanProviderin interfaceMutableBeanStore- Parameters:
name- the name of the bean provider (must not be null)provider- theBeanProviderto be registered- Throws:
IllegalArgumentException- if the name or the provider is null
-
addAnonymousBeanProvider
public String addAnonymousBeanProvider(int index, BeanProvider provider)
Adds an anonymousBeanProvider. This method will generate a special name (which is mainly used internally) for the bean provider to add and store it under this name. The name is returned.- Specified by:
addAnonymousBeanProviderin interfaceMutableBeanStore- Parameters:
index- the index of the bean providerprovider- theBeanProviderto be added (must not be null)- Returns:
- the name used for this
BeanProvider - Throws:
IllegalArgumentException- if the provider is null
-
removeBeanProvider
public BeanProvider removeBeanProvider(String name)
Removes theBeanProviderwith the specified name from this bean store. If this provider cannot be found, this operation has no effect.- Specified by:
removeBeanProviderin interfaceMutableBeanStore- Parameters:
name- the name of the provider to remove- Returns:
- a reference to the removed provider or null if it could not be found
-
clear
public void clear()
Removes allBeanProviders from this bean store.- Specified by:
clearin interfaceMutableBeanStore
-
getBeanProvider
public BeanProvider getBeanProvider(String name)
Returns theBeanProviderwith the specified name. If no such element exists, null is returned.- Specified by:
getBeanProviderin interfaceBeanStore- Parameters:
name- the name of the desired provider- Returns:
- the
BeanProviderwith this name
-
getName
public String getName()
Returns the name of this bean store.
-
setName
public void setName(String n)
Sets the name of this bean store.- Specified by:
setNamein interfaceMutableBeanStore- Parameters:
n- the new name
-
getParent
public BeanStore getParent()
Returns the parent of this bean store or null if this is a top level store.
-
setParent
public void setParent(BeanStore p)
Sets the parent for this bean store.- Specified by:
setParentin interfaceMutableBeanStore- Parameters:
p- the parent
-
providerNames
public Set<String> providerNames()
Returns a set with the names of all contained bean providers. This implementation ensures that the names of anonymous bean providers do not appear in the set returned.- Specified by:
providerNamesin interfaceBeanStore- Returns:
- the names of the registered bean providers
-
-