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.

AutoCommandManager.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2006-2014 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.commandparser.auto;
  23. import java.util.Collections;
  24. import java.util.Map;
  25. import java.util.Set;
  26. import java.util.concurrent.ConcurrentSkipListMap;
  27. import javax.inject.Inject;
  28. import javax.inject.Singleton;
  29. import com.dmdirc.DMDircMBassador;
  30. import static com.google.common.base.Preconditions.checkNotNull;
  31. /**
  32. * Manages {@link AutoCommand}s.
  33. */
  34. @Singleton
  35. public class AutoCommandManager {
  36. /** The bus to listen for events on. */
  37. private final DMDircMBassador eventBus;
  38. /** The factory to use to create handlers. */
  39. private final AutoCommandHandlerFactory factory;
  40. /** Known auto commands, mapped on to their handlers. */
  41. private final Map<AutoCommand, AutoCommandHandler> autoCommands = new ConcurrentSkipListMap<>();
  42. /** Whether the manager has been started or not. */
  43. private boolean started;
  44. /**
  45. * Creates a new instance of {@link AutoCommandManager}.
  46. *
  47. * @param eventBus The bus to listen to events on.
  48. * @param factory The factory to use to create handlers.
  49. */
  50. @Inject
  51. public AutoCommandManager(final DMDircMBassador eventBus, final AutoCommandHandlerFactory factory) {
  52. this.eventBus = eventBus;
  53. this.factory = factory;
  54. }
  55. /**
  56. * Starts handling events and triggering auto commands.
  57. */
  58. public void start() {
  59. started = true;
  60. for (AutoCommandHandler handler : autoCommands.values()) {
  61. eventBus.subscribe(handler);
  62. }
  63. }
  64. /**
  65. * Stops handling events and triggering auto commands.
  66. */
  67. public void stop() {
  68. started = false;
  69. for (AutoCommandHandler handler : autoCommands.values()) {
  70. eventBus.subscribe(handler);
  71. }
  72. }
  73. /**
  74. * Adds an auto command to this manager.
  75. *
  76. * @param autoCommand The command to be added.
  77. */
  78. public void addAutoCommand(final AutoCommand autoCommand) {
  79. checkNotNull(autoCommand);
  80. final AutoCommandHandler handler = factory.getAutoCommandHandler(autoCommand);
  81. if (started) {
  82. eventBus.subscribe(handler);
  83. }
  84. autoCommands.put(autoCommand, handler);
  85. }
  86. /**
  87. * Removes an existing auto command from this manager.
  88. *
  89. * @param autoCommand The command to be removed.
  90. */
  91. public void removeAutoCommand(final AutoCommand autoCommand) {
  92. checkNotNull(autoCommand);
  93. final AutoCommandHandler handler = autoCommands.remove(autoCommand);
  94. if (started) {
  95. eventBus.subscribe(handler);
  96. }
  97. }
  98. /**
  99. * Gets a set of all registered auto commands.
  100. *
  101. * @return The set of all known auto commands.
  102. */
  103. public Set<AutoCommand> getAutoCommands() {
  104. return Collections.unmodifiableSet(autoCommands.keySet());
  105. }
  106. }