jfun.yan.containers
Class ImmutablePreferredContainer

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

public class ImmutablePreferredContainer
extends SimpleContainer

An optimized container that's fast when it is used immutably.

The autowire by type is slow because it needs linear time. If the underlying container is not changing, we can optimize it by caching the result of each getComponentOfType(), getComponentsOfType() and containsType(). thus each type is searched at most once.

After getComponentOfType() is called, the same result will always be returned. Newly registered component or unregistered component will invalidate the cache and the subsequent lookup will perform the slow search again.

This is typically not a problem anyway.

Author:
Ben Yu Nov 17, 2005 8:45:01 AM
See Also:
Serialized Form

Constructor Summary
ImmutablePreferredContainer(Registrar reg)
           
 
Method Summary
 boolean containsType(java.lang.Class type)
          Whether a Component of a certain type or its sub-type is contained in the map.
<T> Component<T>
getComponentOfType(java.lang.Class<T> type)
          Gets a Component object of a certain type.
<T> java.util.List<Component<T>>
getComponentsOfType(java.lang.Class<T> type)
          Gets all component objects that are of a certain 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.
 void registerComponent(java.lang.Object key, Component cc)
          Register a Component identified by a key.
 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.
 
Methods inherited from class jfun.yan.containers.SimpleContainer
containsKey, defaultTransform, equals, getComponent, getComponents, getComponentType, getDelegateTarget, getDependency, getDependency, getDependencyOfType, getDependencyOfType, getInstances, getInstances, getInstances, getInstances, getInstancesOfType, getInstancesOfType, hashCode, inherit, instantiateComponent, instantiateComponent, instantiateComponent, instantiateComponent, keys, registerComponent, registerConstructor, registerConstructor, registerConstructor, registerConstructor, registerStaticMethod, registerStaticMethod, registerStaticMethod, registerStaticMethod, registerValue, registerValue, toString, verify, verify, verifyComponent, verifyKey, verifyType
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ImmutablePreferredContainer

public ImmutablePreferredContainer(Registrar reg)
Method Detail

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
Overrides:
getComponentOfType in class SimpleContainer
Parameters:
type - the component instance type.
Returns:
the Component object.

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
Overrides:
containsType in class SimpleContainer
Parameters:
type - the type of the component.
Returns:
true if the type or its sub-type exists in the map.

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
Overrides:
getComponentsOfType in class SimpleContainer
Parameters:
type - the component instance type.
Returns:
the list of the components with this type.

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
Overrides:
getFactory in class SimpleContainer
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)
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
Overrides:
getFactory in class SimpleContainer
Parameters:
key - the component key.
Returns:
the factory instance.

getFactoryOfType

public <T> Factory<T> getFactoryOfType(java.lang.Class<T> type,
                                       ComponentMap cmap)
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
Overrides:
getFactoryOfType in class SimpleContainer
Parameters:
type - the type.
cmap - the container to resolve dependency.
Returns:
the factory instance.

getFactoryOfType

public <T> Factory<T> getFactoryOfType(java.lang.Class<T> type)
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
Overrides:
getFactoryOfType in class SimpleContainer
Parameters:
type - the type.
Returns:
the factory 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
Overrides:
getInstance in class SimpleContainer
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
Overrides:
getInstance in class SimpleContainer
Parameters:
key - the component key.
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
Overrides:
getInstanceOfType in class SimpleContainer
Parameters:
type - the type.
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
Overrides:
getInstanceOfType in class SimpleContainer
Parameters:
type - the type.
Returns:
the component instance.

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
Overrides:
registerComponent in class SimpleContainer
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
Overrides:
unregisterComponent in class SimpleContainer
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
Overrides:
unregisterComponentsOfType in class SimpleContainer
Parameters:
type - the component instance type.