jfun.yan.containers
Class SimpleRegistrar

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

public class SimpleRegistrar
extends java.lang.Object
implements Registrar, java.io.Serializable

A simple implementation of Registrar. It ensures the Component objects and instances are retrieved and created in the same order as the Component objects are registered.
This implementation enables auto-wiring. It uses SimpleDependency to resolve parameters and properties.
It is also thread-safe.

Codehaus.org.

Author:
Ben Yu
See Also:
Serialized Form

Constructor Summary
SimpleRegistrar()
           
 
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.
 Component getComponentOfType(java.lang.Class type)
          Gets a Component object of a certain type.
 java.util.Collection getComponents()
          Gets all components stored in this map.
 java.util.List getComponentsOfType(java.lang.Class type)
          Gets all component objects that are of a certain type.
 Dependency getDependency(java.lang.Object ckey, ComponentMap cmap)
          This uses SimpleDependency which resolves parameters and arguments by type.
 Dependency getDependencyOfType(java.lang.Class type, ComponentMap cmap)
          This uses SimpleDependency which resolves parameters and arguments by type.
 int hashCode()
           
 java.util.Set keys()
          Get all the component keys.
 void registerComponent(java.lang.Object key, Component cc)
          Register a Component identified by a key.
 int size()
           
 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(ComponentMap cmap)
          Verifies that all the registered components can be satisfied.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SimpleRegistrar

public SimpleRegistrar()
Method Detail

size

public int size()

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.

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.

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.

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.

getComponentOfType

public Component getComponentOfType(java.lang.Class 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 java.util.List getComponentsOfType(java.lang.Class 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.

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.

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.

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.

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.

getDependency

public Dependency getDependency(java.lang.Object ckey,
                                ComponentMap cmap)
This uses SimpleDependency which resolves parameters and arguments by type. Different resolution policy can be provided by overriding this method.

Specified by:
getDependency in interface ComponentMap
Parameters:
ckey - 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,
                                      ComponentMap cmap)
This uses SimpleDependency which resolves parameters and arguments by type. Different resolution policy can be provided by overriding this method.

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.

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