Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

InviteManagerImpl.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.events.ServerInviteExpiredEvent;
  19. import com.dmdirc.interfaces.Connection;
  20. import com.dmdirc.interfaces.InviteManager;
  21. import com.dmdirc.parser.common.ChannelJoinRequest;
  22. import java.util.ArrayList;
  23. import java.util.Collections;
  24. import java.util.List;
  25. import java.util.concurrent.CopyOnWriteArrayList;
  26. /**
  27. * Manages invites on a {@link Connection}.
  28. */
  29. public class InviteManagerImpl implements InviteManager {
  30. /** A list of outstanding invites. */
  31. private final List<Invite> invites = new CopyOnWriteArrayList<>();
  32. /** The connection this manager works on. */
  33. private final Connection connection;
  34. public InviteManagerImpl(final Connection connection) {
  35. this.connection = connection;
  36. }
  37. @Override
  38. public void addInvite(final Invite invite) {
  39. invites.stream()
  40. .filter(oldInvite -> oldInvite.getChannel().equals(invite.getChannel()))
  41. .forEach(this::removeInvite);
  42. invites.add(invite);
  43. }
  44. @Override
  45. public void acceptInvites(final Invite... invites) {
  46. final ChannelJoinRequest[] requests = new ChannelJoinRequest[invites.length];
  47. for (int i = 0; i < invites.length; i++) {
  48. requests[i] = new ChannelJoinRequest(invites[i].getChannel());
  49. }
  50. connection.getGroupChatManager().join(requests);
  51. }
  52. @Override
  53. public void acceptInvites() {
  54. acceptInvites(invites.toArray(new Invite[invites.size()]));
  55. }
  56. @Override
  57. public void removeInvites(final String channel) {
  58. new ArrayList<>(invites).stream().filter(invite -> invite.getChannel().equals(channel))
  59. .forEach(this::removeInvite);
  60. }
  61. @Override
  62. public void removeInvites() {
  63. new ArrayList<>(invites).forEach(this::removeInvite);
  64. }
  65. @Override
  66. public void removeInvite(final Invite invite) {
  67. invites.remove(invite);
  68. connection.getWindowModel().getEventBus().publishAsync(
  69. new ServerInviteExpiredEvent(connection, invite));
  70. }
  71. @Override
  72. public List<Invite> getInvites() {
  73. return Collections.unmodifiableList(invites);
  74. }
  75. }