Class 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 the addBeanProvider() method. They can then be queried through the getBeanProvider 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 Detail

      • DefaultBeanStore

        public DefaultBeanStore()
        Creates a new instance of DefaultBeanStore.
      • DefaultBeanStore

        public DefaultBeanStore​(String name,
                                BeanStore parent)
        Creates a new instance of DefaultBeanStore and sets the name and the reference to the parent.
        Parameters:
        name - the name of this bean store
        parent - the reference to the parent bean store
    • Method Detail

      • setConversionHelper

        public void setConversionHelper​(ConversionHelper conversionHelper)
        Sets the ConversionHelper object to be associated with this instance. The object passed to this method is returned by getConversionHelper().
        Parameters:
        conversionHelper - the ConversionHelper object
      • fetchConversionHelper

        public static ConversionHelper fetchConversionHelper​(BeanStore store,
                                                             boolean createIfNecessary)
        A convenience method for retrieving a ConversionHelper object from a hierarchy of bean stores. This method queries the specified BeanStore for its ConversionHelper. If it has one, the helper object is returned. Otherwise, the parent BeanStore is queried. This continues until a ConversionHelper object is found or the top of the hierarchy is reached. If no ConversionHelper can be found and the createIfNecessary flag is true, a new default helper object is created. Otherwise, the method returns null if no helper can be found.
        Parameters:
        store - the BeanStore where to start the search
        createIfNecessary - 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 specified BeanProvider to this bean store under the given name.
        Specified by:
        addBeanProvider in interface MutableBeanStore
        Parameters:
        name - the name of the bean provider (must not be null)
        provider - the BeanProvider to be registered
        Throws:
        IllegalArgumentException - if the name or the provider is null
      • addAnonymousBeanProvider

        public String addAnonymousBeanProvider​(int index,
                                               BeanProvider provider)
        Adds an anonymous BeanProvider. 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 interface MutableBeanStore
        Parameters:
        index - the index of the bean provider
        provider - the BeanProvider 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 the BeanProvider with the specified name from this bean store. If this provider cannot be found, this operation has no effect.
        Specified by:
        removeBeanProvider in interface MutableBeanStore
        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 all BeanProviders from this bean store.
        Specified by:
        clear in interface MutableBeanStore
      • getBeanProvider

        public BeanProvider getBeanProvider​(String name)
        Returns the BeanProvider with the specified name. If no such element exists, null is returned.
        Specified by:
        getBeanProvider in interface BeanStore
        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.
        Specified by:
        getName in interface BeanStore
        Returns:
        the name of this bean store
      • setName

        public void setName​(String n)
        Sets the name of this bean store.
        Specified by:
        setName in interface MutableBeanStore
        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.
        Specified by:
        getParent in interface BeanStore
        Returns:
        the parent of this bean store
      • setParent

        public void setParent​(BeanStore p)
        Sets the parent for this bean store.
        Specified by:
        setParent in interface MutableBeanStore
        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 interface BeanStore
        Returns:
        the names of the registered bean providers
      • toString

        public String toString()
        Returns a string representation of this object. This implementation returns a string listing all bean providers that belong to this store.
        Overrides:
        toString in class Object
        Returns:
        a string for this object