Class MapBeanProvider

  • All Implemented Interfaces:
    BeanProvider
    Direct Known Subclasses:
    PropertiesBeanProvider

    public class MapBeanProvider
    extends SimpleBeanProvider

    A specialized BeanProvider implementation for creating beans of type java.util.Map.

    With this bean provider implementation either a java.util.HashMap or a java.util.LinkedHashMap can be created and populated. The mechanisms used here are very similar to the ones used by CollectionBeanProvider: an instance is initialized with two lists of Dependency objects - one list represents the key, the other list represents the values of the map. The getDependencies() method returns a union of all these dependencies. When the Map bean is to be created, a new instance of the correct Map class is created. Then the dependencies for the keys and values are resolved, and the corresponding objects are added to the map.

    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: MapBeanProvider.java 205 2012-01-29 18:29:57Z oheger $
    Author:
    Oliver Heger
    • Constructor Detail

      • MapBeanProvider

        public MapBeanProvider​(Collection<Dependency> keyDeps,
                               Collection<Dependency> valDeps,
                               boolean ordered)
        Creates a new instance of MapBeanProvider and initializes it.
        Parameters:
        keyDeps - the dependencies for the keys of the map (must not be null)
        valDeps - the dependencies for the values of the map (must not be null)
        ordered - a flag whether the order of the dependencies should be maintained
        Throws:
        IllegalArgumentException - if either keyDeps or valDeps is null or the sizes of the collections are different
    • Method Detail

      • getKeyDependencies

        public Collection<Dependency> getKeyDependencies()
        Returns the dependencies for the keys of the map.
        Returns:
        the key dependencies
      • getValueDependencies

        public Collection<Dependency> getValueDependencies()
        Returns the dependencies for the values of the map.
        Returns:
        the value dependencies
      • isOrdered

        public boolean isOrdered()
        Returns the ordered flag. This property determines the type of the map created by this provider.
        Returns:
        the ordered flag
      • getBean

        public Object getBean​(DependencyProvider dependencyProvider)
        Returns the bean managed by this provider. This implementation calls createMap() to create the map. Then the dependencies for keys and values are resolved and added to the map.
        Parameters:
        dependencyProvider - the dependency provider
        Returns:
        the managed bean
      • getBeanClass

        public Class<?> getBeanClass​(DependencyProvider dependencyProvider)
        Returns the class of the managed bean. This implementation simply returns the unspecific java.util.Map class.
        Parameters:
        dependencyProvider - the dependency provider
        Returns:
        the class of the bean managed by this provider
      • createMap

        protected Map<Object,​Object> createMap()
        Creates the map managed by this provider. This implementation checks the ordered property and either creates a hash map or a linked hash map.
        Returns:
        the map