jfun.yan.containers
Class DelegatingContainer

java.lang.Object
  extended by jfun.yan.containers.DelegatingContainer
All Implemented Interfaces:
java.io.Serializable, ComponentMap, Container, Registrar

public class DelegatingContainer
extends java.lang.Object
implements Container

The base class for delegating to a Container object. It forwards all method call to the delegated Container object, which makes it handy for customizing a Container object by subclassing.

Codehaus.org.

Author:
Ben Yu
See Also:
Serialized Form

Constructor Summary
DelegatingContainer(Container yan)
          Creates a new DelegatingContainer object.
 
Method Summary
 boolean containsKey(java.lang.Object key)
          Whether a Component with a certain key is contained in the map.
 boolean containsType(java.lang.Class type)
          Whether a Component of a certain type or its sub-type is contained in the map.
 boolean equals(java.lang.Object obj)
           
 Component getComponent(java.lang.Object key)
          Gets a Component identified by a key.
<T> Component<T>
getComponentOfType(java.lang.Class<T> type)
          Gets a Component object of a certain type.
 java.util.Collection getComponents()
          Gets all components stored in this map.
<T> java.util.List<Component<T>>
getComponentsOfType(java.lang.Class<T> type)
          Gets all component objects that are of a certain type.
 java.lang.Class getComponentType(java.lang.Object key)
          Get the type of a component identified by a key.
protected  Container getDelegateTarget()
          Gets the Container object being delegated.
 Dependency getDependency(java.lang.Object key)
          Get the dependency for a component key.
 Dependency getDependency(java.lang.Object key, ComponentMap cmap)
          Gets the Dependency object for a component key.
 Dependency getDependencyOfType(java.lang.Class type)
          Get the dependency for a component type.
 Dependency getDependencyOfType(java.lang.Class type, ComponentMap cmap)
          Gets the Dependency object for a component instance type.
 Factory getFactory(java.lang.Object key)
          Create an instance of Factory for a component identified by a key.
 Factory getFactory(java.lang.Object key, ComponentMap cmap)
          Create an instance of Factory for a component identified by a key.
<T> Factory<T>
getFactoryOfType(java.lang.Class<T> type)
          Create an instance of Factory for a component of the provided type.
<T> Factory<T>
getFactoryOfType(java.lang.Class<T> type, ComponentMap cmap)
          Create an instance of Factory for a component of the provided type.
 java.lang.Object getInstance(java.lang.Object key)
          Create instance for a component identified by a key.
 java.lang.Object getInstance(java.lang.Object key, ComponentMap cmap)
          Create instance for a component identified by a key.
<T> T
getInstanceOfType(java.lang.Class<T> type)
          Create component instance of the provided type.
<T> T
getInstanceOfType(java.lang.Class<T> type, ComponentMap cmap)
          Create component instance of the provided type.
 java.util.List getInstances()
          Create instances of all the components stored in this container.
 java.util.List getInstances(ComponentMap cmap)
          Create instances of all the components stored in this container.
 void getInstances(java.util.Map store)
          Create instances of all the components stored in this container.
 void getInstances(java.util.Map store, ComponentMap cmap)
          Create instances of all the components stored in this container.
<T> java.util.List<T>
getInstancesOfType(java.lang.Class<T> type)
          Create component instances of a provided type.
<T> java.util.List<T>
getInstancesOfType(java.lang.Class<T> type, ComponentMap cmap)
          Create component instances of a provided type.
 int hashCode()
           
 Container inherit(Registrar parent)
          Create a new Container object which represents "this inherits parent" relationship.
<T> T
instantiateComponent(Creator<T> cc)
          Instantiate component instance.
<T> T
instantiateComponent(java.lang.Object key, Creator<T> cc)
          Instantiate component instance.
 java.util.Set keys()
          Get all the component keys.
 void registerComponent(Component cc)
          Register a Component object in the container.
 void registerComponent(java.lang.Object key, Component cc)
          Register a Component identified by a key.
 void registerConstructor(java.lang.Class c)
          Register a constructor in the container.
 void registerConstructor(java.lang.Class c, java.lang.Class[] param_types)
          Register a constructor in the container.
 void registerConstructor(java.lang.Object key, java.lang.Class c)
          Register a constructor in the container.
 void registerConstructor(java.lang.Object key, java.lang.Class c, java.lang.Class[] param_types)
          Register a constructor in the container.
 void registerStaticMethod(java.lang.Class c, java.lang.String name)
          Register a component that uses a static method to create component instance.
 void registerStaticMethod(java.lang.Class c, java.lang.String name, java.lang.Class[] param_types)
          Register a component that uses a static method to create component instance.
 void registerStaticMethod(java.lang.Object key, java.lang.Class c, java.lang.String name)
          Register a component that uses a static method to create component instance.
 void registerStaticMethod(java.lang.Object key, java.lang.Class c, java.lang.String name, java.lang.Class[] param_types)
          Register a component that uses a static method to create component instance.
 void registerValue(java.lang.Object v)
          Register a value in the container.
 void registerValue(java.lang.Object key, java.lang.Object v)
          Register a value in the container.
 java.lang.String toString()
           
 void unregisterComponent(java.lang.Object key)
          Unregister a component identified by a key.
 void unregisterComponentsOfType(java.lang.Class type)
          Unregister components of a certain type.
 void verify()
          Verifies the components in this container.
 void verify(ComponentMap cmap)
          Verifies that all the registered components can be satisfied.
 java.lang.Class verifyComponent(Component cc)
          Verifies that a Component is valid using the current container in resolving dependency.
 java.lang.Class verifyKey(java.lang.Object key)
          Verifies a component identified by key.
 java.lang.Class verifyType(java.lang.Class type)
          Verifies a component for a type.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DelegatingContainer

public DelegatingContainer(Container yan)
Creates a new DelegatingContainer object.

Parameters:
yan - the Container object to delegate to.
Method Detail

getFactory

public Factory getFactory(java.lang.Object key,
                          ComponentMap cmap)
                   throws UnresolvedComponentException,
                          YanException
Description copied from interface: Container
Create an instance of Factory for a component identified by a key. when Factory.create() is called, the component is instantiated.

Specified by:
getFactory in interface Container
Parameters:
key - the component key.
cmap - the container to resolve dependency.
Returns:
the factory instance.
Throws:
UnresolvedComponentException - if this component cannot be resolved.
YanException - for any other exception.

getFactory

public Factory getFactory(java.lang.Object key)
                   throws UnresolvedComponentException,
                          YanException
Description copied from interface: Container
Create an instance of Factory for a component identified by a key. when Factory.create() is called, the component is instantiated.

Specified by:
getFactory in interface Container
Parameters:
key - the component key.
Returns:
the factory instance.
Throws:
UnresolvedComponentException - if this component cannot be resolved.
YanException - for any other exception.

getFactoryOfType

public <T> Factory<T> getFactoryOfType(java.lang.Class<T> type,
                                       ComponentMap cmap)
                            throws AmbiguousComponentResolutionException,
                                   UnresolvedComponentException,
                                   YanException
Description copied from interface: Container
Create an instance of Factory for a component of the provided type. when Factory.create() is called, the component is instantiated.

Specified by:
getFactoryOfType in interface Container
Parameters:
type - the type.
cmap - the container to resolve dependency.
Returns:
the factory instance.
Throws:
AmbiguousComponentResolutionException - if ambiguity happens.
UnresolvedComponentException - if this component cannot be resolved.
YanException - for any other exception.

getFactoryOfType

public <T> Factory<T> getFactoryOfType(java.lang.Class<T> type)
                            throws AmbiguousComponentResolutionException,
                                   UnresolvedComponentException,
                                   YanException
Description copied from interface: Container
Create an instance of Factory for a component of the provided type. when Factory.create() is called, the component is instantiated.

Specified by:
getFactoryOfType in interface Container
Parameters:
type - the type.
Returns:
the factory instance.
Throws:
AmbiguousComponentResolutionException - if ambiguity happens.
UnresolvedComponentException - if this component or any dependent component cannot be resolved.
YanException - for any other exception.

getInstances

public void getInstances(java.util.Map store,
                         ComponentMap cmap)
                  throws ComponentInstantiationException,
                         CyclicDependencyException,
                         UnresolvedComponentException,
                         UnsatisfiedComponentException,
                         YanException
Description copied from interface: Container
Create instances of all the components stored in this container. The component instances are stored into a java.util.Map object under the component key.

The dependencies are resolved in the current container.

Specified by:
getInstances in interface Container
Parameters:
store - the java.util.Map object to save the component instances.
cmap - the container in which to resolve dependency.
Throws:
ComponentInstantiationException - wrapper exception for any checked exception thrown out of the actual creation.
CyclicDependencyException - if cyclic dependency is detected.
UnresolvedComponentException - if this component or any dependent component cannot be resolved.
UnsatisfiedComponentException - if some requirement of the component cannot be satisfied.
YanException - for any other exception.

getInstances

public void getInstances(java.util.Map store)
                  throws ComponentInstantiationException,
                         CyclicDependencyException,
                         UnresolvedComponentException,
                         UnsatisfiedComponentException,
                         YanException
Description copied from interface: Container
Create instances of all the components stored in this container. The component instances are stored into a java.util.Map object under the component key.
The dependencies are resolved in the current container.

Specified by:
getInstances in interface Container
Parameters:
store - the java.util.Map object to save the component instances.
Throws:
ComponentInstantiationException - wrapper exception for any checked exception thrown out of the actual creation.
CyclicDependencyException - if cyclic dependency is detected.
UnresolvedComponentException - if this component or any dependent component cannot be resolved.
UnsatisfiedComponentException - if some requirement of the component cannot be satisfied.
YanException - for any other exception.

instantiateComponent

public <T> T instantiateComponent(java.lang.Object key,
                                  Creator<T> cc)
                       throws AmbiguousComponentResolutionException,
                              ComponentInstantiationException,
                              CyclicDependencyException,
                              UnresolvedComponentException,
                              UnsatisfiedComponentException,
                              YanException
Description copied from interface: Container
Instantiate component instance. The dependencies are resolved in the current container.

Specified by:
instantiateComponent in interface Container
Parameters:
key - the key of the component.
cc - the component to instantiate.
Returns:
the component instance.
Throws:
AmbiguousComponentResolutionException - if ambiguity happens.
ComponentInstantiationException - wrapper exception for any checked exception thrown out of the actual creation.
CyclicDependencyException - if cyclic dependency is detected.
UnresolvedComponentException - if this component or any dependent component cannot be resolved.
UnsatisfiedComponentException - if some requirement of the component cannot be satisfied.
YanException - for any other exception.

instantiateComponent

public <T> T instantiateComponent(Creator<T> cc)
                       throws AmbiguousComponentResolutionException,
                              ComponentInstantiationException,
                              CyclicDependencyException,
                              UnresolvedComponentException,
                              UnsatisfiedComponentException,
                              YanException
Description copied from interface: Container
Instantiate component instance. The dependencies are resolved in the current container.

Specified by:
instantiateComponent in interface Container
Parameters:
cc - the component to instantiate.
Returns:
the component instance.
Throws:
AmbiguousComponentResolutionException - if ambiguity happens.
ComponentInstantiationException - wrapper exception for any checked exception thrown out of the actual creation.
CyclicDependencyException - if cyclic dependency is detected.
UnresolvedComponentException - if this component or any dependent component cannot be resolved.
UnsatisfiedComponentException - if some requirement of the component cannot be satisfied.
YanException - for any other exception.

getDelegateTarget

protected final Container getDelegateTarget()
Gets the Container object being delegated.

Returns:
the Container object.

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object

containsKey

public boolean containsKey(java.lang.Object key)
Description copied from interface: ComponentMap
Whether a Component with a certain key is contained in the map.

Specified by:
containsKey in interface ComponentMap
Parameters:
key - the component key.
Returns:
true if the key exists in the map.

containsType

public boolean containsType(java.lang.Class type)
Description copied from interface: ComponentMap
Whether a Component of a certain type or its sub-type is contained in the map.

Specified by:
containsType in interface ComponentMap
Parameters:
type - the type of the component.
Returns:
true if the type or its sub-type exists in the map.

getComponent

public Component getComponent(java.lang.Object key)
Description copied from interface: ComponentMap
Gets a Component identified by a key.

Specified by:
getComponent in interface ComponentMap
Parameters:
key - the component key.
Returns:
the Component object or null if a component with the given key is not found.

getComponentOfType

public <T> Component<T> getComponentOfType(java.lang.Class<T> type)
Description copied from interface: ComponentMap
Gets a Component object of a certain type. Subtype relationship is honored. So that a Component with type String is considered with type CharSequence as well. If more than one Component is found with the given type, AmbiguousComponentResolutionException is thrown. null is returned if no such component is found.

Specified by:
getComponentOfType in interface ComponentMap
Parameters:
type - the component instance type.
Returns:
the Component object.

getComponents

public java.util.Collection getComponents()
Description copied from interface: ComponentMap
Gets all components stored in this map.

Specified by:
getComponents in interface ComponentMap
Returns:
the components. The returned Collection contains 0 or more Component objects.

getComponentsOfType

public <T> java.util.List<Component<T>> getComponentsOfType(java.lang.Class<T> type)
Description copied from interface: ComponentMap
Gets all component objects that are of a certain type.

Specified by:
getComponentsOfType in interface ComponentMap
Parameters:
type - the component instance type.
Returns:
the list of the components with this type.

getComponentType

public java.lang.Class getComponentType(java.lang.Object key)
Description copied from interface: Container
Get the type of a component identified by a key.

Specified by:
getComponentType in interface Container
Parameters:
key - the component key.
Returns:
the type of the component or null if not found.

getInstance

public java.lang.Object getInstance(java.lang.Object key)
Description copied from interface: Container
Create instance for a component identified by a key. Dependencies are resolved in the current container.

Specified by:
getInstance in interface Container
Parameters:
key - the component key.
Returns:
the component instance.

getInstance

public java.lang.Object getInstance(java.lang.Object key,
                                    ComponentMap cmap)
Description copied from interface: Container
Create instance for a component identified by a key.

Specified by:
getInstance in interface Container
Parameters:
key - the component key.
cmap - the container to resolve dependency.
Returns:
the component instance.

getInstanceOfType

public <T> T getInstanceOfType(java.lang.Class<T> type)
Description copied from interface: Container
Create component instance of the provided type. The dependencies are resolved in the current container.

Specified by:
getInstanceOfType in interface Container
Parameters:
type - the type.
Returns:
the component instance.

getInstanceOfType

public <T> T getInstanceOfType(java.lang.Class<T> type,
                               ComponentMap cmap)
Description copied from interface: Container
Create component instance of the provided type.

Specified by:
getInstanceOfType in interface Container
Parameters:
type - the type.
cmap - the container to resolve dependency.
Returns:
the component instance.

getInstances

public java.util.List getInstances()
Description copied from interface: Container
Create instances of all the components stored in this container.
The dependencies are resolved in the current container.

Specified by:
getInstances in interface Container
Returns:
the list containing all the component instances.

getInstances

public java.util.List getInstances(ComponentMap cmap)
Description copied from interface: Container
Create instances of all the components stored in this container.

The dependencies are resolved in the current container.

Specified by:
getInstances in interface Container
Parameters:
cmap - the container in which to resolve dependency.
Returns:
the list containing all the component instances.

getInstancesOfType

public <T> java.util.List<T> getInstancesOfType(java.lang.Class<T> type)
Description copied from interface: Container
Create component instances of a provided type. If more than one components are found for the same type, both are invoked and the results are collected in the list.
The dependencies are resolved in the current container.

Specified by:
getInstancesOfType in interface Container
Parameters:
type - the type.
Returns:
the list containing all the component instances.

getInstancesOfType

public <T> java.util.List<T> getInstancesOfType(java.lang.Class<T> type,
                                                ComponentMap cmap)
Description copied from interface: Container
Create component instances of a provided type. If more than one components are found for the same type, both are invoked and the results are collected in the list.

Specified by:
getInstancesOfType in interface Container
Parameters:
type - the type.
cmap - the container to resolve dependency.
Returns:
the list containing all the component instances.

getDependency

public Dependency getDependency(java.lang.Object key)
Description copied from interface: Container
Get the dependency for a component key.

Specified by:
getDependency in interface Container
Parameters:
key - the component key.
Returns:
the Dependency object.

getDependency

public Dependency getDependency(java.lang.Object key,
                                ComponentMap cmap)
Description copied from interface: ComponentMap
Gets the Dependency object for a component key.

Specified by:
getDependency in interface ComponentMap
Parameters:
key - The component key.
cmap - The ComponentMap object from which the parts of the component should be obtained. This parameter is typically passed as 'this' so that the parts are created in the same container. When implementing container interaction such as inheritance, this parameter may be passed as a different ComponentMap object.
Returns:
the Dependency object.

getDependencyOfType

public Dependency getDependencyOfType(java.lang.Class type)
Description copied from interface: Container
Get the dependency for a component type.

Specified by:
getDependencyOfType in interface Container
Parameters:
type - the component type.
Returns:
the Dependency object.

getDependencyOfType

public Dependency getDependencyOfType(java.lang.Class type,
                                      ComponentMap cmap)
Description copied from interface: ComponentMap
Gets the Dependency object for a component instance type.

Specified by:
getDependencyOfType in interface ComponentMap
Parameters:
type - The component instance type.
cmap - The ComponentMap object from which the parts of the component should be obtained. This parameter is typically passed as 'this' so that the parts are created in the same container. When implementing container interaction such as inheritance, this parameter may be passed as a different ComponentMap object.
Returns:
the Dependency object.

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

inherit

public Container inherit(Registrar parent)
Description copied from interface: Container
Create a new Container object which represents "this inherits parent" relationship. In this new Container, components in the child container are invisible to the ones in the parent container; while components in the parent container are visible to the ones in the child container.

Specified by:
inherit in interface Container
Parameters:
parent - the parent registrar.
Returns:
the new Container.

keys

public java.util.Set keys()
Description copied from interface: ComponentMap
Get all the component keys.

Specified by:
keys in interface ComponentMap
Returns:
the component keys.

registerComponent

public void registerComponent(Component cc)
Description copied from interface: Container
Register a Component object in the container. Equivalent as registerComponent(cc.getType(), cc). cc.getType() is not allowed to return null.

Specified by:
registerComponent in interface Container
Parameters:
cc - the component.

registerComponent

public void registerComponent(java.lang.Object key,
                              Component cc)
Description copied from interface: Registrar
Register a Component identified by a key.

Specified by:
registerComponent in interface Registrar
Parameters:
key - the component key.
cc - the component.

registerConstructor

public void registerConstructor(java.lang.Class c)
Description copied from interface: Container
Register a constructor in the container. Equivalent as registerConstructor(c, c).

Specified by:
registerConstructor in interface Container
Parameters:
c - the class containing the constructor.

registerConstructor

public void registerConstructor(java.lang.Class c,
                                java.lang.Class[] param_types)
Description copied from interface: Container
Register a constructor in the container. Equivalent as registerConstructor(c, c, param_types).

Specified by:
registerConstructor in interface Container
Parameters:
c - the class containing the constructor.
param_types - the parameter types of the constructor.

registerConstructor

public void registerConstructor(java.lang.Object key,
                                java.lang.Class c)
Description copied from interface: Container
Register a constructor in the container. Equivalent as registerComponent(key, Components.ctor(c)).

Specified by:
registerConstructor in interface Container
Parameters:
key - the component key.
c - the class containing the constructor.

registerConstructor

public void registerConstructor(java.lang.Object key,
                                java.lang.Class c,
                                java.lang.Class[] param_types)
Description copied from interface: Container
Register a constructor in the container. Equivalent as registerComponent(key, Components.ctor(c, param_types)).

Specified by:
registerConstructor in interface Container
Parameters:
key - the component key.
c - the class containing the constructor.
param_types - the parameter types of the constructor.

registerStaticMethod

public void registerStaticMethod(java.lang.Class c,
                                 java.lang.String name)
Description copied from interface: Container
Register a component that uses a static method to create component instance. Equivalent as registerComponent(Components.static_method(c, name)).

Specified by:
registerStaticMethod in interface Container
Parameters:
c - the class containing the static method.
name - the method name.

registerStaticMethod

public void registerStaticMethod(java.lang.Class c,
                                 java.lang.String name,
                                 java.lang.Class[] param_types)
Description copied from interface: Container
Register a component that uses a static method to create component instance. Equivalent as registerComponent(Components.static_method(c, name, param_types));

Specified by:
registerStaticMethod in interface Container
Parameters:
c - the class containing the static method.
name - the method name.

registerStaticMethod

public void registerStaticMethod(java.lang.Object key,
                                 java.lang.Class c,
                                 java.lang.String name)
Description copied from interface: Container
Register a component that uses a static method to create component instance. Equivalent as registerComponent(key, Components.static_method(c, name)).

Specified by:
registerStaticMethod in interface Container
Parameters:
key - the component key.
c - the class containing the static method.
name - the method name.

registerStaticMethod

public void registerStaticMethod(java.lang.Object key,
                                 java.lang.Class c,
                                 java.lang.String name,
                                 java.lang.Class[] param_types)
Description copied from interface: Container
Register a component that uses a static method to create component instance. Equivalent as registerComponent(key, Components.static_method(c, name, param_types)).

Specified by:
registerStaticMethod in interface Container
Parameters:
key - the component key.
c - the class containing the static method.
name - the method name.
param_types - the parameter types.

registerValue

public void registerValue(java.lang.Object v)
Description copied from interface: Container
Register a value in the container. Equivalent as registerValue(v.getClass(), v).

Specified by:
registerValue in interface Container
Parameters:
v - the value.

registerValue

public void registerValue(java.lang.Object key,
                          java.lang.Object v)
Description copied from interface: Container
Register a value in the container.

Specified by:
registerValue in interface Container
Parameters:
key - the key of the value.
v - the value.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

unregisterComponent

public void unregisterComponent(java.lang.Object key)
Description copied from interface: Registrar
Unregister a component identified by a key.

Specified by:
unregisterComponent in interface Registrar
Parameters:
key - the component key.

unregisterComponentsOfType

public void unregisterComponentsOfType(java.lang.Class type)
Description copied from interface: Registrar
Unregister components of a certain type.

Specified by:
unregisterComponentsOfType in interface Registrar
Parameters:
type - the component instance type.

verify

public void verify()
Description copied from interface: Container
Verifies the components in this container. Dependencies are resolved in the current container.

Specified by:
verify in interface Container

verify

public void verify(ComponentMap cmap)
Description copied from interface: Registrar
Verifies that all the registered components can be satisfied.

Specified by:
verify in interface Registrar
Parameters:
cmap - the ComponentMap from which the parts of the registered components are obtained.

verifyComponent

public java.lang.Class verifyComponent(Component cc)
Description copied from interface: Container
Verifies that a Component is valid using the current container in resolving dependency.

Specified by:
verifyComponent in interface Container
Parameters:
cc - the component to verify.
Returns:
the type of the component.

verifyKey

public java.lang.Class verifyKey(java.lang.Object key)
Description copied from interface: Container
Verifies a component identified by key.

Specified by:
verifyKey in interface Container
Parameters:
key - the component key.
Returns:
the component type.

verifyType

public java.lang.Class verifyType(java.lang.Class type)
Description copied from interface: Container
Verifies a component for a type.

Specified by:
verifyType in interface Container
Parameters:
type - the component type.
Returns:
the component type.