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

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