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.Arrays;
24 import java.util.List;
27 * Represents parameter values for a method or constructor and allows for the
28 * configuration of how these values are retrieved.
30 * @author Erik Brakkee
32 public class ParameterValues {
33 private String[] names;
35 private Class[] types;
37 private ValueProvider[] values;
40 * Constructs the configuration. By default no constructor is selected and
41 * one of {@link #select(Class...)} or {@link #greedy()} must be called.
46 public ParameterValues(Class[] aTypes) {
47 names = new String[aTypes.length];
49 for (int i = 0; i < aTypes.length; i++) {
58 * Constructs the configuration. By default no constructor is selected and
59 * one of {@link #select(Class...)} or {@link #greedy()} must be called.
62 * Names of the arguments.
66 public ParameterValues(String[] aNames, Class[] aTypes) {
67 assert aNames.length == aTypes.length;
69 types = Arrays.copyOf(aTypes, aTypes.length);
74 * The types of the parameter values.
78 public Class[] getTypes() {
79 return Arrays.copyOf(types, types.length);
83 * Sets argument i to be optional, meaning that null is allowed to be passed
90 public ParameterValues setOptional(int aArg) {
91 values[aArg] = new RequiredInterfaceProvider(
92 new DefaultRequiredInterface("arg" + aArg, types[aArg], true));
98 * Sets the argument i to a fixed value.
106 public ParameterValues setValue(int aArg, Object aValue) {
107 values[aArg] = new FixedValueProvider(aValue);
115 private void resetValues() {
116 values = new ValueProvider[types.length];
118 for (int i = 0; i < values.length; i++) {
119 values[i] = new RequiredInterfaceProvider(
120 new DefaultRequiredInterface(names[i], types[i]));
125 * Gets the required interfaces to provide values that are not provided in
128 * @return Required interfaces.
130 public List<RequiredInterface> getRequiredInterfaces() {
131 List<RequiredInterface> result = new ArrayList<RequiredInterface>();
133 for (ValueProvider provider : values) {
134 if (provider instanceof RequiredInterfaceProvider) {
135 result.add(((RequiredInterfaceProvider) provider)
136 .getRequiredInterface());
144 * Returns the values to use in the given scope.
147 * Scope within which to retrieve the values.
151 public Object[] values(Scope aScope) {
152 Object[] valueArray = new Object[values.length];
154 for (int i = 0; i < values.length; i++) {
155 valueArray[i] = values[i].getValue(aScope);