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.

BaseChannelInfo.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2006-2013 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.ChannelClientInfo;
  24. import com.dmdirc.parser.interfaces.ChannelInfo;
  25. import com.dmdirc.parser.interfaces.Parser;
  26. import java.util.Collection;
  27. import java.util.HashMap;
  28. import java.util.Map;
  29. /**
  30. * Provides a basic implementation of the {@link ChannelInfo} interface.
  31. */
  32. public abstract class BaseChannelInfo implements ChannelInfo {
  33. /** The parser that owns this client. */
  34. private final Parser parser;
  35. /** A map for random data associated with the client to be stored in. */
  36. private final Map<Object, Object> map = new HashMap<Object, Object>();
  37. /** The clients in this channel. */
  38. private final Map<String, ChannelClientInfo> clients = new HashMap<String, ChannelClientInfo>();
  39. /** The name of this channel. */
  40. private final String name;
  41. public BaseChannelInfo(final Parser parser, final String name) {
  42. this.parser = parser;
  43. this.name = name;
  44. }
  45. /** {@inheritDoc} */
  46. @Override
  47. public Map<Object, Object> getMap() {
  48. return map;
  49. }
  50. /** {@inheritDoc} */
  51. @Override
  52. public String getName() {
  53. return name;
  54. }
  55. /** {@inheritDoc} */
  56. @Override
  57. public Parser getParser() {
  58. return parser;
  59. }
  60. /** {@inheritDoc} */
  61. @Override
  62. public void sendMessage(final String message) {
  63. parser.sendMessage(name, message);
  64. }
  65. /** {@inheritDoc} */
  66. @Override
  67. public void sendAction(final String action) {
  68. parser.sendAction(name, action);
  69. }
  70. /** {@inheritDoc} */
  71. @Override
  72. public Collection<ChannelClientInfo> getChannelClients() {
  73. return clients.values();
  74. }
  75. /** {@inheritDoc} */
  76. @Override
  77. public int getChannelClientCount() {
  78. return clients.size();
  79. }
  80. /** {@inheritDoc} */
  81. @Override
  82. public ChannelClientInfo getChannelClient(final String client) {
  83. return getChannelClient(client, false);
  84. }
  85. /**
  86. * Adds a new client to this channel's user list.
  87. *
  88. * @param key The key to identify the client by
  89. * @param client The client to be added
  90. */
  91. protected void addClient(final String key, final ChannelClientInfo client) {
  92. clients.put(key, client);
  93. }
  94. /**
  95. * Removes an existing client from this channel's user list.
  96. *
  97. * @param key The key that the client is identified by
  98. */
  99. protected void removeClient(final String key) {
  100. clients.remove(key);
  101. }
  102. /**
  103. * Retrieves the client identified by the specified key.
  104. *
  105. * @param key The key identifying the client to be retrieved
  106. * @return The client corresponding to the give key
  107. */
  108. protected ChannelClientInfo getClient(final String key) {
  109. return clients.get(key);
  110. }
  111. }