Class ConstantBeanProvider

  • All Implemented Interfaces:
    BeanProvider, Dependency

    public final class ConstantBeanProvider
    extends SimpleBeanProvider
    implements BeanProvider, Dependency

    A concrete implementation of the BeanProvider interface that will always return the same bean instance.

    When an instance of this class is created, the managed bean instance (and optionally the target class if a type conversion is required) is passed in. On its first call the getBean(DependencyProvider) method checks whether a type conversion if required. If this is the case, the conversion is performed, and the resulting object is stored. Later invocations will then always return this bean instance. The other methods defined by the BeanProvider interface are implemented as dummies: There are no dependencies, and synchronization support is not needed.

    In addition to the BeanProvider interface, the Dependency interface is also implemented. This makes it possible to have static dependencies, which are always resolved to a BeanProvider returning a constant bean. Dependencies of this type may seem strange first, but they make sense in some cases. For instance when a method is to be invoked, its parameters need to be defined. The current parameter values may be dependencies to other beans in the current bean store; so they need to be defined using dependencies. In simple cases however constant values need to be passed (e.g. integers, flags, or string values). For this purpose such a constant dependency can be used.

    Instances of this class can be created using the static getInstance() factory methods. In any case the constant value of the dependency must be passed in. It is also possible to specify the class of this dependency. In this case the class will try a type conversion as described above.

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

      • NULL

        public static final ConstantBeanProvider NULL
        Constant for a dependency that will be resolved to null.
    • Method Detail

      • getBean

        public Object getBean()
        Returns the bean managed by this provider. This is the same bean as was passed to the constructor. No type conversion has been performed.
        Returns:
        the bean passed to the constructor
      • getBean

        public Object getBean​(DependencyProvider dependencyProvider)
        Returns the bean managed by this provider. This implementation operates on the bean that was passed when this object was created. If necessary, type conversion is performed (the result of this conversion is cached, so that the conversion is only done on first access).
        Specified by:
        getBean in interface BeanProvider
        Parameters:
        dependencyProvider - the dependency provider
        Returns:
        the bean managed by this provider
      • getBeanClass

        public Class<?> getBeanClass​(DependencyProvider dependencyProvider)
        Returns the class of the managed bean. If a class was passed on creation time, it is directly returned. Otherwise, the class is obtained from the managed bean. If the bean is null, the type java.lang.Object is returned.
        Specified by:
        getBeanClass in interface BeanProvider
        Parameters:
        dependencyProvider - the dependency provider
        Returns:
        the class of the managed bean
      • resolve

        public BeanProvider resolve​(BeanStore store,
                                    DependencyProvider depProvider)
        Returns the BeanProvider this Dependency refers to. This implementation simply returns the this pointer.
        Specified by:
        resolve in interface Dependency
        Parameters:
        store - the bean store
        depProvider - the dependency provider
        Returns:
        the BeanProvider this Dependency refers to
      • toString

        public String toString()
        Returns a string representation of this object. This string will contain the value of this bean provider.
        Overrides:
        toString in class Object
        Returns:
        a string for this object
      • getInstance

        public static ConstantBeanProvider getInstance​(Object bean)
        Creates a new instance of this class and initializes it with the bean to be managed.
        Parameters:
        bean - the bean to be managed
        Returns:
        the new instance of this class
      • getInstance

        public static ConstantBeanProvider getInstance​(Class<?> valueClass,
                                                       Object value)
        Returns an instance of this class that refers to the specified value of the given class. If necessary, a conversion will be performed to convert the value to the given class.
        Parameters:
        valueClass - the class of this bean provider (can be null)
        value - the value of the managed bean
        Returns:
        the instance representing this value
        Throws:
        IllegalArgumentException - if a conversion is necessary, but cannot be performed