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.

Command.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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 uk.org.ownage.dmdirc.commandparser;
  23. /**
  24. * Represents a generic command.
  25. * @author chris
  26. */
  27. public abstract class Command implements Comparable<Command> {
  28. /**
  29. * Returns the signature of this command. For polyadic commands, the signature
  30. * is simply the name. For other commands, the signature is a concatenation of
  31. * the name, a literal "/", and the arity.
  32. * @return The signature of this command
  33. */
  34. public final String getSignature() {
  35. if (isPolyadic()) {
  36. return getName();
  37. } else {
  38. return getName() + "/" + getArity();
  39. }
  40. }
  41. /**
  42. * Returns this command's name.
  43. * @return The name of this command
  44. */
  45. public abstract String getName();
  46. /**
  47. * Returns whether or not this command should be shown in help messages.
  48. * @return True iff the command should be shown, false otherwise
  49. */
  50. public abstract boolean showInHelp();
  51. /**
  52. * Indicates whether this command is polyadic or not.
  53. * @return True iff this command is polyadic, false otherwise
  54. */
  55. public abstract boolean isPolyadic();
  56. /**
  57. * Returns the arity of this command
  58. * @return This command's arity
  59. */
  60. public abstract int getArity();
  61. /**
  62. * Returns a string representing the help message for this command.
  63. * @return the help message for this command
  64. */
  65. public abstract String getHelp();
  66. /**
  67. * Implodes the given list of arguments.
  68. * @param offset The index to start at
  69. * @param args The arguments to implode
  70. * @return A string containing each argument seperated by a space
  71. */
  72. protected final String implodeArgs(final int offset, final String... args) {
  73. String res = "";
  74. for (int i = offset; i < args.length; i++) {
  75. if (res.length() == 0) {
  76. res = args[i];
  77. } else {
  78. res = res.concat(" " + args[i]);
  79. }
  80. }
  81. return res;
  82. }
  83. /**
  84. * Implodes the given list of arguments.
  85. * @param args The arguments to implode
  86. * @return A string containing each argument seperated by a space
  87. */
  88. protected final String implodeArgs(final String... args) {
  89. return implodeArgs(0, args);
  90. }
  91. /**
  92. * Sends a line, if appropriate, to the specified target.
  93. * @param target The command window to send the line to
  94. * @param isSilent Whether this command is being silenced or not
  95. * @param type The type of message to send
  96. * @param args The arguments of the message
  97. */
  98. protected final void sendLine(final CommandWindow target,
  99. final boolean isSilent, final String type, final Object ... args) {
  100. if (!isSilent && target != null) {
  101. target.addLine(type, args);
  102. }
  103. }
  104. /** {@inheritDoc} */
  105. public int compareTo(final Command o) {
  106. return getSignature().compareTo(o.getSignature());
  107. }
  108. }