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