Mercurial > projects > dwt-addons
diff 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 |
line wrap: on
line diff
--- a/dwtx/core/commands/ParameterizedCommand.d Sun Aug 03 17:01:51 2008 +0200 +++ b/dwtx/core/commands/ParameterizedCommand.d Thu Aug 07 15:01:33 2008 +0200 @@ -14,21 +14,6 @@ module dwtx.core.commands.ParameterizedCommand; -// import java.util.ArrayList; -// import java.util.Collection; -// import java.util.Collections; -// import java.util.HashMap; -// import java.util.Iterator; -// import java.util.List; -// import java.util.Map; - -import tango.util.collection.model.Seq; -import tango.util.collection.model.Map; -import tango.util.collection.model.Set; -import tango.util.collection.ArraySeq; -import tango.util.collection.HashSet; -import tango.util.collection.HashMap; - import dwtx.core.commands.AbstractParameterValueConverter; import dwtx.core.commands.Command; import dwtx.core.commands.CommandManager; @@ -42,6 +27,7 @@ import dwtx.core.commands.common.NotDefinedException; import dwtx.core.internal.commands.util.Util; import dwt.dwthelper.utils; +import dwtx.dwtxhelper.Collection; import tango.text.convert.Format; static import tango.text.Text; @@ -58,7 +44,6 @@ * @since 3.1 */ public final class ParameterizedCommand : Comparable { - private static const Map!(String,String) EMPTY_MAP; /** * The constant integer hash code value meaning the hash code has not yet * been computed. @@ -77,7 +62,6 @@ static this(){ HASH_INITIAL = dwt.dwthelper.utils.toHash(ParameterizedCommand.classinfo.name ); - EMPTY_MAP = new HashMap!(String,String); } /** * The index of the parameter id in the parameter values. @@ -174,15 +158,15 @@ * @return A collection (<code>Collection</code>) of combinations (<code>List</code> * of <code>Parameterization</code>). */ - private static final Seq!(Object) expandParameters(int startIndex, + private static final Collection expandParameters(int startIndex, IParameter[] parameters) { int nextIndex = startIndex + 1; bool noMoreParameters = (nextIndex >= parameters.length); IParameter parameter = parameters[startIndex]; - auto parameterizations = new ArraySeq!(Object); + List parameterizations = new ArrayList(); if (parameter.isOptional()) { - parameterizations.append( cast(Object) null); + parameterizations.add( cast(Object) null); } IParameterValues values = null; @@ -197,21 +181,14 @@ return expandParameters(nextIndex, parameters); } -// Iterator parameterValueItr = parameterValues.entrySet() -// .iterator(); - auto parameterValues = values.getParameterValues(); - auto set = new HashSet!(String); - foreach( k,v; parameterValues ){ - set.add( v ); - } - - - //while (parameterValueItr.hasNext()) { - foreach( v; set ){ - //Map.Entry entry = (Map.Entry) parameterValueItr.next(); + Map parameterValues = values.getParameterValues(); + Iterator parameterValueItr = parameterValues.entrySet() + .iterator(); + while (parameterValueItr.hasNext()) { + Map.Entry entry = cast(Map.Entry) parameterValueItr.next(); Parameterization parameterization = new Parameterization( - parameter, v); - parameterizations.append(parameterization); + parameter, stringcast( entry.getValue())); + parameterizations.add(parameterization); } // Check if another iteration will produce any more names. @@ -221,44 +198,41 @@ for (int i = 0; i < parameterizationCount; i++) { Parameterization parameterization = cast(Parameterization) parameterizations .get(i); - auto combination = new ArraySeq!(Object); - combination.append(parameterization); - parameterizations.replaceAt(i, combination); + List combination = new ArrayList(); + combination.add(parameterization); + parameterizations.set(i, cast(Object)combination); } return parameterizations; } // Make recursive call - auto suffixes = expandParameters(nextIndex, parameters); - suffixes.removeAll(cast(Object)null); - if (suffixes.drained()) { + Collection suffixes = expandParameters(nextIndex, parameters); + while (suffixes.remove(cast(Object)null)) { + // just keep deleting the darn things. + } + if (suffixes.isEmpty()) { // This is it, so just return the current parameterizations. for (int i = 0; i < parameterizationCount; i++) { Parameterization parameterization = cast(Parameterization) parameterizations .get(i); - auto combination = new ArraySeq!(Object); - combination.append(parameterization); - parameterizations.replaceAt(i, combination); + List combination = new ArrayList(); + combination.add(parameterization); + parameterizations.set(i,cast(Object) combination); } return parameterizations; } - auto returnValue = new ArraySeq!(Object); -// Iterator suffixItr = suffixes.iterator(); -// while (suffixItr.hasNext()) { - foreach( v; suffixes ){ -// final List combination = (List) suffixItr.next(); - auto combination = cast(Seq!(Object)) v; + Collection returnValue = new ArrayList(); + Iterator suffixItr = suffixes.iterator(); + while (suffixItr.hasNext()) { + List combination = cast(List) suffixItr.next(); int combinationSize = combination.size(); for (int i = 0; i < parameterizationCount; i++) { Parameterization parameterization = cast(Parameterization) parameterizations .get(i); - auto newCombination = new ArraySeq!(Object); - newCombination.capacity(combinationSize + 1); - newCombination.append(parameterization); - foreach( c; combination ){ - newCombination.append(c); - } - returnValue.append(newCombination); + List newCombination = new ArrayList(combinationSize + 1); + newCombination.add(parameterization); + newCombination.addAll(combination); + returnValue.add(cast(Object)newCombination); } } @@ -286,31 +260,30 @@ * @throws NotDefinedException * If the command is not defined. */ - public static final Seq!(Object) generateCombinations(Command command) { + public static final Collection generateCombinations(Command command) { IParameter[] parameters = command.getParameters(); if (parameters is null) { - auto res = new ArraySeq!(Object); - res.append( new ParameterizedCommand(command, null) ); - return res; + return Collections + .singleton(new ParameterizedCommand(command, null)); } - auto expansion = expandParameters(0, parameters); - auto combinations = new ArraySeq!(Object); - combinations.capacity(expansion.size()); - foreach( v; expansion ){ -// Iterator expansionItr = expansion.iterator(); -// while (expansionItr.hasNext()) { - auto combination = cast(Seq!(Object)) v; + Collection expansion = expandParameters(0, parameters); + Collection combinations = new ArrayList(expansion.size()); + Iterator expansionItr = expansion.iterator(); + while (expansionItr.hasNext()) { + List combination = cast(List) expansionItr.next(); if (combination is null) { - combinations.append(new ParameterizedCommand(command, null)); + combinations.add(new ParameterizedCommand(command, null)); } else { - combination.removeAll(cast(Object)null); - if (combination.drained()) { - combinations.append(new ParameterizedCommand(command, null)); + while (combination.remove(cast(Object)null)) { + // Just keep removing while there are null entries left. + } + if (combination.isEmpty()) { + combinations.add(new ParameterizedCommand(command, null)); } else { - Parameterization[] parameterizations = cast(Parameterization[]) combination - .toArray(); - combinations.append(new ParameterizedCommand(command, + Parameterization[] parameterizations = arraycast!(Parameterization)( combination + .toArray()); + combinations.add(new ParameterizedCommand(command, parameterizations)); } } @@ -333,17 +306,19 @@ * @since 3.4 */ public static final ParameterizedCommand generateCommand(Command command, - Map!(String,Object) parameters) { + Map parameters) { // no parameters - if (parameters is null || parameters.drained()) { + if (parameters is null || parameters.isEmpty()) { return new ParameterizedCommand(command, null); } try { - Parameterization[] parms; + ArrayList parms = new ArrayList(); + Iterator i = parameters.keySet().iterator(); // iterate over given parameters - foreach( key, value; parameters ){ + while (i.hasNext()) { + String key = stringcast( i.next() ); IParameter parameter = null; // get the parameter from the command parameter = command.getParameter(key); @@ -354,24 +329,26 @@ } ParameterType parameterType = command.getParameterType(key); if (parameterType is null) { - parms ~= new Parameterization(parameter, - stringcast(value) ); + parms.add(new Parameterization(parameter, + stringcast( parameters.get(key)))); } else { AbstractParameterValueConverter valueConverter = parameterType .getValueConverter(); if (valueConverter !is null) { - String val = valueConverter.convertToString(value); - parms ~= new Parameterization(parameter, val); + String val = valueConverter.convertToString( parameters + .get(key)); + parms.add(new Parameterization(parameter, val)); } else { - parms ~= new Parameterization(parameter, - stringcast(value)); + parms.add(new Parameterization(parameter, + stringcast(parameters.get(key)))); } } } // convert the parameters to an Parameterization array and create // the command - return new ParameterizedCommand(command, parms ); + return new ParameterizedCommand(command, arraycast!(Parameterization)( parms + .toArray())); } catch (NotDefinedException e) { } catch (ParameterValueConversionException e) { } @@ -611,15 +588,15 @@ * values (<code>String</code>). This map is never * <code>null</code>, but may be empty. */ - public final Map!(String,String) getParameterMap() { + public final Map getParameterMap() { if ((parameterizations is null) || (parameterizations.length is 0)) { - return EMPTY_MAP; + return Collections.EMPTY_MAP; } - auto parameterMap = new HashMap!(String,String); + Map parameterMap = new HashMap(); for (int i = 0; i < parameterizations.length; i++) { Parameterization parameterization = parameterizations[i]; - parameterMap.add(parameterization.getParameter().getId(), + parameterMap.put(parameterization.getParameter().getId(), parameterization.getValue()); } return parameterMap;