jfun.yan.containers
Class SimpleContainer

java.lang.Object
  extended by jfun.yan.containers.SimpleContainer
All Implemented Interfaces:
java.io.Serializable, ComponentMap, Container, Registrar
Direct Known Subclasses:
DefaultContainer, ImmutablePreferredContainer, MonitoringContainer, TransformingContainer

public class SimpleContainer
extends java.lang.Object
implements Container, java.io.Serializable

A simple implementation of interface Container.

It forwards all method call to the delegated Registrar object, which makes it handy for customizing a Registrar object by subclassing.

Plus, SimpleContainer implements Container interface, which provides convenient API's.

In this implementation, registerStaticMethod and registerConstructor use singleton pattern. Override these methods or the defaultTransform(Component) method to disable the singleton or get a different transformation.

Codehaus.org.

Author:
Ben Yu
See Also:
Serialized Form

Constructor Summary
SimpleContainer(Registrar target)
          Create a new SimpleContainer 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.
protected  Component defaultTransform(Component comp)
          Transforms a component by calling singleton().
 boolean equals(java.lang.Object other)
           
 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  Registrar getDelegateTarget()
          Gets the Registrar 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(Creator<T> cc, ComponentMap cmap)
           
<T> T
instantiateComponent(java.lang.Object key, Creator<T> cc)
          Instantiate component instance.
<T> T
instantiateComponent(java.lang.Object key, Creator<T> cc, ComponentMap cmap)
           
 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)
          The component is transformed by defaultTransform(Component), which makes it singleton.
 void registerConstructor(java.lang.Class c, java.lang.Class[] param_types)
          The component is transformed by defaultTransform(Component), which makes it singleton.
 void registerConstructor(java.lang.Object key, java.lang.Class c)
          The component is transformed by defaultTransform(Component), which makes it singleton.
 void registerConstructor(java.lang.Object key, java.lang.Class c, java.lang.Class[] param_types)
          The component is transformed by defaultTransform(Component), which makes it singleton.
 void registerStaticMethod(java.lang.Class c, java.lang.String name)
          The component is transformed by defaultTransform(Component), which makes it singleton.
 void registerStaticMethod(java.lang.Class c, java.lang.String name, java.lang.Class[] param_types)
          The component is transformed by defaultTransform(Component), which makes it singleton.
 void registerStaticMethod(java.lang.Object key, java.lang.Class c, java.lang.String name)
          The component is transformed by defaultTransform(Component), which makes it singleton.
 void registerStaticMethod(java.lang.Object key, java.lang.Class c, java.lang.String name, java.lang.Class[] param_types)
          The component is transformed by defaultTransform(Component), which makes it singleton.
 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

SimpleContainer

public SimpleContainer(Registrar target)
Create a new SimpleContainer object.

Parameters:
target - the Registrar object.
Method Detail

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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

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.

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.

instantiateComponent

public <T> T instantiateComponent(Creator<T> cc,
                                  ComponentMap cmap)
                       throws AmbiguousComponentResolutionException,
                              ComponentInstantiationException,
                              CyclicDependencyException,
                              UnresolvedComponentException,
                              UnsatisfiedComponentException,
                              YanException
Throws:
AmbiguousComponentResolutionException
ComponentInstantiationException
CyclicDependencyException
UnresolvedComponentException
UnsatisfiedComponentException
YanException

instantiateComponent

public <T> T instantiateComponent(java.lang.Object key,
                                  Creator<T> cc,
                                  ComponentMap cmap)
                       throws AmbiguousComponentResolutionException,
                              ComponentInstantiationException,
                              CyclicDependencyException,
                              UnresolvedComponentException,
                              UnsatisfiedComponentException,
                              YanException
Throws:
AmbiguousComponentResolutionException
ComponentInstantiationException
CyclicDependencyException
UnresolvedComponentException
UnsatisfiedComponentException
YanException

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.

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.

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.

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.

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.

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.

registerConstructor

public void registerConstructor(java.lang.Class c)
The component is transformed by defaultTransform(Component), which makes it singleton. Override defaultTransform(Component) if a different default behavior is desired.

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

defaultTransform

protected Component defaultTransform(Component comp)
Transforms a component by calling singleton(). registerConstructor and registerStaticMethod call this function to transform the component before registering to the container. Override this function if singleton() is not desired or a different transformation is needed.

Parameters:
comp - the component.
Returns:
the transformed component.

registerConstructor

public void registerConstructor(java.lang.Object key,
                                java.lang.Class c)
The component is transformed by defaultTransform(Component), which makes it singleton. Override defaultTransform(Component) if a different default behavior is desired.

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

registerConstructor

public void registerConstructor(java.lang.Class c,
                                java.lang.Class[] param_types)
The component is transformed by defaultTransform(Component), which makes it singleton. Override defaultTransform(Component) if a different default behavior is desired.

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,
                                java.lang.Class[] param_types)
The component is transformed by defaultTransform(Component), which makes it singleton. Override defaultTransform(Component) if a different default behavior is desired.

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)
The component is transformed by defaultTransform(Component), which makes it singleton. Override defaultTransform(Component) if a different default behavior is desired.

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)
The component is transformed by defaultTransform(Component), which makes it singleton. Override defaultTransform(Component) if a different default behavior is desired.

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.Class c,
                                 java.lang.String name,
                                 java.lang.Class[] param_types)
The component is transformed by defaultTransform(Component), which makes it singleton. Override defaultTransform(Component) if a different default behavior is desired.

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,
                                 java.lang.Class[] param_types)
The component is transformed by defaultTransform(Component), which makes it singleton. Override defaultTransform(Component) if a different default behavior is desired.

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.

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.

equals

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

hashCode

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

toString

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

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.

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.

getInstances

public void getInstances(java.util.Map store)
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.

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 void getInstances(java.util.Map store,
                         ComponentMap cmap)
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.

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.

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.

getDelegateTarget

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

Returns:
the Container object.