1 10 package org.picocontainer.defaults; 11 12 import org.picocontainer.ComponentAdapter; 13 import org.picocontainer.Parameter; 14 import org.picocontainer.PicoContainer; 15 import org.picocontainer.PicoInstantiationException; 16 import org.picocontainer.PicoIntrospectionException; 17 import org.picocontainer.PicoVisitor; 18 19 20 35 public class ComponentParameter 36 extends BasicComponentParameter { 37 38 41 public static final ComponentParameter DEFAULT = new ComponentParameter(); 42 45 public static final ComponentParameter ARRAY = new ComponentParameter(false); 46 50 public static final ComponentParameter ARRAY_ALLOW_EMPTY = new ComponentParameter(true); 51 52 private final Parameter collectionParameter; 53 54 59 public ComponentParameter(Object componentKey) { 60 this(componentKey, null); 61 } 62 63 66 public ComponentParameter() { 67 this(false); 68 } 69 70 77 public ComponentParameter(boolean emptyCollection) { 78 this(null, emptyCollection ? CollectionComponentParameter.ARRAY_ALLOW_EMPTY : CollectionComponentParameter.ARRAY); 79 } 80 81 90 public ComponentParameter(Class componentValueType, boolean emptyCollection) { 91 this(null, new CollectionComponentParameter(componentValueType, emptyCollection)); 92 } 93 94 105 public ComponentParameter(Class componentKeyType, Class componentValueType, boolean emptyCollection) { 106 this(null, new CollectionComponentParameter(componentKeyType, componentValueType, emptyCollection)); 107 } 108 109 private ComponentParameter(Object componentKey, Parameter collectionParameter) { 110 super(componentKey); 111 this.collectionParameter = collectionParameter; 112 } 113 114 public Object resolveInstance(PicoContainer container, ComponentAdapter adapter, Class expectedType) 115 throws PicoInstantiationException { 116 Object result = super.resolveInstance(container, adapter, expectedType); 118 if (result == null && collectionParameter != null) { 119 result = collectionParameter.resolveInstance(container, adapter, expectedType); 120 } 121 return result; 122 } 123 124 public boolean isResolvable(PicoContainer container, ComponentAdapter adapter, Class expectedType) { 125 if (!super.isResolvable(container, adapter, expectedType)) { 126 if (collectionParameter != null) { 127 return collectionParameter.isResolvable(container, adapter, expectedType); 128 } 129 return false; 130 } 131 return true; 132 } 133 134 140 public void verify(PicoContainer container, ComponentAdapter adapter, Class expectedType) throws PicoIntrospectionException { 141 try { 142 super.verify(container, adapter, expectedType); 143 } catch (UnsatisfiableDependenciesException e) { 144 if (collectionParameter != null) { 145 collectionParameter.verify(container, adapter, expectedType); 146 return; 147 } 148 throw e; 149 } 150 } 151 152 158 public void accept(PicoVisitor visitor) { 159 super.accept(visitor); 160 if (collectionParameter != null) { 161 collectionParameter.accept(visitor); 162 } 163 } 164 } 165 | Popular Tags |