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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc;
  18. import com.dmdirc.interfaces.GroupChat.PartReason;
  19. import com.dmdirc.parser.common.ChannelJoinRequest;
  20. import com.dmdirc.parser.common.DefaultStringConverter;
  21. import com.dmdirc.parser.interfaces.StringConverter;
  22. import java.util.Collection;
  23. import java.util.Collections;
  24. import java.util.Map;
  25. import java.util.Optional;
  26. import java.util.concurrent.ConcurrentSkipListMap;
  27. import java.util.stream.Collectors;
  28. import static com.google.common.base.Preconditions.checkNotNull;
  29. /**
  30. * Utility class that maintains a map of channel names to channels.
  31. */
  32. public class ChannelMap {
  33. /** Map of lowercased channel names to channel objects. */
  34. private final Map<String, Channel> channels = new ConcurrentSkipListMap<>();
  35. /** String converter to use to lowercase names. */
  36. private StringConverter converter = new DefaultStringConverter();
  37. /**
  38. * Sets the string converter that will be used for any future case conversions. If the map is
  39. * non-empty and the converter is changed, duplicate entries may be created.
  40. *
  41. * @param converter The new converter to use.
  42. */
  43. public void setStringConverter(final StringConverter converter) {
  44. this.converter = checkNotNull(converter);
  45. }
  46. /**
  47. * Gets the specified channel, if it exists.
  48. *
  49. * @param channel The channel to retrieve.
  50. *
  51. * @return An optional channel.
  52. */
  53. public Optional<Channel> get(final String channel) {
  54. return Optional.ofNullable(channels.get(converter.toLowerCase(channel)));
  55. }
  56. /**
  57. * Gets all known channels.
  58. *
  59. * @return A collection of all known channels.
  60. */
  61. public Collection<Channel> getAll() {
  62. return Collections.unmodifiableCollection(channels.values());
  63. }
  64. /**
  65. * Adds a channel to the map. If a channel with the same name (when lowercased) is present, it
  66. * will be replaced.
  67. *
  68. * @param channel The new channel to be added.
  69. */
  70. public void add(final Channel channel) {
  71. channels.put(converter.toLowerCase(channel.getName()), channel);
  72. }
  73. /**
  74. * Removes a channel from the map.
  75. *
  76. * @param channel The channel to be removed.
  77. */
  78. public void remove(final String channel) {
  79. channels.remove(converter.toLowerCase(channel));
  80. }
  81. /**
  82. * Closes all channels in the map.
  83. */
  84. public void closeAll() {
  85. channels.values().forEach(Channel::close);
  86. }
  87. /**
  88. * Resets the state of all channels in the map.
  89. */
  90. public void resetAll() {
  91. channels.values().forEach(c -> c.resetWindow(PartReason.DISCONNECTED));
  92. }
  93. /**
  94. * Gets a representation of the channels in this map as a collection of join requests.
  95. * Channels that have been parted explicitly (rather than because of a disconnection) will not
  96. * be included.
  97. *
  98. * @return A collection of join requests corresponding to channels in this map that were left
  99. * due to a disconnection.
  100. */
  101. public Collection<ChannelJoinRequest> asJoinRequests() {
  102. return channels.values().stream()
  103. .filter(channel -> channel.getPartReason() == PartReason.DISCONNECTED)
  104. .map(channel -> new ChannelJoinRequest(channel.getName()))
  105. .collect(Collectors.toList());
  106. }
  107. }