org.picocontainer.defaults
Class CollectionComponentParameter

java.lang.Object
  extended by org.picocontainer.defaults.CollectionComponentParameter
All Implemented Interfaces:
java.io.Serializable, Parameter

public class CollectionComponentParameter
extends java.lang.Object
implements Parameter, java.io.Serializable

A CollectionComponentParameter should be used to support inject an Array, a Collectionor Mapof components automatically. The collection will contain all components of a special type and additionally the type of the key may be specified. In case of a map, the map's keys are the one of the component adapter.

Since:
1.1
Author:
Aslak Hellesøy, Jörg Schaible
See Also:
Serialized Form

Field Summary
static CollectionComponentParameter ARRAY
          Use ARRAY as Parameterfor an Array that must have elements.
static CollectionComponentParameter ARRAY_ALLOW_EMPTY
          Use ARRAY_ALLOW_EMPTY as Parameterfor an Array that may have no elements.
private  java.lang.Class componentKeyType
           
private  java.lang.Class componentValueType
           
private  boolean emptyCollection
           
private static MapFactory mapFactory
           
 
Constructor Summary
CollectionComponentParameter()
          Expect an Arrayof an appropriate type as parameter.
CollectionComponentParameter(boolean emptyCollection)
          Expect an Arrayof an appropriate type as parameter.
CollectionComponentParameter(java.lang.Class componentValueType, boolean emptyCollection)
          Expect any of the collection types Array,Collectionor Mapas parameter.
CollectionComponentParameter(java.lang.Class componentKeyType, java.lang.Class componentValueType, boolean emptyCollection)
          Expect any of the collection types Array,Collectionor Mapas parameter.
 
Method Summary
 void accept(PicoVisitor visitor)
          Visit the current Parameter.
protected  boolean evaluate(ComponentAdapter adapter)
          Evaluate whether the given component adapter will be part of the collective type.
private  java.lang.Object[] getArrayInstance(PicoContainer container, java.lang.Class expectedType, java.util.Map adapterList)
           
private  java.util.Collection getCollectionInstance(PicoContainer container, java.lang.Class expectedType, java.util.Map adapterList)
           
private  java.lang.Class getCollectionType(java.lang.Class collectionType)
           
private  java.util.Map getMapInstance(PicoContainer container, java.lang.Class expectedType, java.util.Map adapterList)
           
protected  java.util.Map getMatchingComponentAdapters(PicoContainer container, ComponentAdapter adapter, java.lang.Class keyType, java.lang.Class valueType)
          Collect the matching ComponentAdapter instances.
private  java.lang.Class getValueType(java.lang.Class collectionType)
           
 boolean isResolvable(PicoContainer container, ComponentAdapter adapter, java.lang.Class expectedType)
          Check for a successful dependency resolution of the parameter for the expected type.
 java.lang.Object resolveInstance(PicoContainer container, ComponentAdapter adapter, java.lang.Class expectedType)
          Resolve the parameter for the expected type.
 void verify(PicoContainer container, ComponentAdapter adapter, java.lang.Class expectedType)
          Verify a successful dependency resolution of the parameter for the expected type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

mapFactory

private static final MapFactory mapFactory

ARRAY

public static final CollectionComponentParameter ARRAY
Use ARRAY as Parameterfor an Array that must have elements.


ARRAY_ALLOW_EMPTY

public static final CollectionComponentParameter ARRAY_ALLOW_EMPTY
Use ARRAY_ALLOW_EMPTY as Parameterfor an Array that may have no elements.


emptyCollection

private final boolean emptyCollection

componentKeyType

private final java.lang.Class componentKeyType

componentValueType

private final java.lang.Class componentValueType
Constructor Detail

CollectionComponentParameter

public CollectionComponentParameter()
Expect an Arrayof an appropriate type as parameter. At least one component of the array's component type must exist.


CollectionComponentParameter

public CollectionComponentParameter(boolean emptyCollection)
Expect an Arrayof an appropriate type as parameter.

Parameters:
emptyCollection - true if an empty array also is a valid dependency resolution.

CollectionComponentParameter

public CollectionComponentParameter(java.lang.Class componentValueType,
                                    boolean emptyCollection)
Expect any of the collection types Array,Collectionor Mapas parameter.

Parameters:
componentValueType - the type of the components (ignored in case of an Array)
emptyCollection - true if an empty collection resolves the dependency.

CollectionComponentParameter

public CollectionComponentParameter(java.lang.Class componentKeyType,
                                    java.lang.Class componentValueType,
                                    boolean emptyCollection)
Expect any of the collection types Array,Collectionor Mapas parameter.

Parameters:
componentKeyType - the type of the component's key
componentValueType - the type of the components (ignored in case of an Array)
emptyCollection - true if an empty collection resolves the dependency.
Method Detail

resolveInstance

public java.lang.Object resolveInstance(PicoContainer container,
                                        ComponentAdapter adapter,
                                        java.lang.Class expectedType)
Resolve the parameter for the expected type. The method will return null If the expected type is not one of the collection types Array, Collectionor Map. An empty collection is only a valid resolution, if the emptyCollection flag was set.

Specified by:
resolveInstance in interface Parameter
Parameters:
container - the container from which dependencies are resolved.
adapter - the ComponentAdapter that is asking for the instance
expectedType - the type that the returned instance needs to match.
Returns:
the instance of the collection type or null
Throws:
PicoInitializationException - if a referenced component could not be instantiated.

isResolvable

public boolean isResolvable(PicoContainer container,
                            ComponentAdapter adapter,
                            java.lang.Class expectedType)
Check for a successful dependency resolution of the parameter for the expected type. The dependency can only be satisfied if the expected type is one of the collection types Array,Collectionor Map. An empty collection is only a valid resolution, if the emptyCollection flag was set.

Specified by:
isResolvable in interface Parameter
Parameters:
container - the container from which dependencies are resolved.
adapter - the ComponentAdapter that is asking for the instance
expectedType - the required type
Returns:
true if matching components were found or an empty collective type is allowed

verify

public void verify(PicoContainer container,
                   ComponentAdapter adapter,
                   java.lang.Class expectedType)
Verify a successful dependency resolution of the parameter for the expected type. The method will only return if the expected type is one of the collection types Array, Collectionor Map. An empty collection is only a valid resolution, if the emptyCollection flag was set.

Specified by:
verify in interface Parameter
Parameters:
container - the container from which dependencies are resolved.
adapter - the ComponentAdapter that is asking for the verification
expectedType - the required type
Throws:
PicoIntrospectionException - if parameter and its dependencies cannot be resolved

accept

public void accept(PicoVisitor visitor)
Visit the current Parameter.

Specified by:
accept in interface Parameter
Parameters:
visitor - the visitor.
See Also:
Parameter.accept(org.picocontainer.PicoVisitor)

evaluate

protected boolean evaluate(ComponentAdapter adapter)
Evaluate whether the given component adapter will be part of the collective type.

Parameters:
adapter - a ComponentAdapter value
Returns:
true if the adapter takes part

getMatchingComponentAdapters

protected java.util.Map getMatchingComponentAdapters(PicoContainer container,
                                                     ComponentAdapter adapter,
                                                     java.lang.Class keyType,
                                                     java.lang.Class valueType)
Collect the matching ComponentAdapter instances.

Parameters:
container - container to use for dependency resolution
adapter - ComponentAdapter to exclude
keyType - the compatible type of the key
valueType - the compatible type of the component
Returns:
a Map with the ComponentAdapter instances and their component keys as map key.

getCollectionType

private java.lang.Class getCollectionType(java.lang.Class collectionType)

getValueType

private java.lang.Class getValueType(java.lang.Class collectionType)

getArrayInstance

private java.lang.Object[] getArrayInstance(PicoContainer container,
                                            java.lang.Class expectedType,
                                            java.util.Map adapterList)

getCollectionInstance

private java.util.Collection getCollectionInstance(PicoContainer container,
                                                   java.lang.Class expectedType,
                                                   java.util.Map adapterList)

getMapInstance

private java.util.Map getMapInstance(PicoContainer container,
                                     java.lang.Class expectedType,
                                     java.util.Map adapterList)