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
BeanStore
interface.This implementation is based on a
HashMap
.BeanProvider
objects can be added to this map using theaddBeanProvider()
method. They can then be queried through thegetBeanProvider
method.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
BeanProvider
objects 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 ofDefaultBeanStore
and sets the name and the reference to the parent.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description String
addAnonymousBeanProvider(int index, BeanProvider provider)
Adds an anonymousBeanProvider
.void
addBeanProvider(String name, BeanProvider provider)
Adds the specifiedBeanProvider
to this bean store under the given name.void
clear()
Removes allBeanProvider
s from this bean store.static ConversionHelper
fetchConversionHelper(BeanStore store, boolean createIfNecessary)
A convenience method for retrieving aConversionHelper
object from a hierarchy of bean stores.BeanProvider
getBeanProvider(String name)
Returns theBeanProvider
with the specified name.ConversionHelper
getConversionHelper()
Returns theConversionHelper
associated with this object.String
getName()
Returns the name of this bean store.BeanStore
getParent()
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.BeanProvider
removeBeanProvider(String name)
Removes theBeanProvider
with the specified name from this bean store.void
setConversionHelper(ConversionHelper conversionHelper)
Sets theConversionHelper
object to be associated with this instance.void
setName(String n)
Sets the name of this bean store.void
setParent(BeanStore p)
Sets the parent for this bean store.String
toString()
Returns a string representation of this object.
-
-
-
Method Detail
-
getConversionHelper
public ConversionHelper getConversionHelper()
Returns theConversionHelper
associated with this object.- Specified by:
getConversionHelper
in interfaceBeanStore
- Returns:
- the
ConversionHelper
-
setConversionHelper
public void setConversionHelper(ConversionHelper conversionHelper)
Sets theConversionHelper
object to be associated with this instance. The object passed to this method is returned bygetConversionHelper()
.- Parameters:
conversionHelper
- theConversionHelper
object
-
fetchConversionHelper
public static ConversionHelper fetchConversionHelper(BeanStore store, boolean createIfNecessary)
A convenience method for retrieving aConversionHelper
object from a hierarchy of bean stores. This method queries the specifiedBeanStore
for itsConversionHelper
. If it has one, the helper object is returned. Otherwise, the parentBeanStore
is queried. This continues until aConversionHelper
object is found or the top of the hierarchy is reached. If noConversionHelper
can be found and thecreateIfNecessary
flag is true, a new default helper object is created. Otherwise, the method returns null if no helper can be found.- Parameters:
store
- theBeanStore
where to start the searchcreateIfNecessary
- a flag whether a default helper instance should be created if none can be found- Returns:
- the found
ConversionHelper
or null
-
addBeanProvider
public void addBeanProvider(String name, BeanProvider provider)
Adds the specifiedBeanProvider
to this bean store under the given name.- Specified by:
addBeanProvider
in interfaceMutableBeanStore
- Parameters:
name
- the name of the bean provider (must not be null)provider
- theBeanProvider
to 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:
addAnonymousBeanProvider
in interfaceMutableBeanStore
- Parameters:
index
- the index of the bean providerprovider
- theBeanProvider
to 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 theBeanProvider
with the specified name from this bean store. If this provider cannot be found, this operation has no effect.- Specified by:
removeBeanProvider
in 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 allBeanProvider
s from this bean store.- Specified by:
clear
in interfaceMutableBeanStore
-
getBeanProvider
public BeanProvider getBeanProvider(String name)
Returns theBeanProvider
with the specified name. If no such element exists, null is returned.- Specified by:
getBeanProvider
in interfaceBeanStore
- Parameters:
name
- the name of the desired provider- Returns:
- the
BeanProvider
with 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:
setName
in 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:
setParent
in 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:
providerNames
in interfaceBeanStore
- Returns:
- the names of the registered bean providers
-
-