(no commit message)
[utils] / system / general / src / main / java / org / wamblee / system / adapters / ParameterValues.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16 package org.wamblee.system.adapters;
17
18 import org.wamblee.system.core.DefaultRequiredInterface;
19 import org.wamblee.system.core.RequiredInterface;
20 import org.wamblee.system.core.Scope;
21
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
25
26 /**
27  * Represents parameter values for a method or constructor and allows for the
28  * configuration of how these values are retrieved.
29  * 
30  * @author Erik Brakkee
31  */
32 public class ParameterValues {
33     private String[] names;
34
35     private Class[] types;
36
37     private ValueProvider[] values;
38
39     /**
40      * Constructs the configuration. By default no constructor is selected and
41      * one of {@link #select(Class...)} or {@link #greedy()} must be called.
42      * 
43      * @param aClass
44      *            Class to construct.
45      */
46     public ParameterValues(Class[] aTypes) {
47         names = new String[aTypes.length];
48
49         for (int i = 0; i < aTypes.length; i++) {
50             names[i] = "arg" + i;
51         }
52
53         types = aTypes;
54         resetValues();
55     }
56
57     /**
58      * Constructs the configuration. By default no constructor is selected and
59      * one of {@link #select(Class...)} or {@link #greedy()} must be called.
60      * 
61      * @param aNames
62      *            Names of the arguments.
63      * @param aClass
64      *            Class to construct.
65      */
66     public ParameterValues(String[] aNames, Class[] aTypes) {
67         assert aNames.length == aTypes.length;
68         names = aNames;
69         types = Arrays.copyOf(aTypes, aTypes.length);
70         resetValues();
71     }
72
73     /**
74      * The types of the parameter values.
75      * 
76      * @return Types.
77      */
78     public Class[] getTypes() {
79         return Arrays.copyOf(types, types.length);
80     }
81
82     /**
83      * Sets argument i to be optional, meaning that null is allowed to be passed
84      * in.
85      * 
86      * @param aArg
87      *            Argument to set.
88      * 
89      */
90     public ParameterValues setOptional(int aArg) {
91         values[aArg] = new RequiredInterfaceProvider(
92             new DefaultRequiredInterface("arg" + aArg, types[aArg], true));
93
94         return this;
95     }
96
97     /**
98      * Sets the argument i to a fixed value.
99      * 
100      * @param aArg
101      *            Argument to set.
102      * @param aValue
103      *            Value.
104      * 
105      */
106     public ParameterValues setValue(int aArg, Object aValue) {
107         values[aArg] = new FixedValueProvider(aValue);
108
109         return this;
110     }
111
112     /**
113      * Resets the values.
114      */
115     private void resetValues() {
116         values = new ValueProvider[types.length];
117
118         for (int i = 0; i < values.length; i++) {
119             values[i] = new RequiredInterfaceProvider(
120                 new DefaultRequiredInterface(names[i], types[i]));
121         }
122     }
123
124     /**
125      * Gets the required interfaces to provide values that are not provided in
126      * another way.
127      * 
128      * @return Required interfaces.
129      */
130     public List<RequiredInterface> getRequiredInterfaces() {
131         List<RequiredInterface> result = new ArrayList<RequiredInterface>();
132
133         for (ValueProvider provider : values) {
134             if (provider instanceof RequiredInterfaceProvider) {
135                 result.add(((RequiredInterfaceProvider) provider)
136                     .getRequiredInterface());
137             }
138         }
139
140         return result;
141     }
142
143     /**
144      * Returns the values to use in the given scope.
145      * 
146      * @param aScope
147      *            Scope within which to retrieve the values.
148      * 
149      * @return Values.
150      */
151     public Object[] values(Scope aScope) {
152         Object[] valueArray = new Object[values.length];
153
154         for (int i = 0; i < values.length; i++) {
155             valueArray[i] = values[i].getValue(aScope);
156         }
157
158         return valueArray;
159     }
160 }