/*****************************************************************************
 * 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.defaults;

import org.picocontainer.ComponentAdapter;
import org.picocontainer.Parameter;
import org.picocontainer.PicoContainer;
import org.picocontainer.PicoVisitor;


/**
 * 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&eacute;n
 * @author Aslak Helles&oslash;y
 * @author J&ouml;rg Schaible
 * @author Thomas Heller
 * @version $Revision: 2285 $
 */
public class ComponentParameter
        extends BasicComponentParameter {

    /**
     * <code>DEFAULT</code> is an instance of ComponentParameter using the default constructor.
     */
    public static final ComponentParameter DEFAULT = new ComponentParameter();
    /**
     * Use <code>ARRAY</code> as {@link Parameter}for an Array that must have elements.
     */
    public static final ComponentParameter ARRAY = new ComponentParameter(false);

    private final Parameter collectionParameter;

    /**
     * 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</code> allows an Array to be empty
     * @since 1.1
     */
    public ComponentParameter(boolean emptyCollection) {
        this(null, emptyCollection ? CollectionComponentParameter.ARRAY_ALLOW_EMPTY : CollectionComponentParameter.ARRAY);
    }

    private ComponentParameter(Object componentKey, Parameter collectionParameter) {
        super(componentKey);
        this.collectionParameter = collectionParameter;
    }

    @Override
    public Object resolveInstance(PicoContainer container, ComponentAdapter adapter, Class expectedType) {
        // type check is done in isResolvable
        Object result = super.resolveInstance(container, adapter, expectedType);
        if (result == null && collectionParameter != null) {
            result = collectionParameter.resolveInstance(container, adapter, expectedType);
        }
        return result;
    }

    @Override
    public boolean isResolvable(PicoContainer container, ComponentAdapter adapter, Class expectedType) {
        if (!super.isResolvable(container, adapter, expectedType)) {
            if (collectionParameter != null) {
                return collectionParameter.isResolvable(container, adapter, expectedType);
            }
            return false;
        }
        return true;
    }

    @Override
    public void verify(PicoContainer container, ComponentAdapter adapter, Class expectedType) {
        try {
            super.verify(container, adapter, expectedType);
        } catch (UnsatisfiableDependenciesException e) {
            if (collectionParameter != null) {
                collectionParameter.verify(container, adapter, expectedType);
                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(PicoVisitor)
     */
    @Override
    public void accept(PicoVisitor visitor) {
        super.accept(visitor);
        if (collectionParameter != null) {
            collectionParameter.accept(visitor);
        }
    }
}

@@ -2122,16 +2122,259 @@
 meter;%0A%0A
+    /**%0A     * Expect a parameter matching a component of a specific key.%0A     *%0A     * @param componentKey the key of the desired component%0A     */%0A    public ComponentParameter(Object componentKey) %7B%0A        this(componentKey, null);%0A    %7D%0A%0A
     /**%0A
@@ -3021,24 +3021,1441 @@
 AY);%0A    %7D%0A%0A
+    /**%0A     * Expect any scalar paramter of the appropriate type or the collecting type%0A     * %7B@link java.lang.reflect.Array%7D,%7B@link java.util.Collection%7Dor %7B@link java.util.Map%7D.%0A     * The components in the collection will be of the specified type.%0A     *%0A     * @param componentValueType the component's type (ignored for an Array)%0A     * @param emptyCollection %3Ccode%3Etrue%3C/code%3E allows the collection to be empty%0A     * @since 1.1%0A     */%0A    public ComponentParameter(Class componentValueType, boolean emptyCollection) %7B%0A        this(null, new CollectionComponentParameter(componentValueType, emptyCollection));%0A    %7D%0A%0A    /**%0A     * Expect any scalar paramter of the appropriate type or the collecting type%0A     * %7B@link java.lang.reflect.Array%7D,%7B@link java.util.Collection%7Dor %7B@link java.util.Map%7D.%0A     * The components in the collection will be of the specified type and their adapter's key%0A     * must have a particular type.%0A     *%0A     * @param componentKeyType the component adapter's key type%0A     * @param componentValueType the component's type (ignored for an Array)%0A     * @param emptyCollection %3Ccode%3Etrue%3C/code%3E allows the collection to be empty%0A     * @since 1.1%0A     */%0A    public ComponentParameter(Class componentKeyType, Class componentValueType, boolean emptyCollection) %7B%0A        this(null, new CollectionComponentParameter(componentKeyType, componentValueType, emptyCollection));%0A    %7D%0A%0A
     private 

remove unused and outdated pico container API (part 8 - IdeTalk NanoContainer)