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.

AliasManager.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.aliases;
  23. import com.dmdirc.interfaces.CommandController;
  24. import com.google.common.collect.Sets;
  25. import java.util.Collections;
  26. import java.util.Map;
  27. import java.util.Optional;
  28. import java.util.Set;
  29. import java.util.concurrent.ConcurrentSkipListMap;
  30. import javax.inject.Inject;
  31. import javax.inject.Singleton;
  32. /**
  33. * Maintains a list of known aliases, and registers and unregisters them with the command system.
  34. */
  35. @Singleton
  36. public class AliasManager {
  37. /** The controller to register aliases with. */
  38. private final CommandController commandController;
  39. /** Map of known alias names to their corresponding aliases. */
  40. private final Map<String, Alias> aliases = new ConcurrentSkipListMap<>();
  41. /** Whether or not changes have been made compared to the stored version. */
  42. private boolean dirty;
  43. @Inject
  44. public AliasManager(final CommandController commandController) {
  45. this.commandController = commandController;
  46. }
  47. /**
  48. * Adds a new alias and registers it with the command system.
  49. * <p>
  50. * If an existing alias with the same name already exists, it is removed.
  51. *
  52. * @param alias The alias to be registered
  53. */
  54. public void addAlias(final Alias alias) {
  55. if (aliases.containsKey(alias.getName())) {
  56. removeAlias(alias);
  57. }
  58. aliases.put(alias.getName(), alias);
  59. commandController.registerCommand(new AliasCommandHandler(commandController, alias), alias);
  60. dirty = true;
  61. }
  62. /**
  63. * Removes any existing alias with the same name, and unregisters it with the command system.
  64. * <p>
  65. * If the alias was not previously added, no action occurs.
  66. *
  67. * @param alias The alias to be removed
  68. */
  69. public void removeAlias(final Alias alias) {
  70. if (aliases.containsKey(alias.getName())) {
  71. commandController.unregisterCommand(aliases.remove(alias.getName()));
  72. dirty = true;
  73. }
  74. }
  75. /**
  76. * Removes an existing alias, and unregisters it with the command system.
  77. * <p>
  78. * If the alias was not previously added, no action occurs.
  79. *
  80. * @param name The name of the alias to be removed
  81. */
  82. public void removeAlias(final String name) {
  83. if (aliases.containsKey(name)) {
  84. removeAlias(aliases.get(name));
  85. dirty = true;
  86. }
  87. }
  88. /**
  89. * Retrieves the set of all known alias names.
  90. *
  91. * @return Set of all known names.
  92. */
  93. public Set<String> getAliasNames() {
  94. return Collections.unmodifiableSet(aliases.keySet());
  95. }
  96. /**
  97. * Retrieves the set of all known aliases.
  98. *
  99. * @return Set of all known aliases.
  100. */
  101. public Set<Alias> getAliases() {
  102. return Collections.unmodifiableSet(Sets.newHashSet(aliases.values()));
  103. }
  104. /**
  105. * Retrieves the alias with the given name.
  106. *
  107. * @param name The name of the alias to receive.
  108. *
  109. * @return The alias, if found.
  110. */
  111. public Optional<Alias> getAlias(final String name) {
  112. return Optional.ofNullable(aliases.get(name));
  113. }
  114. /**
  115. * Gets the dirty state of the manager.
  116. *
  117. * @return True if the manager is dirty with respect to the store, false otherwise.
  118. */
  119. public boolean isDirty() {
  120. return dirty;
  121. }
  122. /**
  123. * Manually sets the dirty state of the manager. This should be done after the manager's
  124. * aliases are loaded or saved.
  125. *
  126. * @param dirty True if the manager is now dirty with respect to the store, false otherwise.
  127. */
  128. public void setDirty(final boolean dirty) {
  129. this.dirty = dirty;
  130. }
  131. }