1 11 12 package org.eclipse.core.commands; 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.Collections ; 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Map ; 21 22 import org.eclipse.core.commands.common.NotDefinedException; 23 import org.eclipse.core.internal.commands.util.Util; 24 25 35 public final class ParameterizedCommand implements Comparable { 36 37 41 private static final int HASH_CODE_NOT_COMPUTED = -1; 42 43 46 private static final int HASH_FACTOR = 89; 47 48 51 private static final int HASH_INITIAL = ParameterizedCommand.class 52 .getName().hashCode(); 53 54 59 public static final int INDEX_PARAMETER_ID = 0; 60 61 67 public static final int INDEX_PARAMETER_NAME = 1; 68 69 75 public static final int INDEX_PARAMETER_VALUE_NAME = 2; 76 77 82 public static final int INDEX_PARAMETER_VALUE_VALUE = 3; 83 84 102 private static final String escape(final String rawText) { 103 104 StringBuffer buffer = null; 106 107 for (int i = 0; i < rawText.length(); i++) { 108 109 char c = rawText.charAt(i); 110 switch (c) { 111 case CommandManager.PARAMETER_START_CHAR: 112 case CommandManager.PARAMETER_END_CHAR: 113 case CommandManager.ID_VALUE_CHAR: 114 case CommandManager.PARAMETER_SEPARATOR_CHAR: 115 case CommandManager.ESCAPE_CHAR: 116 if (buffer == null) { 117 buffer = new StringBuffer (rawText.substring(0, i)); 118 } 119 buffer.append(CommandManager.ESCAPE_CHAR); 120 buffer.append(c); 121 break; 122 default: 123 if (buffer != null) { 124 buffer.append(c); 125 } 126 break; 127 } 128 129 } 130 131 if (buffer == null) { 132 return rawText; 133 } 134 return buffer.toString(); 135 } 136 137 150 private static final Collection expandParameters(final int startIndex, 151 final IParameter[] parameters) { 152 final int nextIndex = startIndex + 1; 153 final boolean noMoreParameters = (nextIndex >= parameters.length); 154 155 final IParameter parameter = parameters[startIndex]; 156 final List parameterizations = new ArrayList (); 157 if (parameter.isOptional()) { 158 parameterizations.add(null); 159 } 160 161 IParameterValues values = null; 162 try { 163 values = parameter.getValues(); 164 } catch (final ParameterValuesException e) { 165 if (noMoreParameters) { 166 return parameterizations; 167 } 168 169 return expandParameters(nextIndex, parameters); 171 } 172 final Map parameterValues = values.getParameterValues(); 173 final Iterator parameterValueItr = parameterValues.entrySet() 174 .iterator(); 175 while (parameterValueItr.hasNext()) { 176 final Map.Entry entry = (Map.Entry ) parameterValueItr.next(); 177 final Parameterization parameterization = new Parameterization( 178 parameter, (String ) entry.getValue()); 179 parameterizations.add(parameterization); 180 } 181 182 final int parameterizationCount = parameterizations.size(); 184 if (noMoreParameters) { 185 for (int i = 0; i < parameterizationCount; i++) { 187 final Parameterization parameterization = (Parameterization) parameterizations 188 .get(i); 189 final List combination = new ArrayList (1); 190 combination.add(parameterization); 191 parameterizations.set(i, combination); 192 } 193 return parameterizations; 194 } 195 196 final Collection suffixes = expandParameters(nextIndex, parameters); 198 while (suffixes.remove(null)) { 199 } 201 if (suffixes.isEmpty()) { 202 for (int i = 0; i < parameterizationCount; i++) { 204 final Parameterization parameterization = (Parameterization) parameterizations 205 .get(i); 206 final List combination = new ArrayList (1); 207 combination.add(parameterization); 208 parameterizations.set(i, combination); 209 } 210 return parameterizations; 211 } 212 final Collection returnValue = new ArrayList (); 213 final Iterator suffixItr = suffixes.iterator(); 214 while (suffixItr.hasNext()) { 215 final List combination = (List ) suffixItr.next(); 216 final int combinationSize = combination.size(); 217 for (int i = 0; i < parameterizationCount; i++) { 218 final Parameterization parameterization = (Parameterization) parameterizations 219 .get(i); 220 final List newCombination = new ArrayList (combinationSize + 1); 221 newCombination.add(parameterization); 222 newCombination.addAll(combination); 223 returnValue.add(newCombination); 224 } 225 } 226 227 return returnValue; 228 } 229 230 251 public static final Collection generateCombinations(final Command command) 252 throws NotDefinedException { 253 final IParameter[] parameters = command.getParameters(); 254 if (parameters == null) { 255 return Collections 256 .singleton(new ParameterizedCommand(command, null)); 257 } 258 259 final Collection expansion = expandParameters(0, parameters); 260 final Collection combinations = new ArrayList (expansion.size()); 261 final Iterator expansionItr = expansion.iterator(); 262 while (expansionItr.hasNext()) { 263 final List combination = (List ) expansionItr.next(); 264 if (combination == null) { 265 combinations.add(new ParameterizedCommand(command, null)); 266 } else { 267 while (combination.remove(null)) { 268 } 270 if (combination.isEmpty()) { 271 combinations.add(new ParameterizedCommand(command, null)); 272 } else { 273 final Parameterization[] parameterizations = (Parameterization[]) combination 274 .toArray(new Parameterization[combination.size()]); 275 combinations.add(new ParameterizedCommand(command, 276 parameterizations)); 277 } 278 } 279 } 280 281 return combinations; 282 } 283 284 288 private final Command command; 289 290 294 private transient int hashCode = HASH_CODE_NOT_COMPUTED; 295 296 300 private final Parameterization[] parameterizations; 301 302 private String name; 303 304 318 public ParameterizedCommand(final Command command, 319 final Parameterization[] parameterizations) { 320 if (command == null) { 321 throw new NullPointerException ( 322 "A parameterized command cannot have a null command"); } 324 325 this.command = command; 326 this.parameterizations = (parameterizations == null || parameterizations.length == 0) ? null 327 : parameterizations; 328 } 329 330 335 public final int compareTo(final Object object) { 336 final ParameterizedCommand command = (ParameterizedCommand) object; 337 final boolean thisDefined = this.command.isDefined(); 338 final boolean otherDefined = command.command.isDefined(); 339 if (!thisDefined || !otherDefined) { 340 return Util.compare(thisDefined, otherDefined); 341 } 342 343 try { 344 final int compareTo = getName().compareTo(command.getName()); 345 if (compareTo == 0) { 346 return getId().compareTo(command.getId()); 347 } 348 return compareTo; 349 } catch (final NotDefinedException e) { 350 throw new Error ( 351 "Concurrent modification of a command's defined state"); } 353 } 354 355 360 public final boolean equals(final Object object) { 361 if (this == object) { 362 return true; 363 } 364 365 if (!(object instanceof ParameterizedCommand)) { 366 return false; 367 } 368 369 final ParameterizedCommand command = (ParameterizedCommand) object; 370 if (!Util.equals(this.command, command.command)) { 371 return false; 372 } 373 374 return Util.equals(this.parameterizations, command.parameterizations); 375 } 376 377 396 public final Object execute(final Object trigger, 397 final Object applicationContext) throws ExecutionException, 398 NotHandledException { 399 return command.execute(new ExecutionEvent(command, getParameterMap(), 400 trigger, applicationContext)); 401 } 402 403 426 public final Object executeWithChecks(final Object trigger, 427 final Object applicationContext) throws ExecutionException, 428 NotDefinedException, NotEnabledException, NotHandledException { 429 return command.executeWithChecks(new ExecutionEvent(command, 430 getParameterMap(), trigger, applicationContext)); 431 } 432 433 439 public final Command getCommand() { 440 return command; 441 } 442 443 449 public final String getId() { 450 return command.getId(); 451 } 452 453 462 public final String getName() throws NotDefinedException { 463 if (name == null) { 464 final StringBuffer nameBuffer = new StringBuffer (); 465 nameBuffer.append(command.getName()); 466 if (parameterizations != null) { 467 nameBuffer.append(" ("); final int parameterizationCount = parameterizations.length; 469 for (int i = 0; i < parameterizationCount; i++) { 470 final Parameterization parameterization = parameterizations[i]; 471 nameBuffer 472 .append(parameterization.getParameter().getName()); 473 nameBuffer.append(": "); try { 475 nameBuffer.append(parameterization.getValueName()); 476 } catch (final ParameterValuesException e) { 477 481 } 482 483 if (i + 1 < parameterizationCount) { 485 nameBuffer.append(", "); } 487 } 488 nameBuffer.append(')'); 489 } 490 name = nameBuffer.toString(); 491 } 492 return name; 493 } 494 495 503 public final Map getParameterMap() { 504 if ((parameterizations == null) || (parameterizations.length == 0)) { 505 return Collections.EMPTY_MAP; 506 } 507 508 final Map parameterMap = new HashMap (); 509 for (int i = 0; i < parameterizations.length; i++) { 510 final Parameterization parameterization = parameterizations[i]; 511 parameterMap.put(parameterization.getParameter().getId(), 512 parameterization.getValue()); 513 } 514 return parameterMap; 515 } 516 517 520 public final int hashCode() { 521 if (hashCode == HASH_CODE_NOT_COMPUTED) { 522 hashCode = HASH_INITIAL * HASH_FACTOR + Util.hashCode(command); 523 hashCode = hashCode * HASH_FACTOR; 524 if (parameterizations!=null) { 525 for (int i = 0; i < parameterizations.length; i++) { 526 hashCode += Util.hashCode(parameterizations[i]); 527 } 528 } 529 if (hashCode == HASH_CODE_NOT_COMPUTED) { 530 hashCode++; 531 } 532 } 533 return hashCode; 534 } 535 536 593 public final String serialize() { 594 final String escapedId = escape(getId()); 595 596 if ((parameterizations == null) || (parameterizations.length == 0)) { 597 return escapedId; 598 } 599 600 final StringBuffer buffer = new StringBuffer (escapedId); 601 buffer.append(CommandManager.PARAMETER_START_CHAR); 602 603 for (int i = 0; i < parameterizations.length; i++) { 604 605 if (i > 0) { 606 buffer.append(CommandManager.PARAMETER_SEPARATOR_CHAR); 608 } 609 610 final Parameterization parameterization = parameterizations[i]; 611 final String parameterId = parameterization.getParameter().getId(); 612 final String escapedParameterId = escape(parameterId); 613 614 buffer.append(escapedParameterId); 615 616 final String parameterValue = parameterization.getValue(); 617 if (parameterValue != null) { 618 final String escapedParameterValue = escape(parameterValue); 619 buffer.append(CommandManager.ID_VALUE_CHAR); 620 buffer.append(escapedParameterValue); 621 } 622 } 623 624 buffer.append(CommandManager.PARAMETER_END_CHAR); 625 626 return buffer.toString(); 627 } 628 629 public final String toString() { 630 final StringBuffer buffer = new StringBuffer (); 631 buffer.append("ParameterizedCommand("); buffer.append(command); 633 buffer.append(','); 634 buffer.append(parameterizations); 635 buffer.append(')'); 636 return buffer.toString(); 637 } 638 } 639 | Popular Tags |