Class CollectionBeanProvider
- java.lang.Object
-
- net.sf.jguiraffe.di.impl.providers.SimpleBeanProvider
-
- net.sf.jguiraffe.di.impl.providers.CollectionBeanProvider
-
- All Implemented Interfaces:
BeanProvider
- Direct Known Subclasses:
ListBeanProvider
,SetBeanProvider
public abstract class CollectionBeanProvider extends SimpleBeanProvider
An abstract base class for
BeanProvider
implementations that create beans derived from collections.This bean provider base implementation can be used when collection beans are to be created. What makes collections a bit special is the fact that their elements are also dependencies, which can reference other beans. So this bean provider has to ensure that all dependencies are registered correctly, so they can be resolved when the collection is created.
This class implements the major part of the functionality required for creating collection beans. It is initialized with a collection of dependencies representing the elements of the final collection. These can be either constant dependencies or references to other beans. In the
getBean()
implementation a collection of the correct type is created, the element dependencies are resolved, and the resulting objects are added to the collection.A concrete subclass is responsible for creating a collection of a specific type. For this purpose the
createCollection()
method has to be defined. It is invoked bygetBean()
for obtaining a new instance of the collection class supported.Bean providers of this type are intended to be used together with a
that controls the creation of new instances.LifeCycleBeanProvider
Implementation note: Objects of this class are immutable and thus can be shared between multiple threads.
- Version:
- $Id: CollectionBeanProvider.java 205 2012-01-29 18:29:57Z oheger $
- Author:
- Oliver Heger
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
CollectionBeanProvider(Collection<Dependency> deps)
Creates a new instance ofCollectionBeanProvider
and initializes it with a collection with the dependencies of the elements.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract Collection<Object>
createCollection(int size)
Creates the collection.Object
getBean(DependencyProvider dependencyProvider)
Returns the bean managed by this provider.Class<?>
getBeanClass(DependencyProvider dependencyProvider)
Returns the class of the bean created by this provider.Set<Dependency>
getDependencies()
Returns a set with the dependencies of this bean provider.Collection<Dependency>
getElementDependencies()
Returns a collection with the dependencies of the collection elements.-
Methods inherited from class net.sf.jguiraffe.di.impl.providers.SimpleBeanProvider
getLockID, isBeanAvailable, setLockID, shutdown
-
-
-
-
Constructor Detail
-
CollectionBeanProvider
protected CollectionBeanProvider(Collection<Dependency> deps)
Creates a new instance ofCollectionBeanProvider
and initializes it with a collection with the dependencies of the elements.- Parameters:
deps
- a collection with the element dependencies (must not be null)- Throws:
IllegalArgumentException
- if the collection is null
-
-
Method Detail
-
getElementDependencies
public Collection<Dependency> getElementDependencies()
Returns a collection with the dependencies of the collection elements. From these dependencies the collection will be populated. Note that the collection returned by this method is immutable.- Returns:
- a collection with the dependencies of the elements
-
getBean
public Object getBean(DependencyProvider dependencyProvider)
Returns the bean managed by this provider. This implementation creates a new collection by callingcreateCollection()
. Then the dependencies for the elements are resolved and added to the collection.- Parameters:
dependencyProvider
- the dependency provider- Returns:
- the bean managed by this provider
-
getBeanClass
public Class<?> getBeanClass(DependencyProvider dependencyProvider)
Returns the class of the bean created by this provider. This implementation simply returnsCollection.class
. We expect that collection beans are not so specific that they are queried by class.- Parameters:
dependencyProvider
- the dependency provider- Returns:
- the class of the bean created by this provider
-
getDependencies
public Set<Dependency> getDependencies()
Returns a set with the dependencies of this bean provider. This implementation creates a new set from the collection with the dependencies for the elements.- Specified by:
getDependencies
in interfaceBeanProvider
- Overrides:
getDependencies
in classSimpleBeanProvider
- Returns:
- the dependencies of this bean provider
- See Also:
Dependency
-
createCollection
protected abstract Collection<Object> createCollection(int size)
Creates the collection. This method is invoked whenever a new bean instance is to be created. Derived classes must here return a new instance of the collection supported.- Parameters:
size
- the initial size of the new collection; here the number of elements is passed in- Returns:
- the collection
-
-