Mercurial > projects > dwt-addons
comparison dwtx/core/commands/ParameterizedCommand.d @ 104:04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
These new wrappers now use the tango.util.containers instead of the tango.util.collections.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 07 Aug 2008 15:01:33 +0200 |
parents | 4878bef4a38e |
children |
comparison
equal
deleted
inserted
replaced
103:2d6540440fe6 | 104:04b47443bb01 |
---|---|
12 * Frank Benoit <benoit@tionex.de> | 12 * Frank Benoit <benoit@tionex.de> |
13 *******************************************************************************/ | 13 *******************************************************************************/ |
14 | 14 |
15 module dwtx.core.commands.ParameterizedCommand; | 15 module dwtx.core.commands.ParameterizedCommand; |
16 | 16 |
17 // import java.util.ArrayList; | |
18 // import java.util.Collection; | |
19 // import java.util.Collections; | |
20 // import java.util.HashMap; | |
21 // import java.util.Iterator; | |
22 // import java.util.List; | |
23 // import java.util.Map; | |
24 | |
25 import tango.util.collection.model.Seq; | |
26 import tango.util.collection.model.Map; | |
27 import tango.util.collection.model.Set; | |
28 import tango.util.collection.ArraySeq; | |
29 import tango.util.collection.HashSet; | |
30 import tango.util.collection.HashMap; | |
31 | |
32 import dwtx.core.commands.AbstractParameterValueConverter; | 17 import dwtx.core.commands.AbstractParameterValueConverter; |
33 import dwtx.core.commands.Command; | 18 import dwtx.core.commands.Command; |
34 import dwtx.core.commands.CommandManager; | 19 import dwtx.core.commands.CommandManager; |
35 import dwtx.core.commands.IParameter; | 20 import dwtx.core.commands.IParameter; |
36 import dwtx.core.commands.IParameterValues; | 21 import dwtx.core.commands.IParameterValues; |
40 import dwtx.core.commands.ParameterValueConversionException; | 25 import dwtx.core.commands.ParameterValueConversionException; |
41 import dwtx.core.commands.ExecutionEvent; | 26 import dwtx.core.commands.ExecutionEvent; |
42 import dwtx.core.commands.common.NotDefinedException; | 27 import dwtx.core.commands.common.NotDefinedException; |
43 import dwtx.core.internal.commands.util.Util; | 28 import dwtx.core.internal.commands.util.Util; |
44 import dwt.dwthelper.utils; | 29 import dwt.dwthelper.utils; |
30 import dwtx.dwtxhelper.Collection; | |
45 import tango.text.convert.Format; | 31 import tango.text.convert.Format; |
46 | 32 |
47 static import tango.text.Text; | 33 static import tango.text.Text; |
48 alias tango.text.Text.Text!(char) StringBuffer; | 34 alias tango.text.Text.Text!(char) StringBuffer; |
49 | 35 |
56 * </p> | 42 * </p> |
57 * | 43 * |
58 * @since 3.1 | 44 * @since 3.1 |
59 */ | 45 */ |
60 public final class ParameterizedCommand : Comparable { | 46 public final class ParameterizedCommand : Comparable { |
61 private static const Map!(String,String) EMPTY_MAP; | |
62 /** | 47 /** |
63 * The constant integer hash code value meaning the hash code has not yet | 48 * The constant integer hash code value meaning the hash code has not yet |
64 * been computed. | 49 * been computed. |
65 */ | 50 */ |
66 private static const int HASH_CODE_NOT_COMPUTED = -1; | 51 private static const int HASH_CODE_NOT_COMPUTED = -1; |
75 */ | 60 */ |
76 private static const int HASH_INITIAL; | 61 private static const int HASH_INITIAL; |
77 | 62 |
78 static this(){ | 63 static this(){ |
79 HASH_INITIAL = dwt.dwthelper.utils.toHash(ParameterizedCommand.classinfo.name ); | 64 HASH_INITIAL = dwt.dwthelper.utils.toHash(ParameterizedCommand.classinfo.name ); |
80 EMPTY_MAP = new HashMap!(String,String); | |
81 } | 65 } |
82 /** | 66 /** |
83 * The index of the parameter id in the parameter values. | 67 * The index of the parameter id in the parameter values. |
84 * | 68 * |
85 * @deprecated no longer used | 69 * @deprecated no longer used |
172 * @param parameters | 156 * @param parameters |
173 * The parameters in to process; must not be <code>null</code>. | 157 * The parameters in to process; must not be <code>null</code>. |
174 * @return A collection (<code>Collection</code>) of combinations (<code>List</code> | 158 * @return A collection (<code>Collection</code>) of combinations (<code>List</code> |
175 * of <code>Parameterization</code>). | 159 * of <code>Parameterization</code>). |
176 */ | 160 */ |
177 private static final Seq!(Object) expandParameters(int startIndex, | 161 private static final Collection expandParameters(int startIndex, |
178 IParameter[] parameters) { | 162 IParameter[] parameters) { |
179 int nextIndex = startIndex + 1; | 163 int nextIndex = startIndex + 1; |
180 bool noMoreParameters = (nextIndex >= parameters.length); | 164 bool noMoreParameters = (nextIndex >= parameters.length); |
181 | 165 |
182 IParameter parameter = parameters[startIndex]; | 166 IParameter parameter = parameters[startIndex]; |
183 auto parameterizations = new ArraySeq!(Object); | 167 List parameterizations = new ArrayList(); |
184 if (parameter.isOptional()) { | 168 if (parameter.isOptional()) { |
185 parameterizations.append( cast(Object) null); | 169 parameterizations.add( cast(Object) null); |
186 } | 170 } |
187 | 171 |
188 IParameterValues values = null; | 172 IParameterValues values = null; |
189 try { | 173 try { |
190 values = parameter.getValues(); | 174 values = parameter.getValues(); |
195 | 179 |
196 // Make recursive call | 180 // Make recursive call |
197 return expandParameters(nextIndex, parameters); | 181 return expandParameters(nextIndex, parameters); |
198 } | 182 } |
199 | 183 |
200 // Iterator parameterValueItr = parameterValues.entrySet() | 184 Map parameterValues = values.getParameterValues(); |
201 // .iterator(); | 185 Iterator parameterValueItr = parameterValues.entrySet() |
202 auto parameterValues = values.getParameterValues(); | 186 .iterator(); |
203 auto set = new HashSet!(String); | 187 while (parameterValueItr.hasNext()) { |
204 foreach( k,v; parameterValues ){ | 188 Map.Entry entry = cast(Map.Entry) parameterValueItr.next(); |
205 set.add( v ); | |
206 } | |
207 | |
208 | |
209 //while (parameterValueItr.hasNext()) { | |
210 foreach( v; set ){ | |
211 //Map.Entry entry = (Map.Entry) parameterValueItr.next(); | |
212 Parameterization parameterization = new Parameterization( | 189 Parameterization parameterization = new Parameterization( |
213 parameter, v); | 190 parameter, stringcast( entry.getValue())); |
214 parameterizations.append(parameterization); | 191 parameterizations.add(parameterization); |
215 } | 192 } |
216 | 193 |
217 // Check if another iteration will produce any more names. | 194 // Check if another iteration will produce any more names. |
218 int parameterizationCount = parameterizations.size(); | 195 int parameterizationCount = parameterizations.size(); |
219 if (noMoreParameters) { | 196 if (noMoreParameters) { |
220 // This is it, so just return the current parameterizations. | 197 // This is it, so just return the current parameterizations. |
221 for (int i = 0; i < parameterizationCount; i++) { | 198 for (int i = 0; i < parameterizationCount; i++) { |
222 Parameterization parameterization = cast(Parameterization) parameterizations | 199 Parameterization parameterization = cast(Parameterization) parameterizations |
223 .get(i); | 200 .get(i); |
224 auto combination = new ArraySeq!(Object); | 201 List combination = new ArrayList(); |
225 combination.append(parameterization); | 202 combination.add(parameterization); |
226 parameterizations.replaceAt(i, combination); | 203 parameterizations.set(i, cast(Object)combination); |
227 } | 204 } |
228 return parameterizations; | 205 return parameterizations; |
229 } | 206 } |
230 | 207 |
231 // Make recursive call | 208 // Make recursive call |
232 auto suffixes = expandParameters(nextIndex, parameters); | 209 Collection suffixes = expandParameters(nextIndex, parameters); |
233 suffixes.removeAll(cast(Object)null); | 210 while (suffixes.remove(cast(Object)null)) { |
234 if (suffixes.drained()) { | 211 // just keep deleting the darn things. |
212 } | |
213 if (suffixes.isEmpty()) { | |
235 // This is it, so just return the current parameterizations. | 214 // This is it, so just return the current parameterizations. |
236 for (int i = 0; i < parameterizationCount; i++) { | 215 for (int i = 0; i < parameterizationCount; i++) { |
237 Parameterization parameterization = cast(Parameterization) parameterizations | 216 Parameterization parameterization = cast(Parameterization) parameterizations |
238 .get(i); | 217 .get(i); |
239 auto combination = new ArraySeq!(Object); | 218 List combination = new ArrayList(); |
240 combination.append(parameterization); | 219 combination.add(parameterization); |
241 parameterizations.replaceAt(i, combination); | 220 parameterizations.set(i,cast(Object) combination); |
242 } | 221 } |
243 return parameterizations; | 222 return parameterizations; |
244 } | 223 } |
245 auto returnValue = new ArraySeq!(Object); | 224 Collection returnValue = new ArrayList(); |
246 // Iterator suffixItr = suffixes.iterator(); | 225 Iterator suffixItr = suffixes.iterator(); |
247 // while (suffixItr.hasNext()) { | 226 while (suffixItr.hasNext()) { |
248 foreach( v; suffixes ){ | 227 List combination = cast(List) suffixItr.next(); |
249 // final List combination = (List) suffixItr.next(); | |
250 auto combination = cast(Seq!(Object)) v; | |
251 int combinationSize = combination.size(); | 228 int combinationSize = combination.size(); |
252 for (int i = 0; i < parameterizationCount; i++) { | 229 for (int i = 0; i < parameterizationCount; i++) { |
253 Parameterization parameterization = cast(Parameterization) parameterizations | 230 Parameterization parameterization = cast(Parameterization) parameterizations |
254 .get(i); | 231 .get(i); |
255 auto newCombination = new ArraySeq!(Object); | 232 List newCombination = new ArrayList(combinationSize + 1); |
256 newCombination.capacity(combinationSize + 1); | 233 newCombination.add(parameterization); |
257 newCombination.append(parameterization); | 234 newCombination.addAll(combination); |
258 foreach( c; combination ){ | 235 returnValue.add(cast(Object)newCombination); |
259 newCombination.append(c); | |
260 } | |
261 returnValue.append(newCombination); | |
262 } | 236 } |
263 } | 237 } |
264 | 238 |
265 return returnValue; | 239 return returnValue; |
266 } | 240 } |
284 * representing all of the possible combinations. This value is | 258 * representing all of the possible combinations. This value is |
285 * never empty and it is never <code>null</code>. | 259 * never empty and it is never <code>null</code>. |
286 * @throws NotDefinedException | 260 * @throws NotDefinedException |
287 * If the command is not defined. | 261 * If the command is not defined. |
288 */ | 262 */ |
289 public static final Seq!(Object) generateCombinations(Command command) { | 263 public static final Collection generateCombinations(Command command) { |
290 IParameter[] parameters = command.getParameters(); | 264 IParameter[] parameters = command.getParameters(); |
291 if (parameters is null) { | 265 if (parameters is null) { |
292 auto res = new ArraySeq!(Object); | 266 return Collections |
293 res.append( new ParameterizedCommand(command, null) ); | 267 .singleton(new ParameterizedCommand(command, null)); |
294 return res; | 268 } |
295 } | 269 |
296 | 270 Collection expansion = expandParameters(0, parameters); |
297 auto expansion = expandParameters(0, parameters); | 271 Collection combinations = new ArrayList(expansion.size()); |
298 auto combinations = new ArraySeq!(Object); | 272 Iterator expansionItr = expansion.iterator(); |
299 combinations.capacity(expansion.size()); | 273 while (expansionItr.hasNext()) { |
300 foreach( v; expansion ){ | 274 List combination = cast(List) expansionItr.next(); |
301 // Iterator expansionItr = expansion.iterator(); | |
302 // while (expansionItr.hasNext()) { | |
303 auto combination = cast(Seq!(Object)) v; | |
304 if (combination is null) { | 275 if (combination is null) { |
305 combinations.append(new ParameterizedCommand(command, null)); | 276 combinations.add(new ParameterizedCommand(command, null)); |
306 } else { | 277 } else { |
307 combination.removeAll(cast(Object)null); | 278 while (combination.remove(cast(Object)null)) { |
308 if (combination.drained()) { | 279 // Just keep removing while there are null entries left. |
309 combinations.append(new ParameterizedCommand(command, null)); | 280 } |
281 if (combination.isEmpty()) { | |
282 combinations.add(new ParameterizedCommand(command, null)); | |
310 } else { | 283 } else { |
311 Parameterization[] parameterizations = cast(Parameterization[]) combination | 284 Parameterization[] parameterizations = arraycast!(Parameterization)( combination |
312 .toArray(); | 285 .toArray()); |
313 combinations.append(new ParameterizedCommand(command, | 286 combinations.add(new ParameterizedCommand(command, |
314 parameterizations)); | 287 parameterizations)); |
315 } | 288 } |
316 } | 289 } |
317 } | 290 } |
318 | 291 |
331 * @return the parameterized command, or <code>null</code> if it could not | 304 * @return the parameterized command, or <code>null</code> if it could not |
332 * be generated | 305 * be generated |
333 * @since 3.4 | 306 * @since 3.4 |
334 */ | 307 */ |
335 public static final ParameterizedCommand generateCommand(Command command, | 308 public static final ParameterizedCommand generateCommand(Command command, |
336 Map!(String,Object) parameters) { | 309 Map parameters) { |
337 // no parameters | 310 // no parameters |
338 if (parameters is null || parameters.drained()) { | 311 if (parameters is null || parameters.isEmpty()) { |
339 return new ParameterizedCommand(command, null); | 312 return new ParameterizedCommand(command, null); |
340 } | 313 } |
341 | 314 |
342 try { | 315 try { |
343 Parameterization[] parms; | 316 ArrayList parms = new ArrayList(); |
317 Iterator i = parameters.keySet().iterator(); | |
344 | 318 |
345 // iterate over given parameters | 319 // iterate over given parameters |
346 foreach( key, value; parameters ){ | 320 while (i.hasNext()) { |
321 String key = stringcast( i.next() ); | |
347 IParameter parameter = null; | 322 IParameter parameter = null; |
348 // get the parameter from the command | 323 // get the parameter from the command |
349 parameter = command.getParameter(key); | 324 parameter = command.getParameter(key); |
350 | 325 |
351 // if the parameter is defined add it to the parameter list | 326 // if the parameter is defined add it to the parameter list |
352 if (parameter is null) { | 327 if (parameter is null) { |
353 return null; | 328 return null; |
354 } | 329 } |
355 ParameterType parameterType = command.getParameterType(key); | 330 ParameterType parameterType = command.getParameterType(key); |
356 if (parameterType is null) { | 331 if (parameterType is null) { |
357 parms ~= new Parameterization(parameter, | 332 parms.add(new Parameterization(parameter, |
358 stringcast(value) ); | 333 stringcast( parameters.get(key)))); |
359 } else { | 334 } else { |
360 AbstractParameterValueConverter valueConverter = parameterType | 335 AbstractParameterValueConverter valueConverter = parameterType |
361 .getValueConverter(); | 336 .getValueConverter(); |
362 if (valueConverter !is null) { | 337 if (valueConverter !is null) { |
363 String val = valueConverter.convertToString(value); | 338 String val = valueConverter.convertToString( parameters |
364 parms ~= new Parameterization(parameter, val); | 339 .get(key)); |
340 parms.add(new Parameterization(parameter, val)); | |
365 } else { | 341 } else { |
366 parms ~= new Parameterization(parameter, | 342 parms.add(new Parameterization(parameter, |
367 stringcast(value)); | 343 stringcast(parameters.get(key)))); |
368 } | 344 } |
369 } | 345 } |
370 } | 346 } |
371 | 347 |
372 // convert the parameters to an Parameterization array and create | 348 // convert the parameters to an Parameterization array and create |
373 // the command | 349 // the command |
374 return new ParameterizedCommand(command, parms ); | 350 return new ParameterizedCommand(command, arraycast!(Parameterization)( parms |
351 .toArray())); | |
375 } catch (NotDefinedException e) { | 352 } catch (NotDefinedException e) { |
376 } catch (ParameterValueConversionException e) { | 353 } catch (ParameterValueConversionException e) { |
377 } | 354 } |
378 return null; | 355 return null; |
379 } | 356 } |
609 * | 586 * |
610 * @return The map of parameter ids (<code>String</code>) to parameter | 587 * @return The map of parameter ids (<code>String</code>) to parameter |
611 * values (<code>String</code>). This map is never | 588 * values (<code>String</code>). This map is never |
612 * <code>null</code>, but may be empty. | 589 * <code>null</code>, but may be empty. |
613 */ | 590 */ |
614 public final Map!(String,String) getParameterMap() { | 591 public final Map getParameterMap() { |
615 if ((parameterizations is null) || (parameterizations.length is 0)) { | 592 if ((parameterizations is null) || (parameterizations.length is 0)) { |
616 return EMPTY_MAP; | 593 return Collections.EMPTY_MAP; |
617 } | 594 } |
618 | 595 |
619 auto parameterMap = new HashMap!(String,String); | 596 Map parameterMap = new HashMap(); |
620 for (int i = 0; i < parameterizations.length; i++) { | 597 for (int i = 0; i < parameterizations.length; i++) { |
621 Parameterization parameterization = parameterizations[i]; | 598 Parameterization parameterization = parameterizations[i]; |
622 parameterMap.add(parameterization.getParameter().getId(), | 599 parameterMap.put(parameterization.getParameter().getId(), |
623 parameterization.getValue()); | 600 parameterization.getValue()); |
624 } | 601 } |
625 return parameterMap; | 602 return parameterMap; |
626 } | 603 } |
627 | 604 |