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