Class 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 by getBean() for obtaining a new instance of the collection class supported.

    Bean providers of this type are intended to be used together with a LifeCycleBeanProvider that controls the creation of new instances.

    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 Detail

      • CollectionBeanProvider

        protected CollectionBeanProvider​(Collection<Dependency> deps)
        Creates a new instance of CollectionBeanProvider 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 calling createCollection(). 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 returns Collection.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
      • 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