01: /*****************************************************************************
02: * Copyright (C) PicoContainer Organization. All rights reserved. *
03: * ------------------------------------------------------------------------- *
04: * The software in this package is published under the terms of the BSD *
05: * style license a copy of which has been included with this distribution in *
06: * the LICENSE.txt file. *
07: *****************************************************************************/package org.picocontainer.gems.constraints;
08:
09: import org.picocontainer.ComponentAdapter;
10: import org.picocontainer.NameBinding;
11: import org.picocontainer.PicoContainer;
12: import org.picocontainer.PicoCompositionException;
13: import org.picocontainer.injectors.AbstractInjector;
14: import org.picocontainer.parameters.CollectionComponentParameter;
15:
16: import java.lang.reflect.Array;
17: import java.lang.annotation.Annotation;
18: import java.util.Map;
19:
20: /**
21: * Base class for parameter constraints.
22: *
23: * @author Nick Sieger
24: */
25: public abstract class AbstractConstraint extends
26: CollectionComponentParameter implements Constraint {
27:
28: /** Construct an AbstractContraint. */
29: protected AbstractConstraint() {
30: super (false);
31: }
32:
33: public Object resolveInstance(PicoContainer container,
34: ComponentAdapter adapter, Class expectedType,
35: NameBinding expectedNameBinding, boolean useNames,
36: Annotation binding) throws PicoCompositionException {
37: final Object[] array = (Object[]) super .resolveInstance(
38: container, adapter, getArrayType(expectedType),
39: expectedNameBinding, useNames, null);
40: if (array.length == 1) {
41: return array[0];
42: }
43: return null;
44: }
45:
46: public boolean isResolvable(PicoContainer container,
47: ComponentAdapter adapter, Class expectedType,
48: NameBinding expectedNameBinding, boolean useNames,
49: Annotation binding) throws PicoCompositionException {
50: return super .isResolvable(container, adapter,
51: getArrayType(expectedType), expectedNameBinding,
52: useNames, binding);
53: }
54:
55: public void verify(PicoContainer container,
56: ComponentAdapter adapter, Class expectedType,
57: NameBinding expectedNameBinding, boolean useNames,
58: Annotation binding) throws PicoCompositionException {
59: super .verify(container, adapter, getArrayType(expectedType),
60: expectedNameBinding, useNames, binding);
61: }
62:
63: public abstract boolean evaluate(ComponentAdapter adapter);
64:
65: protected Map<Object, ComponentAdapter<?>> getMatchingComponentAdapters(
66: PicoContainer container, ComponentAdapter adapter,
67: Class keyType, Class valueType) {
68: final Map<Object, ComponentAdapter<?>> map = super
69: .getMatchingComponentAdapters(container, adapter,
70: keyType, valueType);
71: if (map.size() > 1) {
72: throw new AbstractInjector.AmbiguousComponentResolutionException(
73: valueType, map.keySet().toArray(
74: new Object[map.size()]));
75: }
76: return map;
77: }
78:
79: private Class getArrayType(Class expectedType) {
80: return Array.newInstance(expectedType, 0).getClass();
81: }
82: }
|