|
PicoContainer example source code file (ComponentParameter.java)
The PicoContainer ComponentParameter.java source code/***************************************************************************** * Copyright (C) PicoContainer Organization. All rights reserved. * * ------------------------------------------------------------------------- * * The software in this package is published under the terms of the BSD * * style license a copy of which has been included with this distribution in * * the LICENSE.txt file. * * * * Original code by * *****************************************************************************/ package org.picocontainer.parameters; import org.picocontainer.ComponentAdapter; import org.picocontainer.Parameter; import org.picocontainer.PicoContainer; import org.picocontainer.PicoVisitor; import org.picocontainer.ParameterName; import org.picocontainer.injectors.AbstractInjector; /** * A ComponentParameter should be used to pass in a particular component as argument to a * different component's constructor. This is particularly useful in cases where several * components of the same type have been registered, but with a different key. Passing a * ComponentParameter as a parameter when registering a component will give PicoContainer a hint * about what other component to use in the constructor. Collecting parameter types are * supported for {@link java.lang.reflect.Array},{@link java.util.Collection}and * {@link java.util.Map}. * * @author Jon Tirsén * @author Aslak Hellesøy * @author Jörg Schaible * @author Thomas Heller * @version $Revision: 3602 $ */ public class ComponentParameter extends BasicComponentParameter { /** * <code>DEFAULT is an instance of ComponentParameter using the default constructor. */ public static final ComponentParameter DEFAULT = new ComponentParameter(); /** * Use <code>ARRAY as {@link Parameter}for an Array that must have elements. */ public static final ComponentParameter ARRAY = new ComponentParameter(false); /** * Use <code>ARRAY_ALLOW_EMPTY as {@link Parameter}for an Array that may have no * elements. */ public static final ComponentParameter ARRAY_ALLOW_EMPTY = new ComponentParameter(true); private final Parameter collectionParameter; /** * Expect a parameter matching a component of a specific key. * * @param componentKey the key of the desired addComponent */ public ComponentParameter(Object componentKey) { this(componentKey, null); } /** * Expect any scalar paramter of the appropriate type or an {@link java.lang.reflect.Array}. */ public ComponentParameter() { this(false); } /** * Expect any scalar paramter of the appropriate type or an {@link java.lang.reflect.Array}. * Resolve the parameter even if no compoennt is of the array's component type. * * @param emptyCollection <code>true allows an Array to be empty */ public ComponentParameter(boolean emptyCollection) { this(null, emptyCollection ? CollectionComponentParameter.ARRAY_ALLOW_EMPTY : CollectionComponentParameter.ARRAY); } /** * Expect any scalar paramter of the appropriate type or the collecting type * {@link java.lang.reflect.Array},{@link java.util.Collection}or {@link java.util.Map}. * The components in the collection will be of the specified type. * * @param componentValueType the component's type (ignored for an Array) * @param emptyCollection <code>true allows the collection to be empty */ public ComponentParameter(Class componentValueType, boolean emptyCollection) { this(null, new CollectionComponentParameter(componentValueType, emptyCollection)); } /** * Expect any scalar paramter of the appropriate type or the collecting type * {@link java.lang.reflect.Array},{@link java.util.Collection}or {@link java.util.Map}. * The components in the collection will be of the specified type and their adapter's key * must have a particular type. * * @param componentKeyType the component adapter's key type * @param componentValueType the component's type (ignored for an Array) * @param emptyCollection <code>true allows the collection to be empty */ public ComponentParameter(Class componentKeyType, Class componentValueType, boolean emptyCollection) { this(null, new CollectionComponentParameter(componentKeyType, componentValueType, emptyCollection)); } private ComponentParameter(Object componentKey, Parameter collectionParameter) { super(componentKey); this.collectionParameter = collectionParameter; } public Object resolveInstance(PicoContainer container, ComponentAdapter adapter, Class expectedType, ParameterName expectedParameterName) { // type check is done in isResolvable Object result = super.resolveInstance(container, adapter, expectedType, expectedParameterName); if (result == null && collectionParameter != null) { result = collectionParameter.resolveInstance(container, adapter, expectedType, expectedParameterName); } return result; } public boolean isResolvable(PicoContainer container, ComponentAdapter adapter, Class expectedType, ParameterName expectedParameterName) { if (!super.isResolvable(container, adapter, expectedType, expectedParameterName)) { if (collectionParameter != null) { return collectionParameter.isResolvable(container, adapter, expectedType, expectedParameterName); } return false; } return true; } public void verify(PicoContainer container, ComponentAdapter adapter, Class expectedType, ParameterName expectedParameterName) { try { super.verify(container, adapter, expectedType, expectedParameterName); } catch (AbstractInjector.UnsatisfiableDependenciesException e) { if (collectionParameter != null) { collectionParameter.verify(container, adapter, expectedType, expectedParameterName); return; } throw e; } } /** * Accept the visitor for the current {@link Parameter}. If internally a * {@link CollectionComponentParameter}is used, it is visited also. * * @see BasicComponentParameter#accept(org.picocontainer.PicoVisitor) */ public void accept(PicoVisitor visitor) { super.accept(visitor); if (collectionParameter != null) { collectionParameter.accept(visitor); } } } Other PicoContainer examples (source code examples)Here is a short list of links related to this PicoContainer ComponentParameter.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.