(no commit message)
[utils] / system / general / src / main / java / org / wamblee / system / adapters / ParameterValues.java
1 /*
2  * Copyright 2008 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 java.util.ArrayList;
19 import java.util.List;
20
21 import org.wamblee.system.core.DefaultRequiredInterface;
22 import org.wamblee.system.core.RequiredInterface;
23 import org.wamblee.system.core.Scope;
24
25 /**
26  * Represents parameter values for a method or constructor and allows for the configuration
27  * of how these values are retrieved. 
28  * 
29  * @author Erik Brakkee
30  */
31 public class ParameterValues {
32         private Class[] _types;
33         private ValueProvider[] _values;
34
35         /**
36          * Constructs the configuration. By default no constructor is selected and 
37          * one of {@link #select(Class...)} or 
38          * {@link #greedy()} must be called.  
39          * @param aClass Class to construct. 
40          */
41         public ParameterValues(Class[] aTypes) {
42                 _types = aTypes; 
43                 resetValues();   
44         }
45         
46         public Class[] getTypes() {
47                 return _types;
48         }
49         
50         /**
51          * Sets argument i to be optional, meaning that null is allowed to be passed in.  
52          * @param aArg Argument to set.
53          */
54         public ParameterValues setOptional(int aArg) { 
55                 _values[aArg] = new RequiredInterfaceProvider(new DefaultRequiredInterface(
56                                 "arg" + aArg, _types[aArg], true));
57                 return this; 
58         }
59         
60         /**
61          * Sets the argument i to a fixed value.  
62          * @param aArg Argument to set. 
63          * @param aValue Value. 
64          */
65         public ParameterValues setValue(int aArg, Object aValue) { 
66                 _values[aArg] = new FixedValueProvider(aValue);
67                 return this; 
68         }
69         
70         /**
71          * Resets the values. 
72          */
73         private void resetValues() { 
74                 _values = new ValueProvider[_types.length];
75                 for (int i = 0; i < _values.length; i++) { 
76                         _values[i] = new RequiredInterfaceProvider(new DefaultRequiredInterface(
77                                         "arg" + i, _types[i]));
78                 }
79         }
80         
81         public List<RequiredInterface> getRequiredInterfaces() { 
82                 List<RequiredInterface> result = new ArrayList<RequiredInterface>(); 
83                 for (ValueProvider provider: _values) { 
84                         if ( provider instanceof RequiredInterfaceProvider) { 
85                                 result.add( ((RequiredInterfaceProvider)provider).getRequiredInterface());
86                         }
87                 }
88                 return result; 
89         }
90
91         public Object[] values(Scope aScope) {
92                 Object[] values = new Object[_values.length];
93                 for (int i = 0; i < _values.length; i++) {
94                         values[i] = _values[i].getValue(aScope);
95                 }
96                 return values; 
97         }
98         
99 }