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.

ChannelMap.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2006-2015 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;
  23. import com.dmdirc.parser.common.ChannelJoinRequest;
  24. import com.dmdirc.parser.common.DefaultStringConverter;
  25. import com.dmdirc.parser.interfaces.StringConverter;
  26. import java.util.Collection;
  27. import java.util.Collections;
  28. import java.util.Date;
  29. import java.util.Map;
  30. import java.util.Optional;
  31. import java.util.concurrent.ConcurrentSkipListMap;
  32. import java.util.stream.Collectors;
  33. import static com.google.common.base.Preconditions.checkNotNull;
  34. /**
  35. * Utility class that maintains a map of channel names to channels.
  36. */
  37. public class ChannelMap {
  38. /** Map of lowercased channel names to channel objects. */
  39. private final Map<String, Channel> channels = new ConcurrentSkipListMap<>();
  40. /** String converter to use to lowercase names. */
  41. private StringConverter converter = new DefaultStringConverter();
  42. /**
  43. * Sets the string converter that will be used for any future case conversions. If the map is
  44. * non-empty and the converter is changed, duplicate entries may be created.
  45. *
  46. * @param converter The new converter to use.
  47. */
  48. public void setStringConverter(final StringConverter converter) {
  49. this.converter = checkNotNull(converter);
  50. }
  51. /**
  52. * Gets the specified channel, if it exists.
  53. *
  54. * @param channel The channel to retrieve.
  55. *
  56. * @return An optional channel.
  57. */
  58. public Optional<Channel> get(final String channel) {
  59. return Optional.ofNullable(channels.get(converter.toLowerCase(channel)));
  60. }
  61. /**
  62. * Gets all known channels.
  63. *
  64. * @return A collection of all known channels.
  65. */
  66. public Collection<Channel> getAll() {
  67. return Collections.unmodifiableCollection(channels.values());
  68. }
  69. /**
  70. * Adds a channel to the map. If a channel with the same name (when lowercased) is present, it
  71. * will be replaced.
  72. *
  73. * @param channel The new channel to be added.
  74. */
  75. public void add(final Channel channel) {
  76. channels.put(converter.toLowerCase(channel.getName()), channel);
  77. }
  78. /**
  79. * Removes a channel from the map.
  80. *
  81. * @param channel The channel to be removed.
  82. */
  83. public void remove(final String channel) {
  84. channels.remove(converter.toLowerCase(channel));
  85. }
  86. /**
  87. * Closes all channels in the map.
  88. */
  89. public void closeAll() {
  90. channels.values().forEach(Channel::close);
  91. }
  92. /**
  93. * Resets the state of all channels in the map.
  94. */
  95. public void resetAll() {
  96. channels.values().forEach(Channel::resetWindow);
  97. }
  98. /**
  99. * Adds a line to all channels in the map.
  100. *
  101. * @param messageType The type of message to be added.
  102. * @param date The time of origin of the message.
  103. * @param args The arguments for the message.
  104. */
  105. public void addLineToAll(final String messageType, final Date date, final Object... args) {
  106. channels.values().forEach(c -> c.addLine(messageType, date, args));
  107. }
  108. /**
  109. * Gets a representation of the channels in this map as a collection of join requests.
  110. *
  111. * @return A collection of join requests corresponding to channels in this map.
  112. */
  113. public Collection<ChannelJoinRequest> asJoinRequests() {
  114. return channels.values().stream()
  115. .map(channel -> new ChannelJoinRequest(channel.getName()))
  116. .collect(Collectors.toList());
  117. }
  118. /**
  119. * Creates a runnable that, when run, will cause each channel in the map at that time to check
  120. * its 'who' status.
  121. *
  122. * @return A new runnable.
  123. */
  124. public Runnable getWhoRunnable() {
  125. return () -> channels.values().forEach(Channel::checkWho);
  126. }
  127. }