You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Options.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* *******************************************************************
  2. * Copyright (c) 2003 Contributors.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Wes Isberg initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.testing.util.options;
  13. import java.util.ArrayList;
  14. import java.util.Iterator;
  15. import org.aspectj.util.LangUtil;
  16. /**
  17. * A bunch of options that handle search boilerplate.
  18. * This enforces an initialization phase by permitting
  19. * options to be added only until frozen, and
  20. * permitting matching only after frozen.
  21. */
  22. public class Options {
  23. /** if true, then perform extra checks to debug problems */
  24. // private static final boolean verifying = false;
  25. private static final boolean FROZEN = true;
  26. /**
  27. * List input unmatched by options, if any.
  28. * @param input the String[] used to generate the values
  29. * @param values the Option.Value[] found from the input
  30. * @return null if no values are null, String list of missed otherwise
  31. */
  32. public static String missedMatchError(
  33. String[] input,
  34. Values values) {
  35. int[] missed = values.indexMissedMatches();
  36. LangUtil.throwIaxIfNull(input, "input");
  37. LangUtil.throwIaxIfNull(values, "values");
  38. LangUtil.throwIaxIfFalse(
  39. input.length == values.length(),
  40. "input is not matched by values");
  41. if (0 == missed.length) {
  42. return null;
  43. }
  44. StringBuffer sb = new StringBuffer();
  45. sb.append("missed values: [");
  46. for (int i = 0; i < missed.length; i++) {
  47. if (i > 0) {
  48. sb.append(", ");
  49. }
  50. sb.append(missed[i] + ": " + input[missed[i]]);
  51. }
  52. sb.append("]");
  53. return sb.toString();
  54. }
  55. private final ArrayList options = new ArrayList();
  56. private final boolean stopAtFirstMatch;
  57. private boolean frozen = !FROZEN;
  58. public Options(boolean stopAtFirstMatch) {
  59. this.stopAtFirstMatch = stopAtFirstMatch;
  60. }
  61. public void freeze() {
  62. if (frozen != FROZEN) {
  63. frozen = FROZEN;
  64. }
  65. }
  66. public boolean isFrozen() {
  67. return (frozen == FROZEN);
  68. }
  69. public void addOption(Option option) {
  70. checkFrozen("adding option", !FROZEN);
  71. LangUtil.throwIaxIfNull(option, "option");
  72. options.add(option);
  73. }
  74. /**
  75. * Associate options matched, if any, with input by index.
  76. * If an input element is not matched, the corresponding
  77. * result element will be null.
  78. * If there are multi-argument options matched, then
  79. * only the initial element will be non-null, but it
  80. * will contain the accumulated value of the arguments.
  81. * @param input the String[] of input
  82. * @return Option.Value[] corresponding to input
  83. * @throws Option.InvalidInputException when encountering
  84. * invalid arguments to a matched multi-argument option.
  85. */
  86. public Values acceptInput(String[] input)
  87. throws Option.InvalidInputException {
  88. checkFrozen("matching options", FROZEN);
  89. if ((null == input) || (0 == input.length)) {
  90. return Values.EMPTY;
  91. }
  92. Option.Value[] results = new Option.Value[input.length];
  93. for (int i = 0; i < input.length; i++) {
  94. Option.Value result = firstMatch(input[i]);
  95. final int index = i;
  96. if (null != result) {
  97. for (int len = result.option.numArguments();
  98. len > 0;
  99. len--) {
  100. i++;
  101. if (i >= input.length) {
  102. throw new Option.InvalidInputException(
  103. "not enough arguments",
  104. null,
  105. result.option);
  106. }
  107. result = result.nextInput(input[i]);
  108. }
  109. }
  110. results[index] = result;
  111. }
  112. return Values.wrapValues(results);
  113. }
  114. private void checkFrozen(String actionLabel, boolean expectFrozen) {
  115. if (expectFrozen != isFrozen()) {
  116. if (null == actionLabel) {
  117. actionLabel = "use";
  118. }
  119. if (expectFrozen) {
  120. actionLabel = "must freeze before " + actionLabel;
  121. } else {
  122. actionLabel = "frozen before " + actionLabel;
  123. }
  124. throw new IllegalStateException(actionLabel);
  125. }
  126. }
  127. private Option.Value firstMatch(String value) {
  128. LangUtil.throwIaxIfNull(value, "value");
  129. // ArrayList list = new ArrayList();
  130. for (Iterator iter = options.iterator(); iter.hasNext();) {
  131. Option option = (Option) iter.next();
  132. Option.Value result = option.acceptValue(value);
  133. if (null != result) {
  134. return result;
  135. }
  136. }
  137. return null;
  138. }
  139. }