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