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.

Alias.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (c) 2006-2010 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.actions.wrappers;
  23. import com.dmdirc.actions.Action;
  24. import com.dmdirc.actions.ActionCondition;
  25. import com.dmdirc.actions.interfaces.ActionType;
  26. import com.dmdirc.actions.CoreActionComparison;
  27. import com.dmdirc.actions.CoreActionComponent;
  28. import com.dmdirc.actions.CoreActionType;
  29. import java.io.Serializable;
  30. import java.util.ArrayList;
  31. import java.util.Arrays;
  32. import java.util.List;
  33. /**
  34. * Actions alias wrapper.
  35. */
  36. public final class Alias implements Serializable {
  37. /**
  38. * A version number for this class. It should be changed whenever the class
  39. * structure is changed (or anything else that would prevent serialized
  40. * objects being unserialized with the new class).
  41. */
  42. private static final long serialVersionUID = 1;
  43. /** Alias command. */
  44. private String command;
  45. /** Alias arguments. */
  46. private List<ActionCondition> arguments;
  47. /** Alias response. */
  48. private String[] response;
  49. /**
  50. * Creates a new Alias wrapper.
  51. *
  52. * @param command Alias command
  53. */
  54. public Alias(final String command) {
  55. this.command = command;
  56. this.arguments = new ArrayList<ActionCondition>();
  57. this.arguments.add(new ActionCondition(1, CoreActionComponent.STRING_STRING,
  58. CoreActionComparison.STRING_EQUALS, command));
  59. this.response = new String[]{"", };
  60. }
  61. /**
  62. * Wraps an existing Action in an Alias.
  63. *
  64. * @param command Alias command
  65. * @param arguments List of arguments for the alias
  66. * @param response Response for the alias
  67. */
  68. public Alias(final String command, final List<ActionCondition> arguments,
  69. final String[] response) {
  70. this.command = command;
  71. this.arguments = new ArrayList<ActionCondition>(arguments);
  72. this.response = response.clone();
  73. }
  74. /**
  75. * Returns the aliases command.
  76. *
  77. * @return Aliases command
  78. */
  79. public String getCommand() {
  80. return command;
  81. }
  82. /**
  83. * Sets the aliases command.
  84. *
  85. * @param command Command to give the alias
  86. */
  87. public void setCommand(final String command) {
  88. if (!this.command.equals(command)) {
  89. this.command = command;
  90. ActionCondition argument;
  91. argument = arguments.get(0);
  92. if (argument.getComparison() != CoreActionComparison.STRING_EQUALS) {
  93. argument = arguments.get(1);
  94. }
  95. argument.setTarget(command);
  96. }
  97. }
  98. /**
  99. * Returns the aliases name.
  100. *
  101. * @return Aliases name
  102. */
  103. public String getName() {
  104. final ActionCondition condition = getArgsArgument();
  105. if (condition == null) {
  106. return command + "-Any";
  107. } else {
  108. final String comparison;
  109. if (condition.getComparison().equals(CoreActionComparison.INT_EQUALS)) {
  110. comparison = "equals";
  111. } else if (condition.getComparison().equals(CoreActionComparison.INT_GREATER)) {
  112. comparison = "greater";
  113. } else if (condition.getComparison().equals(CoreActionComparison.INT_LESS)) {
  114. comparison = "less";
  115. } else {
  116. comparison = condition.getComparison().toString();
  117. }
  118. return command + "-" + comparison + "-" + condition.getTarget();
  119. }
  120. }
  121. /**
  122. * Gets the aliases arguments.
  123. *
  124. * @return Argument list
  125. */
  126. public List<ActionCondition> getArguments() {
  127. return new ArrayList<ActionCondition>(arguments);
  128. }
  129. /**
  130. * Gets the aliases number of arguments argument.
  131. *
  132. * @return Number of arguments ActionCondition or null
  133. */
  134. public ActionCondition getArgsArgument() {
  135. ActionCondition argument;
  136. argument = arguments.get(0);
  137. if (argument.getComparison() == CoreActionComparison.STRING_EQUALS) {
  138. if (arguments.size() > 1) {
  139. argument = arguments.get(1);
  140. } else {
  141. argument = null;
  142. }
  143. }
  144. return argument;
  145. }
  146. /**
  147. * Sets the aliases arguments.
  148. *
  149. * @param arguments A new list of arguments to set
  150. */
  151. public void setArguments(final List<ActionCondition> arguments) {
  152. if (!this.arguments.equals(arguments)) {
  153. this.arguments = new ArrayList<ActionCondition>(arguments);
  154. }
  155. }
  156. /**
  157. * Gets the aliases response.
  158. *
  159. * @return Response
  160. */
  161. public String[] getResponse() {
  162. return response.clone();
  163. }
  164. /**
  165. * Sets the aliases response.
  166. *
  167. * @param response New Response
  168. */
  169. public void setResponse(final String[] response) {
  170. if (!Arrays.equals(this.response, response)) {
  171. this.response = response.clone();
  172. }
  173. }
  174. /**
  175. * Updates this alias with the details of another alias.
  176. *
  177. * @param alias Alias to retrieve details from
  178. */
  179. public void update(final Alias alias) {
  180. setArguments(alias.getArguments());
  181. setCommand(alias.getCommand());
  182. setResponse(alias.getResponse());
  183. }
  184. /**
  185. * Checks if the specified alias matches this one
  186. *
  187. * @param alias Alias to check a match with
  188. *
  189. * @return true iif the alias matches this one
  190. */
  191. public boolean matches(final Alias alias) {
  192. return alias.getCommand().equalsIgnoreCase(command)
  193. && alias.getArguments().equals(arguments);
  194. }
  195. /**
  196. * Creates an action corresponding to this alias.
  197. *
  198. * @return A new action for this alias.
  199. */
  200. public Action createAction() {
  201. return new Action(
  202. AliasWrapper.getAliasWrapper().getName(),
  203. getName(),
  204. new ActionType[] {CoreActionType.UNKNOWN_COMMAND, },
  205. getResponse(),
  206. getArguments(),
  207. "");
  208. }
  209. /** {@inheritDoc} */
  210. @Override
  211. public String toString() {
  212. return "[name=aliases/" + getName() + ", triggers="
  213. + "[UNKNOWN_COMMAND], response="
  214. + Arrays.toString(response) + ", "
  215. + arguments + ", format='']";
  216. }
  217. }