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.

BaseParser.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2006-2011 DMDirc Developers
  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.callbacks.CallbackInterface;
  25. import java.net.URI;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. /**
  29. * Implements common base functionality for parsers.<p>
  30. * Implementations of this class must be annotated with
  31. * {@link ChildImplementations} to define the implementations to use for
  32. * instances of {@link ClientInfo}, {@link ChannelInfo}, etc.
  33. */
  34. public abstract class BaseParser implements Parser {
  35. /** The URI that this parser was constructed for. */
  36. private final URI uri;
  37. /** The ignore list for this parser. */
  38. private IgnoreList ignoreList;
  39. /** The ping timer interval for this parser. */
  40. private long pingTimerInterval;
  41. /** The ping timer fraction for this parser. */
  42. private int pingTimerFraction;
  43. /** The cached name of the server this parser is connected to. */
  44. private String serverName;
  45. /** The callback manager to use for this parser. */
  46. private final CallbackManager callbackManager;
  47. /** A map for callers to use to store things for no sane reason. */
  48. private final Map<Object, Object> map = new HashMap<Object, Object>();
  49. /**
  50. * Creates a new base parser for the specified URI.
  51. *
  52. * @param uri The URI this parser will connect to.
  53. */
  54. public BaseParser(final URI uri) {
  55. this.uri = uri;
  56. final Map<Class<?>, Class<?>> implementations
  57. = new HashMap<Class<?>, Class<?>>();
  58. for (Class<?> child : this.getClass().getAnnotation(ChildImplementations.class).value()) {
  59. for (Class<?> iface : child.getInterfaces()) {
  60. implementations.put(iface, child);
  61. }
  62. }
  63. this.callbackManager = new CallbackManager(this, implementations);
  64. }
  65. /** {@inheritDoc} */
  66. @Override
  67. public URI getURI() {
  68. return uri;
  69. }
  70. /** {@inheritDoc} */
  71. @Override
  72. public IgnoreList getIgnoreList() {
  73. return ignoreList;
  74. }
  75. /** {@inheritDoc} */
  76. @Override
  77. public void setIgnoreList(final IgnoreList ignoreList) {
  78. this.ignoreList = ignoreList;
  79. }
  80. /** {@inheritDoc} */
  81. @Override
  82. public long getPingTimerInterval() {
  83. return pingTimerInterval;
  84. }
  85. /** {@inheritDoc} */
  86. @Override
  87. public void setPingTimerInterval(final long newValue) {
  88. pingTimerInterval = newValue;
  89. }
  90. /** {@inheritDoc} */
  91. @Override
  92. public int getPingTimerFraction() {
  93. return pingTimerFraction;
  94. }
  95. /** {@inheritDoc} */
  96. @Override
  97. public void setPingTimerFraction(final int newValue) {
  98. pingTimerFraction = newValue;
  99. }
  100. /** {@inheritDoc} */
  101. @Override
  102. public CallbackManager getCallbackManager() {
  103. return callbackManager;
  104. }
  105. /**
  106. * Gets a proxy object which can be used to despatch callbacks of the
  107. * specified type. Callers may pass <code>null</code> for the first two
  108. * arguments of any callback, and these will automatically be replaced
  109. * by the relevant Parser instance and the current date.
  110. *
  111. * @param <T> The type of the callback to retrieve
  112. * @param callback The callback to retrieve
  113. * @return A proxy object which can be used to call the specified callback
  114. */
  115. protected <T extends CallbackInterface> T getCallback(final Class<T> callback) {
  116. return callbackManager.getCallback(callback);
  117. }
  118. /** {@inheritDoc} */
  119. @Override
  120. public Map<Object, Object> getMap() {
  121. return map;
  122. }
  123. /** {@inheritDoc} */
  124. @Override
  125. public void joinChannel(final String channel) {
  126. joinChannels(new ChannelJoinRequest(channel));
  127. }
  128. /** {@inheritDoc} */
  129. @Override
  130. public void joinChannel(final String channel, final String key) {
  131. joinChannels(new ChannelJoinRequest(channel, key));
  132. }
  133. /** {@inheritDoc} */
  134. @Override
  135. public String getServerName() {
  136. return serverName;
  137. }
  138. /**
  139. * Sets the name of this parser's server.
  140. *
  141. * @param serverName The new name for this parser's server
  142. */
  143. protected void setServerName(final String serverName) {
  144. this.serverName = serverName;
  145. }
  146. }