Class CombinedBeanStore

  • All Implemented Interfaces:
    BeanStore

    public class CombinedBeanStore
    extends Object
    implements BeanStore

    A specialized implementation of the BeanStore interface that combines multiple physical BeanStore objects to a single logic view.

    In some use cases a hierarchy of BeanStore objects is not sufficient to express complex relations between stores. One example are BeanStore objects created by separate builder operations or based on different, disjunct implementations. If now beans in these stores refer to each other, it may be impossible to find a parent-child relationship that allows resolving all possible dependencies.

    This implementation of BeanStore provides a solution for this problem. It can be initialized with an arbitrary number of child BeanStore objects and generates a logic view on top of these stores as if their beans comprised a single BeanStore. This is achieved by corresponding implementations of the methods defined by the BeanStore interface, for instance the providerNames() method returns a union of the names of all providers found in the child stores, or the getBeanProvider() method checks all child stores whether the searched provider can be found. More information about the implementation of the single methods can be found in the Javadocs of the corresponding methods.

    Implementation note: The thread-safety of this class depends on the implementations of the child bean stores. This class stores the child stores in immutable structures; so provided that all child stores are immutable, the resulting object is immutable, too.

    Version:
    $Id: CombinedBeanStore.java 205 2012-01-29 18:29:57Z oheger $
    Author:
    Oliver Heger
    • Constructor Detail

      • CombinedBeanStore

        public CombinedBeanStore​(String storeName,
                                 BeanStore... beanStores)
        Creates a new instance of CombinedBeanStore and initializes it with the given name and the child bean stores. A defensive copy of the passed in array with bean stores is created. The single elements must not be null, otherwise an exception is thrown.
        Parameters:
        storeName - the name of this BeanStore
        beanStores - the child bean stores
        Throws:
        IllegalArgumentException - if the array with the child bean stores is null or contains null references
      • CombinedBeanStore

        public CombinedBeanStore​(BeanStore... beanStores)
        Creates a new instance of CombinedBeanStore and initializes it with the given child bean stores. A default name for this store is generated.
        Parameters:
        beanStores - the child bean stores
        Throws:
        IllegalArgumentException - if the array with the child bean stores is null or contains null references
    • Method Detail

      • getBeanProvider

        public BeanProvider getBeanProvider​(String name)
        Returns the BeanProvider with the given name. This implementation iterates over all child BeanStore objects. The first result different from null is returned.
        Specified by:
        getBeanProvider in interface BeanStore
        Parameters:
        name - the name of the BeanProvider
        Returns:
        the corresponding BeanProvider or null if it cannot be found
      • size

        public int size()
        Returns the number of child BeanStore objects stored in this combined store.
        Returns:
        the number of child stores
      • getChildStore

        public BeanStore getChildStore​(int idx)
        Returns the child BeanStore with the given index. Indices are 0-based and must be in the range 0 <= idx < size().
        Parameters:
        idx - the index
        Returns:
        the child BeanStore at this index
        Throws:
        ArrayIndexOutOfBoundsException - if the index is invalid
      • getName

        public String getName()
        Returns the name of this store. If a name was provided at construction time, this name is returned. Otherwise, a name was automatically generated for this store.
        Specified by:
        getName in interface BeanStore
        Returns:
        the name of this store
      • getParent

        public BeanStore getParent()
        Returns the parent store of this CombinedBeanStore. If none of the child stores has a parent, result is null. If exactly one child store has a parent store, then this store is returned. Otherwise, result is again a CombinedBeanStore with all non-null parent stores as its children.
        Specified by:
        getParent in interface BeanStore
        Returns:
        the parent store of this store
      • providerNames

        public Set<String> providerNames()
        Returns a set with the names of all BeanProvider objects that can be queried from this CombinedBeanStore. This implementation queries all child stores and returns a union of their provider names. Note: the returned set can be modified. It is not connected to this object in any way.
        Specified by:
        providerNames in interface BeanStore
        Returns:
        a set with the names of all BeanProvider objects available
      • getConversionHelper

        public ConversionHelper getConversionHelper()
        Returns the ConversionHelper associated with this instance. This implementation iterates over the child stores. The first ConversionHelper instance that is found is returned. If none of the child stores returns such a helper object, result is null.
        Specified by:
        getConversionHelper in interface BeanStore
        Returns:
        the ConversionHelper associated with this instance
      • toString

        public String toString()
        Returns a string representation of this object. This string contains the name of this store and the string representations of all child stores.
        Overrides:
        toString in class Object
        Returns:
        a string for this object