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.

CommandFlagResult.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2006-2012 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.commandparser.commands.flags;
  23. import com.dmdirc.commandparser.CommandArguments;
  24. import java.util.Map;
  25. /**
  26. * Convenient wrapper around the results of a {@link CommandFlagHandler}'s
  27. * parsing routines.
  28. */
  29. public class CommandFlagResult {
  30. /** The original arguments for the command. */
  31. private final CommandArguments arguments;
  32. /** The offsets of the first argument for each flag. */
  33. private final Map<CommandFlag, Integer> offsets;
  34. /**
  35. * Creates a new CommandFlagResult with the specified results.
  36. *
  37. * @param arguments The original arguments for the command
  38. * @param offsets The offsets for each flag's arguments
  39. */
  40. public CommandFlagResult(final CommandArguments arguments,
  41. final Map<CommandFlag, Integer> offsets) {
  42. this.arguments = arguments;
  43. this.offsets = offsets;
  44. }
  45. /**
  46. * Determines if the specified command flag has been used when the command
  47. * was executed.
  48. *
  49. * @param flag The flag to be checked
  50. * @return True iff the flag was specified legally, false otherwise
  51. */
  52. public boolean hasFlag(final CommandFlag flag) {
  53. return offsets.containsKey(flag);
  54. }
  55. /**
  56. * Retrieves all of the arguments passed for the specified flag as a string.
  57. *
  58. * @param flag The flag to retrieve arguments for
  59. * @return The arguments passed with the specified flag
  60. * @see #getArguments(com.dmdirc.commandparser.commands.flags.CommandFlag)
  61. */
  62. public String getArgumentsAsString(final CommandFlag flag) {
  63. return flag == null ? (offsets.get(flag) > arguments.getArguments().length
  64. ? "" : arguments.getArgumentsAsString(offsets.get(flag)))
  65. : arguments.getArgumentsAsString(offsets.get(flag),
  66. offsets.get(flag) + flag.getDelayedArgs() + flag.getImmediateArgs() - 1);
  67. }
  68. /**
  69. * Retrieves all arguments not associated with any flags as a string.
  70. *
  71. * @see CommandArguments#getArgumentsAsString()
  72. * @see #getArguments()
  73. * @return All arguments that aren't associated with a flag, as a string
  74. */
  75. public String getArgumentsAsString() {
  76. return getArgumentsAsString(null);
  77. }
  78. /**
  79. * Retrieves a subset of the arguments not associated with any flags,
  80. * starting at the specified offset.
  81. *
  82. * @see CommandArguments#getArgumentsAsString(int)
  83. * @param offset The offset of the argument to start at
  84. * @return All arguments that aren't associated with a flag, as a string,
  85. * starting with the argument at the specified offset
  86. */
  87. public String getArgumentsAsString(final int offset) {
  88. return arguments.getArgumentsAsString(offsets.get(null) + offset);
  89. }
  90. /**
  91. * Retrieves the arguments passed with the specified flag as an array.
  92. *
  93. * @param flag The flag to be checked
  94. * @return The arguments passed with the specified flag
  95. * @see #getArgumentsAsString(com.dmdirc.commandparser.commands.flags.CommandFlag)
  96. */
  97. public String[] getArguments(final CommandFlag flag) {
  98. final String[] args = arguments.getArguments();
  99. final int end = flag == null ? args.length
  100. : offsets.get(flag) + flag.getDelayedArgs() + flag.getImmediateArgs();
  101. final int offset = offsets.get(flag);
  102. final int size = end - offset;
  103. final String[] result = new String[size];
  104. System.arraycopy(args, offset, result, 0, size);
  105. return result;
  106. }
  107. /**
  108. * Retrieves all arguments not associated with any flags.
  109. *
  110. * @see CommandArguments#getArguments()
  111. * @see #getArgumentsAsString()
  112. * @return All arguments that aren't associated with a flag
  113. */
  114. public String[] getArguments() {
  115. return getArguments(null);
  116. }
  117. }