fa0f008e55058314339a42c79f8b674064daf4cc
[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 String[] names; 
33         private Class[] types;
34         private ValueProvider[] values;
35
36         /**
37          * Constructs the configuration. By default no constructor is selected and 
38          * one of {@link #select(Class...)} or 
39          * {@link #greedy()} must be called.  
40          * @param aClass Class to construct. 
41          */
42         public ParameterValues(Class[] aTypes) {
43             names = new String[aTypes.length];
44             for (int i = 0; i < aTypes.length; i++) { 
45                 names[i] = "arg" + i; 
46             }
47                 types = aTypes; 
48                 resetValues();   
49         }
50         
51         /**
52      * Constructs the configuration. By default no constructor is selected and 
53      * one of {@link #select(Class...)} or 
54      * {@link #greedy()} must be called.
55      * @param aNames Names of the arguments.   
56      * @param aClass Class to construct. 
57      */
58     public ParameterValues(String[] aNames, Class[] aTypes) {
59         assert aNames.length == aTypes.length; 
60         names = aNames;
61         types = aTypes; 
62         resetValues();   
63     }
64         
65         /**
66          * The types of the parameter values. 
67          * @return Types. 
68          */
69         public Class[] getTypes() {
70                 return types;
71         }
72         
73         /**
74          * Sets argument i to be optional, meaning that null is allowed to be passed in.  
75          * @param aArg Argument to set.
76          */
77         public ParameterValues setOptional(int aArg) { 
78                 values[aArg] = new RequiredInterfaceProvider(new DefaultRequiredInterface(
79                                 "arg" + aArg, types[aArg], true));
80                 return this; 
81         }
82         
83         /**
84          * Sets the argument i to a fixed value.  
85          * @param aArg Argument to set. 
86          * @param aValue Value. 
87          */
88         public ParameterValues setValue(int aArg, Object aValue) { 
89                 values[aArg] = new FixedValueProvider(aValue);
90                 return this; 
91         }
92         
93         /**
94          * Resets the values. 
95          */
96         private void resetValues() { 
97                 values = new ValueProvider[types.length];
98                 for (int i = 0; i < values.length; i++) { 
99                         values[i] = new RequiredInterfaceProvider(new DefaultRequiredInterface(
100                                         names[i], types[i]));
101                 }
102         }
103         
104         /**
105          * Gets the required interfaces to provide values that are not provided
106          * in another way. 
107          * @return Required interfaces. 
108          */
109         public List<RequiredInterface> getRequiredInterfaces() { 
110                 List<RequiredInterface> result = new ArrayList<RequiredInterface>(); 
111                 for (ValueProvider provider: values) { 
112                         if ( provider instanceof RequiredInterfaceProvider) { 
113                                 result.add( ((RequiredInterfaceProvider)provider).getRequiredInterface());
114                         }
115                 }
116                 return result; 
117         }
118
119         /**
120          * Returns the values to use in the given scope. 
121          * @param aScope Scope within which to retrieve the values. 
122          * @return Values. 
123          */
124         public Object[] values(Scope aScope) {
125                 Object[] valueArray = new Object[values.length];
126                 for (int i = 0; i < values.length; i++) {
127                         valueArray[i] = values[i].getValue(aScope);
128                 }
129                 return valueArray; 
130         }
131         
132 }