1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.openpermis.policy; |
11 | |
|
12 | |
import java.io.Serializable; |
13 | |
import java.util.ArrayList; |
14 | |
import java.util.HashSet; |
15 | |
import java.util.Iterator; |
16 | |
import java.util.List; |
17 | |
import java.util.Set; |
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
public final class ParameterList |
25 | |
implements Iterable<ParameterList.Parameter>, |
26 | |
Serializable |
27 | |
{ |
28 | |
|
29 | |
|
30 | |
|
31 | |
private static final long serialVersionUID = 7726102676246748249L; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public static final ParameterList empty () { |
39 | 181 | return new ParameterList(); |
40 | |
} |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
private List<Parameter> parameters; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | 3685 | public ParameterList () { |
56 | 3685 | this.parameters = new ArrayList<Parameter>(); |
57 | 3685 | } |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
public void add (String name, Class<?> type) { |
68 | 624 | if (name == null || type == null) { |
69 | 3 | throw new IllegalArgumentException("Name or type is null."); |
70 | |
} |
71 | 621 | this.parameters.add(new Parameter(name, type)); |
72 | 621 | } |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public boolean isValid () { |
80 | |
|
81 | 3 | final Set<String> names = new HashSet<String>(); |
82 | 3 | for (Parameter parameter : this) { |
83 | 5 | if (names.contains(parameter.getName())) { |
84 | 1 | return false; |
85 | |
} |
86 | 4 | names.add(parameter.getName()); |
87 | |
} |
88 | 2 | return true; |
89 | |
} |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
public String getName (int position) { |
98 | 3 | if (position >= this.parameters.size()) { |
99 | 1 | throw new IllegalArgumentException("Position out of bounds."); |
100 | |
} |
101 | 2 | return this.parameters.get(position).getName(); |
102 | |
} |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
public boolean contains (String name) { |
111 | 100 | for (Parameter parameter : this.parameters) { |
112 | 75 | if (parameter.getName().equals(name)) { |
113 | 1 | return true; |
114 | |
} |
115 | |
} |
116 | 99 | return false; |
117 | |
} |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
public Class<?> getType (int position) { |
126 | 10 | if (position >= this.parameters.size()) { |
127 | 1 | throw new IllegalArgumentException("Position out of bounds."); |
128 | |
} |
129 | 9 | return this.parameters.get(position).getType(); |
130 | |
} |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
public Class<?> getType (String name) { |
139 | 3 | for (Parameter parameter : this.parameters) { |
140 | 2 | if (parameter.getName().equals(name)) { |
141 | 2 | return parameter.getType(); |
142 | |
} |
143 | |
} |
144 | 1 | throw new IllegalArgumentException("No such parameter name."); |
145 | |
} |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
public int getParameterCount () { |
153 | 1831 | return this.parameters.size(); |
154 | |
} |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
public boolean matchesArguments (List<?> arguments) { |
163 | 35 | if (getParameterCount() != arguments.size()) { |
164 | 0 | return false; |
165 | |
} |
166 | 40 | for (int i = 0; i < getParameterCount(); i++) { |
167 | 9 | if (!arguments.get(i).getClass().equals(getType(i))) { |
168 | 4 | return false; |
169 | |
} |
170 | |
} |
171 | 31 | return true; |
172 | |
} |
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
public Iterator<Parameter> iterator () { |
178 | 2130 | return this.parameters.iterator(); |
179 | |
} |
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
public boolean equals (Object obj) { |
187 | 869 | if (obj == null) { |
188 | 0 | return false; |
189 | 869 | } else if (obj == this) { |
190 | 0 | return true; |
191 | 869 | } else if (obj instanceof ParameterList) { |
192 | 869 | final ParameterList other = (ParameterList) obj; |
193 | 869 | if (getParameterCount() != other.getParameterCount()) { |
194 | 131 | return false; |
195 | |
} |
196 | 738 | Iterator<Parameter> it1 = iterator(); |
197 | 738 | Iterator<Parameter> it2 = other.iterator(); |
198 | 813 | while (it1.hasNext() && it2.hasNext()) { |
199 | 81 | if (!it1.next().equals(it2.next())) { |
200 | 6 | return false; |
201 | |
} |
202 | |
} |
203 | 732 | return true; |
204 | |
} |
205 | 0 | return false; |
206 | |
} |
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
public int hashCode () { |
212 | 110 | return this.parameters.hashCode(); |
213 | |
} |
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
public ParameterList getCopy () { |
219 | 1754 | ParameterList result = new ParameterList(); |
220 | 1754 | for (Parameter parameter : this.parameters) { |
221 | 341 | result.add(parameter.getName(), parameter.getType()); |
222 | |
} |
223 | 1754 | return result; |
224 | |
} |
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
public static class Parameter { |
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
private String name; |
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
private Class<?> type; |
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | 621 | public Parameter (String name, Class<?> type) { |
254 | 621 | this.name = name; |
255 | 621 | this.type = type; |
256 | 621 | } |
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
public String getName () { |
266 | 885 | return this.name; |
267 | |
} |
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
public Class<?> getType () { |
275 | 810 | return this.type; |
276 | |
} |
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
public boolean equals (Object obj) { |
284 | 81 | if (obj == null) { |
285 | 0 | return false; |
286 | |
} |
287 | 81 | if (this == obj) { |
288 | 0 | return true; |
289 | |
} |
290 | 81 | if (obj instanceof Parameter) { |
291 | 81 | final Parameter other = (Parameter) obj; |
292 | 81 | if (getName().equals(other.getName()) && |
293 | |
getType().equals(other.getType()) |
294 | |
) { |
295 | 75 | return true; |
296 | |
} |
297 | |
} |
298 | 6 | return false; |
299 | |
} |
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
public int hashCode () { |
305 | 34 | return getName().hashCode() * getType().hashCode(); |
306 | |
} |
307 | |
} |
308 | |
|
309 | |
} |