001    /*****************************************************************************
002     * Copyright (C) PicoContainer Organization. All rights reserved.            *
003     * ------------------------------------------------------------------------- *
004     * The software in this package is published under the terms of the BSD      *
005     * style license a copy of which has been included with this distribution in *
006     * the LICENSE.txt file.                                                     *
007     *****************************************************************************/
008    
009    package org.picocontainer.gems.constraints;
010    
011    import org.picocontainer.ComponentAdapter;
012    import org.picocontainer.PicoContainer;
013    import org.picocontainer.PicoIntrospectionException;
014    import org.picocontainer.defaults.AmbiguousComponentResolutionException;
015    import org.picocontainer.defaults.CollectionComponentParameter;
016    
017    import java.lang.reflect.Array;
018    import java.util.Map;
019    
020    /**
021     * Base class for parameter constraints.
022     *
023     * @author Nick Sieger
024     * @version 1.1
025     */
026    public abstract class AbstractConstraint extends CollectionComponentParameter implements Constraint {
027    
028        /**
029         * Construct an AbstractContraint.
030         */
031        protected AbstractConstraint() {
032            super(false);
033        }
034        
035        public Object resolveInstance(PicoContainer container, ComponentAdapter adapter, Class expectedType) throws PicoIntrospectionException {
036            final Object[] array = (Object[]) super.resolveInstance(container, adapter, getArrayType(expectedType));
037            if (array.length == 1) {
038                return array[0];
039            }
040            return null;
041        }
042    
043        public boolean isResolvable(PicoContainer container, ComponentAdapter adapter, Class expectedType) throws PicoIntrospectionException {
044            return super.isResolvable(container, adapter, getArrayType(expectedType));
045        }
046    
047        public void verify(PicoContainer container, ComponentAdapter adapter, Class expectedType) throws PicoIntrospectionException {
048            super.verify(container, adapter, getArrayType(expectedType));
049        }
050        
051        public abstract boolean evaluate(ComponentAdapter adapter);
052    
053        protected Map getMatchingComponentAdapters(PicoContainer container, ComponentAdapter adapter, Class keyType, Class valueType) {
054            final Map map = super.getMatchingComponentAdapters(container, adapter, keyType, valueType);
055            if (map.size() > 1) {
056                throw new AmbiguousComponentResolutionException(valueType, map.keySet().toArray(new Object[map.size()]));
057            }
058            return map;
059        }
060        
061        private Class getArrayType(Class expectedType) {
062            return Array.newInstance(expectedType, 0).getClass();
063        }
064    }