2 * Copyright 2005-2010 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.system.adapters;
18 import org.wamblee.system.core.DefaultRequiredInterface;
19 import org.wamblee.system.core.RequiredInterface;
20 import org.wamblee.system.core.Scope;
22 import java.util.ArrayList;
23 import java.util.List;
26 * Represents parameter values for a method or constructor and allows for the
27 * configuration of how these values are retrieved.
29 * @author Erik Brakkee
31 public class ParameterValues {
32 private String[] names;
34 private Class[] types;
36 private ValueProvider[] values;
39 * Constructs the configuration. By default no constructor is selected and
40 * one of {@link #select(Class...)} or {@link #greedy()} must be called.
45 public ParameterValues(Class[] aTypes) {
46 names = new String[aTypes.length];
48 for (int i = 0; i < aTypes.length; i++) {
57 * Constructs the configuration. By default no constructor is selected and
58 * one of {@link #select(Class...)} or {@link #greedy()} must be called.
61 * Names of the arguments.
65 public ParameterValues(String[] aNames, Class[] aTypes) {
66 assert aNames.length == aTypes.length;
73 * The types of the parameter values.
77 public Class[] getTypes() {
82 * Sets argument i to be optional, meaning that null is allowed to be passed
89 public ParameterValues setOptional(int aArg) {
90 values[aArg] = new RequiredInterfaceProvider(
91 new DefaultRequiredInterface("arg" + aArg, types[aArg], true));
97 * Sets the argument i to a fixed value.
105 public ParameterValues setValue(int aArg, Object aValue) {
106 values[aArg] = new FixedValueProvider(aValue);
114 private void resetValues() {
115 values = new ValueProvider[types.length];
117 for (int i = 0; i < values.length; i++) {
118 values[i] = new RequiredInterfaceProvider(
119 new DefaultRequiredInterface(names[i], types[i]));
124 * Gets the required interfaces to provide values that are not provided in
127 * @return Required interfaces.
129 public List<RequiredInterface> getRequiredInterfaces() {
130 List<RequiredInterface> result = new ArrayList<RequiredInterface>();
132 for (ValueProvider provider : values) {
133 if (provider instanceof RequiredInterfaceProvider) {
134 result.add(((RequiredInterfaceProvider) provider)
135 .getRequiredInterface());
143 * Returns the values to use in the given scope.
146 * Scope within which to retrieve the values.
150 public Object[] values(Scope aScope) {
151 Object[] valueArray = new Object[values.length];
153 for (int i = 0; i < values.length; i++) {
154 valueArray[i] = values[i].getValue(aScope);