jfun.yan.containers
Class InheritedRegistrar

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

public class InheritedRegistrar
extends java.lang.Object
implements Registrar

This class manages parent-child relationship between two conainers. In this container, components in the child container can use those from parent container as dependency. But components from parent container cannot see those from child containers.

Codehaus.org.

Author:
Ben Yu
See Also:
Serialized Form

Constructor Summary
InheritedRegistrar(Registrar parent, Registrar child)
          Create an InheritedRegistrar 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)
           
 Registrar getChild()
          Get the child container.
 Component getComponent(java.lang.Object key)
          retrieves a component identified by a key.
 Component getComponentOfType(java.lang.Class type)
          retrieves a component of a provided type.
 java.util.Collection getComponents()
          gets all components from both the parent container and the child container.
 java.util.List getComponentsOfType(java.lang.Class type)
          retrieves all components of a provided type from both the parent container and the child container.
 Dependency getDependency(java.lang.Object key, ComponentMap cmap)
          Gets the Dependency object for a component identified by a key.
 Dependency getDependencyOfType(java.lang.Class type, ComponentMap cmap)
          Gets the Dependency object for a component of a provided type.
 Registrar getParent()
          Get the parent container.
 int hashCode()
           
 java.util.Set keys()
          Get the component keys from both parent and child containers.
 void registerComponent(java.lang.Object key, Component cc)
          registers the component to the child container.
 java.lang.String toString()
           
 void unregisterComponent(java.lang.Object key)
          unregisters a component identified by a key from child container.
 void unregisterComponentsOfType(java.lang.Class type)
          unregisters components of a provided type from child container.
 void verify(ComponentMap cmap)
          Verifies all components in this container.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

InheritedRegistrar

public InheritedRegistrar(Registrar parent,
                          Registrar child)
Create an InheritedRegistrar object. The parent container and the child container are not modified.

Parameters:
parent - the parent container.
child - the child contaiiner.
Method Detail

getChild

public Registrar getChild()
Get the child container.

Returns:
the child container.

getParent

public Registrar getParent()
Get the parent container.

Returns:
the parent container.

registerComponent

public void registerComponent(java.lang.Object key,
                              Component cc)
registers the component to the child container.

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

unregisterComponentsOfType

public void unregisterComponentsOfType(java.lang.Class type)
unregisters components of a provided type from child container.

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

unregisterComponent

public void unregisterComponent(java.lang.Object key)
unregisters a component identified by a key from child container. Nothing is done if the key is not found in the child container.

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

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)
retrieves a component identified by a key. The key is first looked up in the child container, if not found, the parent container is looked up.

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 Component getComponentOfType(java.lang.Class type)
retrieves a component of a provided type. the child container is first looked up, if no such component is found, the parent container is looked up.

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

getComponentsOfType

public java.util.List getComponentsOfType(java.lang.Class type)
retrieves all components of a provided type from both the parent container and the child container.
components from the parent container are placed before those from child container.

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

getComponents

public java.util.Collection getComponents()
gets all components from both the parent container and the child container.

Components from the parent container are placed before those from the child container.

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

getDependencyOfType

public Dependency getDependencyOfType(java.lang.Class type,
                                      ComponentMap cmap)
Gets the Dependency object for a component of a provided type.
If the component type can be found in the child container, the dependency is looked up in the child container. Otherwise, parent container is looked up and the parent container is used to resolve all cascaded dependencies.

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)
Gets the Dependency object for a component identified by a key.
If the component key can be found in the child container, the dependency is looked up in the child container. Otherwise, parent container is looked up and the parent container is used to resolve all cascaded dependencies.

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.

verify

public void verify(ComponentMap cmap)
Verifies all components in this container. Components in the child container use the provided ComponentMap to resolve dependency; while those from the parent container use the parent container itself for resolving dependency.

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

keys

public java.util.Set keys()
Get the component keys from both parent and child containers. Keys from the parent container are placed before those from the child container.

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

equals

public boolean equals(java.lang.Object obj)
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