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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2006-2017 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<>();
  37. /** The clients in this channel. */
  38. private final Map<String, ChannelClientInfo> clients = new HashMap<>();
  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. @Override
  46. @SuppressWarnings("ReturnOfCollectionOrArrayField")
  47. public Map<Object, Object> getMap() {
  48. return map;
  49. }
  50. @Override
  51. public String getName() {
  52. return name;
  53. }
  54. @Override
  55. public Parser getParser() {
  56. return parser;
  57. }
  58. @Override
  59. public String getPassword() {
  60. return "";
  61. }
  62. @Override
  63. public void sendMessage(final String message) {
  64. parser.sendMessage(name, message);
  65. }
  66. @Override
  67. public void sendAction(final String action) {
  68. parser.sendAction(name, action);
  69. }
  70. @Override
  71. public Collection<ChannelClientInfo> getChannelClients() {
  72. return clients.values();
  73. }
  74. @Override
  75. public int getChannelClientCount() {
  76. return clients.size();
  77. }
  78. @Override
  79. public ChannelClientInfo getChannelClient(final String client) {
  80. return getChannelClient(client, false);
  81. }
  82. /**
  83. * Adds a new client to this channel's user list.
  84. *
  85. * @param key The key to identify the client by
  86. * @param client The client to be added
  87. */
  88. protected void addClient(final String key, final ChannelClientInfo client) {
  89. clients.put(key, client);
  90. }
  91. /**
  92. * Removes an existing client from this channel's user list.
  93. *
  94. * @param key The key that the client is identified by
  95. */
  96. protected void removeClient(final String key) {
  97. clients.remove(key);
  98. }
  99. /**
  100. * Retrieves the client identified by the specified key.
  101. *
  102. * @param key The key identifying the client to be retrieved
  103. * @return The client corresponding to the give key
  104. */
  105. protected ChannelClientInfo getClient(final String key) {
  106. return clients.get(key);
  107. }
  108. }