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.

CallbackManager.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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.common;
  23. import com.dmdirc.parser.interfaces.Parser;
  24. import com.dmdirc.parser.interfaces.SpecificCallback;
  25. import com.dmdirc.parser.interfaces.callbacks.*;
  26. import java.util.Hashtable;
  27. import java.util.Map;
  28. /**
  29. * IRC Parser Callback Manager.
  30. * Manages adding/removing/calling callbacks.
  31. *
  32. * @param <T> The type of parser which this manager managers callbacks for
  33. * @author Shane Mc Cormack
  34. */
  35. public abstract class CallbackManager<T extends Parser> {
  36. protected static final Class[] CLASSES = {
  37. AwayStateListener.class, OtherAwayStateListener.class,
  38. ChannelOtherAwayStateListener.class, ChannelActionListener.class,
  39. ChannelCtcpListener.class, ChannelCtcpReplyListener.class,
  40. ChannelListModeListener.class, ChannelNamesListener.class,
  41. ChannelJoinListener.class, ChannelKickListener.class,
  42. ChannelMessageListener.class, ChannelModeChangeListener.class,
  43. ChannelNickChangeListener.class, ChannelNonUserModeChangeListener.class,
  44. ChannelModeMessageListener.class, ChannelModeNoticeListener.class,
  45. ChannelNoticeListener.class, ChannelPartListener.class, ChannelQuitListener.class,
  46. ChannelSelfJoinListener.class, ChannelSingleModeChangeListener.class,
  47. ChannelTopicListener.class, ChannelUserModeChangeListener.class,
  48. ConnectErrorListener.class, DataInListener.class, DataOutListener.class,
  49. DebugInfoListener.class, ErrorInfoListener.class,
  50. NetworkDetectedListener.class, InviteListener.class,
  51. MotdEndListener.class, MotdLineListener.class, MotdStartListener.class,
  52. NickChangeListener.class, NickInUseListener.class,
  53. AuthNoticeListener.class, NumericListener.class,
  54. PasswordRequiredListener.class, PingFailureListener.class,
  55. PingSuccessListener.class, PingSentListener.class,
  56. PrivateActionListener.class, PrivateCtcpListener.class,
  57. PrivateCtcpReplyListener.class, PrivateMessageListener.class,
  58. PrivateNoticeListener.class, Post005Listener.class, QuitListener.class,
  59. ServerErrorListener.class, ServerReadyListener.class,
  60. SocketCloseListener.class, UnknownActionListener.class,
  61. UnknownCtcpListener.class, UnknownCtcpReplyListener.class,
  62. UnknownMessageListener.class, UnknownNoticeListener.class,
  63. UserModeChangeListener.class, UserModeDiscoveryListener.class,
  64. WallDesyncListener.class, WallopListener.class, WalluserListener.class,
  65. };
  66. /** Hashtable used to store the different types of callback known. */
  67. private final Map<Class<? extends CallbackInterface>, CallbackObject> callbackHash
  68. = new Hashtable<Class<? extends CallbackInterface>, CallbackObject>();
  69. /**
  70. * Constructor to create a CallbackManager.
  71. *
  72. * @param parser Parser that owns this callback manager.
  73. */
  74. public CallbackManager(final T parser) {
  75. initialise(parser);
  76. }
  77. /**
  78. * Initialises this callback manager.
  79. *
  80. * @param parser The parser associated with this CallbackManager
  81. */
  82. protected void initialise(T parser) {
  83. for (Class<?> type : CLASSES) {
  84. if (type.isAnnotationPresent(SpecificCallback.class)) {
  85. addCallbackType(getSpecificCallbackObject(parser, type));
  86. } else {
  87. addCallbackType(getCallbackObject(parser, type));
  88. }
  89. }
  90. }
  91. /**
  92. * Retrieves a relevant {@link CallbackObject} for the specified type.
  93. *
  94. * @param parser The parser that this manager belongs to
  95. * @param type The type of callback to create an object for
  96. * @return The relevant CallbackObject
  97. */
  98. protected abstract CallbackObject getCallbackObject(T parser, Class<?> type);
  99. /**
  100. * Retrieves a relevant {@link CallbackObjectSpecific} for the specified type.
  101. *
  102. * @param parser The parser that this manager belongs to
  103. * @param type The type of callback to create an object for
  104. * @return The relevant CallbackObject
  105. */
  106. protected abstract CallbackObjectSpecific getSpecificCallbackObject(T parser, Class<?> type);
  107. /**
  108. * Add new callback type.
  109. *
  110. * @param callback CallbackObject subclass for the callback.
  111. * @return if adding succeeded or not.
  112. */
  113. public boolean addCallbackType(final CallbackObject callback) {
  114. if (!callbackHash.containsKey(callback.getType())) {
  115. callbackHash.put(callback.getType(), callback);
  116. return true;
  117. }
  118. return false;
  119. }
  120. /**
  121. * Remove a callback type.
  122. *
  123. * @param callback CallbackObject subclass to remove.
  124. * @return if removal succeeded or not.
  125. */
  126. public boolean delCallbackType(final CallbackObject callback) {
  127. if (callbackHash.containsKey(callback.getType())) {
  128. callbackHash.remove(callback.getType());
  129. return true;
  130. }
  131. return false;
  132. }
  133. /**
  134. * Get reference to callback object.
  135. *
  136. * @param callback Name of type of callback object.
  137. * @return CallbackObject returns the callback object for this type
  138. */
  139. public CallbackObject getCallbackType(final Class<? extends CallbackInterface> callback) {
  140. if (!callbackHash.containsKey(callback)) {
  141. throw new CallbackNotFoundException("Callback not found: " + callback.getName());
  142. }
  143. return callbackHash.get(callback);
  144. }
  145. /**
  146. * Remove all callbacks associated with a specific object.
  147. *
  148. * @param o instance of ICallbackInterface to remove.
  149. */
  150. public void delAllCallback(final CallbackInterface o) {
  151. for (CallbackObject cb : callbackHash.values()) {
  152. if (cb != null) {
  153. cb.del(o);
  154. }
  155. }
  156. }
  157. /**
  158. * Add all callbacks.
  159. *
  160. * @param o instance of ICallbackInterface to add.
  161. */
  162. public void addAllCallback(final CallbackInterface o) {
  163. for (CallbackObject cb : callbackHash.values()) {
  164. if (cb != null) {
  165. cb.add(o);
  166. }
  167. }
  168. }
  169. /**
  170. * Add a callback.
  171. * This method will throw a CallbackNotFoundException if the callback does not exist.
  172. *
  173. * @param <T> The type of callback
  174. * @param callback Type of callback object
  175. * @param o instance of ICallbackInterface to add.
  176. * @throws CallbackNotFoundException If callback is not found.
  177. * @throws NullPointerException If 'o' is null
  178. */
  179. public <T extends CallbackInterface> void addCallback(
  180. final Class<T> callback, final T o) throws CallbackNotFoundException {
  181. if (o == null) {
  182. throw new NullPointerException("CallbackInterface is null");
  183. }
  184. final CallbackObject cb = getCallbackType(callback);
  185. if (cb != null) {
  186. cb.add(o);
  187. }
  188. }
  189. /**
  190. * Add a callback with a specific target.
  191. * This method will throw a CallbackNotFoundException if the callback does not exist.
  192. *
  193. * @param <T> The type of callback
  194. * @param callback Type of callback object.
  195. * @param o instance of ICallbackInterface to add.
  196. * @param target Parameter to specify that a callback should only fire for specific things
  197. * @throws CallbackNotFoundException If callback is not found.
  198. * @throws NullPointerException If 'o' is null
  199. */
  200. public <T extends CallbackInterface> void addCallback(
  201. final Class<T> callback,
  202. final T o, final String target) throws CallbackNotFoundException {
  203. if (o == null) {
  204. throw new NullPointerException("CallbackInterface is null");
  205. }
  206. ((CallbackObjectSpecific) getCallbackType(callback)).add(o, target);
  207. }
  208. /**
  209. * Add a callback without an exception.
  210. * This should be used if a callback is not essential for execution (ie the DebugOut callback)
  211. *
  212. * @param <T> The type of callback object
  213. * @param callback Type of callback object.
  214. * @param o instance of ICallbackInterface to add.
  215. * @return true/false if the callback was added or not.
  216. */
  217. public <T extends CallbackInterface> boolean addNonCriticalCallback(
  218. final Class<T> callback, final T o) {
  219. try {
  220. addCallback(callback, o);
  221. return true;
  222. } catch (CallbackNotFoundException e) {
  223. return false;
  224. }
  225. }
  226. /**
  227. * Add a callback with a specific target.
  228. * This should be used if a callback is not essential for execution
  229. *
  230. * @param <T> The type of callback
  231. * @param callback Type of callback object.
  232. * @param o instance of ICallbackInterface to add.
  233. * @param target Parameter to specify that a callback should only fire for specific things
  234. * @return true/false if the callback was added or not.
  235. */
  236. public <T extends CallbackInterface> boolean addNonCriticalCallback(
  237. final Class<T> callback, final T o, final String target) {
  238. try {
  239. addCallback(callback, o, target);
  240. return true;
  241. } catch (CallbackNotFoundException e) {
  242. return false;
  243. }
  244. }
  245. /**
  246. * Remove a callback.
  247. *
  248. * @param callback Type of callback object.
  249. * @param o instance of ICallbackInterface to remove.
  250. */
  251. public void delCallback(final Class<? extends CallbackInterface> callback,
  252. final CallbackInterface o) {
  253. getCallbackType(callback).del(o);
  254. }
  255. }