Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*****************************************************************************
* 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.NameBinding;
import org.picocontainer.PicoContainer;
import org.picocontainer.PicoCompositionException;
import org.picocontainer.PicoVisitor;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.lang.reflect.Type;
import java.lang.reflect.ParameterizedType;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
/**
* A CollectionComponentParameter should be used to support inject an {@link Array}, a
* {@link Collection}or {@link Map}of 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.
*
* @author Aslak Hellesøy
* @author Jörg Schaible
*/
@SuppressWarnings("serial")
public class CollectionComponentParameter extends AbstractParameter implements Parameter, Serializable {
/**
* Use ARRAY as {@link Parameter}for an Array that must have elements.
*/
public static final CollectionComponentParameter ARRAY = new CollectionComponentParameter();
/**
* Use ARRAY_ALLOW_EMPTY as {@link Parameter}for an Array that may have no
* elements.
*/
public static final CollectionComponentParameter ARRAY_ALLOW_EMPTY = new CollectionComponentParameter(true);
private final boolean emptyCollection;
private final Class componentKeyType;
private final Class componentValueType;
/**
* Expect an {@link Array}of an appropriate type as parameter. At least one component of
* the array's component type must exist.
*/
public CollectionComponentParameter() {
this(false);
}
/**
* Expect an {@link Array}of an appropriate type as parameter.
*
* @param emptyCollection true if an empty array also is a valid dependency
* resolution.
*/
public CollectionComponentParameter(boolean emptyCollection) {
this(Void.TYPE, emptyCollection);
}
/**
* Expect any of the collection types {@link Array},{@link Collection}or {@link Map}as
* parameter.
*
* @param componentValueType the type of the components (ignored in case of an Array)
* @param emptyCollection true if an empty collection resolves the
* dependency.
*/
public CollectionComponentParameter(Class componentValueType, boolean emptyCollection) {
this(Object.class, componentValueType, emptyCollection);
}
/**
* Expect any of the collection types {@link Array},{@link Collection}or {@link Map}as
* parameter.
*
* @param componentKeyType the type of the component's key
* @param componentValueType the type of the components (ignored in case of an Array)
* @param emptyCollection true if an empty collection resolves the
* dependency.
*/
public CollectionComponentParameter(Class componentKeyType, Class componentValueType, boolean emptyCollection) {
this.emptyCollection = emptyCollection;
this.componentKeyType = componentKeyType;
this.componentValueType = componentValueType;
}
/**
* 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
* {@link Array},{@link Collection}or {@link Map}. An empty collection is only a valid
* resolution, if the emptyCollection flag was set.
*
* @param container {@inheritDoc}
* @param injecteeAdapter
*@param expectedType {@inheritDoc}
* @param expectedNameBinding {@inheritDoc}
* @param useNames
* @param binding @return true if matching components were found or an empty collective type
* is allowed
*/
public Resolver resolve(final PicoContainer container, final ComponentAdapter> forAdapter,
ComponentAdapter> injecteeAdapter, final Type expectedType, final NameBinding expectedNameBinding,
final boolean useNames, Annotation binding) {
final Class collectionType = getCollectionType(expectedType);
if (collectionType != null) {
final Map