2 * Copyright 2008 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;
27 * Represents parameter values for a method or constructor and allows for
28 * the configuration of how these values are retrieved.
30 * @author Erik Brakkee
32 public class ParameterValues {
36 private String[] names;
41 private Class[] types;
46 private ValueProvider[] values;
49 * Constructs the configuration. By default no constructor is selected and
50 * one of {@link #select(Class...)} or
51 * {@link #greedy()} must be called.
52 * @param aClass Class to construct.
54 public ParameterValues(Class[] aTypes) {
55 names = new String[aTypes.length];
57 for (int i = 0; i < aTypes.length; i++) {
66 * Constructs the configuration. By default no constructor is selected and
67 * one of {@link #select(Class...)} or
68 * {@link #greedy()} must be called.
69 * @param aNames Names of the arguments.
70 * @param aClass Class to construct.
72 public ParameterValues(String[] aNames, Class[] aTypes) {
73 assert aNames.length == aTypes.length;
80 * The types of the parameter values.
84 public Class[] getTypes() {
89 * Sets argument i to be optional, meaning that null is allowed to
92 * @param aArg Argument to set.
94 * @return DOCUMENT ME!
96 public ParameterValues setOptional(int aArg) {
97 values[aArg] = new RequiredInterfaceProvider(new DefaultRequiredInterface(
98 "arg" + aArg, types[aArg], true));
104 * Sets the argument i to a fixed value.
106 * @param aArg Argument to set.
107 * @param aValue Value.
109 * @return DOCUMENT ME!
111 public ParameterValues setValue(int aArg, Object aValue) {
112 values[aArg] = new FixedValueProvider(aValue);
120 private void resetValues() {
121 values = new ValueProvider[types.length];
123 for (int i = 0; i < values.length; i++) {
124 values[i] = new RequiredInterfaceProvider(new DefaultRequiredInterface(
125 names[i], types[i]));
130 * Gets the required interfaces to provide values that are not
131 * provided in another way.
133 * @return Required interfaces.
135 public List<RequiredInterface> getRequiredInterfaces() {
136 List<RequiredInterface> result = new ArrayList<RequiredInterface>();
138 for (ValueProvider provider : values) {
139 if (provider instanceof RequiredInterfaceProvider) {
140 result.add(((RequiredInterfaceProvider) provider)
141 .getRequiredInterface());
149 * Returns the values to use in the given scope.
151 * @param aScope Scope within which to retrieve the values.
155 public Object[] values(Scope aScope) {
156 Object[] valueArray = new Object[values.length];
158 for (int i = 0; i < values.length; i++) {
159 valueArray[i] = values[i].getValue(aScope);