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 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.commandparser.auto;
  23. import com.dmdirc.interfaces.EventBus;
  24. import java.util.Collections;
  25. import java.util.Map;
  26. import java.util.Optional;
  27. import java.util.Set;
  28. import java.util.concurrent.ConcurrentHashMap;
  29. import javax.inject.Inject;
  30. import javax.inject.Singleton;
  31. import static com.google.common.base.Preconditions.checkNotNull;
  32. /**
  33. * Manages {@link AutoCommand}s.
  34. */
  35. @Singleton
  36. public class AutoCommandManager {
  37. /** The bus to listen for events on. */
  38. private final EventBus eventBus;
  39. /** The factory to use to create handlers. */
  40. private final AutoCommandHandlerFactory factory;
  41. /** Known auto commands, mapped on to their handlers. */
  42. private final Map<AutoCommand, AutoCommandHandler> autoCommands = new ConcurrentHashMap<>();
  43. /** Whether the manager has been started or not. */
  44. private boolean started;
  45. /**
  46. * Creates a new instance of {@link AutoCommandManager}.
  47. *
  48. * @param eventBus The bus to listen to events on.
  49. * @param factory The factory to use to create handlers.
  50. */
  51. @Inject
  52. public AutoCommandManager(
  53. final EventBus eventBus,
  54. final AutoCommandHandlerFactory factory) {
  55. this.eventBus = eventBus;
  56. this.factory = factory;
  57. }
  58. /**
  59. * Starts handling events and triggering auto commands.
  60. */
  61. public void start() {
  62. started = true;
  63. autoCommands.values().forEach(eventBus::subscribe);
  64. }
  65. /**
  66. * Stops handling events and triggering auto commands.
  67. */
  68. public void stop() {
  69. started = false;
  70. autoCommands.values().forEach(eventBus::unsubscribe);
  71. }
  72. /**
  73. * Adds an auto command to this manager.
  74. *
  75. * <p>Only one auto command may exist for each combination of network, server and profile
  76. * targets. This method will throw an {@link IllegalStateException} if the given command is
  77. * a duplicate.
  78. *
  79. * @param autoCommand The command to be added.
  80. * @throws IllegalStateException If a command with the same target already exists.
  81. */
  82. public void addAutoCommand(final AutoCommand autoCommand) {
  83. checkNotNull(autoCommand);
  84. if (getAutoCommand(autoCommand.getNetwork(), autoCommand.getServer(),
  85. autoCommand.getProfile()).isPresent()) {
  86. throw new IllegalStateException("Only one AutoCommand may exist per " +
  87. "network/server/profile");
  88. }
  89. final AutoCommandHandler handler = factory.getAutoCommandHandler(autoCommand);
  90. if (started) {
  91. eventBus.subscribe(handler);
  92. }
  93. autoCommands.put(autoCommand, handler);
  94. }
  95. /**
  96. * Removes an existing auto command from this manager.
  97. *
  98. * @param autoCommand The command to be removed.
  99. */
  100. public void removeAutoCommand(final AutoCommand autoCommand) {
  101. checkNotNull(autoCommand);
  102. final AutoCommandHandler handler = autoCommands.remove(autoCommand);
  103. if (started) {
  104. eventBus.unsubscribe(handler);
  105. }
  106. }
  107. /**
  108. * 'Replaces' one AutoCommand with another, by removing the original and adding the replacement.
  109. *
  110. * @param original The original command to be replaced.
  111. * @param replacement The new command to be added.
  112. */
  113. public void replaceAutoCommand(final AutoCommand original, final AutoCommand replacement) {
  114. removeAutoCommand(original);
  115. addAutoCommand(replacement);
  116. }
  117. /**
  118. * Gets a set of all registered auto commands.
  119. *
  120. * @return The set of all known auto commands.
  121. */
  122. public Set<AutoCommand> getAutoCommands() {
  123. return Collections.unmodifiableSet(autoCommands.keySet());
  124. }
  125. /**
  126. * Returns the single global auto command, if it exists.
  127. *
  128. * @return The global auto-command, if it exists.
  129. */
  130. public Optional<AutoCommand> getGlobalAutoCommand() {
  131. return getAutoCommand(Optional.empty(), Optional.empty(), Optional.empty());
  132. }
  133. /**
  134. * Returns a single auto command matching the given parameters, if one exists.
  135. *
  136. * @param network The network to match
  137. * @param server The server to match
  138. * @param profile The profile to match
  139. * @return The matched auto command, if it exists.
  140. */
  141. public Optional<AutoCommand> getAutoCommand(final Optional<String> network,
  142. final Optional<String> server, final Optional<String> profile) {
  143. return getAutoCommands()
  144. .parallelStream()
  145. .filter(c -> c.getNetwork().equals(network))
  146. .filter(c -> c.getServer().equals(server))
  147. .filter(c -> c.getProfile().equals(profile))
  148. .findAny();
  149. }
  150. /**
  151. * Returns a single auto command matching the given parameters, or creates a new one if it
  152. * doesn't exist.
  153. *
  154. * @param network The network to match
  155. * @param server The server to match
  156. * @param profile The profile to match
  157. * @return The matched auto command, or a new auto command with the given targets.
  158. */
  159. public AutoCommand getOrCreateAutoCommand(final Optional<String> network,
  160. final Optional<String> server, final Optional<String> profile) {
  161. return getAutoCommand(network, server, profile).orElse(
  162. AutoCommand.create(server, network, profile, ""));
  163. }
  164. }