您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CommandType.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2006-2009 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 com.dmdirc.commandparser;
  23. import com.dmdirc.commandparser.commands.ChannelCommand;
  24. import com.dmdirc.commandparser.commands.ChatCommand;
  25. import com.dmdirc.commandparser.commands.Command;
  26. import com.dmdirc.commandparser.commands.GlobalCommand;
  27. import com.dmdirc.commandparser.commands.QueryCommand;
  28. import com.dmdirc.commandparser.commands.ServerCommand;
  29. /**
  30. * Defines the possible targets for commands.
  31. *
  32. * @author chris
  33. */
  34. public enum CommandType {
  35. /** A global command, which may be executed anywhere. */
  36. TYPE_GLOBAL,
  37. /** A server command, which only makes sense in the context of a connection. */
  38. TYPE_SERVER,
  39. /** A chat command, which needs a MessageTarget to make sense. */
  40. TYPE_CHAT,
  41. /** A channel command. */
  42. TYPE_CHANNEL,
  43. /** A query command. */
  44. TYPE_QUERY;
  45. /**
  46. * Looks up the command type for the specified command, by inspecting its
  47. * class.
  48. *
  49. * @param command The command to look up
  50. * @return The type of the specified command
  51. */
  52. public static CommandType fromCommand(final Command command) {
  53. if (command instanceof GlobalCommand) {
  54. return TYPE_GLOBAL;
  55. } else if (command instanceof ServerCommand) {
  56. return TYPE_SERVER;
  57. } else if (command instanceof ChatCommand) {
  58. return TYPE_CHAT;
  59. } else if (command instanceof ChannelCommand) {
  60. return TYPE_CHANNEL;
  61. } else if (command instanceof QueryCommand) {
  62. return TYPE_QUERY;
  63. } else {
  64. return null;
  65. }
  66. }
  67. /**
  68. * Retrieves an array of component types that make up this command type.
  69. * Generally this will only contain the type itself, but some commands may
  70. * be registered in multiple queues (such as CHANNEL commands going into
  71. * both CHAT and CHANNEL queues). Note that for obvious reasons there is
  72. * no recursion done on the values returned here.
  73. *
  74. * @since 0.6.3m1
  75. * @return An array of types which this type should be registered as.
  76. */
  77. public CommandType[] getComponentTypes() {
  78. if (this == TYPE_CHANNEL || this == TYPE_QUERY) {
  79. return new CommandType[]{this, TYPE_CHAT};
  80. } else {
  81. return new CommandType[]{this};
  82. }
  83. }
  84. /** {@inheritDoc} */
  85. @Override
  86. public String toString() {
  87. switch (this) {
  88. case TYPE_CHANNEL:
  89. return "Channel";
  90. case TYPE_CHAT:
  91. return "Chat";
  92. case TYPE_GLOBAL:
  93. return "Global";
  94. case TYPE_QUERY:
  95. return "Query";
  96. case TYPE_SERVER:
  97. return "Server";
  98. default:
  99. return "Unknown";
  100. }
  101. }
  102. }