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

CallbackManager.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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.parser.irc.callbacks;
  23. import com.dmdirc.parser.irc.IRCParser;
  24. import com.dmdirc.parser.irc.callbacks.interfaces.*;
  25. import java.util.Hashtable;
  26. import java.util.Map;
  27. /**
  28. * IRC Parser Callback Manager.
  29. * Manages adding/removing/calling callbacks.
  30. *
  31. * @author Shane Mc Cormack
  32. */
  33. public final class CallbackManager {
  34. private static final Class[] CLASSES = {
  35. IAwayState.class, IAwayStateOther.class, IChannelAwayStateOther.class,
  36. IChannelAction.class, IChannelCTCP.class, IChannelCTCPReply.class,
  37. IChannelGotListModes.class, IChannelGotNames.class, IChannelJoin.class,
  38. IChannelKick.class, IChannelMessage.class, IChannelModeChanged.class,
  39. IChannelNickChanged.class, IChannelNonUserModeChanged.class,
  40. IChannelNotice.class, IChannelPart.class, IChannelQuit.class,
  41. IChannelSelfJoin.class, IChannelSingleModeChanged.class,
  42. IChannelTopic.class, IChannelUserModeChanged.class, IConnectError.class,
  43. IDataIn.class, IDataOut.class, IDebugInfo.class, IErrorInfo.class,
  44. IGotNetwork.class, IInvite.class, IMOTDEnd.class, IMOTDLine.class,
  45. IMOTDStart.class, INickChanged.class, INickInUse.class,
  46. INoticeAuth.class, INumeric.class, IPasswordRequired.class,
  47. IPingFailed.class, IPingSuccess.class, IPingSent.class, IPrivateAction.class,
  48. IPrivateCTCP.class, IPrivateCTCPReply.class, IPrivateMessage.class,
  49. IPrivateNotice.class, IPost005.class, IQuit.class, IServerError.class,
  50. IServerReady.class, ISocketClosed.class, IUnknownAction.class,
  51. IUnknownCTCP.class, IUnknownCTCPReply.class, IUnknownMessage.class,
  52. IUnknownNotice.class, IUserModeChanged.class, IUserModeDiscovered.class,
  53. IWallDesync.class, IWallop.class, IWalluser.class,
  54. };
  55. /** Reference to the parser object that owns this CallbackManager. */
  56. IRCParser myParser;
  57. /** Hashtable used to store the different types of callback known. */
  58. private final Map<String, CallbackObject> callbackHash = new Hashtable<String, CallbackObject>();
  59. /**
  60. * Constructor to create a CallbackManager.
  61. *
  62. * @param parser IRCParser that owns this callback manager.
  63. */
  64. public CallbackManager(final IRCParser parser) {
  65. myParser = parser;
  66. for (Class<?> type : CLASSES) {
  67. if (type.isAnnotationPresent(SpecificCallback.class)) {
  68. addCallbackType(new CallbackObjectSpecific(myParser, this,
  69. type.asSubclass(ICallbackInterface.class)));
  70. } else {
  71. addCallbackType(new CallbackObject(myParser, this,
  72. type.asSubclass(ICallbackInterface.class)));
  73. }
  74. }
  75. }
  76. /**
  77. * Add new callback type.
  78. *
  79. * @param callback CallbackObject subclass for the callback.
  80. * @return if adding succeeded or not.
  81. */
  82. public boolean addCallbackType(final CallbackObject callback) {
  83. if (!callbackHash.containsKey(callback.getLowerName())) {
  84. callbackHash.put(callback.getLowerName(), callback);
  85. return true;
  86. }
  87. return false;
  88. }
  89. /**
  90. * Remove a callback type.
  91. *
  92. * @param callback CallbackObject subclass to remove.
  93. * @return if removal succeeded or not.
  94. */
  95. public boolean delCallbackType(final CallbackObject callback) {
  96. if (callbackHash.containsKey(callback.getLowerName())) {
  97. callbackHash.remove(callback.getLowerName());
  98. return true;
  99. }
  100. return false;
  101. }
  102. /**
  103. * Get reference to callback object.
  104. *
  105. * @param callbackName Name of callback object.
  106. * @return CallbackObject returns the callback object for this type
  107. */
  108. public CallbackObject getCallbackType(final String callbackName) {
  109. if (!callbackHash.containsKey(callbackName.toLowerCase())) {
  110. throw new CallbackNotFoundException("Callback not found: " + callbackName);
  111. }
  112. return callbackHash.get(callbackName.toLowerCase());
  113. }
  114. /**
  115. * Remove all callbacks associated with a specific object.
  116. *
  117. * @param o instance of ICallbackInterface to remove.
  118. */
  119. public void delAllCallback(final ICallbackInterface o) {
  120. for (CallbackObject cb : callbackHash.values()) {
  121. if (cb != null) { cb.del(o); }
  122. }
  123. }
  124. /**
  125. * Add all callbacks.
  126. *
  127. * @param o instance of ICallbackInterface to add.
  128. */
  129. public void addAllCallback(final ICallbackInterface o) {
  130. for (CallbackObject cb : callbackHash.values()) {
  131. if (cb != null) { cb.add(o); }
  132. }
  133. }
  134. /**
  135. * Add a callback.
  136. * This method will throw a CallbackNotFoundException if the callback does not exist.
  137. *
  138. * @param callbackName Name of callback object.
  139. * @param o instance of ICallbackInterface to add.
  140. * @throws CallbackNotFoundException If callback is not found.
  141. * @throws NullPointerException If 'o' is null
  142. */
  143. public void addCallback(final String callbackName, final ICallbackInterface o) throws CallbackNotFoundException {
  144. if (o == null) {
  145. throw new NullPointerException("CallbackInterface is null");
  146. }
  147. final CallbackObject cb = getCallbackType(callbackName);
  148. if (cb != null) { cb.add(o); }
  149. }
  150. /**
  151. * Add a callback with a specific target.
  152. * This method will throw a CallbackNotFoundException if the callback does not exist.
  153. *
  154. * @param callbackName Name of callback object.
  155. * @param o instance of ICallbackInterface to add.
  156. * @param target Parameter to specify that a callback should only fire for specific things
  157. * @throws CallbackNotFoundException If callback is not found.
  158. * @throws NullPointerException If 'o' is null
  159. */
  160. public void addCallback(final String callbackName, final ICallbackInterface o, final String target) throws CallbackNotFoundException {
  161. if (o == null) { throw new NullPointerException("CallbackInterface is null"); }
  162. ((CallbackObjectSpecific) getCallbackType(callbackName)).add(o,target);
  163. }
  164. /**
  165. * Add a callback without an exception.
  166. * This should be used if a callback is not essential for execution (ie the DebugOut callback)
  167. *
  168. * @param callbackName Name of callback object.
  169. * @param o instance of ICallbackInterface to add.
  170. * @return true/false if the callback was added or not.
  171. */
  172. public boolean addNonCriticalCallback(final String callbackName, final ICallbackInterface o) {
  173. try {
  174. addCallback(callbackName, o);
  175. return true;
  176. } catch (CallbackNotFoundException e) { return false; }
  177. }
  178. /**
  179. * Add a callback with a specific target.
  180. * This should be used if a callback is not essential for execution
  181. *
  182. * @param callbackName Name of callback object.
  183. * @param o instance of ICallbackInterface to add.
  184. * @param target Parameter to specify that a callback should only fire for specific things
  185. * @return true/false if the callback was added or not.
  186. */
  187. public boolean addNonCriticalCallback(final String callbackName, final ICallbackInterface o, final String target) {
  188. try {
  189. addCallback(callbackName, o, target);
  190. return true;
  191. } catch (CallbackNotFoundException e) { return false; }
  192. }
  193. /**
  194. * Remove a callback.
  195. *
  196. * @param callbackName Name of callback object.
  197. * @param o instance of ICallbackInterface to remove.
  198. */
  199. public void delCallback(final String callbackName, final ICallbackInterface o) {
  200. getCallbackType(callbackName).del(o);
  201. }
  202. }